From 03d0f62ccd675f4324bee2b60ba2aacf1769e02a Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 12 Sep 2022 14:30:46 +0200 Subject: [PATCH] Add GroupMinimum to the SetupKey response --- management/server/http/api/openapi.yml | 2 +- management/server/http/api/types.gen.go | 2 +- management/server/http/groups.go | 8 +++++++ management/server/http/setupkeys.go | 28 ++++++++++++++++++++---- management/server/http/setupkeys_test.go | 8 +++---- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/management/server/http/api/openapi.yml b/management/server/http/api/openapi.yml index c76bc9c9b..95aba61c0 100644 --- a/management/server/http/api/openapi.yml +++ b/management/server/http/api/openapi.yml @@ -138,7 +138,7 @@ components: description: Setup key groups to auto-assign to peers registered with this key type: array items: - type: string + $ref: '#/components/schemas/GroupMinimum' updated_at: description: Setup key last update date type: string diff --git a/management/server/http/api/types.gen.go b/management/server/http/api/types.gen.go index 8811ae348..b579ceeef 100644 --- a/management/server/http/api/types.gen.go +++ b/management/server/http/api/types.gen.go @@ -300,7 +300,7 @@ type RulePatchOperationPath string // SetupKey defines model for SetupKey. type SetupKey struct { // Setup key groups to auto-assign to peers registered with this key - AutoGroups []string `json:"auto_groups"` + AutoGroups []GroupMinimum `json:"auto_groups"` // Setup Key expiration date Expires time.Time `json:"expires"` diff --git a/management/server/http/groups.go b/management/server/http/groups.go index 185e0ec0c..7bc3f9426 100644 --- a/management/server/http/groups.go +++ b/management/server/http/groups.go @@ -344,6 +344,14 @@ func peerIPsToKeys(account *server.Account, peerIPs *[]string) []string { return mappedPeerKeys } +func toGroupMinimumResponse(group *server.Group) *api.GroupMinimum { + return &api.GroupMinimum{ + Id: group.ID, + Name: group.Name, + PeersCount: len(group.Peers), + } +} + func toGroupResponse(account *server.Account, group *server.Group) *api.Group { cache := make(map[string]api.PeerMinimum) gr := api.Group{ diff --git a/management/server/http/setupkeys.go b/management/server/http/setupkeys.go index 632b7afdb..74d2fdcca 100644 --- a/management/server/http/setupkeys.go +++ b/management/server/http/setupkeys.go @@ -181,9 +181,17 @@ func (h *SetupKeys) GetAllSetupKeysHandler(w http.ResponseWriter, r *http.Reques http.Redirect(w, r, "/", http.StatusInternalServerError) return } + + groups, err := h.accountManager.ListGroups(account.Id) + if err != nil { + log.Error(err) + http.Redirect(w, r, "/", http.StatusInternalServerError) + return + } + apiSetupKeys := make([]*api.SetupKey, 0) for _, key := range setupKeys { - apiSetupKeys = append(apiSetupKeys, toResponseBody(key)) + apiSetupKeys = append(apiSetupKeys, toResponseBody(groups, key)) } writeJSONObject(w, apiSetupKeys) @@ -192,14 +200,15 @@ func (h *SetupKeys) GetAllSetupKeysHandler(w http.ResponseWriter, r *http.Reques func writeSuccess(w http.ResponseWriter, key *server.SetupKey) { w.WriteHeader(200) w.Header().Set("Content-Type", "application/json") - err := json.NewEncoder(w).Encode(toResponseBody(key)) + err := json.NewEncoder(w).Encode(toResponseBody(nil, key)) if err != nil { http.Error(w, "failed handling request", http.StatusInternalServerError) return } } -func toResponseBody(key *server.SetupKey) *api.SetupKey { +// toResponseBody takes a list of all groups, a key, finds groups that belong to the key and add them to the response +func toResponseBody(groups []*server.Group, key *server.SetupKey) *api.SetupKey { var state string if key.IsExpired() { state = "expired" @@ -211,6 +220,17 @@ func toResponseBody(key *server.SetupKey) *api.SetupKey { state = "valid" } + // should be instantiated like that to ensure if empty, we return an empty array to the client + autoGroups := []api.GroupMinimum{} + for _, group := range groups { + for _, keyGroup := range key.AutoGroups { + if group.ID == keyGroup { + autoGroups = append(autoGroups, *toGroupMinimumResponse(group)) + break + } + } + } + return &api.SetupKey{ Id: key.Id, Key: key.Key, @@ -222,7 +242,7 @@ func toResponseBody(key *server.SetupKey) *api.SetupKey { UsedTimes: key.UsedTimes, LastUsed: key.LastUsed, State: state, - AutoGroups: key.AutoGroups, + AutoGroups: autoGroups, UpdatedAt: key.UpdatedAt, } } diff --git a/management/server/http/setupkeys_test.go b/management/server/http/setupkeys_test.go index c37702e47..127cb7bd1 100644 --- a/management/server/http/setupkeys_test.go +++ b/management/server/http/setupkeys_test.go @@ -110,7 +110,7 @@ func TestSetupKeysHandlers(t *testing.T) { requestPath: "/api/setup-keys", expectedStatus: http.StatusOK, expectedBody: true, - expectedSetupKeys: []*api.SetupKey{toResponseBody(defaultSetupKey)}, + expectedSetupKeys: []*api.SetupKey{toResponseBody(nil, defaultSetupKey)}, }, { name: "Get Existing Setup Key", @@ -118,7 +118,7 @@ func TestSetupKeysHandlers(t *testing.T) { requestPath: "/api/setup-keys/" + existingSetupKeyID, expectedStatus: http.StatusOK, expectedBody: true, - expectedSetupKey: toResponseBody(defaultSetupKey), + expectedSetupKey: toResponseBody(nil, defaultSetupKey), }, { name: "Get Not Existing Setup Key", @@ -135,7 +135,7 @@ func TestSetupKeysHandlers(t *testing.T) { []byte(fmt.Sprintf("{\"name\":\"%s\",\"type\":\"%s\"}", newSetupKey.Name, newSetupKey.Type))), expectedStatus: http.StatusOK, expectedBody: true, - expectedSetupKey: toResponseBody(newSetupKey), + expectedSetupKey: toResponseBody(nil, newSetupKey), }, { name: "Update Setup Key", @@ -149,7 +149,7 @@ func TestSetupKeysHandlers(t *testing.T) { ))), expectedStatus: http.StatusOK, expectedBody: true, - expectedSetupKey: toResponseBody(updatedDefaultSetupKey), + expectedSetupKey: toResponseBody(nil, updatedDefaultSetupKey), }, }