-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.py
More file actions
153 lines (126 loc) · 4.76 KB
/
cli.py
File metadata and controls
153 lines (126 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import sys
import typer
from diffsync.diff import Diff
from rich import print
from rich.console import Console
from rich.table import Table
from diff_nautobot_understack.device.main import ironic_nodes_diff_from_nautobot_devices
from diff_nautobot_understack.network.main import (
openstack_network_diff_from_ucvni_network,
)
from diff_nautobot_understack.project.main import (
openstack_all_projects_diff_from_nautobot_tenant,
)
from diff_nautobot_understack.project.main import (
openstack_project_diff_from_nautobot_tenant,
)
from diff_nautobot_understack.settings import app_settings as settings
from diff_nautobot_understack.subnet.main import (
openstack_subnets_diff_from_nautobot_prefixes,
)
required_env_vars = ["NAUTOBOT_TOKEN", "NAUTOBOT_URL", "OS_CLOUD"]
app = typer.Typer(
name="diff",
add_completion=False,
help="compare data between Openstack and Nautobot.",
)
diff_outputs = {
"project": {"title": "Project Diff", "id_column_name": "Project ID"},
"network": {"title": "Network Diff", "id_column_name": "Network ID"},
"subnet": {"title": "Subnet Diff", "id_column_name": "Subnet ID"},
"device": {"title": "Device Diff", "id_column_name": "Device ID"},
}
def display_output(
diff_result: Diff, diff_output: str, output_format: str | None = None
):
print(diff_result.summary())
__output_format = (
output_format if output_format is not None else settings.output_format
)
if __output_format == "table":
diff_output_props = diff_outputs.get(diff_output)
tabular_output(
diff_result.dict().get(diff_output, {}),
diff_output_props.get("title"),
diff_output_props.get("id_column_name"),
)
elif __output_format == "human":
print("Summary:\n")
print(diff_result.str())
else:
print(diff_result.dict())
def tabular_output(diffs, title, id_column_name):
table = Table(title=title, show_lines=True)
table.add_column(id_column_name, style="cyan", no_wrap=True)
table.add_column("Change Type", style="magenta")
table.add_column("Details", style="yellow")
for diff_id, changes in diffs.items():
for change_type, details in changes.items():
table.add_row(diff_id, change_type, str(details))
console = Console()
console.print(table)
@app.command()
def projects(
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
output_format: str = typer.Option(
"json", "--format", help="Available formats json, table"
),
):
"""OpenStack projects ⟹ Nautobot tenants"""
settings.debug = debug
diff_result = openstack_all_projects_diff_from_nautobot_tenant()
display_output(diff_result, "project", output_format)
@app.command()
def project(
name: str,
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
output_format: str = typer.Option(
"json", "--format", help="Available formats json, table"
),
):
"""OpenStack projects ⟹ Nautobot tenants"""
settings.debug = debug
diff_result = openstack_project_diff_from_nautobot_tenant(os_project=name)
display_output(diff_result, "project", output_format)
@app.command()
def network(
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
output_format: str = typer.Option(
"json", "--format", help="Available formats: json, table, human"
),
):
"""OpenStack networks ⟹ Nautobot UCVNIs"""
settings.debug = debug
diff_result = openstack_network_diff_from_ucvni_network()
display_output(diff_result, "network", output_format)
@app.command()
def subnets(
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
output_format: str = typer.Option(
"json", "--format", help="Available formats: json, table, human"
),
):
"""OpenStack subnets ⟹ Nautobot prefixes"""
settings.debug = debug
diff_result = openstack_subnets_diff_from_nautobot_prefixes()
display_output(diff_result, "subnet", output_format)
@app.command()
def devices(
debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"),
output_format: str = typer.Option(
"json", "--format", help="Available formats: json, table, human"
),
):
"""Ironic nodes ⟹ Nautobot devices"""
settings.debug = debug
diff_result = ironic_nodes_diff_from_nautobot_devices()
display_output(diff_result, "device", output_format)
def check_env_vars(required_vars):
missing_vars = [var for var in required_vars if var not in os.environ]
if missing_vars:
print(f"Error: Missing environment variables: {', '.join(missing_vars)}")
sys.exit(1)
else:
print("All required environment variables are set.")
check_env_vars(required_env_vars)