Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to mackerel.Client #566

Merged
merged 22 commits into from Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions command/command.go
Expand Up @@ -553,10 +553,10 @@ func reportCheckMonitors(app *App, reports []*checks.Report) {
}

// collectHostParam collects host specs (correspond to "name", "meta", "interfaces" and "customIdentifier" fields in API v0)
func collectHostParam(conf *config.Config, ameta *AgentMeta) (mkr.CreateHostParam, error) {
func collectHostParam(conf *config.Config, ameta *AgentMeta) (*mkr.CreateHostParam, error) {
hostname, err := os.Hostname()
if err != nil {
return mkr.CreateHostParam{}, fmt.Errorf("failed to obtain hostname: %s", err.Error())
return nil, fmt.Errorf("failed to obtain hostname: %s", err.Error())
}

specGens := specGenerators()
Expand All @@ -576,7 +576,7 @@ func collectHostParam(conf *config.Config, ameta *AgentMeta) (mkr.CreateHostPara

interfaces, err := interfaceGenerator().Generate()
if err != nil {
return mkr.CreateHostParam{}, fmt.Errorf("failed to collect interfaces: %s", err.Error())
return nil, fmt.Errorf("failed to collect interfaces: %s", err.Error())
}

meta.AgentVersion = ameta.Version
Expand All @@ -592,7 +592,7 @@ func collectHostParam(conf *config.Config, ameta *AgentMeta) (mkr.CreateHostPara
})
}

return mkr.CreateHostParam{
return &mkr.CreateHostParam{
Name: hostname,
Meta: meta,
Interfaces: interfaces,
Expand All @@ -613,7 +613,7 @@ func (app *App) UpdateHostSpecs() {
return
}

err = app.API.UpdateHost(app.Host.ID, mkr.UpdateHostParam(hostParam))
err = app.API.UpdateHost(app.Host.ID, mkr.UpdateHostParam(*hostParam))

if err != nil {
logger.Errorf("Error while updating host specs: %s", err)
Expand Down Expand Up @@ -696,7 +696,7 @@ func runOncePayload(conf *config.Config, ameta *AgentMeta) ([]*mkr.GraphDefsPara
ag := NewAgent(conf)
graphdefs := ag.CollectGraphDefsOfPlugins()
metrics := ag.CollectMetrics(time.Now())
return graphdefs, &hostParam, metrics, nil
return graphdefs, hostParam, metrics, nil
}

// NewAgent creates a new instance of agent.Agent from its configuration conf.
Expand Down
22 changes: 2 additions & 20 deletions mackerel/api.go
Expand Up @@ -199,26 +199,8 @@ func (api *API) FindHostByCustomIdentifier(customIdentifier string) (*mkr.Host,
}

// CreateHost register the host to mackerel
func (api *API) CreateHost(hostParam mkr.CreateHostParam) (string, error) {
resp, err := api.postJSON("/api/v0/hosts", hostParam)
defer closeResp(resp)
if err != nil {
return "", err
}

if resp.StatusCode != 200 {
return "", apiError(resp.StatusCode, "API request failed")
}

var data struct {
ID string `json:"id"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", err
}

return data.ID, nil
func (api *API) CreateHost(hostParam *mkr.CreateHostParam) (string, error) {
return api.c.CreateHost(hostParam)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// UpdateHost updates the host information on Mackerel.
Expand Down
4 changes: 2 additions & 2 deletions mackerel/api_test.go
Expand Up @@ -139,7 +139,7 @@ func TestCreateHost(t *testing.T) {
IPAddress: "10.0.4.7",
MacAddress: "01:23:45:67:89:ab",
})
hostParam := mkr.CreateHostParam{
hostParam := &mkr.CreateHostParam{
Name: "dummy",
Meta: mkr.HostMeta{
AgentName: "mackerel-agent",
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestCreateHostWithNilArgs(t *testing.T) {
api, _ := NewAPI(ts.URL, "dummy-key", false)

// with nil args
hostParam := mkr.CreateHostParam{
hostParam := &mkr.CreateHostParam{
Name: "nilsome",
}
hostID, err := api.CreateHost(hostParam)
Expand Down