8000 fix(anta.cli): Add --group-by in nrfu table command by titom73 · Pull Request #288 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(anta.cli): Add --group-by in nrfu table command #288

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

Merged
merged 4 commits into from
Jul 25, 2023
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
2 changes: 1 addition & 1 deletion anta/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def anta(ctx: click.Context, inventory: pathlib.Path, ignore_status: bool, ignor
ctx.obj["ignore_error"] = ignore_error


@anta.group()
@anta.group(cls=IgnoreRequiredWithHelp)
@click.pass_context
@click.option(
"--catalog",
Expand Down
14 changes: 11 additions & 3 deletions anta/cli/nrfu/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import click

from anta.cli.console import console
from anta.cli.utils import parse_tags, return_code
from anta.models import AntaTest
from anta.result_manager import ResultManager
Expand All @@ -24,16 +25,20 @@

@click.command()
@click.pass_context
@click.option("--tags", "-t", help="List of tags using comma as separator: tag1,tag2,tag3", type=str, required=False, callback=parse_tags)
@click.option("--tags", help="List of tags using comma as separator: tag1,tag2,tag3", type=str, required=False, callback=parse_tags)
@click.option("--device", "-d", help="Show a summary for this device", type=str, required=False)
@click.option("--test", "-t", help="Show a summary for this test", type=str, required=False)
def table(ctx: click.Context, tags: Optional[List[str]], device: Optional[str], test: Optional[str]) -> None:
@click.option(
"--group-by", default=None, type=click.Choice(["device", "test"], case_sensitive=False), help="Group result by test or host. default none", required=False
)
def table(ctx: click.Context, tags: Optional[List[str]], device: Optional[str], test: Optional[str], group_by: str) -> None:
"""ANTA command to check network states with table result"""
print_settings(ctx)
results = ResultManager()
with anta_progress_bar() as AntaTest.progress:
asyncio.run(main(results, ctx.obj["inventory"], ctx.obj["catalog"], tags=tags))
print_table(results=results, device=device, test=test)

print_table(results=results, device=device, group_by=group_by, test=test)

# TODO make a util method to avoid repeating the same three line
ignore_status = ctx.obj["ignore_status"]
Expand All @@ -58,6 +63,7 @@ def json(ctx: click.Context, tags: Optional[List[str]], output: Optional[pathlib
results = ResultManager()
with anta_progress_bar() as AntaTest.progress:
asyncio.run(main(results, ctx.obj["inventory"], ctx.obj["catalog"], tags=tags))
console.print("\n")
print_json(results=results, output=output)

ignore_status = ctx.obj["ignore_status"]
Expand All @@ -76,6 +82,7 @@ def text(ctx: click.Context, tags: Optional[List[str]], search: Optional[str], s
results = ResultManager()
with anta_progress_bar() as AntaTest.progress:
asyncio.run(main(results, ctx.obj["inventory"], ctx.obj["catalog"], tags=tags))
console.print("\n")
print_text(results=results, search=search, skip_error=skip_error)

ignore_status = ctx.obj["ignore_status"]
Expand Down Expand Up @@ -108,6 +115,7 @@ def tpl_report(ctx: click.Context, tags: Optional[List[str]], template: pathlib.
results = ResultManager()
with anta_progress_bar() as AntaTest.progress:
asyncio.run(main(results, ctx.obj["inventory"], ctx.obj["catalog"], tags=tags))
console.print("\n")
print_jinja(results=results, template=template, output=output)

ignore_status = ctx.obj["ignore_status"]
Expand Down
15 changes: 10 additions & 5 deletions anta/cli/nrfu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ def print_settings(context: click.Context, report_template: Optional[pathlib.Pat
console.print(Panel.fit(message, style="cyan", title="[green]Settings"))


def print_table(results: ResultManager, device: Optional[str] = None, test: Optional[str] = None) -> None:
def print_table(results: ResultManager, device: Optional[str] = None, test: Optional[str] = None, group_by: Optional[str] = None) -> None:
"""Print result in a table"""
reporter = ReportTable()
if device:
console.print(reporter.report_summary_hosts(result_manager=results, host=device))
if test:
console.print(reporter.report_summary_tests(result_manager=results, testcase=test))
if device is None and test is None:
console.print(reporter.report_all(result_manager=results, host=device))
elif test:
console.print(reporter.report_all(result_manager=results, testcase=test))
elif group_by == "device":
console.print(reporter.report_summary_hosts(result_manager=results, host=None))
elif group_by == "test":
console.print(reporter.report_summary_tests(result_manager=results, testcase=None))
else:
console.print(reporter.report_all(result_manager=results))


Expand Down Expand Up @@ -110,6 +114,7 @@ def anta_progress_bar() -> Progress:
"""
Return a customized Progress for progress bar
"""
console.print("\n")
return Progress(
SpinnerColumn("anta"),
TextColumn("•"),
Expand Down
4 changes: 4 additions & 0 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def parse_catalog(ctx: click.Context, param: Option, value: str) -> List[Tuple[C
"""
Click option callback to parse an ANTA tests catalog YAML file
"""
if ctx.obj.get("_anta_help"):
# Currently looking for help for a subcommand so no
# need to parse the Catalog - return an empty list
return []
try:
with open(value, "r", encoding="UTF-8") as file:
data = safe_load(file)
Expand Down
35 changes: 28 additions & 7 deletions docs/cli/nrfu.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,21 @@ Usage: anta nrfu table [OPTIONS]
ANTA command to check network states with table result

Options:
-t, --tags TEXT List of tags using comma as separator: tag1,tag2,tag3
-d, --device TEXT Show a summary for this device
-t, --test TEXT Show a summary for this test
--help Show this message and exit.
--tags TEXT List of tags using comma as separator:
tag1,tag2,tag3
-d, --device TEXT Show a summary for this device
-t, --test TEXT Show a summary for this test
--group-by [device|test] Group result by test or host. default none
--help Show this message and exit.
```

The `--tags` option can be used to target specific devices in your inventory.

The `--device` and `--test` options show a summarized view of the test results for a specific host or test case, respectively.

### Example
The `--group-by` option show a summarized view of the test results per host or per test.

### Examples

```bash
anta nrfu table --tags LEAF
Expand All @@ -95,9 +99,26 @@ anta nrfu table --tags LEAF
For larger setups, you can also group the results by host or test to get a summarized view:

```bash
anta nrfu table --tags LEAF --device DC1-LEAF1A
anta nrfu table --group-by device
```
[![anta nrfu table group_by_host_output](../imgs/anta-nrfu-table-group-by-host-output.png){ loading=lazy width="1600" }](../imgs/anta-nrfu-table-group-by-host-output.png)

```bash
anta nrfu table --group-by test
```
[![anta nrfu table group_by_test_output](../imgs/anta-nrfu-table-group-by-test-output.png){ loading=lazy width="1600" }](../imgs/anta-nrfu-table-group-by-test-output.png)

To get more specific information, it is possible to filter on a single device or a single test:

```bash
anta nrfu table --device spine1
```
[![anta nrfu table filter_host_output](../imgs/anta-nrfu-table-filter-host-output.png){ loading=lazy width="1600" }](../imgs/anta-nrfu-table-filter-host-output.png)

```bash
anta nrfu table --test VerifyZeroTouch
```
[![anta nrfu table per host results](../imgs/anta-nrfu-table-per-host-output.png){ loading=lazy width="1600" }](../imgs/anta-nrfu-table-per-host-output.png)
[![anta nrfu table filter_test_output](../imgs/anta-nrfu-table-filter-test-output.png){ loading=lazy width="1600" }](../imgs/anta-nrfu-table-filter-test-output.png)

## Performing NRFU with JSON rendering

Expand Down
Binary file added docs/imgs/anta-nrfu-table-filter-host-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/imgs/anta-nrfu-table-filter-test-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
0