Skip to content

Commit 7237685

Browse files
committed
DeleteHosts should wait for the operation result
1 parent 0ac6530 commit 7237685

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

pkg/client/client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ func (c *clientImpl) DeleteHosts(names []string) error {
158158
wg.Add(1)
159159
go func(name string) {
160160
defer wg.Done()
161-
if err := c.httpHelper.NewDeleteRequest("/hosts/" + name).JSONResDo(nil); err != nil {
161+
var op apiv1.Operation
162+
if err := c.httpHelper.NewDeleteRequest("/hosts/" + name).JSONResDo(&op); err != nil {
163+
mu.Lock()
164+
defer mu.Unlock()
165+
merr = multierror.Append(merr, fmt.Errorf("delete host %q failed: %w", name, err))
166+
return
167+
}
168+
ins := &apiv1.HostInstance{}
169+
if err := c.waitForOperation(&op, ins); err != nil {
162170
mu.Lock()
163171
defer mu.Unlock()
164172
merr = multierror.Append(merr, fmt.Errorf("delete host %q failed: %w", name, err))

pkg/client/client_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"io"
2020
"net/http"
2121
"net/http/httptest"
22-
"regexp"
2322
"testing"
2423

2524
apiv1 "github.com/google/cloud-android-orchestration/api/v1"
@@ -28,19 +27,17 @@ import (
2827
)
2928

3029
func TestDeleteHosts(t *testing.T) {
31-
existingNames := map[string]struct{}{"bar": {}, "baz": {}}
3230
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33-
if r.Method != "DELETE" {
34-
panic("unexpected method: " + r.Method)
35-
}
36-
re := regexp.MustCompile(`^/hosts/(.*)$`)
37-
matches := re.FindStringSubmatch(r.URL.Path)
38-
if len(matches) != 2 {
39-
panic("unexpected path: " + r.URL.Path)
40-
}
41-
if _, ok := existingNames[matches[1]]; ok {
42-
writeOK(w, "")
43-
} else {
31+
switch ep := r.Method + " " + r.URL.Path; ep {
32+
case "DELETE /hosts/bar":
33+
writeOK(w, apiv1.Operation{Name: "deletingbar"})
34+
case "DELETE /hosts/baz":
35+
writeOK(w, apiv1.Operation{Name: "deletingbaz"})
36+
case "POST /operations/deletingbar/:wait":
37+
writeOK(w, apiv1.HostInstance{Name: "bar"})
38+
case "POST /operations/deletingbaz/:wait":
39+
writeOK(w, apiv1.HostInstance{Name: "baz"})
40+
default:
4441
writeErr(w, 404)
4542
}
4643
}))

0 commit comments

Comments
 (0)