diff --git a/hosts.go b/hosts.go index e23a67f..6cbc333 100644 --- a/hosts.go +++ b/hosts.go @@ -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" @@ -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) diff --git a/hosts_test.go b/hosts_test.go index 7de0c3f..da89104 100644 --- a/hosts_test.go +++ b/hosts_test.go @@ -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" {