2015-01-14 00:26:24 +00:00
|
|
|
#
|
2020-06-16 04:29:03 +00:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2015-01-14 00:26:24 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2020-11-19 04:51:03 +00:00
|
|
|
API routes for appliances.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2015-03-16 09:18:37 +00:00
|
|
|
|
2021-10-18 07:34:30 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Response, status
|
2021-04-18 06:10:38 +00:00
|
|
|
from typing import Optional, List
|
2021-10-18 07:34:30 +00:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from gns3server import schemas
|
|
|
|
from gns3server.controller import Controller
|
2021-10-19 04:45:10 +00:00
|
|
|
from gns3server.controller.controller_error import (
|
|
|
|
ControllerError,
|
|
|
|
ControllerBadRequestError,
|
|
|
|
ControllerNotFoundError
|
|
|
|
)
|
|
|
|
|
2021-10-18 07:34:30 +00:00
|
|
|
from gns3server.db.repositories.images import ImagesRepository
|
|
|
|
from gns3server.db.repositories.templates import TemplatesRepository
|
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
|
|
|
|
|
|
|
from .dependencies.authentication import get_current_active_user
|
|
|
|
from .dependencies.database import get_repository
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 00:26:24 +00:00
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
router = APIRouter()
|
2015-01-14 00:26:24 +00:00
|
|
|
|
2015-03-16 09:18:37 +00:00
|
|
|
|
2023-07-03 08:55:19 +00:00
|
|
|
@router.get("", response_model=List[schemas.Appliance], response_model_exclude_unset=True)
|
2021-10-18 11:16:50 +00:00
|
|
|
async def get_appliances(
|
|
|
|
update: Optional[bool] = False,
|
2022-07-25 18:39:03 +00:00
|
|
|
symbol_theme: Optional[str] = None
|
2021-10-18 11:16:50 +00:00
|
|
|
) -> List[schemas.Appliance]:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Return all appliances known by the controller.
|
|
|
|
"""
|
2016-11-25 14:11:31 +00:00
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
controller = Controller.instance()
|
|
|
|
if update:
|
|
|
|
await controller.appliance_manager.download_appliances()
|
2022-07-25 18:39:03 +00:00
|
|
|
controller.appliance_manager.load_appliances(symbol_theme)
|
2021-04-17 14:04:28 +00:00
|
|
|
return [c.asdict() for c in controller.appliance_manager.appliances.values()]
|
2021-10-18 07:34:30 +00:00
|
|
|
|
|
|
|
|
2023-07-03 08:55:19 +00:00
|
|
|
@router.get("/{appliance_id}", response_model=schemas.Appliance, response_model_exclude_unset=True)
|
2021-10-18 11:16:50 +00:00
|
|
|
def get_appliance(appliance_id: UUID) -> schemas.Appliance:
|
2021-10-18 07:34:30 +00:00
|
|
|
"""
|
2021-10-18 11:16:50 +00:00
|
|
|
Get an appliance file.
|
2021-10-18 07:34:30 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
appliance = controller.appliance_manager.appliances.get(str(appliance_id))
|
|
|
|
if not appliance:
|
|
|
|
raise ControllerNotFoundError(message=f"Could not find appliance '{appliance_id}'")
|
2021-10-18 11:16:50 +00:00
|
|
|
return appliance.asdict()
|
2021-10-18 07:34:30 +00:00
|
|
|
|
|
|
|
|
2021-10-19 04:45:10 +00:00
|
|
|
@router.post("/{appliance_id}/version", status_code=status.HTTP_201_CREATED)
|
2023-02-17 13:25:19 +00:00
|
|
|
def add_appliance_version(appliance_id: UUID, appliance_version: schemas.ApplianceVersion) -> dict:
|
2021-10-19 04:45:10 +00:00
|
|
|
"""
|
|
|
|
Add a version to an appliance
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
appliance = controller.appliance_manager.appliances.get(str(appliance_id))
|
|
|
|
if not appliance:
|
|
|
|
raise ControllerNotFoundError(message=f"Could not find appliance '{appliance_id}'")
|
|
|
|
|
|
|
|
if not appliance.versions:
|
|
|
|
raise ControllerBadRequestError(message=f"Appliance '{appliance_id}' do not have versions")
|
|
|
|
|
|
|
|
if not appliance_version.images:
|
|
|
|
raise ControllerBadRequestError(message=f"Version '{appliance_version.name}' must contain images")
|
|
|
|
|
|
|
|
for version in appliance.versions:
|
|
|
|
if version.get("name") == appliance_version.name:
|
|
|
|
raise ControllerError(message=f"Appliance '{appliance_id}' already has version '{appliance_version.name}'")
|
|
|
|
|
|
|
|
appliance.versions.append(appliance_version.dict(exclude_unset=True))
|
|
|
|
return appliance.asdict()
|
|
|
|
|
|
|
|
|
2021-10-18 07:34:30 +00:00
|
|
|
@router.post("/{appliance_id}/install", status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
async def install_appliance(
|
|
|
|
appliance_id: UUID,
|
|
|
|
version: Optional[str] = None,
|
|
|
|
images_repo: ImagesRepository = Depends(get_repository(ImagesRepository)),
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
|
|
|
current_user: schemas.User = Depends(get_current_active_user),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2022-07-15 22:12:18 +00:00
|
|
|
) -> None:
|
2021-10-18 07:34:30 +00:00
|
|
|
"""
|
|
|
|
Install an appliance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
await controller.appliance_manager.install_appliance(
|
|
|
|
appliance_id,
|
|
|
|
version,
|
|
|
|
images_repo,
|
|
|
|
templates_repo,
|
|
|
|
rbac_repo,
|
|
|
|
current_user
|
|
|
|
)
|