|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +import typer |
| 5 | +from diffsync.diff import Diff |
| 6 | +from rich import print |
| 7 | +from rich.console import Console |
| 8 | +from rich.table import Table |
| 9 | + |
| 10 | +from diff_nautobot_understack.network.main import ( |
| 11 | + openstack_network_diff_from_ucvni_network, |
| 12 | +) |
| 13 | +from diff_nautobot_understack.project.main import ( |
| 14 | + openstack_project_diff_from_nautobot_tenant, |
| 15 | +) |
| 16 | +from diff_nautobot_understack.settings import app_settings as settings |
| 17 | + |
| 18 | +required_env_vars = ["NAUTOBOT_TOKEN", "NAUTOBOT_URL", "OS_CLOUD"] |
| 19 | + |
| 20 | + |
| 21 | +app = typer.Typer( |
| 22 | + name="diff", |
| 23 | + add_completion=False, |
| 24 | + help="compare data between Openstack and Nautobot.", |
| 25 | +) |
| 26 | +diff_outputs = { |
| 27 | + "project": {"title": "Project Diff", "id_column_name": "Project ID"}, |
| 28 | + "network": {"title": "Network Diff", "id_column_name": "Network ID"}, |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +def display_output( |
| 33 | + diff_result: Diff, diff_output: str, output_format: str | None = None |
| 34 | +): |
| 35 | + print(diff_result.summary()) |
| 36 | + __output_format = ( |
| 37 | + output_format if output_format is not None else settings.output_format |
| 38 | + ) |
| 39 | + if __output_format == "table": |
| 40 | + diff_output_props = diff_outputs.get(diff_output) |
| 41 | + tabular_output( |
| 42 | + diff_result.dict().get(diff_output, {}), |
| 43 | + diff_output_props.get("title"), |
| 44 | + diff_output_props.get("id_column_name"), |
| 45 | + ) |
| 46 | + else: |
| 47 | + print(diff_result.dict()) |
| 48 | + |
| 49 | + |
| 50 | +def tabular_output(diffs, title, id_column_name): |
| 51 | + table = Table(title=title, show_lines=True) |
| 52 | + |
| 53 | + table.add_column(id_column_name, style="cyan", no_wrap=True) |
| 54 | + table.add_column("Change Type", style="magenta") |
| 55 | + table.add_column("Details", style="yellow") |
| 56 | + |
| 57 | + for diff_id, changes in diffs.items(): |
| 58 | + for change_type, details in changes.items(): |
| 59 | + table.add_row(diff_id, change_type, str(details)) |
| 60 | + |
| 61 | + console = Console() |
| 62 | + console.print(table) |
| 63 | + |
| 64 | + |
| 65 | +@app.command() |
| 66 | +def project( |
| 67 | + name: str, |
| 68 | + debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"), |
| 69 | + output_format: str = typer.Option( |
| 70 | + "json", "--format", help="Available formats json, table" |
| 71 | + ), |
| 72 | +): |
| 73 | + """Nautobot tenants ⟹ Openstack projects""" |
| 74 | + settings.debug = debug |
| 75 | + diff_result = openstack_project_diff_from_nautobot_tenant(os_project=name) |
| 76 | + display_output(diff_result, "project", output_format) |
| 77 | + |
| 78 | + |
| 79 | +@app.command() |
| 80 | +def network( |
| 81 | + debug: bool = typer.Option(False, "--debug", "-v", help="Enable debug mode"), |
| 82 | + output_format: str = typer.Option( |
| 83 | + "json", "--format", help="Available formats json, table" |
| 84 | + ), |
| 85 | +): |
| 86 | + """Nautobot ucvnis ⟹ Openstack networks""" |
| 87 | + settings.debug = debug |
| 88 | + diff_result = openstack_network_diff_from_ucvni_network() |
| 89 | + display_output(diff_result, "network", output_format) |
| 90 | + |
| 91 | + |
| 92 | +def check_env_vars(required_vars): |
| 93 | + missing_vars = [var for var in required_vars if var not in os.environ] |
| 94 | + |
| 95 | + if missing_vars: |
| 96 | + print(f"Error: Missing environment variables: {', '.join(missing_vars)}") |
| 97 | + sys.exit(1) |
| 98 | + else: |
| 99 | + print("All required environment variables are set.") |
| 100 | + |
| 101 | + |
| 102 | +check_env_vars(required_env_vars) |
0 commit comments