Skip to content

Commit

Permalink
Feat: Add an --enable flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc committed Jul 7, 2023
1 parent 97e4272 commit b14b091
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Options:
ANTA_TIMEOUT; default: 5]
--insecure Disable SSH Host Key validation [env var:
ANTA_INSECURE]
--enable Add enable mode towards the devices if
required to connect [env var: ANTA_ENABLE]
--enable-password TEXT Enable password if required to connect [env
var: ANTA_ENABLE_PASSWORD]
-i, --inventory FILE Path to the inventory YAML file [env var:
Expand Down
10 changes: 9 additions & 1 deletion anta/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@
help="Disable SSH Host Key validation",
show_default=True,
)
@click.option(
"--enable",
show_envvar=True,
is_flag=True,
default=False,
help="Add enable mode towards the devices if required to connect",
show_default=True,
)
@click.option(
"--enable-password",
show_envvar=True,
help="Enable password if required to connect",
help="Enable password if required to connect, --enable MUST be set",
)
@click.option(
"--inventory",
Expand Down
3 changes: 2 additions & 1 deletion anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def parse_inventory(ctx: click.Context, path: Path) -> AntaInventory:
inventory_file=str(path),
username=ctx.params["username"],
password=ctx.params["password"],
enable=ctx.params["enable"],
enable_password=ctx.params["enable_password"],
timeout=ctx.params["timeout"],
insecure=ctx.params["insecure"],
Expand All @@ -62,7 +63,7 @@ def parse_inventory(ctx: click.Context, path: Path) -> AntaInventory:
if __DEBUG__:
logger.exception(message)
else:
logger.error(message + f": {exc_to_str(e)}")
logger.error(f"{message}: {exc_to_str(e)}")

ctx.fail(message)
return inventory
Expand Down
2 changes: 1 addition & 1 deletion anta/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def collect(self, command: AntaCommand) -> None:
if __DEBUG__:
logger.exception(message)
else:
logger.error(message + f": {exc_to_str(e)}")
logger.error(f"{message}: {exc_to_str(e)}")
command.failed = e
logger.debug(command)

Expand Down
18 changes: 16 additions & 2 deletions anta/inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ def __str__(self) -> str:

@staticmethod
def parse(
inventory_file: str, username: str, password: str, enable_password: Optional[str] = None, timeout: Optional[float] = None, insecure: bool = False
inventory_file: str,
username: str,
password: str,
enable: bool = False,
enable_password: Optional[str] = None,
timeout: Optional[float] = None,
insecure: bool = False,
) -> AntaInventory:
# pylint: disable=too-many-arguments
"""
Expand All @@ -55,6 +61,7 @@ def parse(
inventory_file (str): Path to inventory YAML file where user has described his inputs
username (str): Username to use to connect to devices
password (str): Password to use to connect to devices
enable (bool): Whether or not the commands need to be run in enable mode towards the devices
timeout (float, optional): timeout in seconds for every API call.
Raises:
Expand All @@ -64,7 +71,14 @@ def parse(
"""

inventory = AntaInventory()
kwargs: Dict[str, Any] = {"username": username, "password": password, "enable_password": enable_password, "timeout": timeout, "insecure": insecure}
kwargs: Dict[str, Any] = {
"username": username,
"password": password,
"enable": enable,
"enable_password": enable_password,
"timeout": timeout,
"insecure": insecure,
}
kwargs = {k: v for k, v in kwargs.items() if v is not None}

with open(inventory_file, "r", encoding="UTF-8") as file:
Expand Down
8 changes: 7 additions & 1 deletion anta/tools/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def exc_to_str(exception: Exception) -> str:
"""
Helper function that returns a human readable string from an Exception object
"""
return f"{type(exception).__name__}{f' ({str(exception)})' if str(exception) else ''}"
res = f"{type(exception).__name__}"
if str(exception):
res += f" ({str(exception)})"
elif hasattr(exception, "errmsg"):
# TODO - remove when we bump aio-eapi once our PR is merged there
res += f" ({exception.errmsg})"
return res


def tb_to_str(exception: Exception) -> str:
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This repository is a Python package to automate tests on Arista devices.

This repository comes with a [cli](cli/overview.md) to run __Arista Network Test Automation__ (ANTA) framework using your preferred shell:

!!! info username/password/enable/enable-password are the same for all devices

```bash
# Install ANTA
pip install anta
Expand All @@ -36,6 +38,8 @@ Options:
ANTA_TIMEOUT; default: 5]
--insecure Disable SSH Host Key validation [env var:
ANTA_INSECURE]
--enable Add enable mode towards the devices if
required to connect [env var: ANTA_ENABLE]
--enable-password TEXT Enable password if required to connect [env
var: ANTA_ENABLE_PASSWORD]
-i, --inventory FILE Path to the inventory YAML file [env var:
Expand Down

0 comments on commit b14b091

Please sign in to comment.