From 7072d99f5cb4349434beb51389016243beadd0fe Mon Sep 17 00:00:00 2001 From: jszwedko Date: Wed, 8 Oct 2014 11:13:41 -0400 Subject: [PATCH 1/2] Prefer to place hostnames in groups rather than IP addresses More readible for humans. Create groups using the IPs to alias to the hostname. --- tory/inventory.go | 12 ++++++------ tory/server.go | 9 ++++----- tory/server_test.go | 17 ++++++----------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/tory/inventory.go b/tory/inventory.go index 87a1c5f..5217d14 100644 --- a/tory/inventory.go +++ b/tory/inventory.go @@ -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) { @@ -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) } } } diff --git a/tory/server.go b/tory/server.go index c73e88d..9657c06 100644 --- a/tory/server.go +++ b/tory/server.go @@ -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 { @@ -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) } } @@ -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) } } diff --git a/tory/server_test.go b/tory/server_test.go index 4a91698..3772873 100644 --- a/tory/server_test.go +++ b/tory/server_test.go @@ -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) } @@ -541,18 +536,18 @@ func TestHandleFilterHosts(t *testing.T) { t.Error(err) } - hostGroup, ok := res[h.Name] + ipGroup, ok := res[h.IP.Addr] 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(hostGroup, &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") } } From a2260f21faed8b77d4b8d443b85f3d78b4a9f89d Mon Sep 17 00:00:00 2001 From: jszwedko Date: Wed, 8 Oct 2014 14:56:51 -0400 Subject: [PATCH 2/2] Few more tweaks to tests to account for groups containing hostnames --- tory/server_test.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tory/server_test.go b/tory/server_test.go index 3772873..47b90b6 100644 --- a/tory/server_test.go +++ b/tory/server_test.go @@ -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, "") @@ -283,8 +283,8 @@ 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")) @@ -292,15 +292,15 @@ func TestHandleUpdateHost(t *testing.T) { 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")) @@ -308,15 +308,15 @@ func TestHandleUpdateHost(t *testing.T) { 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, "") @@ -536,13 +536,13 @@ func TestHandleFilterHosts(t *testing.T) { t.Error(err) } - ipGroup, ok := res[h.IP.Addr] + ipGroup, ok := res[h.IP] if !ok { t.Fatalf("ip group not present") } ipGroupSlice := []string{} - err = json.Unmarshal(hostGroup, &ipGroupSlice) + err = json.Unmarshal(ipGroup, &ipGroupSlice) if err != nil { t.Error(err) }