1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-25 16:28:11 +00:00

Fix deleting resource from resource pool. Ref #2293

This commit is contained in:
grossmj 2023-09-28 18:38:38 +10:00
parent 66b66cc3e1
commit a27db6b4eb
3 changed files with 26 additions and 3 deletions

View File

@ -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")

View File

@ -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")

View File

@ -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.