From 0e76527ce2ca73753754886446e289b44d23befe Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 23 Jan 2015 16:13:58 +0100 Subject: [PATCH] Update a project --- gns3server/handlers/project_handler.py | 22 +++++++++++++++++++++- gns3server/modules/project.py | 10 ++++++++++ gns3server/schemas/project.py | 15 ++++++++++++++- tests/api/test_project.py | 10 ++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/gns3server/handlers/project_handler.py b/gns3server/handlers/project_handler.py index 83bfa401..a7cbff0f 100644 --- a/gns3server/handlers/project_handler.py +++ b/gns3server/handlers/project_handler.py @@ -16,7 +16,7 @@ # along with this program. If not, see . from ..web.route import Route -from ..schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA +from ..schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA from ..modules.project_manager import ProjectManager from aiohttp.web import HTTPConflict @@ -39,6 +39,26 @@ class ProjectHandler: ) response.json(p) + @classmethod + @Route.put( + r"/project/{uuid}", + description="Update a project", + parameters={ + "uuid": "Project instance UUID", + }, + status_codes={ + 200: "Project updated", + 404: "Project instance doesn't exist" + }, + output=PROJECT_OBJECT_SCHEMA, + input=PROJECT_UPDATE_SCHEMA) + def update(request, response): + + pm = ProjectManager.instance() + project = pm.get_project(request.match_info["uuid"]) + project.temporary = request.json.get("temporary", project.temporary) + response.json(project) + @classmethod @Route.post( r"/project/{uuid}/commit", diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index 40ba0601..b12379a7 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -76,6 +76,16 @@ class Project: return self._vms + @property + def temporary(self): + + return self._temporary + + @temporary.setter + def temporary(self, temporary): + + self._temporary = temporary + def vm_working_directory(self, vm): """ Return a working directory for a specific VM. diff --git a/gns3server/schemas/project.py b/gns3server/schemas/project.py index 08ef042d..e0c2209a 100644 --- a/gns3server/schemas/project.py +++ b/gns3server/schemas/project.py @@ -41,9 +41,22 @@ PROJECT_CREATE_SCHEMA = { "additionalProperties": False, } +PROJECT_UPDATE_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Request validation to update a Project instance", + "type": "object", + "properties": { + "temporary": { + "description": "If project is a temporary project", + "type": "boolean" + }, + }, + "additionalProperties": False, +} + PROJECT_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to create a new Project instance", + "description": "Project instance", "type": "object", "properties": { "location": { diff --git a/tests/api/test_project.py b/tests/api/test_project.py index 28cbb046..4c2c6d22 100644 --- a/tests/api/test_project.py +++ b/tests/api/test_project.py @@ -60,6 +60,16 @@ def test_create_project_with_uuid(server): assert response.json["location"] == "/tmp" +def test_update_temporary_project(server): + query = {"temporary": True} + response = server.post("/project", query) + assert response.status == 200 + query = {"temporary": False} + response = server.put("/project/{uuid}".format(uuid=response.json["uuid"]), query) + assert response.status == 200 + assert response.json["temporary"] is False + + def test_commit_project(server, project): with asyncio_patch("gns3server.modules.project.Project.commit", return_value=True) as mock: response = server.post("/project/{uuid}/commit".format(uuid=project.uuid), example=True)