Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] properly add and remove resources from groups #3067

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions management/cmd/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ var (
userManager := users.NewManager(store)
settingsManager := settings.NewManager(store)
permissionsManager := permissions.NewManager(userManager, settingsManager)
resourcesManager := resources.NewManager(store, permissionsManager, accountManager)
groupsManager := groups.NewManager(store, permissionsManager)
resourcesManager := resources.NewManager(store, permissionsManager, groupsManager, accountManager)
routersManager := routers.NewManager(store, permissionsManager, accountManager)
networksManager := networks.NewManager(store, permissionsManager, resourcesManager)
groupsManager := groups.NewManager(store, permissionsManager)

httpAPIHandler, err := httpapi.APIHandler(ctx, accountManager, networksManager, resourcesManager, routersManager, groupsManager, geo, *jwtValidator, appMetrics, httpAPIAuthCfg, integratedPeerValidator)
if err != nil {
Expand Down
44 changes: 43 additions & 1 deletion management/server/groups/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import (

type Manager interface {
GetAllGroups(ctx context.Context, accountID, userID string) (map[string]*types.Group, error)
GetResourceGroupsInTransaction(ctx context.Context, transaction store.Store, lockingStrength store.LockingStrength, accountID, resourceID string) ([]*types.Group, error)
AddResourceToGroup(ctx context.Context, accountID, userID, groupID string, resourceID *types.Resource) error
AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID string, resourceID *types.Resource) error
RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) error
}

type managerImpl struct {
store store.Store
permissionsManager permissions.Manager
}

type mockManager struct {
}

func NewManager(store store.Store, permissionsManager permissions.Manager) Manager {
return &managerImpl{
store: store,
Expand Down Expand Up @@ -58,7 +64,19 @@ func (m *managerImpl) AddResourceToGroup(ctx context.Context, accountID, userID,
return err
}

return m.store.AddResourceToGroup(ctx, accountID, groupID, resource)
return m.AddResourceToGroupInTransaction(ctx, m.store, accountID, groupID, resource)
}

func (m *managerImpl) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID string, resource *types.Resource) error {
return transaction.AddResourceToGroup(ctx, accountID, groupID, resource)
}

func (m *managerImpl) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) error {
return transaction.RemoveResourceFromGroup(ctx, accountID, groupID, resourceID)
}

func (m *managerImpl) GetResourceGroupsInTransaction(ctx context.Context, transaction store.Store, lockingStrength store.LockingStrength, accountID, resourceID string) ([]*types.Group, error) {
return transaction.GetResourceGroups(ctx, lockingStrength, accountID, resourceID)
}

func ToGroupsInfo(groups map[string]*types.Group, id string) []api.GroupMinimum {
Expand Down Expand Up @@ -97,3 +115,27 @@ func ToGroupsInfo(groups map[string]*types.Group, id string) []api.GroupMinimum
}
return groupsInfo
}

func (m *mockManager) GetAllGroups(ctx context.Context, accountID, userID string) (map[string]*types.Group, error) {
return map[string]*types.Group{}, nil
}

func (m *mockManager) GetResourceGroupsInTransaction(ctx context.Context, transaction store.Store, lockingStrength store.LockingStrength, accountID, resourceID string) ([]*types.Group, error) {
return []*types.Group{}, nil
}

func (m *mockManager) AddResourceToGroup(ctx context.Context, accountID, userID, groupID string, resourceID *types.Resource) error {
return nil
}

func (m *mockManager) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID string, resourceID *types.Resource) error {
return nil
}

func (m *mockManager) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) error {
return nil
}

func NewManagerMock() Manager {
return &mockManager{}
}
25 changes: 0 additions & 25 deletions management/server/http/handlers/networks/resources_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/netbirdio/netbird/management/server/jwtclaims"
"github.com/netbirdio/netbird/management/server/networks/resources"
"github.com/netbirdio/netbird/management/server/networks/resources/types"
nbtypes "github.com/netbirdio/netbird/management/server/types"
)

