From 62f87de23a8e38b39ce6917d46924811e7d00e43 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 6 Jun 2018 20:46:44 +0700 Subject: [PATCH] Add API endpoint to return all links attached to a node. --- gns3server/compute/project_manager.py | 6 ++--- gns3server/controller/node.py | 2 +- .../handlers/api/controller/node_handler.py | 22 +++++++++++++++++++ tests/controller/test_link.py | 6 ++--- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/gns3server/compute/project_manager.py b/gns3server/compute/project_manager.py index 6af4a64a..5ed61542 100644 --- a/gns3server/compute/project_manager.py +++ b/gns3server/compute/project_manager.py @@ -90,9 +90,9 @@ class ProjectManager: return # send a warning if used disk space is >= 90% if used_disk_space >= 90: - message = 'Only {}% or less of free disk space detected in "{}" on "{}"'.format(100 - used_disk_space, - project.path, - platform.node()) + message = 'Only {:.2f}% or less of free disk space detected in "{}" on "{}"'.format(100 - used_disk_space, + project.path, + platform.node()) log.warning(message) project.emit("log.warning", {"message": message}) diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 1b5882ad..f496e3d9 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -321,7 +321,7 @@ class Node: self._links.remove(link) @property - def link(self): + def links(self): return self._links @asyncio.coroutine diff --git a/gns3server/handlers/api/controller/node_handler.py b/gns3server/handlers/api/controller/node_handler.py index 65e3a308..b9993cf3 100644 --- a/gns3server/handlers/api/controller/node_handler.py +++ b/gns3server/handlers/api/controller/node_handler.py @@ -307,6 +307,28 @@ class NodeHandler: yield from project.delete_node(request.match_info["node_id"]) response.set_status(204) + @Route.get( + r"/projects/{project_id}/nodes/{node_id}/links", + parameters={ + "project_id": "Project UUID", + "node_id": "Node UUID" + }, + status_codes={ + 200: "Links returned", + 400: "Invalid request", + 404: "Instance doesn't exist" + }, + description="Return all the links connected to this node") + def links(request, response): + + project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"]) + node = project.get_node(request.match_info["node_id"]) + links = [] + for link in node.links: + links.append(link.__json__()) + response.json(links) + response.set_status(200) + @Route.get( r"/projects/{project_id}/nodes/{node_id}/dynamips/auto_idlepc", parameters={ diff --git a/tests/controller/test_link.py b/tests/controller/test_link.py index 97d9bcd6..2e769a42 100644 --- a/tests/controller/test_link.py +++ b/tests/controller/test_link.py @@ -98,7 +98,7 @@ def test_add_node(async_run, project, compute): assert link.create.called link._project.controller.notification.emit.assert_called_with("link.created", link.__json__()) - assert link in node2.link + assert link in node2.links def test_add_node_already_connected(async_run, project, compute): @@ -348,10 +348,10 @@ def test_delete(async_run, project, compute): node2._ports = [EthernetPort("E0", 0, 0, 4)] async_run(link.add_node(node2, 0, 4)) - assert link in node2.link + assert link in node2.links async_run(link.delete()) - assert link not in node2.link + assert link not in node2.links def test_update_filters(async_run, project, compute):