Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions virl2_client/models/cl_pyats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from __future__ import annotations

import io
import re
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -120,6 +121,37 @@ def sync_testbed(self, username: str, password: str) -> None:
self._testbed = self._load_pyats_testbed(testbed_yaml)
self.set_termserv_credentials(username, password)

def switch_pyats_serial_console(self, node_label: str, console_number: int) -> None:
"""
Switch to different serial console that is used to execute PyAts commands
should be executed after sync_testbed
and re-executed after every sync_testbed call.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

speaking of - if you use console 0, then switch to console 1 using this method, will the next command perhaps reuse the connection it already has established with console 0? Maybe coordinate with the other ticket about closing an existing connection if the connection number does not match.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you keep older connection open then it will still use console 0.


:param node_label: The label/title of the device.
:param console_number: The serial console number to be used for PyAts.
"""

try:
pyats_device: Device = self._testbed.devices[node_label]
except KeyError:
raise PyatsDeviceNotFound(node_label)

node = self._lab.get_node_by_label(node_label)

# will raise API error inside if console does not exist for device
node.console_key(console_number)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't want to store and use the key? what's the reason of calling it then? I'd let it fail later

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, let it fail on connect if the console cannot get actually connected

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is just a simple way how to verify that console with this number exists. we do not have API for getting list of available consoles. so calling this api raises error like "console 5 does not exist". if this console does not exist then we can not use it for pyats as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as virlos said - "let it fail on connect". I am removing this check at all here. just replacing serial number in connection string


old_connect_command = pyats_device.connections["a"]["command"]

pattern = rf"({re.escape(node_label)})/\d+"
new_console_cfg = f"/{console_number}"

new_connect_command = re.sub(
pattern, rf"\1{new_console_cfg}", old_connect_command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should rather check that the console number is 0-3, and only change the last character. No need to use regex here

)

pyats_device.connections["a"]["command"] = new_connect_command

def set_termserv_credentials(
self,
username: str | None = None,
Expand Down
6 changes: 4 additions & 2 deletions virl2_client/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,16 @@ def console_logs(self, console_id: int, lines: int | None = None) -> dict:
return self._session.get(url).json()

@check_stale
def console_key(self) -> str:
def console_key(self, console_number: int = 0) -> str:
"""
Get the console key of the node.

:param console_number: The console number (defaults to 0).
:returns: The console key.
"""
params = {"line": console_number}
url = self._url_for("console_key")
return self._session.get(url).json()
return self._session.get(url, params=params).json()

@check_stale
def vnc_key(self) -> str:
Expand Down