type resourceHandler struct {
Expand Down Expand Up @@ -130,18 +129,6 @@ func (h *resourceHandler) createResource(w http.ResponseWriter, r *http.Request)
return
}

res := nbtypes.Resource{
ID: resource.ID,
Type: resource.Type.String(),
}
for _, groupID := range req.Groups {
err = h.groupsManager.AddResourceToGroup(r.Context(), accountID, userID, groupID, &res)
if err != nil {
util.WriteError(r.Context(), err, w)
return
}
}

grps, err := h.groupsManager.GetAllGroups(r.Context(), accountID, userID)
if err != nil {
util.WriteError(r.Context(), err, w)
Expand Down Expand Up @@ -205,18 +192,6 @@ func (h *resourceHandler) updateResource(w http.ResponseWriter, r *http.Request)
return
}

res := nbtypes.Resource{
ID: resource.ID,
Type: resource.Type.String(),
}
for _, groupID := range req.Groups {
err = h.groupsManager.AddResourceToGroup(r.Context(), accountID, userID, groupID, &res)
if err != nil {
util.WriteError(r.Context(), err, w)
return
}
}

grps, err := h.groupsManager.GetAllGroups(r.Context(), accountID, userID)
if err != nil {
util.WriteError(r.Context(), err, w)
Expand Down
31 changes: 21 additions & 10 deletions management/server/networks/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/netbirdio/netbird/management/server/groups"
"github.com/netbirdio/netbird/management/server/mock_server"
"github.com/netbirdio/netbird/management/server/networks/resources"
"github.com/netbirdio/netbird/management/server/networks/types"
Expand All @@ -25,7 +26,8 @@ func Test_GetAllNetworksReturnsNetworks(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

networks, err := manager.GetAllNetworks(ctx, accountID, userID)
Expand All @@ -46,7 +48,8 @@ func Test_GetAllNetworksReturnsPermissionDenied(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

networks, err := manager.GetAllNetworks(ctx, accountID, userID)
Expand All @@ -67,7 +70,8 @@ func Test_GetNetworkReturnsNetwork(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

networks, err := manager.GetNetwork(ctx, accountID, userID, networkID)
Expand All @@ -88,7 +92,8 @@ func Test_GetNetworkReturnsPermissionDenied(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

network, err := manager.GetNetwork(ctx, accountID, userID, networkID)
Expand All @@ -111,7 +116,8 @@ func Test_CreateNetworkSuccessfully(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

createdNetwork, err := manager.CreateNetwork(ctx, userID, network)
Expand All @@ -134,7 +140,8 @@ func Test_CreateNetworkFailsWithPermissionDenied(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

createdNetwork, err := manager.CreateNetwork(ctx, userID, network)
Expand All @@ -155,7 +162,8 @@ func Test_DeleteNetworkSuccessfully(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

err = manager.DeleteNetwork(ctx, accountID, userID, networkID)
Expand All @@ -175,7 +183,8 @@ func Test_DeleteNetworkFailsWithPermissionDenied(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

err = manager.DeleteNetwork(ctx, accountID, userID, networkID)
Expand All @@ -198,7 +207,8 @@ func Test_UpdateNetworkSuccessfully(t *testing.T) {
t.Cleanup(cleanUp)
am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

updatedNetwork, err := manager.UpdateNetwork(ctx, userID, network)
Expand All @@ -223,7 +233,8 @@ func Test_UpdateNetworkFailsWithPermissionDenied(t *testing.T) {

am := mock_server.MockAccountManager{}
permissionsManager := permissions.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, &am)
groupsManager := groups.NewManagerMock()
resourcesManager := resources.NewManager(s, permissionsManager, groupsManager, &am)
manager := NewManager(s, permissionsManager, resourcesManager)

updatedNetwork, err := manager.UpdateNetwork(ctx, userID, network)
Expand Down
Loading
Loading