From bc80e713ae2a0bf5bd7102cb3283020c31990109 Mon Sep 17 00:00:00 2001 From: krlosromero Date: Tue, 3 Nov 2020 21:46:53 +0000 Subject: [PATCH] Creating delete_links for issue #79 provide delete_link function --- gns3fy/gns3fy.py | 73 ++++++++++++++++++++++++++++++++++++++++---- tests/test_gns3fy.py | 12 ++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/gns3fy/gns3fy.py b/gns3fy/gns3fy.py index c175579..33958ae 100644 --- a/gns3fy/gns3fy.py +++ b/gns3fy/gns3fy.py @@ -1758,17 +1758,13 @@ def create_link(self, node_a, port_a, node_b, port_b): node_id=_node_a.node_id, adapter_number=_port_a["adapter_number"], port_number=_port_a["port_number"], - label=dict( - text=_port_a["name"], - ), + label=dict(text=_port_a["name"]), ), dict( node_id=_node_b.node_id, adapter_number=_port_b["adapter_number"], port_number=_port_b["port_number"], - label=dict( - text=_port_b["name"], - ), + label=dict(text=_port_b["name"]), ), ], ) @@ -1777,6 +1773,71 @@ def create_link(self, node_a, port_a, node_b, port_b): self.links.append(_link) print(f"Created Link-ID: {_link.link_id} -- Type: {_link.link_type}") + def delete_link(self, node_a, port_a, node_b, port_b): + """ + Deletes a link. + + **Required Attributes:** + + - `project_id` + - `connector` + - `node_a`: Node name of the A side + - `port_a`: Port name of the A side (must match the `name` attribute of the + port) + - `node_b`: Node name of the B side + - `port_b`: Port name of the B side (must match the `name` attribute of the + port) + """ + if not self.nodes: + self.get_nodes() + if not self.links: + self.get_links() + + # checking link info + _node_a = self.get_node(name=node_a) + if not _node_a: + raise ValueError(f"node_a: {node_a} not found") + try: + _port_a = [_p for _p in _node_a.ports if _p["name"] == port_a][0] + except IndexError: + raise ValueError(f"port_a: {port_a} not found") + + _node_b = self.get_node(name=node_b) + if not _node_b: + raise ValueError(f"node_b: {node_b} not found") + try: + _port_b = [_p for _p in _node_b.ports if _p["name"] == port_b][0] + except IndexError: + raise ValueError(f"port_b: {port_b} not found") + + _matches = [] + for _l in self.links: + if not _l.nodes: + continue + if ( + _l.nodes[0]["node_id"] == _node_a.node_id + and _l.nodes[0]["adapter_number"] == _port_a["adapter_number"] + and _l.nodes[0]["port_number"] == _port_a["port_number"] + ): + _matches.append(_l) + elif ( + _l.nodes[1]["node_id"] == _node_b.node_id + and _l.nodes[1]["adapter_number"] == _port_b["adapter_number"] + and _l.nodes[1]["port_number"] == _port_b["port_number"] + ): + _matches.append(_l) + if not _matches: + raise ValueError(f"Link not found: {node_a, port_a, node_b, port_b}") + + # now to delete the link via GNS3_api + _link = _matches[0] + self.links.remove(_link) + _link_id = _link.link_id + _link.delete() + print( + f"Deleted Link-ID: {_link_id} From node {node_a }, port: {port_a} <--> to node {node_b}, port: {port_b} " + ) + @verify_connector_and_id def get_snapshots(self): """ diff --git a/tests/test_gns3fy.py b/tests/test_gns3fy.py index 7486507..0d16a63 100644 --- a/tests/test_gns3fy.py +++ b/tests/test_gns3fy.py @@ -436,6 +436,11 @@ def _apply_responses(self): f"{self.base_url}/projects/{CPROJECT['id']}/drawings/{CDRAWING['id']}", status_code=204, ) + self.adapter.register_uri( + "DELETE", + f"{self.base_url}/projects/{CPROJECT['id']}/links/NEW_LINK_ID", + status_code=204, + ) # Extra project self.adapter.register_uri( "GET", @@ -1429,6 +1434,13 @@ def test_create_link(self, api_test_project): assert link.link_id == "NEW_LINK_ID" assert link.link_type == "ethernet" + def test_delete_link(self, api_test_project): + api_test_project.links = [] + api_test_project.create_link("IOU1", "Ethernet1/1", "vEOS", "Ethernet2") + link = api_test_project.get_link(link_id="NEW_LINK_ID") + api_test_project.delete_link("IOU1", "Ethernet1/1", "vEOS", "Ethernet2") + assert api_test_project.get_link(link_id="NEW_LINK_ID") is None + @pytest.mark.parametrize( "link,expected", [