gns3-server/gns3server/handlers/api/controller/drawing_handler.py

121 lines
4.0 KiB
Python
Raw Normal View History

2016-06-20 16:45:31 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 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/>.
from gns3server.web.route import Route
from gns3server.controller import Controller
2016-06-23 09:17:23 +00:00
from gns3server.schemas.drawing import (
DRAWING_OBJECT_SCHEMA,
2016-06-20 16:45:31 +00:00
)
2016-06-23 09:17:23 +00:00
class DrawingHandler:
2016-06-20 16:45:31 +00:00
"""
2016-06-23 09:17:23 +00:00
API entry point for Drawing
2016-06-20 16:45:31 +00:00
"""
@Route.get(
2016-06-23 09:17:23 +00:00
r"/projects/{project_id}/drawings",
2016-06-20 16:45:31 +00:00
parameters={
"project_id": "Project UUID"
},
status_codes={
2016-06-23 09:17:23 +00:00
200: "List of drawings returned",
2016-06-20 16:45:31 +00:00
},
2016-06-23 09:17:23 +00:00
description="List drawings of a project")
async def list_drawings(request, response):
2016-06-20 16:45:31 +00:00
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
2016-06-23 09:17:23 +00:00
response.json([v for v in project.drawings.values()])
2016-06-20 16:45:31 +00:00
@Route.post(
2016-06-23 09:17:23 +00:00
r"/projects/{project_id}/drawings",
2016-06-20 16:45:31 +00:00
parameters={
"project_id": "Project UUID"
},
status_codes={
2016-06-23 09:17:23 +00:00
201: "Drawing created",
2016-06-20 16:45:31 +00:00
400: "Invalid request"
},
2016-06-23 09:17:23 +00:00
description="Create a new drawing instance",
input=DRAWING_OBJECT_SCHEMA,
output=DRAWING_OBJECT_SCHEMA)
async def create(request, response):
2016-06-20 16:45:31 +00:00
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
drawing = await project.add_drawing(**request.json)
2016-06-20 16:45:31 +00:00
response.set_status(201)
2016-06-23 09:17:23 +00:00
response.json(drawing)
2016-06-20 16:45:31 +00:00
@Route.get(
r"/projects/{project_id}/drawings/{drawing_id}",
parameters={
"project_id": "Project UUID",
"drawing_id": "Drawing UUID"
},
status_codes={
200: "Drawing found",
400: "Invalid request",
404: "Drawing doesn't exist"
},
description="Get a drawing instance",
output=DRAWING_OBJECT_SCHEMA)
async def get_drawing(request, response):
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
drawing = project.get_drawing(request.match_info["drawing_id"])
response.set_status(200)
response.json(drawing)
2016-06-20 16:45:31 +00:00
@Route.put(
2016-06-23 09:17:23 +00:00
r"/projects/{project_id}/drawings/{drawing_id}",
2016-06-20 16:45:31 +00:00
parameters={
"project_id": "Project UUID",
2016-06-23 09:17:23 +00:00
"drawing_id": "Drawing UUID"
2016-06-20 16:45:31 +00:00
},
status_codes={
2016-06-23 09:17:23 +00:00
201: "Drawing updated",
2016-06-20 16:45:31 +00:00
400: "Invalid request"
},
description="Update a drawing instance",
2016-06-23 09:17:23 +00:00
input=DRAWING_OBJECT_SCHEMA,
output=DRAWING_OBJECT_SCHEMA)
async def update(request, response):
2016-06-20 16:45:31 +00:00
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
2016-06-23 09:17:23 +00:00
drawing = project.get_drawing(request.match_info["drawing_id"])
await drawing.update(**request.json)
2016-06-20 16:45:31 +00:00
response.set_status(201)
2016-06-23 09:17:23 +00:00
response.json(drawing)
2016-06-20 16:45:31 +00:00
@Route.delete(
2016-06-23 09:17:23 +00:00
r"/projects/{project_id}/drawings/{drawing_id}",
2016-06-20 16:45:31 +00:00
parameters={
"project_id": "Project UUID",
2016-06-23 09:17:23 +00:00
"drawing_id": "Drawing UUID"
2016-06-20 16:45:31 +00:00
},
status_codes={
2016-06-23 09:17:23 +00:00
204: "Drawing deleted",
2016-06-20 16:45:31 +00:00
400: "Invalid request"
},
2016-06-23 09:17:23 +00:00
description="Delete a drawing instance")
async def delete(request, response):
2016-06-20 16:45:31 +00:00
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
await project.delete_drawing(request.match_info["drawing_id"])
2016-06-20 16:45:31 +00:00
response.set_status(204)