Skip to content

Commit

Permalink
Merge pull request #149 from itchyny/feat-bulk-update-host-status
Browse files Browse the repository at this point in the history
implement BulkUpdateHostStatuses to update status of the hosts
  • Loading branch information
yohfee committed Nov 16, 2023
2 parents 2b4e0de + 88acc73 commit 81608c3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func (c *Client) UpdateHostStatus(hostID string, status string) error {
return err
}

// BulkUpdateHostStatuses updates status of the hosts.
func (c *Client) BulkUpdateHostStatuses(ids []string, status string) error {
_, err := requestPost[any](c, "/api/v0/hosts/bulk-update-statuses",
map[string]any{"ids": ids, "status": status})
return err
}

// UpdateHostRoleFullnames updates host roles.
func (c *Client) UpdateHostRoleFullnames(hostID string, roleFullnames []string) error {
path := fmt.Sprintf("/api/v0/hosts/%s/role-fullnames", hostID)
Expand Down
48 changes: 48 additions & 0 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,54 @@ func TestUpdateHostStatus(t *testing.T) {
}
}

func TestBulkUpdateHostStatuses(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/hosts/bulk-update-statuses" {
t.Error("request URL should be /api/v0/hosts/bulk-update-statuses but: ", req.URL.Path)
}

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

body, _ := io.ReadAll(req.Body)

var data struct {
IDs []string `json:"ids"`
Status string `json:"status"`
}

err := json.Unmarshal(body, &data)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}

if reflect.DeepEqual(data.IDs, []string{"123456ABCD", "789012EFGH"}) != true {
t.Errorf("request IDs should be []string{\"123456ABCD\", \"789012EFGH\"} but: %+v", data.IDs)
}

if data.Status != "maintenance" {
t.Error("request sends json including status but: ", data.Status)
}

respJSON, _ := json.Marshal(map[string]bool{
"success": true,
})

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

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
ids := []string{"123456ABCD", "789012EFGH"}
err := client.BulkUpdateHostStatuses(ids, "maintenance")

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

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

0 comments on commit 81608c3

Please sign in to comment.