From 0ca937d979d8662eb2b0578c0b6c597d72db7302 Mon Sep 17 00:00:00 2001 From: yseto Date: Fri, 30 Sep 2022 11:16:42 +0900 Subject: [PATCH 1/2] FindHostByCustomIdentifier --- hosts.go | 33 +++++++++++++++++++++++++++++++++ hosts_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) 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..6666a1a 100644 --- a/hosts_test.go +++ b/hosts_test.go @@ -151,6 +151,51 @@ 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.Path != "/api/v0/hosts-by-custom-identifier/mydb001" { + t.Error("request URL should be /api/v0/hosts-by-custom-identifier/mydb001 but: ", req.URL.Path) + } + + 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", + }, + }) + + 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", &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) + } +} + func TestCreateHost(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { if req.URL.Path != "/api/v0/hosts" { From c8aa3a5668a64c2dd4e038da97fdacf449c5fa3c Mon Sep 17 00:00:00 2001 From: yseto Date: Wed, 5 Oct 2022 16:35:37 +0900 Subject: [PATCH 2/2] fix tests --- hosts_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/hosts_test.go b/hosts_test.go index 6666a1a..da89104 100644 --- a/hosts_test.go +++ b/hosts_test.go @@ -153,8 +153,8 @@ 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.Path != "/api/v0/hosts-by-custom-identifier/mydb001" { - t.Error("request URL should be /api/v0/hosts-by-custom-identifier/mydb001 but: ", req.URL.Path) + 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() @@ -172,7 +172,7 @@ func TestFindHostByCustomIdentifier(t *testing.T) { "name": "mydb001", "status": "working", "memo": "hello", - "customIdentifier": "mydb001", + "customIdentifier": "mydb001/001", }, }) @@ -182,7 +182,7 @@ func TestFindHostByCustomIdentifier(t *testing.T) { defer ts.Close() client, _ := NewClientWithOptions("dummy-key", ts.URL, false) - host, err := client.FindHostByCustomIdentifier("mydb001", &FindHostByCustomIdentifierParam{CaseInsensitive: true}) + host, err := client.FindHostByCustomIdentifier("mydb001/001", &FindHostByCustomIdentifierParam{CaseInsensitive: true}) if err != nil { t.Error("err should be nil but: ", err) @@ -194,6 +194,9 @@ func TestFindHostByCustomIdentifier(t *testing.T) { 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) {