2020-10-02 09:37:50 +03:00
|
|
|
#
|
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# 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-11-19 06:51:03 +02:00
|
|
|
API routes for computes.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
from fastapi import APIRouter, Depends, status
|
2020-10-02 09:37:50 +03:00
|
|
|
from typing import List, Union
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from gns3server.controller import Controller
|
2021-04-05 07:51:41 +03:00
|
|
|
from gns3server.db.repositories.computes import ComputesRepository
|
|
|
|
from gns3server.services.computes import ComputesService
|
2020-10-31 07:32:21 +02:00
|
|
|
from gns3server import schemas
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
from .dependencies.database import get_repository
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
responses = {404: {"model": schemas.ErrorMessage, "description": "Compute not found"}}
|
2020-10-14 03:19:29 +03:00
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
router = APIRouter(responses=responses)
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.post(
|
|
|
|
"",
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
response_model=schemas.Compute,
|
|
|
|
responses={
|
|
|
|
404: {"model": schemas.ErrorMessage, "description": "Could not connect to compute"},
|
|
|
|
409: {"model": schemas.ErrorMessage, "description": "Could not create compute"},
|
|
|
|
401: {"model": schemas.ErrorMessage, "description": "Invalid authentication for compute"},
|
|
|
|
},
|
|
|
|
)
|
2021-04-05 07:51:41 +03:00
|
|
|
async def create_compute(
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_create: schemas.ComputeCreate,
|
|
|
|
computes_repo: ComputesRepository = Depends(get_repository(ComputesRepository)),
|
2021-04-05 07:51:41 +03:00
|
|
|
) -> schemas.Compute:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
Create a new compute on the controller.
|
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
return await ComputesService(computes_repo).create_compute(compute_create)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.get("/{compute_id}", response_model=schemas.Compute, response_model_exclude_unset=True)
|
2021-04-05 07:51:41 +03:00
|
|
|
async def get_compute(
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_id: Union[str, UUID], computes_repo: ComputesRepository = Depends(get_repository(ComputesRepository))
|
2021-04-05 07:51:41 +03:00
|
|
|
) -> schemas.Compute:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Return a compute from the controller.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
return await ComputesService(computes_repo).get_compute(compute_id)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.get("", response_model=List[schemas.Compute], response_model_exclude_unset=True)
|
2021-04-05 07:51:41 +03:00
|
|
|
async def get_computes(
|
2021-04-13 12:16:50 +03:00
|
|
|
computes_repo: ComputesRepository = Depends(get_repository(ComputesRepository)),
|
2021-04-05 07:51:41 +03:00
|
|
|
) -> List[schemas.Compute]:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Return all computes known by the controller.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
return await ComputesService(computes_repo).get_computes()
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.put("/{compute_id}", response_model=schemas.Compute, response_model_exclude_unset=True)
|
2021-04-05 07:51:41 +03:00
|
|
|
async def update_compute(
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_id: Union[str, UUID],
|
|
|
|
compute_update: schemas.ComputeUpdate,
|
|
|
|
computes_repo: ComputesRepository = Depends(get_repository(ComputesRepository)),
|
2021-04-05 07:51:41 +03:00
|
|
|
) -> schemas.Compute:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
Update a compute on the controller.
|
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
return await ComputesService(computes_repo).update_compute(compute_id, compute_update)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.delete("/{compute_id}", status_code=status.HTTP_204_NO_CONTENT)
|
2021-04-05 07:51:41 +03:00
|
|
|
async def delete_compute(
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_id: Union[str, UUID], computes_repo: ComputesRepository = Depends(get_repository(ComputesRepository))
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> None:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
Delete a compute from the controller.
|
|
|
|
"""
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
await ComputesService(computes_repo).delete_compute(compute_id)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
@router.get("/{compute_id}/{emulator}/images")
|
2021-04-18 09:10:38 +03:00
|
|
|
async def get_images(compute_id: Union[str, UUID], emulator: str) -> List[str]:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
Return the list of images available on a compute for a given emulator type.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
compute = controller.get_compute(str(compute_id))
|
|
|
|
return await compute.images(emulator)
|
|
|
|
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
@router.get("/{compute_id}/{emulator}/{endpoint_path:path}")
|
2021-04-18 09:10:38 +03:00
|
|
|
async def forward_get(compute_id: Union[str, UUID], emulator: str, endpoint_path: str) -> dict:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Forward a GET request to a compute.
|
2020-11-19 06:51:03 +02:00
|
|
|
Read the full compute API documentation for available routes.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
compute = Controller.instance().get_compute(str(compute_id))
|
|
|
|
result = await compute.forward("GET", emulator, endpoint_path)
|
|
|
|
return result
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
@router.post("/{compute_id}/{emulator}/{endpoint_path:path}")
|
2021-04-18 09:10:38 +03:00
|
|
|
async def forward_post(compute_id: Union[str, UUID], emulator: str, endpoint_path: str, compute_data: dict) -> dict:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Forward a POST request to a compute.
|
2020-11-19 06:51:03 +02:00
|
|
|
Read the full compute API documentation for available routes.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
compute = Controller.instance().get_compute(str(compute_id))
|
|
|
|
return await compute.forward("POST", emulator, endpoint_path, data=compute_data)
|
|
|
|
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
@router.put("/{compute_id}/{emulator}/{endpoint_path:path}")
|
2021-04-18 09:10:38 +03:00
|
|
|
async def forward_put(compute_id: Union[str, UUID], emulator: str, endpoint_path: str, compute_data: dict) -> dict:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Forward a PUT request to a compute.
|
2020-11-19 06:51:03 +02:00
|
|
|
Read the full compute API documentation for available routes.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
compute = Controller.instance().get_compute(str(compute_id))
|
|
|
|
return await compute.forward("PUT", emulator, endpoint_path, data=compute_data)
|
|
|
|
|
|
|
|
|
2021-04-05 07:51:41 +03:00
|
|
|
@router.post("/{compute_id}/auto_idlepc")
|
2021-04-18 09:10:38 +03:00
|
|
|
async def autoidlepc(compute_id: Union[str, UUID], auto_idle_pc: schemas.AutoIdlePC) -> str:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Find a suitable Idle-PC value for a given IOS image. This may take a few minutes.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2021-04-13 12:16:50 +03:00
|
|
|
return await controller.autoidlepc(str(compute_id), auto_idle_pc.platform, auto_idle_pc.image, auto_idle_pc.ram)
|