From d9f1a7c7d3b2d71ccaac20d4481fa7882c5b9895 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 24 Jul 2015 10:09:16 +0200 Subject: [PATCH] API for listing current projects Fix #274 --- gns3server/handlers/api/project_handler.py | 17 ++++++++++++++++- gns3server/schemas/project.py | 7 +++++++ gns3server/web/response.py | 7 +++++++ tests/handlers/api/test_project.py | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/gns3server/handlers/api/project_handler.py b/gns3server/handlers/api/project_handler.py index 782a2c31..d2c80a12 100644 --- a/gns3server/handlers/api/project_handler.py +++ b/gns3server/handlers/api/project_handler.py @@ -21,7 +21,7 @@ import json import os from ...web.route import Route -from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA +from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA, PROJECT_LIST_SCHEMA from ...modules.project_manager import ProjectManager from ...modules import MODULES from ...utils.asyncio import wait_run_in_executor @@ -35,6 +35,21 @@ class ProjectHandler: # How many clients has subcribe to notifications _notifications_listening = 0 + @classmethod + @Route.get( + r"/projects", + description="List projects opened on the server", + status_codes={ + 200: "Project list", + } + # output=PROJECT_LIST_SCHEMA) + ) + def list_projects(request, response): + + pm = ProjectManager.instance() + response.set_status(200) + response.json(list(pm.projects)) + @classmethod @Route.post( r"/projects", diff --git a/gns3server/schemas/project.py b/gns3server/schemas/project.py index a07039b6..59542b75 100644 --- a/gns3server/schemas/project.py +++ b/gns3server/schemas/project.py @@ -104,6 +104,13 @@ PROJECT_OBJECT_SCHEMA = { "required": ["location", "project_id", "temporary"] } +PROJECT_LIST_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "List of projects", + "type": "array", + "items": PROJECT_OBJECT_SCHEMA +} + PROJECT_FILE_LIST_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "List files in the project", diff --git a/gns3server/web/response.py b/gns3server/web/response.py index 95e3894b..492d615a 100644 --- a/gns3server/web/response.py +++ b/gns3server/web/response.py @@ -87,6 +87,13 @@ class Response(aiohttp.web.Response): self.content_type = "application/json" if hasattr(answer, '__json__'): answer = answer.__json__() + elif isinstance(answer, list): + newanswer = [] + for elem in answer: + if hasattr(elem, '__json__'): + elem = elem.__json__() + newanswer.append(elem) + answer = newanswer if self._output_schema is not None: try: jsonschema.validate(answer, self._output_schema) diff --git a/tests/handlers/api/test_project.py b/tests/handlers/api/test_project.py index 843af8e2..dd74ecf1 100644 --- a/tests/handlers/api/test_project.py +++ b/tests/handlers/api/test_project.py @@ -82,6 +82,23 @@ def test_show_project_invalid_uuid(server): assert response.status == 404 +def test_list_projects(server): + ProjectManager.instance()._projects = {} + + query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"} + response = server.post("/projects", query) + assert response.status == 201 + query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0b"} + response = server.post("/projects", query) + assert response.status == 201 + + response = server.get("/projects", example=True) + assert response.status == 200 + print(response.json) + assert len(response.json) == 2 + assert response.json[0]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b" or response.json[1]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b" + + def test_update_temporary_project(server): query = {"name": "test", "temporary": True} response = server.post("/projects", query)