Skip to content

Commit

Permalink
Add support for a starting index when connecting nodes. (#126)
Browse files Browse the repository at this point in the history
* Add support for a starting index when connecting nodes.

By default, the `lab.connect_two_nodes()` connects the next two
available indexes.  Extend this to allow an index where you can start
at, for example, 1.  This could allow to skip mgmt interfaces.

* Consume the position element.

Spotted by: Patrick Mosko

---------

Co-authored-by: Joe Clarke <jclarke@cisco.com>
  • Loading branch information
xorrkaz and jclarke-csco authored Dec 10, 2024
1 parent 22693cd commit af60c0b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions virl2_client/models/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,16 +955,17 @@ def _create_link_local(

@check_stale
@locked
def connect_two_nodes(self, node1: Node, node2: Node) -> Link:
def connect_two_nodes(self, node1: Node, node2: Node, index: int = 0) -> Link:
"""
Connect two nodes within a lab.
:param node1: The first node object.
:param node2: The second node object.
:param index: An optional starting interface index (default: 0).
:returns: The created link.
"""
iface1 = node1.next_available_interface() or node1.create_interface()
iface2 = node2.next_available_interface() or node2.create_interface()
iface1 = node1.next_available_interface(index) or node1.create_interface()
iface2 = node2.next_available_interface(index) or node2.create_interface()
return self.create_link(iface1, iface2)

@check_stale
Expand Down
5 changes: 3 additions & 2 deletions virl2_client/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,19 @@ def create_interface(
return self._lab.create_interface(self, slot, wait=wait)

@locked
def next_available_interface(self) -> Interface | None:
def next_available_interface(self, index: int = 0) -> Interface | None:
"""
Return the next available physical interface on this node.
Note: On XR 9000v, the first two physical interfaces are marked
as "do not use"... Only the third physical interface can be used
to connect to other nodes!
:param index: An optional starting interface index (default: 0).
:returns: An available physical interface or None if all existing
ones are connected.
"""
for iface in self.interfaces():
for _, iface in enumerate(self.interfaces(), index):
if not iface.connected and iface.physical:
return iface
return None
Expand Down

0 comments on commit af60c0b

Please sign in to comment.