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/>.
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
|
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
|
|
|
|
2016-06-21 07:49:16 +00:00
|
|
|
from gns3server.schemas.shape import (
|
|
|
|
SHAPE_OBJECT_SCHEMA,
|
2016-06-20 16:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-06-21 07:49:16 +00:00
|
|
|
class ShapeHandler:
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-21 07:49:16 +00:00
|
|
|
API entry point for Shape
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
@Route.get(
|
2016-06-21 07:49:16 +00:00
|
|
|
r"/projects/{project_id}/shapes",
|
2016-06-20 16:45:31 +00:00
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
2016-06-21 07:49:16 +00:00
|
|
|
200: "List of shapes returned",
|
2016-06-20 16:45:31 +00:00
|
|
|
},
|
2016-06-21 07:49:16 +00:00
|
|
|
description="List shapes of a project")
|
|
|
|
def list_shapes(request, response):
|
2016-06-20 16:45:31 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-06-21 07:49:16 +00:00
|
|
|
response.json([v for v in project.shapes.values()])
|
2016-06-20 16:45:31 +00:00
|
|
|
|
|
|
|
@Route.post(
|
2016-06-21 07:49:16 +00:00
|
|
|
r"/projects/{project_id}/shapes",
|
2016-06-20 16:45:31 +00:00
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
2016-06-21 07:49:16 +00:00
|
|
|
201: "Shape created",
|
2016-06-20 16:45:31 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-06-21 07:49:16 +00:00
|
|
|
description="Create a new shape instance",
|
|
|
|
input=SHAPE_OBJECT_SCHEMA,
|
|
|
|
output=SHAPE_OBJECT_SCHEMA)
|
2016-06-20 16:45:31 +00:00
|
|
|
def create(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-06-21 07:49:16 +00:00
|
|
|
shape = yield from project.add_shape(**request.json)
|
2016-06-20 16:45:31 +00:00
|
|
|
response.set_status(201)
|
2016-06-21 07:49:16 +00:00
|
|
|
response.json(shape)
|
2016-06-20 16:45:31 +00:00
|
|
|
|
|
|
|
@Route.put(
|
2016-06-21 07:49:16 +00:00
|
|
|
r"/projects/{project_id}/shapes/{shape_id}",
|
2016-06-20 16:45:31 +00:00
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
2016-06-21 07:49:16 +00:00
|
|
|
"shape_id": "Shape UUID"
|
2016-06-20 16:45:31 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-06-21 07:49:16 +00:00
|
|
|
201: "Shape updated",
|
2016-06-20 16:45:31 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-06-21 07:49:16 +00:00
|
|
|
description="Create a new shape instance",
|
|
|
|
input=SHAPE_OBJECT_SCHEMA,
|
|
|
|
output=SHAPE_OBJECT_SCHEMA)
|
2016-06-20 16:45:31 +00:00
|
|
|
def update(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-06-21 07:49:16 +00:00
|
|
|
shape = project.get_shape(request.match_info["shape_id"])
|
|
|
|
yield from shape.update(**request.json)
|
2016-06-20 16:45:31 +00:00
|
|
|
response.set_status(201)
|
2016-06-21 07:49:16 +00:00
|
|
|
response.json(shape)
|
2016-06-20 16:45:31 +00:00
|
|
|
|
|
|
|
@Route.delete(
|
2016-06-21 07:49:16 +00:00
|
|
|
r"/projects/{project_id}/shapes/{shape_id}",
|
2016-06-20 16:45:31 +00:00
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
2016-06-21 07:49:16 +00:00
|
|
|
"shape_id": "Shape UUID"
|
2016-06-20 16:45:31 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-06-21 07:49:16 +00:00
|
|
|
204: "Shape deleted",
|
2016-06-20 16:45:31 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-06-21 07:49:16 +00:00
|
|
|
description="Delete a shape instance")
|
2016-06-20 16:45:31 +00:00
|
|
|
def delete(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-06-21 07:49:16 +00:00
|
|
|
yield from project.delete_shape(request.match_info["shape_id"])
|
2016-06-20 16:45:31 +00:00
|
|
|
response.set_status(204)
|
|
|
|
|