Skip to content
Snippets Groups Projects
netbox.py 1.13 KiB
"""A CLI for interacting with Netbox."""

import typer
from pynetbox import RequestError

from gso.services.netbox_client import NetboxClient
from gso.utils.device_info import DEFAULT_SITE, ROUTER_ROLE

app: typer.Typer = typer.Typer()


@app.command()
def netbox_initial_setup() -> None:
    """Set up NetBox for the first time.

    It includes:
    - Creating a default site (GEANT)
    - Creating device roles (Router)
    """
    typer.echo("Initial setup of NetBox ...")
    typer.echo("Connecting to NetBox ...")

    nbclient = NetboxClient()

    typer.echo("Creating GEANT site ...")
    try:
        nbclient.create_device_site(DEFAULT_SITE["name"], DEFAULT_SITE["slug"])
        typer.echo("Site created successfully.")
    except RequestError as e:
        typer.echo(f"Error creating site: {e}")

    typer.echo("Creating Router device role ...")
    try:
        nbclient.create_device_role(ROUTER_ROLE["name"], ROUTER_ROLE["slug"])
        typer.echo("Device role created successfully.")
    except RequestError as e:
        typer.echo(f"Error creating device role: {e}")

    typer.echo("NetBox initial setup completed successfully.")