diff --git a/management/server/account.go b/management/server/account.go index 7d0cfe9a6d0..ef5e9c7d7c2 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -19,9 +19,6 @@ import ( "github.com/eko/gocache/v3/cache" cacheStore "github.com/eko/gocache/v3/store" - "github.com/hashicorp/go-multierror" - "github.com/miekg/dns" - "github.com/netbirdio/netbird/management/server/networks" gocache "github.com/patrickmn/go-cache" "github.com/rs/xid" log "github.com/sirupsen/logrus" diff --git a/management/server/account_test.go b/management/server/account_test.go index f62eeb7af59..32cc1f2900a 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -16,7 +16,11 @@ import ( "time" "github.com/golang-jwt/jwt" - "github.com/netbirdio/netbird/management/server/networks" + + resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types" + routerTypes "github.com/netbirdio/netbird/management/server/networks/routers/types" + networkTypes "github.com/netbirdio/netbird/management/server/networks/types" + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -1790,12 +1794,12 @@ func TestAccount_Copy(t *testing.T) { }, }, Settings: &types.Settings{}, - Networks: []*networks.Network{ + Networks: []*networkTypes.Network{ { ID: "network1", }, }, - NetworkRouters: []*networks.NetworkRouter{ + NetworkRouters: []*routerTypes.NetworkRouter{ { ID: "router1", NetworkID: "network1", @@ -1804,7 +1808,7 @@ func TestAccount_Copy(t *testing.T) { Metric: 0, }, }, - NetworkResources: []*networks.NetworkResource{ + NetworkResources: []*resourceTypes.NetworkResource{ { ID: "resource1", NetworkID: "network1", diff --git a/management/server/http/handlers/networks/handler.go b/management/server/http/handlers/networks/handler.go index 7d19f84665f..1b49b10b3ec 100644 --- a/management/server/http/handlers/networks/handler.go +++ b/management/server/http/handlers/networks/handler.go @@ -12,6 +12,7 @@ import ( "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/networks" + "github.com/netbirdio/netbird/management/server/networks/types" "github.com/netbirdio/netbird/management/server/status" ) @@ -81,7 +82,7 @@ func (h *handler) createNetwork(w http.ResponseWriter, r *http.Request) { return } - network := &networks.Network{} + network := &types.Network{} network.FromAPIRequest(&req) network.AccountID = accountID @@ -140,7 +141,7 @@ func (h *handler) updateNetwork(w http.ResponseWriter, r *http.Request) { return } - network := &networks.Network{} + network := &types.Network{} network.FromAPIRequest(&req) network.ID = networkID diff --git a/management/server/http/handlers/networks/resources_handler.go b/management/server/http/handlers/networks/resources_handler.go index 0b3214ba02b..c6a5cc211cf 100644 --- a/management/server/http/handlers/networks/resources_handler.go +++ b/management/server/http/handlers/networks/resources_handler.go @@ -12,6 +12,7 @@ import ( "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/networks/resources" + "github.com/netbirdio/netbird/management/server/networks/resources/types" ) type resourceHandler struct { @@ -78,7 +79,7 @@ func (h *resourceHandler) createResource(w http.ResponseWriter, r *http.Request) return } - resource := &resources.NetworkResource{} + resource := &types.NetworkResource{} resource.FromAPIRequest(&req) resource.NetworkID = mux.Vars(r)["networkId"] @@ -126,7 +127,7 @@ func (h *resourceHandler) updateResource(w http.ResponseWriter, r *http.Request) return } - resource := &resources.NetworkResource{} + resource := &types.NetworkResource{} resource.FromAPIRequest(&req) resource.ID = mux.Vars(r)["resourceId"] diff --git a/management/server/http/handlers/networks/routers_handler.go b/management/server/http/handlers/networks/routers_handler.go index 2129df3f73a..20f67a17751 100644 --- a/management/server/http/handlers/networks/routers_handler.go +++ b/management/server/http/handlers/networks/routers_handler.go @@ -12,6 +12,7 @@ import ( "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/networks/routers" + "github.com/netbirdio/netbird/management/server/networks/routers/types" ) type routersHandler struct { @@ -79,7 +80,7 @@ func (h *routersHandler) createRouter(w http.ResponseWriter, r *http.Request) { return } - router := &routers.NetworkRouter{} + router := &types.NetworkRouter{} router.FromAPIRequest(&req) router.NetworkID = networkID @@ -128,7 +129,7 @@ func (h *routersHandler) updateRouter(w http.ResponseWriter, r *http.Request) { return } - router := &routers.NetworkRouter{} + router := &types.NetworkRouter{} router.FromAPIRequest(&req) router.NetworkID = mux.Vars(r)["networkId"] diff --git a/management/server/networks/manager.go b/management/server/networks/manager.go index 5086ca19be0..651da70a6ee 100644 --- a/management/server/networks/manager.go +++ b/management/server/networks/manager.go @@ -6,14 +6,15 @@ import ( "github.com/netbirdio/netbird/management/server/networks/resources" "github.com/netbirdio/netbird/management/server/networks/routers" + "github.com/netbirdio/netbird/management/server/networks/types" "github.com/netbirdio/netbird/management/server/store" ) type Manager interface { - GetAllNetworks(ctx context.Context, accountID, userID string) ([]*Network, error) - CreateNetwork(ctx context.Context, userID string, network *Network) (*Network, error) - GetNetwork(ctx context.Context, accountID, userID, networkID string) (*Network, error) - UpdateNetwork(ctx context.Context, userID string, network *Network) (*Network, error) + GetAllNetworks(ctx context.Context, accountID, userID string) ([]*types.Network, error) + CreateNetwork(ctx context.Context, userID string, network *types.Network) (*types.Network, error) + GetNetwork(ctx context.Context, accountID, userID, networkID string) (*types.Network, error) + UpdateNetwork(ctx context.Context, userID string, network *types.Network) (*types.Network, error) DeleteNetwork(ctx context.Context, accountID, userID, networkID string) error GetResourceManager() resources.Manager GetRouterManager() routers.Manager @@ -33,19 +34,19 @@ func NewManager(store store.Store) Manager { } } -func (m *managerImpl) GetAllNetworks(ctx context.Context, accountID, userID string) ([]*Network, error) { +func (m *managerImpl) GetAllNetworks(ctx context.Context, accountID, userID string) ([]*types.Network, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) CreateNetwork(ctx context.Context, userID string, network *Network) (*Network, error) { +func (m *managerImpl) CreateNetwork(ctx context.Context, userID string, network *types.Network) (*types.Network, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) GetNetwork(ctx context.Context, accountID, userID, networkID string) (*Network, error) { +func (m *managerImpl) GetNetwork(ctx context.Context, accountID, userID, networkID string) (*types.Network, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) UpdateNetwork(ctx context.Context, userID string, network *Network) (*Network, error) { +func (m *managerImpl) UpdateNetwork(ctx context.Context, userID string, network *types.Network) (*types.Network, error) { return nil, errors.New("not implemented") } diff --git a/management/server/networks/resources/manager.go b/management/server/networks/resources/manager.go index b5ac22f3885..cfed34e517c 100644 --- a/management/server/networks/resources/manager.go +++ b/management/server/networks/resources/manager.go @@ -4,14 +4,15 @@ import ( "context" "errors" + "github.com/netbirdio/netbird/management/server/networks/resources/types" "github.com/netbirdio/netbird/management/server/store" ) type Manager interface { - GetAllResources(ctx context.Context, accountID, userID, networkID string) ([]*NetworkResource, error) - CreateResource(ctx context.Context, accountID string, resource *NetworkResource) (*NetworkResource, error) - GetResource(ctx context.Context, accountID, userID, networkID, resourceID string) (*NetworkResource, error) - UpdateResource(ctx context.Context, userID string, resource *NetworkResource) (*NetworkResource, error) + GetAllResources(ctx context.Context, accountID, userID, networkID string) ([]*types.NetworkResource, error) + CreateResource(ctx context.Context, accountID string, resource *types.NetworkResource) (*types.NetworkResource, error) + 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 } @@ -25,19 +26,19 @@ func NewManager(store store.Store) Manager { } } -func (m *managerImpl) GetAllResources(ctx context.Context, accountID, userID, networkID string) ([]*NetworkResource, error) { +func (m *managerImpl) GetAllResources(ctx context.Context, accountID, userID, networkID string) ([]*types.NetworkResource, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) CreateResource(ctx context.Context, accountID string, resource *NetworkResource) (*NetworkResource, error) { +func (m *managerImpl) CreateResource(ctx context.Context, accountID string, resource *types.NetworkResource) (*types.NetworkResource, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) GetResource(ctx context.Context, accountID, userID, networkID, resourceID string) (*NetworkResource, error) { +func (m *managerImpl) GetResource(ctx context.Context, accountID, userID, networkID, resourceID string) (*types.NetworkResource, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) UpdateResource(ctx context.Context, userID string, resource *NetworkResource) (*NetworkResource, error) { +func (m *managerImpl) UpdateResource(ctx context.Context, userID string, resource *types.NetworkResource) (*types.NetworkResource, error) { return nil, errors.New("not implemented") } diff --git a/management/server/networks/resources/resource.go b/management/server/networks/resources/types/resource.go similarity index 99% rename from management/server/networks/resources/resource.go rename to management/server/networks/resources/types/resource.go index 53a153aebbb..dd2bdd6b7d8 100644 --- a/management/server/networks/resources/resource.go +++ b/management/server/networks/resources/types/resource.go @@ -1,4 +1,4 @@ -package resources +package types import ( "errors" diff --git a/management/server/networks/resources/resource_test.go b/management/server/networks/resources/types/resource_test.go similarity index 97% rename from management/server/networks/resources/resource_test.go rename to management/server/networks/resources/types/resource_test.go index d6e9e43bb91..3282ccfb64c 100644 --- a/management/server/networks/resources/resource_test.go +++ b/management/server/networks/resources/types/resource_test.go @@ -1,4 +1,4 @@ -package resources +package types import ( "testing" diff --git a/management/server/networks/routers/manager.go b/management/server/networks/routers/manager.go index 1dd9ec7fbf1..12ad4e66712 100644 --- a/management/server/networks/routers/manager.go +++ b/management/server/networks/routers/manager.go @@ -4,14 +4,15 @@ import ( "context" "errors" + "github.com/netbirdio/netbird/management/server/networks/routers/types" "github.com/netbirdio/netbird/management/server/store" ) type Manager interface { - GetAllRouters(ctx context.Context, accountID, userID, networkID string) ([]*NetworkRouter, error) - CreateRouter(ctx context.Context, userID string, router *NetworkRouter) (*NetworkRouter, error) - GetRouter(ctx context.Context, accountID, userID, networkID, routerID string) (*NetworkRouter, error) - UpdateRouter(ctx context.Context, userID string, router *NetworkRouter) (*NetworkRouter, error) + GetAllRouters(ctx context.Context, accountID, userID, networkID string) ([]*types.NetworkRouter, error) + CreateRouter(ctx context.Context, userID string, router *types.NetworkRouter) (*types.NetworkRouter, error) + 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 } @@ -25,19 +26,19 @@ func NewManager(store store.Store) Manager { } } -func (m *managerImpl) GetAllRouters(ctx context.Context, accountID, userID, networkID string) ([]*NetworkRouter, error) { +func (m *managerImpl) GetAllRouters(ctx context.Context, accountID, userID, networkID string) ([]*types.NetworkRouter, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) CreateRouter(ctx context.Context, userID string, router *NetworkRouter) (*NetworkRouter, error) { +func (m *managerImpl) CreateRouter(ctx context.Context, userID string, router *types.NetworkRouter) (*types.NetworkRouter, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) GetRouter(ctx context.Context, accountID, userID, networkID, routerID string) (*NetworkRouter, error) { +func (m *managerImpl) GetRouter(ctx context.Context, accountID, userID, networkID, routerID string) (*types.NetworkRouter, error) { return nil, errors.New("not implemented") } -func (m *managerImpl) UpdateRouter(ctx context.Context, userID string, router *NetworkRouter) (*NetworkRouter, error) { +func (m *managerImpl) UpdateRouter(ctx context.Context, userID string, router *types.NetworkRouter) (*types.NetworkRouter, error) { return nil, errors.New("not implemented") } diff --git a/management/server/networks/routers/router.go b/management/server/networks/routers/types/router.go similarity index 98% rename from management/server/networks/routers/router.go rename to management/server/networks/routers/types/router.go index dc50272cb1d..72971eda8bb 100644 --- a/management/server/networks/routers/router.go +++ b/management/server/networks/routers/types/router.go @@ -1,4 +1,4 @@ -package routers +package types import ( "errors" diff --git a/management/server/networks/routers/router_test.go b/management/server/networks/routers/types/router_test.go similarity index 99% rename from management/server/networks/routers/router_test.go rename to management/server/networks/routers/types/router_test.go index ac1822f7724..3335f7c895b 100644 --- a/management/server/networks/routers/router_test.go +++ b/management/server/networks/routers/types/router_test.go @@ -1,4 +1,4 @@ -package routers +package types import "testing" diff --git a/management/server/networks/network.go b/management/server/networks/types/network.go similarity index 98% rename from management/server/networks/network.go rename to management/server/networks/types/network.go index 5e1258c1958..70f0aefce1b 100644 --- a/management/server/networks/network.go +++ b/management/server/networks/types/network.go @@ -1,4 +1,4 @@ -package networks +package types import ( "github.com/rs/xid" diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index 60635ec96f8..3c5040ec083 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -15,7 +15,6 @@ import ( "sync" "time" - "github.com/netbirdio/netbird/management/server/networks" log "github.com/sirupsen/logrus" "gorm.io/driver/postgres" "gorm.io/driver/sqlite" @@ -26,6 +25,9 @@ import ( nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/management/server/account" nbgroup "github.com/netbirdio/netbird/management/server/group" + resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types" + 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/management/server/posture" "github.com/netbirdio/netbird/management/server/status" @@ -91,7 +93,7 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine Engine, metrics t &types.SetupKey{}, &nbpeer.Peer{}, &types.User{}, &types.PersonalAccessToken{}, &nbgroup.Group{}, &types.Account{}, &types.Policy{}, &types.PolicyRule{}, &route.Route{}, &nbdns.NameServerGroup{}, &installation{}, &account.ExtraSettings{}, &posture.Checks{}, &nbpeer.NetworkAddress{}, - &networks.Network{}, &networks.NetworkRouter{}, &networks.NetworkResource{}, + &networkTypes.Network{}, &routerTypes.NetworkRouter{}, &resourceTypes.NetworkResource{}, ) if err != nil { return nil, fmt.Errorf("auto migrate: %w", err) @@ -1601,8 +1603,8 @@ func (s *SqlStore) SaveDNSSettings(ctx context.Context, lockStrength LockingStre return nil } -func (s *SqlStore) GetAccountNetworks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*networks.Network, error) { - var networks []*networks.Network +func (s *SqlStore) GetAccountNetworks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*networkTypes.Network, error) { + var networks []*networkTypes.Network result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).Find(&networks, accountIDCondition, accountID) if result.Error != nil { log.WithContext(ctx).Errorf("failed to get networks from the store: %s", result.Error) @@ -1612,8 +1614,8 @@ func (s *SqlStore) GetAccountNetworks(ctx context.Context, lockStrength LockingS return networks, nil } -func (s *SqlStore) GetNetworkByID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) (*networks.Network, error) { - var network *networks.Network +func (s *SqlStore) GetNetworkByID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) (*networkTypes.Network, error) { + var network *networkTypes.Network result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). First(&network, accountAndIDQueryCondition, accountID, networkID) if result.Error != nil { @@ -1628,7 +1630,7 @@ func (s *SqlStore) GetNetworkByID(ctx context.Context, lockStrength LockingStren return network, nil } -func (s *SqlStore) SaveNetwork(ctx context.Context, lockStrength LockingStrength, network *networks.Network) error { +func (s *SqlStore) SaveNetwork(ctx context.Context, lockStrength LockingStrength, network *networkTypes.Network) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).Save(network) if result.Error != nil { log.WithContext(ctx).Errorf("failed to save network to store: %v", result.Error) @@ -1640,7 +1642,7 @@ func (s *SqlStore) SaveNetwork(ctx context.Context, lockStrength LockingStrength func (s *SqlStore) DeleteNetwork(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). - Delete(&networks.Network{}, accountAndIDQueryCondition, accountID, networkID) + Delete(&networkTypes.Network{}, accountAndIDQueryCondition, accountID, networkID) if result.Error != nil { log.WithContext(ctx).Errorf("failed to delete network from store: %v", result.Error) return status.Errorf(status.Internal, "failed to delete network from store") @@ -1653,8 +1655,8 @@ func (s *SqlStore) DeleteNetwork(ctx context.Context, lockStrength LockingStreng return nil } -func (s *SqlStore) GetNetworkRoutersByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*networks.NetworkRouter, error) { - var netRouters []*networks.NetworkRouter +func (s *SqlStore) GetNetworkRoutersByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*routerTypes.NetworkRouter, error) { + var netRouters []*routerTypes.NetworkRouter result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). Find(&netRouters, "account_id = ? AND network_id = ?", accountID, netID) if result.Error != nil { @@ -1665,8 +1667,8 @@ func (s *SqlStore) GetNetworkRoutersByNetID(ctx context.Context, lockStrength Lo return netRouters, nil } -func (s *SqlStore) GetNetworkRouterByID(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) (*networks.NetworkRouter, error) { - var netRouter *networks.NetworkRouter +func (s *SqlStore) GetNetworkRouterByID(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) (*routerTypes.NetworkRouter, error) { + var netRouter *routerTypes.NetworkRouter result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). First(&netRouter, accountAndIDQueryCondition, accountID, routerID) if result.Error != nil { @@ -1680,7 +1682,7 @@ func (s *SqlStore) GetNetworkRouterByID(ctx context.Context, lockStrength Lockin return netRouter, nil } -func (s *SqlStore) SaveNetworkRouter(ctx context.Context, lockStrength LockingStrength, router *networks.NetworkRouter) error { +func (s *SqlStore) SaveNetworkRouter(ctx context.Context, lockStrength LockingStrength, router *routerTypes.NetworkRouter) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).Save(router) if result.Error != nil { log.WithContext(ctx).Errorf("failed to save network router to store: %v", result.Error) @@ -1692,7 +1694,7 @@ func (s *SqlStore) SaveNetworkRouter(ctx context.Context, lockStrength LockingSt func (s *SqlStore) DeleteNetworkRouter(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). - Delete(&networks.NetworkRouter{}, accountAndIDQueryCondition, accountID, routerID) + Delete(&routerTypes.NetworkRouter{}, accountAndIDQueryCondition, accountID, routerID) if result.Error != nil { log.WithContext(ctx).Errorf("failed to delete network router from store: %v", result.Error) return status.Errorf(status.Internal, "failed to delete network router from store") @@ -1705,8 +1707,8 @@ func (s *SqlStore) DeleteNetworkRouter(ctx context.Context, lockStrength Locking return nil } -func (s *SqlStore) GetNetworkResourcesByNetID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) ([]*networks.NetworkResource, error) { - var netResources []*networks.NetworkResource +func (s *SqlStore) GetNetworkResourcesByNetID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) ([]*resourceTypes.NetworkResource, error) { + var netResources []*resourceTypes.NetworkResource result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). Find(&netResources, "account_id = ? AND network_id = ?", accountID, networkID) if result.Error != nil { @@ -1717,8 +1719,8 @@ func (s *SqlStore) GetNetworkResourcesByNetID(ctx context.Context, lockStrength return netResources, nil } -func (s *SqlStore) GetNetworkResourceByID(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) (*networks.NetworkResource, error) { - var netResources *networks.NetworkResource +func (s *SqlStore) GetNetworkResourceByID(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) (*resourceTypes.NetworkResource, error) { + var netResources *resourceTypes.NetworkResource result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). First(&netResources, accountAndIDQueryCondition, accountID, resourceID) if result.Error != nil { @@ -1732,7 +1734,7 @@ func (s *SqlStore) GetNetworkResourceByID(ctx context.Context, lockStrength Lock return netResources, nil } -func (s *SqlStore) SaveNetworkResource(ctx context.Context, lockStrength LockingStrength, resource *networks.NetworkResource) error { +func (s *SqlStore) SaveNetworkResource(ctx context.Context, lockStrength LockingStrength, resource *resourceTypes.NetworkResource) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).Save(resource) if result.Error != nil { log.WithContext(ctx).Errorf("failed to save network resource to store: %v", result.Error) @@ -1744,7 +1746,7 @@ func (s *SqlStore) SaveNetworkResource(ctx context.Context, lockStrength Locking func (s *SqlStore) DeleteNetworkResource(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) error { result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). - Delete(&networks.NetworkResource{}, accountAndIDQueryCondition, accountID, resourceID) + Delete(&resourceTypes.NetworkResource{}, accountAndIDQueryCondition, accountID, resourceID) if result.Error != nil { log.WithContext(ctx).Errorf("failed to delete network resource from store: %v", result.Error) return status.Errorf(status.Internal, "failed to delete network resource from store") diff --git a/management/server/store/sql_store_test.go b/management/server/store/sql_store_test.go index f977bc61797..3d947d7860f 100644 --- a/management/server/store/sql_store_test.go +++ b/management/server/store/sql_store_test.go @@ -21,7 +21,9 @@ import ( nbdns "github.com/netbirdio/netbird/dns" nbgroup "github.com/netbirdio/netbird/management/server/group" - "github.com/netbirdio/netbird/management/server/networks" + resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types" + routerTypes "github.com/netbirdio/netbird/management/server/networks/routers/types" + networkTypes "github.com/netbirdio/netbird/management/server/networks/types" "github.com/netbirdio/netbird/management/server/posture" "github.com/netbirdio/netbird/management/server/types" @@ -312,7 +314,7 @@ func TestSqlite_DeleteAccount(t *testing.T) { Status: &nbpeer.PeerStatus{Connected: true, LastSeen: time.Now().UTC()}, } account.Users[testUserID] = user - account.Networks = []*networks.Network{ + account.Networks = []*networkTypes.Network{ { ID: "network_id", AccountID: account.Id, @@ -320,7 +322,7 @@ func TestSqlite_DeleteAccount(t *testing.T) { Description: "network description", }, } - account.NetworkRouters = []*networks.NetworkRouter{ + account.NetworkRouters = []*routerTypes.NetworkRouter{ { ID: "router_id", NetworkID: account.Networks[0].ID, @@ -330,7 +332,7 @@ func TestSqlite_DeleteAccount(t *testing.T) { Metric: 1, }, } - account.NetworkResources = []*networks.NetworkResource{ + account.NetworkResources = []*resourceTypes.NetworkResource{ { ID: "resource_id", NetworkID: account.Networks[0].ID, @@ -2256,7 +2258,7 @@ func TestSqlStore_SaveNetwork(t *testing.T) { require.NoError(t, err) accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b" - network := &networks.Network{ + network := &networkTypes.Network{ ID: "net-id", AccountID: accountID, Name: "net", @@ -2376,7 +2378,7 @@ func TestSqlStore_SaveNetworkRouter(t *testing.T) { accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b" networkID := "ct286bi7qv930dsrrug0" - netRouter, err := networks.NewNetworkRouter(accountID, networkID, "", []string{"net-router-grp"}, true, 0) + netRouter, err := routerTypes.NewNetworkRouter(accountID, networkID, "", []string{"net-router-grp"}, true, 0) require.NoError(t, err) err = store.SaveNetworkRouter(context.Background(), LockingStrengthUpdate, netRouter) @@ -2493,7 +2495,7 @@ func TestSqlStore_SaveNetworkResource(t *testing.T) { accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b" networkID := "ct286bi7qv930dsrrug0" - netResource, err := networks.NewNetworkResource(accountID, networkID, "resource-name", "", "example.com") + netResource, err := resourceTypes.NewNetworkResource(accountID, networkID, "resource-name", "", "example.com") require.NoError(t, err) err = store.SaveNetworkResource(context.Background(), LockingStrengthUpdate, netResource) diff --git a/management/server/store/store.go b/management/server/store/store.go index 10bebb272c9..e61ecf092d7 100644 --- a/management/server/store/store.go +++ b/management/server/store/store.go @@ -13,7 +13,6 @@ import ( "strings" "time" - "github.com/netbirdio/netbird/management/server/networks" log "github.com/sirupsen/logrus" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -27,6 +26,9 @@ import ( "github.com/netbirdio/netbird/util" "github.com/netbirdio/netbird/management/server/migration" + resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types" + 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/management/server/posture" "github.com/netbirdio/netbird/management/server/testutil" @@ -143,19 +145,19 @@ type Store interface { GetStoreEngine() Engine ExecuteInTransaction(ctx context.Context, f func(store Store) error) error - GetAccountNetworks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*networks.Network, error) - GetNetworkByID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) (*networks.Network, error) - SaveNetwork(ctx context.Context, lockStrength LockingStrength, network *networks.Network) error + GetAccountNetworks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*networkTypes.Network, error) + GetNetworkByID(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) (*networkTypes.Network, error) + SaveNetwork(ctx context.Context, lockStrength LockingStrength, network *networkTypes.Network) error DeleteNetwork(ctx context.Context, lockStrength LockingStrength, accountID, networkID string) error - GetNetworkRoutersByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*networks.NetworkRouter, error) - GetNetworkRouterByID(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) (*networks.NetworkRouter, error) - SaveNetworkRouter(ctx context.Context, lockStrength LockingStrength, router *networks.NetworkRouter) error + GetNetworkRoutersByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*routerTypes.NetworkRouter, error) + GetNetworkRouterByID(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) (*routerTypes.NetworkRouter, error) + SaveNetworkRouter(ctx context.Context, lockStrength LockingStrength, router *routerTypes.NetworkRouter) error DeleteNetworkRouter(ctx context.Context, lockStrength LockingStrength, accountID, routerID string) error - GetNetworkResourcesByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*networks.NetworkResource, error) - GetNetworkResourceByID(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) (*networks.NetworkResource, error) - SaveNetworkResource(ctx context.Context, lockStrength LockingStrength, resource *networks.NetworkResource) error + GetNetworkResourcesByNetID(ctx context.Context, lockStrength LockingStrength, accountID, netID string) ([]*resourceTypes.NetworkResource, error) + GetNetworkResourceByID(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) (*resourceTypes.NetworkResource, error) + SaveNetworkResource(ctx context.Context, lockStrength LockingStrength, resource *resourceTypes.NetworkResource) error DeleteNetworkResource(ctx context.Context, lockStrength LockingStrength, accountID, resourceID string) error } diff --git a/management/server/types/account.go b/management/server/types/account.go index 9362b4d655a..34da7b43b11 100644 --- a/management/server/types/account.go +++ b/management/server/types/account.go @@ -17,6 +17,10 @@ import ( nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/management/domain" nbgroup "github.com/netbirdio/netbird/management/server/group" + + resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types" + 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/management/server/posture" "github.com/netbirdio/netbird/management/server/status" @@ -66,6 +70,10 @@ type Account struct { PostureChecks []*posture.Checks `gorm:"foreignKey:AccountID;references:id"` // Settings is a dictionary of Account settings Settings *Settings `gorm:"embedded;embeddedPrefix:settings_"` + + Networks []*networkTypes.Network `gorm:"foreignKey:AccountID;references:id"` + NetworkRouters []*routerTypes.NetworkRouter `gorm:"foreignKey:AccountID;references:id"` + NetworkResources []*resourceTypes.NetworkResource `gorm:"foreignKey:AccountID;references:id"` } // Subclass used in gorm to only load network and not whole account @@ -727,6 +735,21 @@ func (a *Account) Copy() *Account { postureChecks = append(postureChecks, postureCheck.Copy()) } + nets := []*networkTypes.Network{} + for _, network := range a.Networks { + nets = append(nets, network.Copy()) + } + + networkRouters := []*routerTypes.NetworkRouter{} + for _, router := range a.NetworkRouters { + networkRouters = append(networkRouters, router.Copy()) + } + + networkResources := []*resourceTypes.NetworkResource{} + for _, resource := range a.NetworkResources { + networkResources = append(networkResources, resource.Copy()) + } + return &Account{ Id: a.Id, CreatedBy: a.CreatedBy, @@ -745,6 +768,9 @@ func (a *Account) Copy() *Account { DNSSettings: dnsSettings, PostureChecks: postureChecks, Settings: settings, + Networks: nets, + NetworkRouters: networkRouters, + NetworkResources: networkResources, } }