1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 17:28:08 +00:00

Change URL for projects: /project becomes /projects and project_id is used instead of uuid.

This commit is contained in:
Jeremy 2015-02-03 18:23:11 -07:00
parent aeb83a7945
commit d2699f051d
5 changed files with 56 additions and 57 deletions

View File

@ -18,15 +18,14 @@
from ..web.route import Route from ..web.route import Route
from ..schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA from ..schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA
from ..modules.project_manager import ProjectManager from ..modules.project_manager import ProjectManager
from aiohttp.web import HTTPConflict
class ProjectHandler: class ProjectHandler:
@classmethod @classmethod
@Route.post( @Route.post(
r"/project", r"/projects",
description="Create a project on the server", description="Create a new project on the server",
output=PROJECT_OBJECT_SCHEMA, output=PROJECT_OBJECT_SCHEMA,
input=PROJECT_CREATE_SCHEMA) input=PROJECT_CREATE_SCHEMA)
def create_project(request, response): def create_project(request, response):
@ -34,99 +33,99 @@ class ProjectHandler:
pm = ProjectManager.instance() pm = ProjectManager.instance()
p = pm.create_project( p = pm.create_project(
location=request.json.get("location"), location=request.json.get("location"),
uuid=request.json.get("uuid"), uuid=request.json.get("project_id"),
temporary=request.json.get("temporary", False) temporary=request.json.get("temporary", False)
) )
response.json(p) response.json(p)
@classmethod @classmethod
@Route.get( @Route.get(
r"/project/{uuid}", r"/projects/{project_id}",
description="Get project information", description="Get project information",
parameters={ parameters={
"uuid": "Project instance UUID", "project_id": "The UUID of the project",
}, },
status_codes={ status_codes={
200: "OK", 200: "Success",
404: "Project instance doesn't exist" 404: "The project doesn't exist"
}, },
output=PROJECT_OBJECT_SCHEMA) output=PROJECT_OBJECT_SCHEMA)
def show(request, response): def show(request, response):
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["uuid"]) project = pm.get_project(request.match_info["project_id"])
response.json(project) response.json(project)
@classmethod @classmethod
@Route.put( @Route.put(
r"/project/{uuid}", r"/projects/{project_id}",
description="Update a project", description="Update a project",
parameters={ parameters={
"uuid": "Project instance UUID", "project_id": "The UUID of the project",
}, },
status_codes={ status_codes={
200: "Project updated", 200: "The project has been updated",
404: "Project instance doesn't exist" 404: "The project doesn't exist"
}, },
output=PROJECT_OBJECT_SCHEMA, output=PROJECT_OBJECT_SCHEMA,
input=PROJECT_UPDATE_SCHEMA) input=PROJECT_UPDATE_SCHEMA)
def update(request, response): def update(request, response):
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["uuid"]) project = pm.get_project(request.match_info["project_id"])
project.temporary = request.json.get("temporary", project.temporary) project.temporary = request.json.get("temporary", project.temporary)
response.json(project) response.json(project)
@classmethod @classmethod
@Route.post( @Route.post(
r"/project/{uuid}/commit", r"/projects/{project_id}/commit",
description="Write changes on disk", description="Write changes on disk",
parameters={ parameters={
"uuid": "Project instance UUID", "project_id": "The UUID of the project",
}, },
status_codes={ status_codes={
204: "Changes write on disk", 204: "Changes have been written on disk",
404: "Project instance doesn't exist" 404: "The project doesn't exist"
}) })
def commit(request, response): def commit(request, response):
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["uuid"]) project = pm.get_project(request.match_info["project_id"])
yield from project.commit() yield from project.commit()
response.set_status(204) response.set_status(204)
@classmethod @classmethod
@Route.post( @Route.post(
r"/project/{uuid}/close", r"/projects/{project_id}/close",
description="Close project", description="Close a project",
parameters={ parameters={
"uuid": "Project instance UUID", "project_id": "The UUID of the project",
}, },
status_codes={ status_codes={
204: "Project closed", 204: "The project has been closed",
404: "Project instance doesn't exist" 404: "The project doesn't exist"
}) })
def close(request, response): def close(request, response):
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["uuid"]) project = pm.get_project(request.match_info["project_id"])
yield from project.close() yield from project.close()
response.set_status(204) response.set_status(204)
@classmethod @classmethod
@Route.delete( @Route.delete(
r"/project/{uuid}", r"/projects/{project_id}",
description="Delete a project from disk", description="Delete a project from disk",
parameters={ parameters={
"uuid": "Project instance UUID", "project_id": "The UUID of the project",
}, },
status_codes={ status_codes={
204: "Changes write on disk", 204: "Changes have been written on disk",
404: "Project instance doesn't exist" 404: "The project doesn't exist"
}) })
def delete(request, response): def delete(request, response):
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["uuid"]) project = pm.get_project(request.match_info["project_id"])
yield from project.delete() yield from project.delete()
response.set_status(204) response.set_status(204)

View File

@ -152,7 +152,7 @@ class Project:
def __json__(self): def __json__(self):
return { return {
"uuid": self._uuid, "project_id": self._uuid,
"location": self._location, "location": self._location,
"temporary": self._temporary "temporary": self._temporary
} }

View File

