Skip to content

Commit 9bfee4e

Browse files
haseebsyed12cardoe
authored andcommitted
PUC-740: pretty output
1 parent 287450c commit 9bfee4e

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

python/diff-nautobot-understack/diff_nautobot_understack/cli.py

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,87 @@
1+
from typing import Optional
2+
13
import typer
4+
from diffsync.diff import Diff
25
from diff_nautobot_understack.project.main import (
36
openstack_project_diff_from_nautobot_tenant,
47
)
58
from diff_nautobot_understack.network.main import (
69
openstack_network_diff_from_ucvni_network,
710
)
811
from diff_nautobot_understack.settings import app_settings as settings
12+
from rich.console import Console
13+
from rich.table import Table
14+
from rich import print
915

1016
app = typer.Typer(
1117
name="diff",
1218
add_completion=False,
1319
help="compare data between Openstack and Nautobot.",
1420
)
21+
diff_outputs = {
22+
"project": {"title": "Project Diff", "id_column_name": "Project ID"},
23+
"network": {"title": "Network Diff", "id_column_name": "Network ID"},
24+
}
25+
26+
27+
def display_output(
28+
diff_result: Diff, diff_output: str, output_format: Optional[str] = None
29+
):
30+
print(diff_result.summary())
31+
__output_format = (
32+
output_format if output_format is not None else settings.output_format
33+
)
34+
if __output_format == "table":
35+
diff_output_props = diff_outputs.get(diff_output)
36+
tabular_output(
37+
diff_result.dict().get(diff_output, {}),
38+
diff_output_props.get("title"),
39+
diff_output_props.get("id_column_name"),
40+
)
41+
else:
42+
print(diff_result.dict())
43+
44+
45+
def tabular_output(diffs, title, id_column_name):
46+
table = Table(title=title, show_lines=True)
47+
48+
table.add_column(id_column_name, style="cyan", no_wrap=True)
49+
table.add_column("Change Type", style="magenta")
50+
table.add_column("Details", style="yellow")
51+
52+
for diff_id, changes in diffs.items():
53+
for change_type, details in changes.items():
54+
table.add_row(diff_id, change_type, str(details))
55+
56+
console = Console()
57+
console.print(table)
1558

1659

1760
@app.command()
1861
def project(
1962
name: str,
2063
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
64+
output_format: str = typer.Option(
65+
"json", "--format", help="Available formats json, table"
66+
),
2167
):
2268
"""Nautobot tenants ⟹ Openstack projects"""
2369
settings.debug = debug
24-
openstack_project_diff_from_nautobot_tenant(os_project=name)
70+
diff_result = openstack_project_diff_from_nautobot_tenant(os_project=name)
71+
display_output(diff_result, "project", output_format)
2572

2673

2774
@app.command()
2875
def network(
2976
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
77+
output_format: str = typer.Option(
78+
"json", "--format", help="Available formats json, table"
79+
),
3080
):
3181
"""Nautobot ucvnis ⟹ Openstack networks"""
3282
settings.debug = debug
33-
openstack_network_diff_from_ucvni_network()
83+
diff_result = openstack_network_diff_from_ucvni_network()
84+
display_output(diff_result, "network", output_format)
3485

3586

3687
if __name__ == "__main__":
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from pprint import pprint
1+
from diffsync.diff import Diff
22
from diffsync.enum import DiffSyncFlags
33
from diff_nautobot_understack.network.adapters.openstack_network import (
44
Network as OpenstackNetwork,
55
)
66
from diff_nautobot_understack.network.adapters.ucvni import Network as UcvniNetwork
77

88

9-
def openstack_network_diff_from_ucvni_network():
9+
def openstack_network_diff_from_ucvni_network() -> Diff:
1010
openstack_network = OpenstackNetwork()
1111
openstack_network.load()
1212

@@ -15,7 +15,4 @@ def openstack_network_diff_from_ucvni_network():
1515
openstack_network_destination_ucvni_source = openstack_network.diff_from(
1616
ucvni_network, flags=DiffSyncFlags.CONTINUE_ON_FAILURE
1717
)
18-
pprint(" Nautobot ucvnis ⟹ Openstack networks ")
19-
summary = openstack_network_destination_ucvni_source.summary()
20-
pprint(summary, width=120)
21-
pprint(openstack_network_destination_ucvni_source.dict(), width=120)
18+
return openstack_network_destination_ucvni_source
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from pprint import pprint
1+
from diffsync.diff import Diff
22
from diffsync.enum import DiffSyncFlags
33
from diff_nautobot_understack.project.adapters.openstack_project import Project
44
from diff_nautobot_understack.project.adapters.nautobot_tenant import Tenant
55
from diff_nautobot_understack.settings import app_settings as settings
66

77

8-
def openstack_project_diff_from_nautobot_tenant(os_project=None):
8+
def openstack_project_diff_from_nautobot_tenant(os_project=None) -> Diff:
99
project_name = os_project if os_project is not None else settings.os_project
1010
openstack_project = Project(name=project_name)
1111
openstack_project.load()
@@ -15,7 +15,4 @@ def openstack_project_diff_from_nautobot_tenant(os_project=None):
1515
openstack_project_destination_tenant_source = openstack_project.diff_from(
1616
nautobot_tenant, flags=DiffSyncFlags.CONTINUE_ON_FAILURE
1717
)
18-
pprint(" Nautobot tenants ⟹ Openstack projects ")
19-
summary = openstack_project_destination_tenant_source.summary()
20-
pprint(summary, width=120)
21-
pprint(openstack_project_destination_tenant_source.dict(), width=120)
18+
return openstack_project_destination_tenant_source

python/diff-nautobot-understack/diff_nautobot_understack/settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class Settings(BaseSettings):
1010
nautobot_url: str = "https://nautobot.dev.undercloud.rackspace.net"
1111
debug: bool = False
1212
os_cloud: Optional[str] = None
13-
os_project: Optional[str] = "default"
13+
os_project: str = "default"
1414
os_client_config_file: Optional[str] = None
15+
output_format: str = "table"
1516

1617

1718
Settings.model_rebuild()

0 commit comments

Comments
 (0)