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/>.
|
|
|
|
|
|
|
|
|
2021-11-18 10:07:10 +02:00
|
|
|
from fastapi import FastAPI, Request, Depends
|
2020-10-02 09:37:50 +03:00
|
|
|
from fastapi.responses import JSONResponse
|
2020-11-02 09:38:25 +02:00
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
|
|
|
|
from gns3server.compute.error import ImageMissingError, NodeError
|
2020-11-19 06:57:33 +02:00
|
|
|
from gns3server.compute.ubridge.ubridge_error import UbridgeError
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-11-18 10:07:10 +02:00
|
|
|
from .dependencies.authentication import compute_authentication
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.compute.compute_error import (
|
|
|
|
ComputeError,
|
|
|
|
ComputeNotFoundError,
|
|
|
|
ComputeTimeoutError,
|
|
|
|
ComputeForbiddenError,
|
2021-04-13 12:16:50 +03:00
|
|
|
ComputeUnauthorizedError,
|
2020-10-02 09:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
from . import capabilities
|
|
|
|
from . import compute
|
|
|
|
from . import projects
|
|
|
|
from . import notifications
|
|
|
|
from . import images
|
|
|
|
from . import atm_switch_nodes
|
|
|
|
from . import cloud_nodes
|
|
|
|
from . import docker_nodes
|
|
|
|
from . import dynamips_nodes
|
|
|
|
from . import ethernet_hub_nodes
|
|
|
|
from . import ethernet_switch_nodes
|
|
|
|
from . import frame_relay_switch_nodes
|
|
|
|
from . import iou_nodes
|
|
|
|
from . import nat_nodes
|
|
|
|
from . import qemu_nodes
|
|
|
|
from . import virtualbox_nodes
|
|
|
|
from . import vmware_nodes
|
|
|
|
from . import vpcs_nodes
|
|
|
|
|
2022-04-07 11:21:47 +03:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api = FastAPI(
|
|
|
|
title="GNS3 compute API",
|
2021-11-18 10:07:10 +02:00
|
|
|
dependencies=[Depends(compute_authentication)],
|
2021-04-13 12:16:50 +03:00
|
|
|
description="This page describes the private compute API for GNS3. PLEASE DO NOT USE DIRECTLY!",
|
|
|
|
version="v3",
|
|
|
|
)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-10-21 14:08:36 +03:00
|
|
|
compute_api.state.controller_host = None
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
@compute_api.exception_handler(ComputeError)
|
|
|
|
async def controller_error_handler(request: Request, exc: ComputeError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(ComputeTimeoutError)
|
|
|
|
async def controller_timeout_error_handler(request: Request, exc: ComputeTimeoutError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute timeout error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=408,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(ComputeUnauthorizedError)
|
|
|
|
async def controller_unauthorized_error_handler(request: Request, exc: ComputeUnauthorizedError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute unauthorized error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=401,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(ComputeForbiddenError)
|
|
|
|
async def controller_forbidden_error_handler(request: Request, exc: ComputeForbiddenError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute forbidden error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=403,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(ComputeNotFoundError)
|
|
|
|
async def controller_not_found_error_handler(request: Request, exc: ComputeNotFoundError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute not found error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=404,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(GNS3VMError)
|
|
|
|
async def controller_error_handler(request: Request, exc: GNS3VMError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute GNS3 VM error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc)},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(ImageMissingError)
|
|
|
|
async def image_missing_error_handler(request: Request, exc: ImageMissingError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute image missing error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc), "image": exc.image, "exception": exc.__class__.__name__},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(NodeError)
|
2020-10-19 10:58:01 +03:00
|
|
|
async def node_error_handler(request: Request, exc: NodeError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute node error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc), "exception": exc.__class__.__name__},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@compute_api.exception_handler(UbridgeError)
|
2020-10-19 10:58:01 +03:00
|
|
|
async def ubridge_error_handler(request: Request, exc: UbridgeError):
|
2022-04-07 11:21:47 +03:00
|
|
|
log.error(f"Compute uBridge error: {exc}")
|
2020-10-02 09:37:50 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=409,
|
|
|
|
content={"message": str(exc), "exception": exc.__class__.__name__},
|
|
|
|
)
|
|
|
|
|
2020-11-02 09:38:25 +02:00
|
|
|
|
|
|
|
# make sure the content key is "message", not "detail" per default
|
|
|
|
@compute_api.exception_handler(StarletteHTTPException)
|
|
|
|
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={"message": exc.detail},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
2021-04-15 11:42:08 +03:00
|
|
|
capabilities.router,
|
|
|
|
tags=["Capabilities"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|
2021-04-15 11:42:08 +03:00
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
compute.router,
|
|
|
|
tags=["Compute"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
notifications.router,
|
|
|
|
tags=["Notifications"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
projects.router,
|
|
|
|
tags=["Projects"]
|
|
|
|
)
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
2021-04-15 11:42:08 +03:00
|
|
|
images.router,
|
|
|
|
tags=["Images"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|
2021-04-15 11:42:08 +03:00
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
atm_switch_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/atm_switch/nodes",
|
|
|
|
tags=["ATM switch"]
|
|
|
|
)
|
|
|
|
compute_api.include_router(
|
|
|
|
cloud_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/cloud/nodes",
|
|
|
|
tags=["Cloud nodes"]
|
|
|
|
)
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
2021-04-15 11:42:08 +03:00
|
|
|
docker_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/docker/nodes",
|
|
|
|
tags=["Docker nodes"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|
2021-04-15 11:42:08 +03:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
2021-04-15 11:42:08 +03:00
|
|
|
dynamips_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/dynamips/nodes",
|
|
|
|
tags=["Dynamips nodes"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|
2021-04-15 11:42:08 +03:00
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
ethernet_hub_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/ethernet_hub/nodes",
|
|
|
|
tags=["Ethernet hub nodes"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
ethernet_switch_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/ethernet_switch/nodes",
|
|
|
|
tags=["Ethernet switch nodes"]
|
|
|
|
)
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
|
|
|
frame_relay_switch_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/frame_relay_switch/nodes",
|
2021-04-15 11:42:08 +03:00
|
|
|
tags=["Frame Relay switch nodes"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
iou_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/iou/nodes",
|
|
|
|
tags=["IOU nodes"])
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
nat_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/nat/nodes",
|
|
|
|
tags=["NAT nodes"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|
2021-04-15 11:42:08 +03:00
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
qemu_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/qemu/nodes",
|
|
|
|
tags=["Qemu nodes"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
virtualbox_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/virtualbox/nodes",
|
|
|
|
tags=["VirtualBox nodes"]
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_api.include_router(
|
|
|
|
vmware_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/vmware/nodes",
|
|
|
|
tags=["VMware nodes"]
|
|
|
|
)
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
compute_api.include_router(
|
2021-04-15 11:42:08 +03:00
|
|
|
vpcs_nodes.router,
|
|
|
|
prefix="/projects/{project_id}/vpcs/nodes",
|
|
|
|
tags=["VPCS nodes"]
|
2021-04-13 12:16:50 +03:00
|
|
|
)
|