From c8c61f2ae87e34950892135632b0e62605827e36 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 19 Apr 2016 16:53:41 +0200 Subject: [PATCH] Add endpoint to list the compute nodes --- gns3server/controller/__init__.py | 4 +-- .../api/controller/compute_handler.py | 12 ++++++++ tests/controller/test_controller.py | 1 + tests/handlers/api/controller/test_compute.py | 30 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 82094687..48b6ebac 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -48,14 +48,14 @@ class Controller: Save the controller configuration on disk """ data = { - "computes": [ { + "computes": [{ "host": c.host, "port": c.port, "protocol": c.protocol, "user": c.user, "password": c.password, "compute_id": c.id - } for c in self._computes.values() ], + } for c in self._computes.values()], "version": __version__ } os.makedirs(os.path.dirname(self._config_file), exist_ok=True) diff --git a/gns3server/handlers/api/controller/compute_handler.py b/gns3server/handlers/api/controller/compute_handler.py index fcebc552..5795f64a 100644 --- a/gns3server/handlers/api/controller/compute_handler.py +++ b/gns3server/handlers/api/controller/compute_handler.py @@ -48,6 +48,18 @@ class ComputeHandler: response.set_status(201) response.json(compute) + @classmethod + @Route.get( + r"/computes", + description="List compute nodes", + status_codes={ + 200: "Compute list" + }) + def list(request, response): + + controller = Controller.instance() + response.json([ c for c in controller.computes.values() ]) + @classmethod @Route.post( r"/computes/shutdown", diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 3c3d5f64..8c651834 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -82,6 +82,7 @@ def test_addCompute(controller, controller_config_path, async_run): async_run(controller.addCompute("test2")) assert len(controller.computes) == 2 + def test_addComputeConfigFile(controller, controller_config_path, async_run): async_run(controller.addCompute("test1")) assert len(controller.computes) == 1 diff --git a/tests/handlers/api/controller/test_compute.py b/tests/handlers/api/controller/test_compute.py index cfc786b3..ff8b078d 100644 --- a/tests/handlers/api/controller/test_compute.py +++ b/tests/handlers/api/controller/test_compute.py @@ -34,3 +34,33 @@ def test_compute_create(http_controller, controller): assert len(controller.computes) == 1 assert controller.computes["my_compute_id"].host == "example.com" + + +def test_compute_list(http_controller, controller): + + params = { + "compute_id": "my_compute_id", + "protocol": "http", + "host": "example.com", + "port": 84, + "user": "julien", + "password": "secure" + } + response = http_controller.post("/computes", params) + assert response.status == 201 + assert response.route == "/computes" + assert response.json["user"] == "julien" + assert "password" not in response.json + + response = http_controller.get("/computes", example=True) + assert response.json == [ + { + 'compute_id': 'my_compute_id', + 'connected': False, + 'host': 'example.com', + 'port': 84, + 'protocol': 'http', + 'user': 'julien' + } + ] +