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

implement BulkUpdateHostStatuses to update status of the hosts #149

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading