Skip to content

Commit

Permalink
Merge pull request #64 from modcloth/prefer-hostnames-to-ips
Browse files Browse the repository at this point in the history
Prefer to place hostnames in groups rather than IP addresses
  • Loading branch information
Rafe Colton committed Oct 8, 2014
2 parents 9dda366 + a2260f2 commit 62596cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
12 changes: 6 additions & 6 deletions tory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ func (inv *inventory) GetGroup(group string) []string {
return nil
}

func (inv *inventory) AddIPToGroup(group, ip string) {
func (inv *inventory) AddHostnameToGroup(group, hostname string) {
sanitizedGroup := groupNameUnsafe.ReplaceAllString(strings.ToLower(group), "_")
sanitizedGroup = strings.Replace(sanitizedGroup, ".", "_", -1)
inv.AddIPToGroupUnsanitized(sanitizedGroup, ip)
inv.AddHostnameToGroupUnsanitized(sanitizedGroup, hostname)
}

func (inv *inventory) AddIPToGroupUnsanitized(group, ip string) {
func (inv *inventory) AddHostnameToGroupUnsanitized(group, hostname string) {
inv.groupMutex.Lock()
defer inv.groupMutex.Unlock()

if _, ok := inv.groups[group]; !ok {
inv.groups[group] = []string{}
}
inv.groups[group] = append(inv.groups[group], ip)
inv.groups[group] = append(inv.groups[group], hostname)
}

func (inv *inventory) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -81,8 +81,8 @@ func (inv *inventory) UnmarshalJSON(b []byte) error {
group := []string{}
err := json.Unmarshal(value, &group)
if err == nil {
for _, ip := range group {
inv.AddIPToGroupUnsanitized(key, ip)
for _, hostname := range group {
inv.AddHostnameToGroupUnsanitized(key, hostname)
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions tory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ func (srv *server) getHostInventory(w http.ResponseWriter, r *http.Request) {

inv := newInventory()
for _, host := range hosts {
inv.AddIPToGroupUnsanitized(host.Name, host.IP.Addr)
inv.AddHostnameToGroupUnsanitized(host.IP.Addr, host.Name)

if host.Type.String != "" {
inv.AddIPToGroup(fmt.Sprintf("type_%s",
strings.ToLower(host.Type.String)), host.IP.Addr)
inv.AddHostnameToGroup(fmt.Sprintf("type_%s",
strings.ToLower(host.Type.String)), host.Name)
}

if host.Tags != nil && host.Tags.Map != nil {
Expand All @@ -229,7 +229,7 @@ func (srv *server) getHostInventory(w http.ResponseWriter, r *http.Request) {
}
invKey := fmt.Sprintf("tag_%s_%s",
strings.ToLower(key), strings.ToLower(value.String))
inv.AddIPToGroup(invKey, host.IP.Addr)
inv.AddHostnameToGroup(invKey, host.Name)
}
}

Expand All @@ -238,7 +238,6 @@ func (srv *server) getHostInventory(w http.ResponseWriter, r *http.Request) {
}

for key, value := range host.CollapsedVars() {
inv.Meta.AddHostvar(host.IP.Addr, key, value)
inv.Meta.AddHostvar(host.Name, key, value)
}
}
Expand Down
53 changes: 24 additions & 29 deletions tory/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ func TestHandleGetHostInventory(t *testing.T) {

for _, s := range []string{
`/ansible/hosts/test`,
`/ansible/hosts/test?since=2000-01-01T01:00:00Z`,
`/ansible/hosts/test?before=3000-01-01T01:00:00Z`,
fmt.Sprintf(`/ansible/hosts/test?since=%s`, time.Now().Add(-24*time.Hour).Format(time.RFC3339)),
fmt.Sprintf(`/ansible/hosts/test?before=%s`, time.Now().Add(24*time.Hour).Format(time.RFC3339)),
fmt.Sprintf(`/ansible/hosts/test?since=%s&before=%s`,
time.Now().Add(-1*time.Hour).Format(time.RFC3339),
time.Now().Add(1*time.Hour).Format(time.RFC3339)),
time.Now().Add(-24*time.Hour).Format(time.RFC3339),
time.Now().Add(24*time.Hour).Format(time.RFC3339)),
`/ansible/hosts/test?vars-only=`,
} {
w := makeRequest("GET", s, nil, "")
Expand All @@ -178,11 +178,6 @@ func TestHandleGetHostInventory(t *testing.T) {
t.Fatalf("GET %s: body meta does not contain \"hostvars\"", s)
}

if _, ok := inv.Meta.Hostvars[h.IP]; !ok {
t.Fatalf("GET %s: body meta does not contain \"hostvars\" by IP", s)
}

fmt.Printf("host vars %+v\n", inv.Meta.Hostvars[h.Name])
if _, ok := inv.Meta.Hostvars[h.Name]; !ok {
t.Fatalf("GET %s: body meta does not contain \"hostvars\" by name", s)
}
Expand Down Expand Up @@ -288,40 +283,40 @@ func TestHandleUpdateHost(t *testing.T) {
inv := newInventory()
err = json.NewDecoder(w.Body).Decode(inv)

if g := inv.GetGroup(h.Name); g == nil {
t.Fatalf("response does not contain host name as group")
if g := inv.GetGroup(h.IP); g == nil {
t.Fatalf("response does not contain IP as group")
}

tagTeamGroup := inv.GetGroup(fmt.Sprintf("tag_team_fribbles"))
if tagTeamGroup == nil {
t.Fatalf("response does not contain tag team group")
}

hasIP := false
for _, ip := range tagTeamGroup {
if ip == h.IP {
hasIP = true
hasHostname := false
for _, hostname := range tagTeamGroup {
if hostname == h.Name {
hasHostname = true
}
}

if !hasIP {
t.Fatalf("test host ip %q not in tag team group", h.IP)
if !hasHostname {
t.Fatalf("test host %q not in tag team group", h.Name)
}

typeGroup := inv.GetGroup(fmt.Sprintf("type_virtualmachine"))
if typeGroup == nil {
t.Fatalf("response does not contain type group")
}

hasIP = false
for _, ip := range typeGroup {
if ip == h.IP {
hasIP = true
hasHostname = false
for _, hostname := range typeGroup {
if hostname == h.Name {
hasHostname = true
}
}

if !hasIP {
t.Fatalf("test host ip %q not in tag team group", h.IP)
if !hasHostname {
t.Fatalf("test host %q not in tag team group", h.Name)
}

w = makeRequest("GET", `/ansible/hosts/test/`+h.Name, nil, "")
Expand Down Expand Up @@ -541,18 +536,18 @@ func TestHandleFilterHosts(t *testing.T) {
t.Error(err)
}

hostGroup, ok := res[h.Name]
ipGroup, ok := res[h.IP]
if !ok {
t.Fatalf("host group not present")
t.Fatalf("ip group not present")
}

hostGroupSlice := []string{}
err = json.Unmarshal(hostGroup, &hostGroupSlice)
ipGroupSlice := []string{}
err = json.Unmarshal(ipGroup, &ipGroupSlice)
if err != nil {
t.Error(err)
}

if len(hostGroupSlice) == 0 {
t.Fatalf("host group is empty")
if len(ipGroupSlice) == 0 {
t.Fatalf("ip group is empty")
}
}

0 comments on commit 62596cc

Please # to comment.