From a27db6b4eb1f8134843ecdc9bf2bab5f4853694c Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 28 Sep 2023 18:38:38 +1000 Subject: [PATCH] Fix deleting resource from resource pool. Ref #2293 --- gns3server/api/routes/controller/acl.py | 2 +- gns3server/api/routes/controller/pools.py | 15 +++++++++++++-- gns3server/db/repositories/pools.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/gns3server/api/routes/controller/acl.py b/gns3server/api/routes/controller/acl.py index cef43bf2..5ba72006 100644 --- a/gns3server/api/routes/controller/acl.py +++ b/gns3server/api/routes/controller/acl.py @@ -183,7 +183,7 @@ async def create_ace( route_path = re.sub(r"/{[\w:]+}", r"/\\w+", route_path) if re.fullmatch(route_path, ace_create.path): - log.info("Creating ACE for route path", ace_create.path, route_path) + log.info(f"Creating ACE for route path {route_path}") return await rbac_repo.create_ace(ace_create) raise ControllerBadRequestError(f"Path '{ace_create.path}' doesn't match any existing endpoint") diff --git a/gns3server/api/routes/controller/pools.py b/gns3server/api/routes/controller/pools.py index 17e1c17c..6c8e865f 100644 --- a/gns3server/api/routes/controller/pools.py +++ b/gns3server/api/routes/controller/pools.py @@ -191,6 +191,7 @@ async def add_resource_to_pool( if not resource_pool: raise ControllerNotFoundError(f"Resource pool '{resource_pool_id}' not found") + # TODO: consider if a resource can belong to multiple pools resources = await pools_repo.get_pool_resources(resource_pool_id) for resource in resources: if resource.resource_id == resource_id: @@ -198,8 +199,13 @@ async def add_resource_to_pool( # we only support projects in resource pools for now project = Controller.instance().get_project(str(resource_id)) - resource_create = schemas.ResourceCreate(resource_id=resource_id, resource_type="project", name=project.name) - resource = await pools_repo.create_resource(resource_create) + + resource = await pools_repo.get_resource(resource_id) + if not resource: + # the resource is not in the database yet, create it + resource_create = schemas.ResourceCreate(resource_id=resource_id, resource_type="project", name=project.name) + resource = await pools_repo.create_resource(resource_create) + await pools_repo.add_resource_to_pool(resource_pool_id, resource) @@ -226,3 +232,8 @@ async def remove_resource_from_pool( resource_pool = await pools_repo.remove_resource_from_pool(resource_pool_id, resource) if not resource_pool: raise ControllerNotFoundError(f"Resource pool '{resource_pool_id}' not found") + + # TODO: consider if a resource can belong to multiple pools + success = await pools_repo.delete_resource(resource.resource_id) + if not success: + raise ControllerError(f"Resource '{resource_id}' could not be deleted") diff --git a/gns3server/db/repositories/pools.py b/gns3server/db/repositories/pools.py index 5f3f259d..9cf11bf3 100644 --- a/gns3server/db/repositories/pools.py +++ b/gns3server/db/repositories/pools.py @@ -80,6 +80,18 @@ class ResourcePoolsRepository(BaseRepository): await self._db_session.commit() return result.rowcount > 0 + async def get_resource_memberships(self, resource_id: UUID) -> List[models.UserGroup]: + """ + Get all resource memberships in resource pools. + """ + + query = select(models.ResourcePool).\ + join(models.ResourcePool.resources).\ + filter(models.Resource.resource_id == resource_id) + + result = await self._db_session.execute(query) + return result.scalars().all() + async def get_resource_pool(self, resource_pool_id: UUID) -> Optional[models.ResourcePool]: """ Get a resource pool by its ID.