@ -26,7 +26,7 @@ PROJECT_CREATE_SCHEMA = {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1
}, },
"uuid": { "project_id": {
"description": "Project UUID", "description": "Project UUID",
"type": ["string", "null"], "type": ["string", "null"],
"minLength": 36, "minLength": 36,
@ -64,7 +64,7 @@ PROJECT_OBJECT_SCHEMA = {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1
}, },
"uuid": { "project_id": {
"description": "Project UUID", "description": "Project UUID",
"type": "string", "type": "string",
"minLength": 36, "minLength": 36,
@ -77,5 +77,5 @@ PROJECT_OBJECT_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["location", "uuid", "temporary"] "required": ["location", "project_id", "temporary"]
} }

View File

@ -26,89 +26,89 @@ from tests.utils import asyncio_patch
def test_create_project_with_dir(server, tmpdir): def test_create_project_with_dir(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/project", {"location": str(tmpdir)}) response = server.post("/projects", {"location": str(tmpdir)})
assert response.status == 200 assert response.status == 200
assert response.json["location"] == str(tmpdir) assert response.json["location"] == str(tmpdir)
def test_create_project_without_dir(server): def test_create_project_without_dir(server):
query = {} query = {}
response = server.post("/project", query) response = server.post("/projects", query)
assert response.status == 200 assert response.status == 200
assert response.json["uuid"] is not None assert response.json["project_id"] is not None
assert response.json["temporary"] is False assert response.json["temporary"] is False
def test_create_temporary_project(server): def test_create_temporary_project(server):
query = {"temporary": True} query = {"temporary": True}
response = server.post("/project", query) response = server.post("/projects", query)
assert response.status == 200 assert response.status == 200
assert response.json["uuid"] is not None assert response.json["project_id"] is not None
assert response.json["temporary"] is True assert response.json["temporary"] is True
def test_create_project_with_uuid(server): def test_create_project_with_uuid(server):
query = {"uuid": "00010203-0405-0607-0809-0a0b0c0d0e0f"} query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
response = server.post("/project", query) response = server.post("/projects", query)
assert response.status == 200 assert response.status == 200
assert response.json["uuid"] == "00010203-0405-0607-0809-0a0b0c0d0e0f" assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
def test_show_project(server): def test_show_project(server):
query = {"uuid": "00010203-0405-0607-0809-0a0b0c0d0e0f", "location": "/tmp", "temporary": False} query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f", "location": "/tmp", "temporary": False}
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/project", query) response = server.post("/projects", query)
assert response.status == 200 assert response.status == 200
response = server.get("/project/00010203-0405-0607-0809-0a0b0c0d0e0f", example=True) response = server.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e0f", example=True)
assert response.json == query assert response.json == query
def test_show_project_invalid_uuid(server): def test_show_project_invalid_uuid(server):
response = server.get("/project/00010203-0405-0607-0809-0a0b0c0d0e42") response = server.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e42")
assert response.status == 404 assert response.status == 404
def test_update_temporary_project(server): def test_update_temporary_project(server):
query = {"temporary": True} query = {"temporary": True}
response = server.post("/project", query) response = server.post("/projects", query)
assert response.status == 200 assert response.status == 200
query = {"temporary": False} query = {"temporary": False}
response = server.put("/project/{uuid}".format(uuid=response.json["uuid"]), query, example=True) response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200 assert response.status == 200
assert response.json["temporary"] is False assert response.json["temporary"] is False
def test_commit_project(server, project): def test_commit_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.commit", return_value=True) as mock: 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) response = server.post("/projects/{project_id}/commit".format(project_id=project.uuid), example=True)
assert response.status == 204 assert response.status == 204
assert mock.called assert mock.called
def test_commit_project_invalid_project_uuid(server, project): def test_commit_project_invalid_uuid(server):
response = server.post("/project/{uuid}/commit".format(uuid=uuid.uuid4())) response = server.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
assert response.status == 404 assert response.status == 404
def test_delete_project(server, project): def test_delete_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.delete", return_value=True) as mock: with asyncio_patch("gns3server.modules.project.Project.delete", return_value=True) as mock:
response = server.delete("/project/{uuid}".format(uuid=project.uuid), example=True) response = server.delete("/projects/{project_id}".format(project_id=project.uuid), example=True)
assert response.status == 204 assert response.status == 204
assert mock.called assert mock.called
def test_delete_project_invalid_uuid(server, project): def test_delete_project_invalid_uuid(server):
response = server.delete("/project/{uuid}".format(uuid=uuid.uuid4())) response = server.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
assert response.status == 404 assert response.status == 404
def test_close_project(server, project): def test_close_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.close", return_value=True) as mock: with asyncio_patch("gns3server.modules.project.Project.close", return_value=True) as mock:
response = server.post("/project/{uuid}/close".format(uuid=project.uuid), example=True) response = server.post("/projects/{project_id}/close".format(project_id=project.uuid), example=True)
assert response.status == 204 assert response.status == 204
assert mock.called assert mock.called
def test_close_project_invalid_uuid(server, project): def test_close_project_invalid_uuid(server, project):
response = server.post("/project/{uuid}/close".format(uuid=uuid.uuid4())) response = server.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
assert response.status == 404 assert response.status == 404

View File

@ -68,7 +68,7 @@ def test_changing_location_not_allowed(tmpdir):
def test_json(tmpdir): def test_json(tmpdir):
p = Project() p = Project()
assert p.__json__() == {"location": p.location, "uuid": p.uuid, "temporary": False} assert p.__json__() == {"location": p.location, "project_id": p.uuid, "temporary": False}
def test_vm_working_directory(tmpdir, vm): def test_vm_working_directory(tmpdir, vm):