From e72b07bf5c34fb00905fbbb44145cb4ab86f4492 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 2 Sep 2023 18:42:40 +0700 Subject: [PATCH] Prepare resource pools table for future development --- gns3server/api/routes/controller/nodes.py | 2 +- gns3server/db/models/__init__.py | 1 + gns3server/db/models/resource_pools.py | 36 +++++++++++++++-------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/gns3server/api/routes/controller/nodes.py b/gns3server/api/routes/controller/nodes.py index cc93c0c1..b096b634 100644 --- a/gns3server/api/routes/controller/nodes.py +++ b/gns3server/api/routes/controller/nodes.py @@ -144,7 +144,7 @@ def get_nodes(project: Project = Depends(dep_project)) -> List[schemas.Node]: return [v.asdict() for v in project.nodes.values()] -@router.post("/start", status_code=status.HTTP_204_NO_CONTENT, dependencies=[Depends(has_privilege("Node."))]) +@router.post("/start", status_code=status.HTTP_204_NO_CONTENT, dependencies=[Depends(has_privilege("Node.PowerMgmt"))]) async def start_all_nodes(project: Project = Depends(dep_project)) -> None: """ Start all nodes belonging to a given project. diff --git a/gns3server/db/models/__init__.py b/gns3server/db/models/__init__.py index 91d6a314..c31c21b1 100644 --- a/gns3server/db/models/__init__.py +++ b/gns3server/db/models/__init__.py @@ -22,6 +22,7 @@ from .roles import Role from .privileges import Privilege from .computes import Compute from .images import Image +from .resource_pools import Resource, ResourcePool from .templates import ( Template, CloudTemplate, diff --git a/gns3server/db/models/resource_pools.py b/gns3server/db/models/resource_pools.py index 80f65070..dce9bf5b 100644 --- a/gns3server/db/models/resource_pools.py +++ b/gns3server/db/models/resource_pools.py @@ -15,28 +15,38 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from sqlalchemy import Column, String, Boolean, ForeignKey +from sqlalchemy import Table, Column, String, ForeignKey from sqlalchemy.orm import relationship -from .base import BaseTable, generate_uuid, GUID +from .base import Base, BaseTable, generate_uuid, GUID import logging log = logging.getLogger(__name__) +resource_pool_map = Table( + "resource_pool_map", + Base.metadata, + Column("resource_id", GUID, ForeignKey("resources.resource_id", ondelete="CASCADE")), + Column("resource_pool_id", GUID, ForeignKey("resource_pools.resource_pool_id", ondelete="CASCADE")) +) + + +class Resource(BaseTable): + + __tablename__ = "resources" + + resource_id = Column(GUID, primary_key=True) + name = Column(String, unique=True, index=True) + resource_type = Column(String) + resource_pools = relationship("ResourcePool", secondary=resource_pool_map, back_populates="resources") + + class ResourcePool(BaseTable): __tablename__ = "resource_pools" - resource_id = Column(GUID, primary_key=True) - resource_type = Column(String) - - # Create a self-referential relationship to represent a hierarchy of resources - parent_id = Column(GUID, ForeignKey("resources.resource_id", ondelete="CASCADE")) - children = relationship( - "Resource", - remote_side=[resource_id], - cascade="all, delete-orphan", - single_parent=True - ) + resource_pool_id = Column(GUID, primary_key=True, default=generate_uuid) + name = Column(String, unique=True, index=True) + resources = relationship("Resource", secondary=resource_pool_map, back_populates="resource_pools")