diff --git a/gns3server/handlers/api/controller/compute_handler.py b/gns3server/handlers/api/controller/compute_handler.py index 5795f64a..8ce3b8e3 100644 --- a/gns3server/handlers/api/controller/compute_handler.py +++ b/gns3server/handlers/api/controller/compute_handler.py @@ -58,7 +58,7 @@ class ComputeHandler: def list(request, response): controller = Controller.instance() - response.json([ c for c in controller.computes.values() ]) + response.json([c for c in controller.computes.values()]) @classmethod @Route.post( diff --git a/gns3server/handlers/api/controller/project_handler.py b/gns3server/handlers/api/controller/project_handler.py index dff30738..1958dff8 100644 --- a/gns3server/handlers/api/controller/project_handler.py +++ b/gns3server/handlers/api/controller/project_handler.py @@ -51,6 +51,17 @@ class ProjectHandler: response.set_status(201) response.json(project) + @classmethod + @Route.get( + r"/projects", + description="List projects", + status_codes={ + 200: "List of projects", + }) + def list_projects(request, response): + controller = Controller.instance() + response.json([ p for p in controller.projects.values() ]) + @classmethod @Route.get( r"/projects/{project_id}", diff --git a/gns3server/handlers/api/controller/vm_handler.py b/gns3server/handlers/api/controller/vm_handler.py index 7674a8c3..8934f8be 100644 --- a/gns3server/handlers/api/controller/vm_handler.py +++ b/gns3server/handlers/api/controller/vm_handler.py @@ -48,6 +48,22 @@ class VMHandler: response.set_status(201) response.json(vm) + @classmethod + @Route.get( + r"/projects/{project_id}/vms", + parameters={ + "project_id": "UUID for the project" + }, + status_codes={ + 200: "List of VMS", + }, + description="List VMs of a project") + def list_vms(request, response): + + controller = Controller.instance() + project = controller.getProject(request.match_info["project_id"]) + response.json([ v for v in project.vms.values() ]) + @classmethod @Route.put( r"/projects/{project_id}/vms/{vm_id}", diff --git a/tests/handlers/api/controller/test_compute.py b/tests/handlers/api/controller/test_compute.py index ff8b078d..0eb3b358 100644 --- a/tests/handlers/api/controller/test_compute.py +++ b/tests/handlers/api/controller/test_compute.py @@ -63,4 +63,3 @@ def test_compute_list(http_controller, controller): 'user': 'julien' } ] - diff --git a/tests/handlers/api/controller/test_project.py b/tests/handlers/api/controller/test_project.py index 8d753392..de32168f 100644 --- a/tests/handlers/api/controller/test_project.py +++ b/tests/handlers/api/controller/test_project.py @@ -75,6 +75,13 @@ def test_create_project_with_uuid(http_controller): assert response.json["name"] == "test" +def test_list_projects(http_controller, tmpdir): + http_controller.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}) + response = http_controller.get("/projects", example=True) + assert response.status == 200 + projects = response.json + assert projects[0]["name"] == "test" + def test_commit_project(http_controller, project): with asyncio_patch("gns3server.controller.project.Project.commit", return_value=True) as mock: response = http_controller.post("/projects/{project_id}/commit".format(project_id=project.id), example=True) diff --git a/tests/handlers/api/controller/test_vm.py b/tests/handlers/api/controller/test_vm.py index fd9ffc18..fc2bcb66 100644 --- a/tests/handlers/api/controller/test_vm.py +++ b/tests/handlers/api/controller/test_vm.py @@ -72,6 +72,24 @@ def test_create_vm(http_controller, tmpdir, project, compute): assert "name" not in response.json["properties"] +def test_list_vm(http_controller, tmpdir, project, compute): + response = MagicMock() + response.json = {"console": 2048} + compute.post = AsyncioMagicMock(return_value=response) + + response = http_controller.post("/projects/{}/vms".format(project.id), { + "name": "test", + "vm_type": "vpcs", + "compute_id": "example.com", + "properties": { + "startup_script": "echo test" + } + }) + response = http_controller.get("/projects/{}/vms".format(project.id), example=True) + assert response.status == 200 + assert response.json[0]["name"] == "test" + + def test_update_vm(http_controller, tmpdir, project, compute, vm): response = MagicMock() response.json = {"console": 2048}