Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating delete_links for issue #79 provide delete_link function #91

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions gns3fy/gns3fy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
),
],
)
Expand All @@ -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):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_gns3fy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
[
Expand Down