diff --git a/management/server/groups/manager.go b/management/server/groups/manager.go index 1162348bd07..f5abb212e13 100644 --- a/management/server/groups/manager.go +++ b/management/server/groups/manager.go @@ -16,8 +16,8 @@ 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) (func(), error) - RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) (func(), error) + AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID string, resourceID *types.Resource) (func(), error) + RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID, resourceID string) (func(), error) } type managerImpl struct { @@ -68,7 +68,7 @@ func (m *managerImpl) AddResourceToGroup(ctx context.Context, accountID, userID, return err } - event, err := m.AddResourceToGroupInTransaction(ctx, m.store, accountID, groupID, resource) + event, err := m.AddResourceToGroupInTransaction(ctx, m.store, accountID, userID, groupID, resource) if err != nil { return fmt.Errorf("error adding resource to group: %w", err) } @@ -78,27 +78,49 @@ func (m *managerImpl) AddResourceToGroup(ctx context.Context, accountID, userID, return nil } -func (m *managerImpl) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID string, resource *types.Resource) (func(), error) { +func (m *managerImpl) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID string, resource *types.Resource) (func(), error) { err := transaction.AddResourceToGroup(ctx, accountID, groupID, resource) if err != nil { return nil, fmt.Errorf("error adding resource to group: %w", err) } + group, err := transaction.GetGroupByID(ctx, store.LockingStrengthShare, accountID, groupID) + if err != nil { + return nil, fmt.Errorf("error getting group: %w", err) + } + + // TODO: at some point, this will need to become a switch statement + networkResource, err := transaction.GetNetworkResourceByID(ctx, store.LockingStrengthShare, accountID, resource.ID) + if err != nil { + return nil, fmt.Errorf("error getting network resource: %w", err) + } + event := func() { - m.accountManager.StoreEvent(ctx, accountID, groupID, accountID, activity.ResourceAddedToGroup, nil) + m.accountManager.StoreEvent(ctx, userID, groupID, accountID, activity.ResourceAddedToGroup, group.EventMetaResource(networkResource)) } return event, nil } -func (m *managerImpl) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) (func(), error) { +func (m *managerImpl) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID, resourceID string) (func(), error) { err := transaction.RemoveResourceFromGroup(ctx, accountID, groupID, resourceID) if err != nil { return nil, fmt.Errorf("error removing resource from group: %w", err) } + group, err := transaction.GetGroupByID(ctx, store.LockingStrengthShare, accountID, groupID) + if err != nil { + return nil, fmt.Errorf("error getting group: %w", err) + } + + // TODO: at some point, this will need to become a switch statement + networkResource, err := transaction.GetNetworkResourceByID(ctx, store.LockingStrengthShare, accountID, resourceID) + if err != nil { + return nil, fmt.Errorf("error getting network resource: %w", err) + } + event := func() { - m.accountManager.StoreEvent(ctx, accountID, groupID, accountID, activity.ResourceRemovedFromGroup, nil) + m.accountManager.StoreEvent(ctx, userID, groupID, accountID, activity.ResourceRemovedFromGroup, group.EventMetaResource(networkResource)) } return event, nil @@ -157,13 +179,13 @@ func (m *mockManager) AddResourceToGroup(ctx context.Context, accountID, userID, return nil } -func (m *mockManager) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID string, resourceID *types.Resource) (func(), error) { +func (m *mockManager) AddResourceToGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID string, resourceID *types.Resource) (func(), error) { return func() { // noop }, nil } -func (m *mockManager) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, groupID, resourceID string) (func(), error) { +func (m *mockManager) RemoveResourceFromGroupInTransaction(ctx context.Context, transaction store.Store, accountID, userID, groupID, resourceID string) (func(), error) { return func() { // noop }, nil diff --git a/management/server/networks/manager.go b/management/server/networks/manager.go index 00ff7fe3e0b..4a7b3db775c 100644 --- a/management/server/networks/manager.go +++ b/management/server/networks/manager.go @@ -137,7 +137,7 @@ func (m *managerImpl) DeleteNetwork(ctx context.Context, accountID, userID, netw } for _, resource := range resources { - event, err := m.resourcesManager.DeleteResourceInTransaction(ctx, transaction, accountID, networkID, resource.ID) + event, err := m.resourcesManager.DeleteResourceInTransaction(ctx, transaction, accountID, userID, networkID, resource.ID) if err != nil { return fmt.Errorf("failed to delete resource: %w", err) } @@ -150,7 +150,7 @@ func (m *managerImpl) DeleteNetwork(ctx context.Context, accountID, userID, netw } for _, router := range routers { - event, err := m.routersManager.DeleteRouterInTransaction(ctx, transaction, accountID, networkID, router.ID) + event, err := m.routersManager.DeleteRouterInTransaction(ctx, transaction, accountID, userID, networkID, router.ID) if err != nil { return fmt.Errorf("failed to delete router: %w", err) } diff --git a/management/server/networks/resources/manager.go b/management/server/networks/resources/manager.go index e7fd099ceda..0fff5bcf8e9 100644 --- a/management/server/networks/resources/manager.go +++ b/management/server/networks/resources/manager.go @@ -24,7 +24,7 @@ type Manager interface { GetResource(ctx context.Context, accountID, userID, networkID, resourceID string) (*types.NetworkResource, error) UpdateResource(ctx context.Context, userID string, resource *types.NetworkResource) (*types.NetworkResource, error) DeleteResource(ctx context.Context, accountID, userID, networkID, resourceID string) error - DeleteResourceInTransaction(ctx context.Context, transaction store.Store, accountID, networkID, resourceID string) ([]func(), error) + DeleteResourceInTransaction(ctx context.Context, transaction store.Store, accountID, userID, networkID, resourceID string) ([]func(), error) } type managerImpl struct { @@ -124,7 +124,7 @@ func (m *managerImpl) CreateResource(ctx context.Context, userID string, resourc } event := func() { - m.accountManager.StoreEvent(ctx, userID, resource.ID, resource.AccountID, activity.NetworkResourceCreated, resource.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, resource.ID, resource.AccountID, activity.NetworkResourceCreated, resource.EventMeta(network)) } eventsToStore = append(eventsToStore, event) @@ -133,7 +133,7 @@ func (m *managerImpl) CreateResource(ctx context.Context, userID string, resourc Type: resource.Type.String(), } for _, groupID := range resource.GroupIDs { - event, err := m.groupsManager.AddResourceToGroupInTransaction(ctx, transaction, resource.AccountID, groupID, &res) + event, err := m.groupsManager.AddResourceToGroupInTransaction(ctx, transaction, resource.AccountID, userID, groupID, &res) if err != nil { return fmt.Errorf("failed to add resource to group: %w", err) } @@ -233,14 +233,14 @@ func (m *managerImpl) UpdateResource(ctx context.Context, userID string, resourc return fmt.Errorf("failed to save network resource: %w", err) } - events, err := m.updateResourceGroups(ctx, transaction, resource, oldResource) + events, err := m.updateResourceGroups(ctx, transaction, userID, resource, oldResource) if err != nil { return fmt.Errorf("failed to update resource groups: %w", err) } eventsToStore = append(eventsToStore, events...) eventsToStore = append(eventsToStore, func() { - m.accountManager.StoreEvent(ctx, userID, resource.ID, resource.AccountID, activity.NetworkResourceUpdated, resource.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, resource.ID, resource.AccountID, activity.NetworkResourceUpdated, resource.EventMeta(network)) }) err = transaction.IncrementNetworkSerial(ctx, store.LockingStrengthUpdate, resource.AccountID) @@ -263,7 +263,7 @@ func (m *managerImpl) UpdateResource(ctx context.Context, userID string, resourc return resource, nil } -func (m *managerImpl) updateResourceGroups(ctx context.Context, transaction store.Store, newResource, oldResource *types.NetworkResource) ([]func(), error) { +func (m *managerImpl) updateResourceGroups(ctx context.Context, transaction store.Store, userID string, newResource, oldResource *types.NetworkResource) ([]func(), error) { res := nbtypes.Resource{ ID: newResource.ID, Type: newResource.Type.String(), @@ -282,7 +282,7 @@ func (m *managerImpl) updateResourceGroups(ctx context.Context, transaction stor var eventsToStore []func() groupsToAdd := util.Difference(newResource.GroupIDs, oldGroupsIds) for _, groupID := range groupsToAdd { - events, err := m.groupsManager.AddResourceToGroupInTransaction(ctx, transaction, newResource.AccountID, groupID, &res) + events, err := m.groupsManager.AddResourceToGroupInTransaction(ctx, transaction, newResource.AccountID, userID, groupID, &res) if err != nil { return nil, fmt.Errorf("failed to add resource to group: %w", err) } @@ -291,7 +291,7 @@ func (m *managerImpl) updateResourceGroups(ctx context.Context, transaction stor groupsToRemove := util.Difference(oldGroupsIds, newResource.GroupIDs) for _, groupID := range groupsToRemove { - events, err := m.groupsManager.RemoveResourceFromGroupInTransaction(ctx, transaction, newResource.AccountID, groupID, res.ID) + events, err := m.groupsManager.RemoveResourceFromGroupInTransaction(ctx, transaction, newResource.AccountID, userID, groupID, res.ID) if err != nil { return nil, fmt.Errorf("failed to add resource to group: %w", err) } @@ -315,7 +315,7 @@ func (m *managerImpl) DeleteResource(ctx context.Context, accountID, userID, net var events []func() err = m.store.ExecuteInTransaction(ctx, func(transaction store.Store) error { - events, err = m.DeleteResourceInTransaction(ctx, transaction, accountID, networkID, resourceID) + events, err = m.DeleteResourceInTransaction(ctx, transaction, accountID, userID, networkID, resourceID) if err != nil { return fmt.Errorf("failed to delete resource: %w", err) } @@ -340,7 +340,7 @@ func (m *managerImpl) DeleteResource(ctx context.Context, accountID, userID, net return nil } -func (m *managerImpl) DeleteResourceInTransaction(ctx context.Context, transaction store.Store, accountID, networkID, resourceID string) ([]func(), error) { +func (m *managerImpl) DeleteResourceInTransaction(ctx context.Context, transaction store.Store, accountID, userID, networkID, resourceID string) ([]func(), error) { resource, err := transaction.GetNetworkResourceByID(ctx, store.LockingStrengthUpdate, accountID, resourceID) if err != nil { return nil, fmt.Errorf("failed to get network resource: %w", err) @@ -363,7 +363,7 @@ func (m *managerImpl) DeleteResourceInTransaction(ctx context.Context, transacti var eventsToStore []func() for _, group := range groups { - event, err := m.groupsManager.RemoveResourceFromGroupInTransaction(ctx, transaction, accountID, group.ID, resourceID) + event, err := m.groupsManager.RemoveResourceFromGroupInTransaction(ctx, transaction, accountID, userID, group.ID, resourceID) if err != nil { return nil, fmt.Errorf("failed to remove resource from group: %w", err) } @@ -376,7 +376,7 @@ func (m *managerImpl) DeleteResourceInTransaction(ctx context.Context, transacti } eventsToStore = append(eventsToStore, func() { - m.accountManager.StoreEvent(ctx, accountID, resourceID, accountID, activity.NetworkResourceDeleted, resource.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, resourceID, accountID, activity.NetworkResourceDeleted, resource.EventMeta(network)) }) return eventsToStore, nil diff --git a/management/server/networks/resources/types/resource.go b/management/server/networks/resources/types/resource.go index 88735e5b4e8..7eecdce0fef 100644 --- a/management/server/networks/resources/types/resource.go +++ b/management/server/networks/resources/types/resource.go @@ -10,6 +10,7 @@ import ( nbDomain "github.com/netbirdio/netbird/management/domain" routerTypes "github.com/netbirdio/netbird/management/server/networks/routers/types" + networkTypes "github.com/netbirdio/netbird/management/server/networks/types" nbpeer "github.com/netbirdio/netbird/management/server/peer" "github.com/netbirdio/netbird/route" @@ -142,8 +143,8 @@ func (n *NetworkResource) ToRoute(peer *nbpeer.Peer, router *routerTypes.Network return r } -func (n *NetworkResource) EventMeta(networkName string) map[string]any { - return map[string]any{"name": n.Name, "type": n.Type, "network_name": networkName} +func (n *NetworkResource) EventMeta(network *networkTypes.Network) map[string]any { + return map[string]any{"name": n.Name, "type": n.Type, "network_name": network.Name, "network_id": network.ID} } // GetResourceType returns the type of the resource based on the address diff --git a/management/server/networks/routers/manager.go b/management/server/networks/routers/manager.go index 37ba38fcf96..3b32810a2c6 100644 --- a/management/server/networks/routers/manager.go +++ b/management/server/networks/routers/manager.go @@ -23,7 +23,7 @@ type Manager interface { GetRouter(ctx context.Context, accountID, userID, networkID, routerID string) (*types.NetworkRouter, error) UpdateRouter(ctx context.Context, userID string, router *types.NetworkRouter) (*types.NetworkRouter, error) DeleteRouter(ctx context.Context, accountID, userID, networkID, routerID string) error - DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, networkID, routerID string) (func(), error) + DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, userID, networkID, routerID string) (func(), error) } type managerImpl struct { @@ -118,7 +118,7 @@ func (m *managerImpl) CreateRouter(ctx context.Context, userID string, router *t return nil, err } - m.accountManager.StoreEvent(ctx, userID, router.ID, router.AccountID, activity.NetworkRouterCreated, router.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, router.ID, router.AccountID, activity.NetworkRouterCreated, router.EventMeta(network)) go m.accountManager.UpdateAccountPeers(ctx, router.AccountID) @@ -185,7 +185,7 @@ func (m *managerImpl) UpdateRouter(ctx context.Context, userID string, router *t return nil, err } - m.accountManager.StoreEvent(ctx, userID, router.ID, router.AccountID, activity.NetworkRouterUpdated, router.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, router.ID, router.AccountID, activity.NetworkRouterUpdated, router.EventMeta(network)) go m.accountManager.UpdateAccountPeers(ctx, router.AccountID) @@ -206,7 +206,7 @@ func (m *managerImpl) DeleteRouter(ctx context.Context, accountID, userID, netwo var event func() err = m.store.ExecuteInTransaction(ctx, func(transaction store.Store) error { - event, err = m.DeleteRouterInTransaction(ctx, transaction, accountID, networkID, routerID) + event, err = m.DeleteRouterInTransaction(ctx, transaction, accountID, userID, networkID, routerID) if err != nil { return fmt.Errorf("failed to delete network router: %w", err) } @@ -229,7 +229,7 @@ func (m *managerImpl) DeleteRouter(ctx context.Context, accountID, userID, netwo return nil } -func (m *managerImpl) DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, networkID, routerID string) (func(), error) { +func (m *managerImpl) DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, userID, networkID, routerID string) (func(), error) { network, err := transaction.GetNetworkByID(ctx, store.LockingStrengthShare, accountID, networkID) if err != nil { return nil, fmt.Errorf("failed to get network: %w", err) @@ -250,7 +250,7 @@ func (m *managerImpl) DeleteRouterInTransaction(ctx context.Context, transaction } event := func() { - m.accountManager.StoreEvent(ctx, "", routerID, accountID, activity.NetworkRouterDeleted, router.EventMeta(network.Name)) + m.accountManager.StoreEvent(ctx, userID, routerID, accountID, activity.NetworkRouterDeleted, router.EventMeta(network)) } return event, nil @@ -284,6 +284,6 @@ func (m *mockManager) DeleteRouter(ctx context.Context, accountID, userID, netwo return nil } -func (m *mockManager) DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, networkID, routerID string) (func(), error) { +func (m *mockManager) DeleteRouterInTransaction(ctx context.Context, transaction store.Store, accountID, userID, networkID, routerID string) (func(), error) { return func() {}, nil } diff --git a/management/server/networks/routers/types/router.go b/management/server/networks/routers/types/router.go index 4c2e11e905b..f37ae0861a0 100644 --- a/management/server/networks/routers/types/router.go +++ b/management/server/networks/routers/types/router.go @@ -6,6 +6,7 @@ import ( "github.com/rs/xid" "github.com/netbirdio/netbird/management/server/http/api" + "github.com/netbirdio/netbird/management/server/networks/types" ) type NetworkRouter struct { @@ -69,6 +70,6 @@ func (n *NetworkRouter) Copy() *NetworkRouter { } } -func (n *NetworkRouter) EventMeta(networkName string) map[string]any { - return map[string]any{"network_name": networkName} +func (n *NetworkRouter) EventMeta(network *types.Network) map[string]any { + return map[string]any{"network_name": network.Name, "network_id": network.ID, "peer": n.Peer, "peer_groups": n.PeerGroups} } diff --git a/management/server/types/group.go b/management/server/types/group.go index 462f702a98d..00a28fa7702 100644 --- a/management/server/types/group.go +++ b/management/server/types/group.go @@ -2,6 +2,7 @@ package types import ( "github.com/netbirdio/netbird/management/server/integration_reference" + "github.com/netbirdio/netbird/management/server/networks/resources/types" ) const ( @@ -38,6 +39,10 @@ func (g *Group) EventMeta() map[string]any { return map[string]any{"name": g.Name} } +func (g *Group) EventMetaResource(resource *types.NetworkResource) map[string]any { + return map[string]any{"name": g.Name, "id": g.ID, "resource_name": resource.Name, "resource_id": resource.ID, "resource_type": resource.Type} +} + func (g *Group) Copy() *Group { group := &Group{ ID: g.ID,