Add GroupMinimum to the SetupKey response

This commit is contained in:
braginini
2022-09-12 14:30:46 +02:00
parent be7d829858
commit 03d0f62ccd
5 changed files with 38 additions and 10 deletions

View File

@@ -138,7 +138,7 @@ components:
description: Setup key groups to auto-assign to peers registered with this key description: Setup key groups to auto-assign to peers registered with this key
type: array type: array
items: items:
type: string $ref: '#/components/schemas/GroupMinimum'
updated_at: updated_at:
description: Setup key last update date description: Setup key last update date
type: string type: string

View File

@@ -300,7 +300,7 @@ type RulePatchOperationPath string
// SetupKey defines model for SetupKey. // SetupKey defines model for SetupKey.
type SetupKey struct { type SetupKey struct {
// Setup key groups to auto-assign to peers registered with this key // 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 // Setup Key expiration date
Expires time.Time `json:"expires"` Expires time.Time `json:"expires"`

View File

@@ -344,6 +344,14 @@ func peerIPsToKeys(account *server.Account, peerIPs *[]string) []string {
return mappedPeerKeys 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 { func toGroupResponse(account *server.Account, group *server.Group) *api.Group {
cache := make(map[string]api.PeerMinimum) cache := make(map[string]api.PeerMinimum)
gr := api.Group{ gr := api.Group{

View File

@@ -181,9 +181,17 @@ func (h *SetupKeys) GetAllSetupKeysHandler(w http.ResponseWriter, r *http.Reques
http.Redirect(w, r, "/", http.StatusInternalServerError) http.Redirect(w, r, "/", http.StatusInternalServerError)
return 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) apiSetupKeys := make([]*api.SetupKey, 0)
for _, key := range setupKeys { for _, key := range setupKeys {
apiSetupKeys = append(apiSetupKeys, toResponseBody(key)) apiSetupKeys = append(apiSetupKeys, toResponseBody(groups, key))
} }
writeJSONObject(w, apiSetupKeys) 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) { func writeSuccess(w http.ResponseWriter, key *server.SetupKey) {
w.WriteHeader(200) w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json") 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 { if err != nil {
http.Error(w, "failed handling request", http.StatusInternalServerError) http.Error(w, "failed handling request", http.StatusInternalServerError)
return 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 var state string
if key.IsExpired() { if key.IsExpired() {
state = "expired" state = "expired"
@@ -211,6 +220,17 @@ func toResponseBody(key *server.SetupKey) *api.SetupKey {
state = "valid" 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{ return &api.SetupKey{
Id: key.Id, Id: key.Id,
Key: key.Key, Key: key.Key,
@@ -222,7 +242,7 @@ func toResponseBody(key *server.SetupKey) *api.SetupKey {
UsedTimes: key.UsedTimes, UsedTimes: key.UsedTimes,
LastUsed: key.LastUsed, LastUsed: key.LastUsed,
State: state, State: state,
AutoGroups: key.AutoGroups, AutoGroups: autoGroups,
UpdatedAt: key.UpdatedAt, UpdatedAt: key.UpdatedAt,
} }
} }

View File

@@ -110,7 +110,7 @@ func TestSetupKeysHandlers(t *testing.T) {
requestPath: "/api/setup-keys", requestPath: "/api/setup-keys",
expectedStatus: http.StatusOK, expectedStatus: http.StatusOK,
expectedBody: true, expectedBody: true,
expectedSetupKeys: []*api.SetupKey{toResponseBody(defaultSetupKey)}, expectedSetupKeys: []*api.SetupKey{toResponseBody(nil, defaultSetupKey)},
}, },
{ {
name: "Get Existing Setup Key", name: "Get Existing Setup Key",
@@ -118,7 +118,7 @@ func TestSetupKeysHandlers(t *testing.T) {
requestPath: "/api/setup-keys/" + existingSetupKeyID, requestPath: "/api/setup-keys/" + existingSetupKeyID,
expectedStatus: http.StatusOK, expectedStatus: http.StatusOK,
expectedBody: true, expectedBody: true,
expectedSetupKey: toResponseBody(defaultSetupKey), expectedSetupKey: toResponseBody(nil, defaultSetupKey),
}, },
{ {
name: "Get Not Existing Setup Key", 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))), []byte(fmt.Sprintf("{\"name\":\"%s\",\"type\":\"%s\"}", newSetupKey.Name, newSetupKey.Type))),
expectedStatus: http.StatusOK, expectedStatus: http.StatusOK,
expectedBody: true, expectedBody: true,
expectedSetupKey: toResponseBody(newSetupKey), expectedSetupKey: toResponseBody(nil, newSetupKey),
}, },
{ {
name: "Update Setup Key", name: "Update Setup Key",
@@ -149,7 +149,7 @@ func TestSetupKeysHandlers(t *testing.T) {
))), ))),
expectedStatus: http.StatusOK, expectedStatus: http.StatusOK,
expectedBody: true, expectedBody: true,
expectedSetupKey: toResponseBody(updatedDefaultSetupKey), expectedSetupKey: toResponseBody(nil, updatedDefaultSetupKey),
}, },
} }