Skip to content

Commit

Permalink
Merge pull request #93 from mackerelio/feature/host-nickname-hostspec
Browse files Browse the repository at this point in the history
Feature/host nickname hostspec
  • Loading branch information
Songmu committed May 25, 2015
2 parents be5d5f7 + 22435ab commit 6f7919f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 65 deletions.
41 changes: 26 additions & 15 deletions command/command.go
Expand Up @@ -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) map[string]interface{} {
return map[string]interface{}{
"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) {
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 0 additions & 41 deletions command/command_test.go
Expand Up @@ -17,47 +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"])
}

_, 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"])
}
}

func TestDelayByHost(t *testing.T) {
delay1 := time.Duration(delayByHost(&mackerel.Host{
ID: "246PUVUngPo",
Expand Down
2 changes: 1 addition & 1 deletion mackerel/api.go
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions mackerel/api_test.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions mackerel/host.go
Expand Up @@ -7,3 +7,13 @@ type Host struct {
Type string `json:"type"` // TODO ENUM
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"`
Interfaces []map[string]interface{} `json:"interfaces"`
RoleFullnames []string `json:"roleFullnames"`
Checks []string `json:"checks,omitempty"`
DisplayName string `json:"displayName,omitempty"`
}

0 comments on commit 6f7919f

Please sign in to comment.