HTTP API: Implement setting worker groups
authorMaciej Wereski <m.wereski@partner.samsung.com>
Fri, 13 Oct 2017 10:57:05 +0000 (12:57 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 5 Jun 2018 10:48:28 +0000 (12:48 +0200)
Change-Id: I7d0c6ad8da766c0716ab1186b3894079044e58d8
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
server/api/v1/handlers.go
server/api/v1/handlers_test.go
server/api/v1/testdata/worker-set-groups-POST.json [deleted file]
server/api/v1/testdata/worker-set-groups-bad-uuid-POST.json [new file with mode: 0644]
server/api/v1/testdata/worker-set-groups-malformed-json-POST.json [new file with mode: 0644]
server/api/v1/testdata/worker-set-groups-missing-POST.json [new file with mode: 0644]
server/api/v1/testdata/worker-set-groups-valid-POST.json [new file with mode: 0644]

index d394a56..ac6d4aa 100644 (file)
@@ -198,7 +198,17 @@ func (api *API) setWorkerStateHandler(r *http.Request, ps map[string]string) res
 // setWorkerGroupsHandler parses HTTP workers for setting worker groups and calls
 // workers.SetGroups().
 func (api *API) setWorkerGroupsHandler(r *http.Request, ps map[string]string) responseData {
-       return newServerError(ErrNotImplemented, "set worker groups")
+       var groups Groups
+       defer r.Body.Close()
+
+       if !isValidUUID(ps["id"]) {
+               return newServerError(ErrBadUUID)
+       }
+
+       if err := json.NewDecoder(r.Body).Decode(&groups); err != nil {
+               return newServerError(err)
+       }
+       return newServerError(api.workers.SetGroups(WorkerUUID(ps["id"]), groups))
 }
 
 // workerDeregister parses HTTP workers for deregistering worker state and calls
index ef21485..a41f42b 100644 (file)
@@ -483,15 +483,39 @@ func TestSetWorkerGroupsHandler(t *testing.T) {
        assert, m, api := initTest(t)
        defer m.finish()
 
+       path := "/api/v1/workers/%s/setgroups"
+       methods := []string{http.MethodPost}
+       prefix := "worker-set-groups-"
+
+       groups := Groups{"foo", "bar"}
+       notFoundTest := testFromTempl(notFoundTestTempl, prefix, fmt.Sprintf(path, missingUUID), methods...)
+       notFoundTest.json = string(jsonMustMarshal(groups))
+       missingErr := NotFoundError("Worker")
+       malformedJSONTest := testFromTempl(malformedJSONTestTempl, prefix, fmt.Sprintf(path, validUUID), methods...)
+
+       m.wm.EXPECT().SetGroups(WorkerUUID(validUUID), groups).Return(nil)
+       m.wm.EXPECT().SetGroups(WorkerUUID(missingUUID), groups).Return(missingErr)
+
        tests := []requestTest{
+               // Set valid groups.
                {
-                       name:        "worker-set-groups",
-                       path:        "/api/v1/workers/8/setgroups",
-                       methods:     []string{http.MethodPost},
-                       json:        ``,
+                       name:    prefix + "valid",
+                       path:    fmt.Sprintf(path, validUUID),
+                       methods: methods,
+                       json:    string(jsonMustMarshal(groups)),
+                       status:  http.StatusNoContent,
+               },
+               // invalid UUID
+               {
+                       name:        prefix + "bad-uuid",
+                       path:        fmt.Sprintf(path, invalidID),
+                       methods:     methods,
+                       json:        string(jsonMustMarshal(groups)),
                        contentType: contentTypeJSON,
-                       status:      http.StatusNotImplemented,
+                       status:      http.StatusBadRequest,
                },
+               notFoundTest,
+               malformedJSONTest,
        }
 
        runTests(assert, api, tests)
diff --git a/server/api/v1/testdata/worker-set-groups-POST.json b/server/api/v1/testdata/worker-set-groups-POST.json
deleted file mode 100644 (file)
index c4315bb..0000000
+++ /dev/null
@@ -1 +0,0 @@
-{"error":"not implemented yet: set worker groups"}
\ No newline at end of file
diff --git a/server/api/v1/testdata/worker-set-groups-bad-uuid-POST.json b/server/api/v1/testdata/worker-set-groups-bad-uuid-POST.json
new file mode 100644 (file)
index 0000000..4145754
--- /dev/null
@@ -0,0 +1 @@
+{"error":"invalid request: ID provided in URL isn't valid UUID"}
\ No newline at end of file
diff --git a/server/api/v1/testdata/worker-set-groups-malformed-json-POST.json b/server/api/v1/testdata/worker-set-groups-malformed-json-POST.json
new file mode 100644 (file)
index 0000000..c59dde1
--- /dev/null
@@ -0,0 +1 @@
+{"error":"invalid request: unexpected EOF"}
\ No newline at end of file
diff --git a/server/api/v1/testdata/worker-set-groups-missing-POST.json b/server/api/v1/testdata/worker-set-groups-missing-POST.json
new file mode 100644 (file)
index 0000000..533be90
--- /dev/null
@@ -0,0 +1 @@
+{"error":"Worker not found"}
\ No newline at end of file
diff --git a/server/api/v1/testdata/worker-set-groups-valid-POST.json b/server/api/v1/testdata/worker-set-groups-valid-POST.json
new file mode 100644 (file)
index 0000000..e69de29