2016-03-10 11:32:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
2016-05-14 03:00:07 +03:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
2016-03-10 11:32:07 +02:00
|
|
|
|
2016-05-14 03:00:07 +03:00
|
|
|
from gns3server.schemas.project import (
|
|
|
|
PROJECT_OBJECT_SCHEMA,
|
|
|
|
PROJECT_CREATE_SCHEMA
|
|
|
|
)
|
2016-03-10 11:32:07 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectHandler:
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects",
|
|
|
|
description="Create a new project on the server",
|
|
|
|
status_codes={
|
|
|
|
201: "Project created",
|
|
|
|
409: "Project already created"
|
|
|
|
},
|
|
|
|
output=PROJECT_OBJECT_SCHEMA,
|
|
|
|
input=PROJECT_CREATE_SCHEMA)
|
|
|
|
def create_project(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-14 03:00:07 +03:00
|
|
|
project = yield from controller.add_project(name=request.json.get("name"),
|
|
|
|
path=request.json.get("path"),
|
2016-05-24 18:54:08 +03:00
|
|
|
project_id=request.json.get("project_id"))
|
2016-03-10 11:32:07 +02:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
|
|
|
|
2016-04-20 15:38:14 +03:00
|
|
|
@Route.get(
|
|
|
|
r"/projects",
|
|
|
|
description="List projects",
|
|
|
|
status_codes={
|
|
|
|
200: "List of projects",
|
|
|
|
})
|
|
|
|
def list_projects(request, response):
|
|
|
|
controller = Controller.instance()
|
2016-05-14 03:00:07 +03:00
|
|
|
response.json([p for p in controller.projects.values()])
|
2016-04-20 15:38:14 +03:00
|
|
|
|
2016-03-17 18:32:37 +02:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}",
|
2016-05-14 03:00:07 +03:00
|
|
|
description="Get a project",
|
2016-03-17 18:32:37 +02:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 18:32:37 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
200: "Project information returned",
|
2016-03-17 18:32:37 +02:00
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def get(request, response):
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 18:32:37 +02:00
|
|
|
response.json(project)
|
|
|
|
|
2016-03-10 11:32:07 +02:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/close",
|
|
|
|
description="Close a project",
|
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
2016-03-10 11:32:07 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "The project has been closed",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def close(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-10 11:32:07 +02:00
|
|
|
yield from project.close()
|
2016-06-15 16:12:38 +03:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/open",
|
|
|
|
description="Open a project",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "The project has been opened",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def open(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
|
|
|
yield from project.open()
|
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
2016-03-10 11:32:07 +02:00
|
|
|
|
|
|
|
@Route.delete(
|
|
|
|
r"/projects/{project_id}",
|
|
|
|
description="Delete a project from disk",
|
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
2016-03-10 11:32:07 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Changes have been written on disk",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def delete(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-10 11:32:07 +02:00
|
|
|
yield from project.delete()
|
2016-05-12 00:19:00 +03:00
|
|
|
controller.remove_project(project)
|
2016-03-10 11:32:07 +02:00
|
|
|
response.set_status(204)
|
2016-03-17 18:32:37 +02:00
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/notifications",
|
2016-05-14 03:00:07 +03:00
|
|
|
description="Receive notifications about projects",
|
2016-03-17 18:32:37 +02:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 18:32:37 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "End of stream",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def notification(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 18:32:37 +02:00
|
|
|
|
|
|
|
response.content_type = "application/json"
|
|
|
|
response.set_status(200)
|
|
|
|
response.enable_chunked_encoding()
|
2016-05-14 03:00:07 +03:00
|
|
|
# Very important: do not send a content length otherwise QT closes the connection (curl can consume the feed)
|
2016-03-17 18:32:37 +02:00
|
|
|
response.content_length = None
|
|
|
|
|
|
|
|
response.start(request)
|
2016-05-18 15:56:23 +03:00
|
|
|
with controller.notification.queue(project) as queue:
|
2016-03-17 18:32:37 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
msg = yield from queue.get_json(5)
|
|
|
|
response.write(("{}\n".format(msg)).encode("utf-8"))
|
|
|
|
except asyncio.futures.CancelledError as e:
|
|
|
|
break
|
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/notifications/ws",
|
2016-05-14 03:00:07 +03:00
|
|
|
description="Receive notifications about projects from a Websocket",
|
2016-03-17 18:32:37 +02:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 18:32:37 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "End of stream",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def notification_ws(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 18:32:37 +02:00
|
|
|
|
|
|
|
ws = aiohttp.web.WebSocketResponse()
|
|
|
|
yield from ws.prepare(request)
|
|
|
|
|
2016-05-18 15:56:23 +03:00
|
|
|
with controller.notification.queue(project) as queue:
|
2016-03-17 18:32:37 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2016-05-14 03:00:07 +03:00
|
|
|
notification = yield from queue.get_json(5)
|
2016-03-17 18:32:37 +02:00
|
|
|
except asyncio.futures.CancelledError as e:
|
|
|
|
break
|
2016-05-14 03:00:07 +03:00
|
|
|
ws.send_str(notification)
|
2016-03-17 18:32:37 +02:00
|
|
|
return ws
|