2020-10-02 06:37:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
API endpoints for drawings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from fastapi import APIRouter, status
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
from typing import List
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from gns3server.controller import Controller
|
2020-10-31 05:32:21 +00:00
|
|
|
from gns3server import schemas
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
responses = {
|
2020-10-31 05:32:21 +00:00
|
|
|
404: {"model": schemas.ErrorMessage, "description": "Project or drawing not found"}
|
2020-10-14 00:19:29 +00:00
|
|
|
}
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
@router.get("",
|
2020-10-31 05:32:21 +00:00
|
|
|
response_model=List[schemas.Drawing],
|
2020-10-02 06:37:50 +00:00
|
|
|
response_model_exclude_unset=True)
|
2020-10-14 00:19:29 +00:00
|
|
|
async def get_drawings(project_id: UUID):
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
Return the list of all drawings for a given project.
|
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
return [v.__json__() for v in project.drawings.values()]
|
|
|
|
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
@router.post("",
|
2020-10-02 06:37:50 +00:00
|
|
|
status_code=status.HTTP_201_CREATED,
|
2020-10-31 05:32:21 +00:00
|
|
|
response_model=schemas.Drawing,
|
2020-10-14 00:19:29 +00:00
|
|
|
responses=responses)
|
2020-10-31 05:32:21 +00:00
|
|
|
async def create_drawing(project_id: UUID, drawing_data: schemas.Drawing):
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Create a new drawing.
|
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
drawing = await project.add_drawing(**jsonable_encoder(drawing_data, exclude_unset=True))
|
|
|
|
return drawing.__json__()
|
|
|
|
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
@router.get("/{drawing_id}",
|
2020-10-31 05:32:21 +00:00
|
|
|
response_model=schemas.Drawing,
|
2020-10-02 06:37:50 +00:00
|
|
|
response_model_exclude_unset=True,
|
2020-10-14 00:19:29 +00:00
|
|
|
responses=responses)
|
2020-10-02 06:37:50 +00:00
|
|
|
async def get_drawing(project_id: UUID, drawing_id: UUID):
|
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Return a drawing.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
drawing = project.get_drawing(str(drawing_id))
|
|
|
|
return drawing.__json__()
|
|
|
|
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
@router.put("/{drawing_id}",
|
2020-10-31 05:32:21 +00:00
|
|
|
response_model=schemas.Drawing,
|
2020-10-02 06:37:50 +00:00
|
|
|
response_model_exclude_unset=True,
|
2020-10-14 00:19:29 +00:00
|
|
|
responses=responses)
|
2020-10-31 05:32:21 +00:00
|
|
|
async def update_drawing(project_id: UUID, drawing_id: UUID, drawing_data: schemas.Drawing):
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Update a drawing.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
drawing = project.get_drawing(str(drawing_id))
|
|
|
|
await drawing.update(**jsonable_encoder(drawing_data, exclude_unset=True))
|
|
|
|
return drawing.__json__()
|
|
|
|
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
@router.delete("/{drawing_id}",
|
2020-10-02 06:37:50 +00:00
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
2020-10-14 00:19:29 +00:00
|
|
|
responses=responses)
|
2020-10-02 06:37:50 +00:00
|
|
|
async def delete_drawing(project_id: UUID, drawing_id: UUID):
|
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Delete a drawing.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
await project.delete_drawing(str(drawing_id))
|