From 131d9f1bc790d47bbdf0ce25d57b9f83aeaf1b8e Mon Sep 17 00:00:00 2001 From: pascal-fischer <32096965+pascal-fischer@users.noreply.github.com> Date: Fri, 19 Jan 2024 15:41:27 +0100 Subject: [PATCH] Add getGroupByName method (#1481) * add get group by name method to account manager * remove contains function and add proper description for GetGroupByName method * add to mock server --- management/server/account.go | 1 + management/server/group.go | 33 +++++++++++++++++++ management/server/mock_server/account_mock.go | 9 +++++ 3 files changed, 43 insertions(+) diff --git a/management/server/account.go b/management/server/account.go index 558c6968c..704616bb0 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -87,6 +87,7 @@ type AccountManager interface { UpdatePeerSSHKey(peerID string, sshKey string) error GetUsersFromAccount(accountID, userID string) ([]*UserInfo, error) GetGroup(accountId, groupID string) (*Group, error) + GetGroupByName(groupName, accountID string) (*Group, error) SaveGroup(accountID, userID string, group *Group) error DeleteGroup(accountId, userId, groupID string) error ListGroups(accountId string) ([]*Group, error) diff --git a/management/server/group.go b/management/server/group.go index cf7320c29..95d0ab306 100644 --- a/management/server/group.go +++ b/management/server/group.go @@ -73,6 +73,39 @@ func (am *DefaultAccountManager) GetGroup(accountID, groupID string) (*Group, er return nil, status.Errorf(status.NotFound, "group with ID %s not found", groupID) } +// GetGroupByName filters all groups in an account by name and returns the one with the most peers +func (am *DefaultAccountManager) GetGroupByName(groupName, accountID string) (*Group, error) { + unlock := am.Store.AcquireAccountLock(accountID) + defer unlock() + + account, err := am.Store.GetAccount(accountID) + if err != nil { + return nil, err + } + + matchingGroups := make([]*Group, 0) + for _, group := range account.Groups { + if group.Name == groupName { + matchingGroups = append(matchingGroups, group) + } + } + + if len(matchingGroups) == 0 { + return nil, status.Errorf(status.NotFound, "group with name %s not found", groupName) + } + + maxPeers := -1 + var groupWithMostPeers *Group + for i, group := range matchingGroups { + if len(group.Peers) > maxPeers { + maxPeers = len(group.Peers) + groupWithMostPeers = matchingGroups[i] + } + } + + return groupWithMostPeers, nil +} + // SaveGroup object of the peers func (am *DefaultAccountManager) SaveGroup(accountID, userID string, newGroup *Group) error { unlock := am.Store.AcquireAccountLock(accountID) diff --git a/management/server/mock_server/account_mock.go b/management/server/mock_server/account_mock.go index 4ca68ac21..f337ef1cf 100644 --- a/management/server/mock_server/account_mock.go +++ b/management/server/mock_server/account_mock.go @@ -29,6 +29,7 @@ type MockAccountManager struct { GetPeerNetworkFunc func(peerKey string) (*server.Network, error) AddPeerFunc func(setupKey string, userId string, peer *nbpeer.Peer) (*nbpeer.Peer, *server.NetworkMap, error) GetGroupFunc func(accountID, groupID string) (*server.Group, error) + GetGroupByNameFunc func(accountID, groupName string) (*server.Group, error) SaveGroupFunc func(accountID, userID string, group *server.Group) error DeleteGroupFunc func(accountID, userId, groupID string) error ListGroupsFunc func(accountID string) ([]*server.Group, error) @@ -245,6 +246,14 @@ func (am *MockAccountManager) GetGroup(accountID, groupID string) (*server.Group return nil, status.Errorf(codes.Unimplemented, "method GetGroup is not implemented") } +// GetGroupByName mock implementation of GetGroupByName from server.AccountManager interface +func (am *MockAccountManager) GetGroupByName(accountID, groupName string) (*server.Group, error) { + if am.GetGroupFunc != nil { + return am.GetGroupByNameFunc(accountID, groupName) + } + return nil, status.Errorf(codes.Unimplemented, "method GetGroupByName is not implemented") +} + // SaveGroup mock implementation of SaveGroup from server.AccountManager interface func (am *MockAccountManager) SaveGroup(accountID, userID string, group *server.Group) error { if am.SaveGroupFunc != nil {