From 3ae4a2f4ab1f4e985d0667eb4b4fb1a53de5f4d9 Mon Sep 17 00:00:00 2001 From: Songmu Date: Mon, 25 May 2015 16:17:28 +0900 Subject: [PATCH 1/4] define mackerel.HostSpec --- command/command.go | 16 ++++++++-------- command/command_test.go | 23 ++++------------------- mackerel/api.go | 2 +- mackerel/api_test.go | 10 +++++----- mackerel/host.go | 9 +++++++++ 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/command/command.go b/command/command.go index 2257d54dd..8baac87ce 100644 --- a/command/command.go +++ b/command/command.go @@ -56,14 +56,14 @@ func saveHostID(root string, id string) error { } // buildHostSpec build data structure for Host specs -func buildHostSpec(name string, meta map[string]interface{}, interfaces []map[string]interface{}, roleFullnames []string, checks []string, displayName string) map[string]interface{} { - return map[string]interface{}{ - "name": name, - "meta": meta, - "interfaces": interfaces, - "roleFullnames": roleFullnames, - "checks": checks, - "displayName": displayName, +func buildHostSpec(name string, meta map[string]interface{}, interfaces []map[string]interface{}, roleFullnames []string, checks []string, displayName string) mackerel.HostSpec { + return mackerel.HostSpec{ + Name: name, + Meta: meta, + Interfaces: interfaces, + RoleFullnames: roleFullnames, + Checks: checks, + DisplayName: displayName, } } diff --git a/command/command_test.go b/command/command_test.go index 21614c7c1..41c759c38 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -34,27 +34,12 @@ func TestBuildHostSpec(t *testing.T) { hostSpec := buildHostSpec("Host123", meta, interfaces, roleFullnames, checks, displayName) - if hostSpec["name"] != "Host123" { - t.Error("name should 'Host123' but:", hostSpec["name"]) + if hostSpec.Name != "Host123" { + t.Error("name should 'Host123' but:", hostSpec.Name) } - _, ok := hostSpec["interfaces"].([]map[string]interface{}) - if !ok { - t.Error("the type of interfaces should be '[]map[string]interface{}'") - } - - _, ok = hostSpec["meta"].(map[string]interface{}) - if !ok { - t.Error("the type of meta should be 'map[string]interface{}'") - } - - _, ok = hostSpec["roleFullnames"].([]string) - if !ok { - t.Error("the type of meta should be '[]string'") - } - - if hostSpec["displayName"] != displayName { - t.Error("name should 'label' but:", hostSpec["displayName"]) + if hostSpec.DisplayName != displayName { + t.Error("name should 'label' but:", hostSpec.DisplayName) } } diff --git a/mackerel/api.go b/mackerel/api.go index 5354a42db..e4c831eec 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -167,7 +167,7 @@ func (api *API) CreateHost(name string, meta map[string]interface{}, interfaces } // UpdateHost updates the host information on Mackerel. -func (api *API) UpdateHost(hostID string, hostSpec map[string]interface{}) error { +func (api *API) UpdateHost(hostID string, hostSpec HostSpec) error { url := api.urlFor("/api/v0/hosts/" + hostID) requestJSON, err := json.Marshal(hostSpec) diff --git a/mackerel/api_test.go b/mackerel/api_test.go index b09389508..8a5ef0fbb 100644 --- a/mackerel/api_test.go +++ b/mackerel/api_test.go @@ -284,13 +284,13 @@ func TestUpdateHost(t *testing.T) { "encap": "Ethernet", }) - hostSpec := map[string]interface{}{ - "name": "dummy", - "meta": map[string]interface{}{ + hostSpec := HostSpec{ + Name: "dummy", + Meta: map[string]interface{}{ "memo": "hello", }, - "interfaces": interfaces, - "roleFullnames": []string{"My-Service:app-default"}, + Interfaces: interfaces, + RoleFullnames: []string{"My-Service:app-default"}, } err := api.UpdateHost("ABCD123", hostSpec) diff --git a/mackerel/host.go b/mackerel/host.go index 5807648d6..94d96b2c6 100644 --- a/mackerel/host.go +++ b/mackerel/host.go @@ -7,3 +7,12 @@ type Host struct { Type string `json:"type"` // TODO ENUM Status string `json:"status"` } + +type HostSpec struct { + Name string `json:"name"` + Meta map[string]interface{} `json:"meta"` + Interfaces []map[string]interface{} `json:"interfaces"` + RoleFullnames []string `json:"roleFullnames"` + Checks []string `json:"checks,omitempty"` + DisplayName string `json:"displayName,omitempty"` +} From d41886bdf7ac0f1d2f17f0cbba7a06235b7669a9 Mon Sep 17 00:00:00 2001 From: Songmu Date: Mon, 25 May 2015 16:36:10 +0900 Subject: [PATCH 2/4] remove fund buildHostSpec --- command/command.go | 41 ++++++++++++++++++++++++++--------------- command/command_test.go | 26 -------------------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/command/command.go b/command/command.go index 8baac87ce..8ea92c24a 100644 --- a/command/command.go +++ b/command/command.go @@ -55,18 +55,6 @@ func saveHostID(root string, id string) error { return nil } -// buildHostSpec build data structure for Host specs -func buildHostSpec(name string, meta map[string]interface{}, interfaces []map[string]interface{}, roleFullnames []string, checks []string, displayName string) mackerel.HostSpec { - return mackerel.HostSpec{ - Name: name, - Meta: meta, - Interfaces: interfaces, - RoleFullnames: roleFullnames, - Checks: checks, - DisplayName: displayName, - } -} - // prepareHost collects specs of the host and sends them to Mackerel server. // A unique host-id is returned by the server if one is not specified. func prepareHost(root string, api *mackerel.API, roleFullnames []string, checks []string, displayName string) (*mackerel.Host, error) { @@ -96,7 +84,15 @@ func prepareHost(root string, api *mackerel.API, roleFullnames []string, checks if err != nil { return nil, fmt.Errorf("Failed to find this host on mackerel (You may want to delete file \"%s\" to register this host to an another organization): %s", idFilePath(root), err.Error()) } - err := api.UpdateHost(hostID, buildHostSpec(hostname, meta, interfaces, roleFullnames, checks, displayName)) + err := api.UpdateHost(hostID, mackerel.HostSpec{ + Name: hostname, + Meta: meta, + Interfaces: interfaces, + RoleFullnames: roleFullnames, + Checks: checks, + DisplayName: displayName, + }) + if err != nil { return nil, fmt.Errorf("Failed to update this host: %s", err.Error()) } @@ -456,7 +452,15 @@ func UpdateHostSpecs(conf *config.Config, api *mackerel.API, host *mackerel.Host return } - err = api.UpdateHost(host.ID, buildHostSpec(hostname, meta, interfaces, conf.Roles, conf.CheckNames(), conf.DisplayName)) + err = api.UpdateHost(host.ID, mackerel.HostSpec{ + Name: hostname, + Meta: meta, + Interfaces: interfaces, + RoleFullnames: conf.Roles, + Checks: conf.CheckNames(), + DisplayName: conf.DisplayName, + }) + if err != nil { logger.Errorf("Error while updating host specs: %s", err) } else { @@ -492,7 +496,14 @@ func RunOnce(conf *config.Config) { logger.Infof("Collecting metrics may take one minutes.") metrics := ag.CollectMetrics(time.Now()) payload := map[string]interface{}{ - "host": buildHostSpec(hostname, meta, interfaces, conf.Roles, conf.CheckNames(), conf.DisplayName), + "host": mackerel.HostSpec{ + Name: hostname, + Meta: meta, + Interfaces: interfaces, + RoleFullnames: conf.Roles, + Checks: conf.CheckNames(), + DisplayName: conf.DisplayName, + }, "metrics": metrics, } json, err := json.Marshal(payload) diff --git a/command/command_test.go b/command/command_test.go index 41c759c38..29b44c63c 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -17,32 +17,6 @@ import ( "github.com/mackerelio/mackerel-agent/metrics" ) -func TestBuildHostSpec(t *testing.T) { - var interfaces []map[string]interface{} - interfaces = append(interfaces, map[string]interface{}{ - "name": "eth0", - "ipAddress": "10.0.4.7", - "macAddress": "01:23:45:67:89:ab", - "encap": "Ethernet", - }) - meta := map[string]interface{}{ - "memo": "hello", - } - roleFullnames := []string{"My-Service:app-default"} - checks := []string{"heartbeat"} - displayName := "label" - - hostSpec := buildHostSpec("Host123", meta, interfaces, roleFullnames, checks, displayName) - - if hostSpec.Name != "Host123" { - t.Error("name should 'Host123' but:", hostSpec.Name) - } - - if hostSpec.DisplayName != displayName { - t.Error("name should 'label' but:", hostSpec.DisplayName) - } -} - func TestDelayByHost(t *testing.T) { delay1 := time.Duration(delayByHost(&mackerel.Host{ ID: "246PUVUngPo", From 722db3b851840fc071532a53cdb2edd37e6a0766 Mon Sep 17 00:00:00 2001 From: Songmu Date: Mon, 25 May 2015 20:13:57 +0900 Subject: [PATCH 3/4] add one line document for HostSpec --- mackerel/host.go | 1 + 1 file changed, 1 insertion(+) diff --git a/mackerel/host.go b/mackerel/host.go index 94d96b2c6..fbd2adfc2 100644 --- a/mackerel/host.go +++ b/mackerel/host.go @@ -8,6 +8,7 @@ type Host struct { Status string `json:"status"` } +// HostSpec is host specifications sent Mackerel server per hour type HostSpec struct { Name string `json:"name"` Meta map[string]interface{} `json:"meta"` From 22435abd8ccff7b017353357e2570a312df350f1 Mon Sep 17 00:00:00 2001 From: Songmu Date: Mon, 25 May 2015 20:17:19 +0900 Subject: [PATCH 4/4] fix typo in test --- mackerel/api_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mackerel/api_test.go b/mackerel/api_test.go index 8a5ef0fbb..afd8f331d 100644 --- a/mackerel/api_test.go +++ b/mackerel/api_test.go @@ -107,7 +107,7 @@ func TestCreateHost(t *testing.T) { var data struct { Name string `json:"name"` - Tame string `json:"type"` + Type string `json:"type"` Status string `json:"status"` Meta map[string]string `json:"meta"` Interfaces []map[string]string `json:"interfaces"` @@ -189,7 +189,7 @@ func TestCreateHostWithNilArgs(t *testing.T) { var data struct { Name string `json:"name"` - Tame string `json:"type"` + Type string `json:"type"` Status string `json:"status"` Meta map[string]string `json:"meta"` Interfaces []map[string]string `json:"interfaces"` @@ -240,7 +240,7 @@ func TestUpdateHost(t *testing.T) { var data struct { Name string `json:"name"` - Tame string `json:"type"` + Type string `json:"type"` Status string `json:"status"` Meta map[string]string `json:"meta"` Interfaces []map[string]string `json:"interfaces"`