Skip to content

Commit

Permalink
Merge pull request #138 from mackerelio/custom-identifier
Browse files Browse the repository at this point in the history
added FindHostByCustomIdentifier
  • Loading branch information
yseto committed Oct 5, 2022
2 parents 5b7f6ca + c8aa3a5 commit 194f7bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ type MonitoredStatusDetail struct {
Memo string `json:"memo,omitempty"`
}

// FindHostByCustomIdentifierParam parameters for FindHostByCustomIdentifier
type FindHostByCustomIdentifierParam struct {
CaseInsensitive bool
}

const (
// HostStatusWorking represents "working" status
HostStatusWorking = "working"
Expand Down Expand Up @@ -227,6 +232,34 @@ func (c *Client) FindHosts(param *FindHostsParam) ([]*Host, error) {
return data.Hosts, err
}

// FindHostByCustomIdentifier finds host via CustomIdentifier
func (c *Client) FindHostByCustomIdentifier(customIdentifier string, param *FindHostByCustomIdentifierParam) (*Host, error) {
v := url.Values{}
if param.CaseInsensitive {
v.Set("caseInsensitive", "true")
}

req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s?%s", c.urlFor("/api/v0/hosts-by-custom-identifier").String(), url.PathEscape(customIdentifier), v.Encode()), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

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

return data.Host, err
}

// CreateHost creates host
func (c *Client) CreateHost(param *CreateHostParam) (string, error) {
resp, err := c.PostJSON("/api/v0/hosts", param)
Expand Down
48 changes: 48 additions & 0 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,54 @@ func TestFindHosts(t *testing.T) {

}

func TestFindHostByCustomIdentifier(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.RawPath != "/api/v0/hosts-by-custom-identifier/mydb001%2F001" {
t.Error("request URL.RawPath should be /api/v0/hosts-by-custom-identifier/mydb001%$2F001 but: ", req.URL.RawPath)
}

query := req.URL.Query()
if query.Get("caseInsensitive") != "true" {
t.Error("request query 'caseInsensitive' param should be true but: ", query.Get("caseInsensitive"))
}

if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}

respJSON, _ := json.Marshal(map[string]map[string]interface{}{
"host": {
"id": "9rxGOHfVF8F",
"name": "mydb001",
"status": "working",
"memo": "hello",
"customIdentifier": "mydb001/001",
},
})

res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
host, err := client.FindHostByCustomIdentifier("mydb001/001", &FindHostByCustomIdentifierParam{CaseInsensitive: true})

if err != nil {
t.Error("err should be nil but: ", err)
}

if host.Memo != "hello" {
t.Error("request sends json including memo but: ", host)
}
if host.ID != "9rxGOHfVF8F" {
t.Error("request sends json including ID but: ", host)
}
if host.CustomIdentifier != "mydb001/001" {
t.Error("request sends json including CustomIdentifier but: ", host)
}
}

func TestCreateHost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/hosts" {
Expand Down

0 comments on commit 194f7bf

Please sign in to comment.