diff --git a/utils/netboxcli.py b/utils/netboxcli.py index cf50af5ef5119884e372e6b71d024e5e96b00f48..40ae84db2d1d36aa4abda23ef9fd3df0db9d67d0 100644 --- a/utils/netboxcli.py +++ b/utils/netboxcli.py @@ -1,157 +1,161 @@ -""" -Command line tool to communicate with -the NetBox API -""" -import click -import pandas as pd - -from gso.services import netbox_client as nc - -# Define url and token for netbox API -# TODO move to config file -token = "<Token>" -api = 'http://127.0.0.1:8001' -nbclient = nc.connect(api, token) - - -def convert_to_table(data, fields): - df = pd.DataFrame(data) - df = df[fields] # Selecting specific fields to display - return df - - -@click.group() -def cli(): - pass - - -@cli.group() -def create(): - pass - - -@create.command() -@click.option('--fqdn', prompt='Enter device name', help='Device name') -@click.option('--model', default="vmx", help='Device model') -@click.option('--role', default="router", help='Device role') -@click.option('--site', default="Amsterdam", help='Device role') -def device(fqdn: str, model: str, role: str, site: str): - click.echo(f"Creating device: fqdn={fqdn}, model={model}, role={role}") - new_device = nc.create_device(nbclient, fqdn, model, role, site) - print(new_device) - - -@create.command() -@click.option('--name', help='Interfacename') -@click.option('--type', default='10gbase-t', help='Interface type, default is 10GBASE-T') -@click.option('--fqdn', help='Device where to create interface') -def interface(name: str, type: str, fqdn: str): - click.echo(f"Creating interface: name={name}, type={type}, fqdn={fqdn}") - new_interface = nc.create_interface(nbclient, name, type, fqdn) - print(new_interface) - - -@create.command() -@click.option('--name', help='Manufacturer name') -@click.option('--slug', help='Short name for manufacturer') -def manufacturer(name: str, slug: str): - click.echo(f"Creating manufacturer: name={name}") - manufacturer = nc.create_device_manufacturer(nbclient, name, slug) - print(manufacturer) - - -@create.command() -@click.option('--manufacturer', help='Manufacturer for device') -@click.option('--model', help='Model for device') -@click.option('--slug', help='Short name for manufacturer') -def device_type(manufacturer: str, model: str, slug: str): - click.echo(f"Creating device type: manufacturer={manufacturer} model = {model}") - device_type = nc.create_device_type(nbclient, manufacturer, model, slug) - print(device_type) - - -@create.command() -@click.option('--name', help='Name for device role') -@click.option('--slug', help='Short name for device role') -def device_role(name: str, slug: str): - click.echo(f"Creating device role: name={name}") - device_role = nc.create_device_role(nbclient, name, slug) - print(device_role) - - -@create.command() -@click.option('--name', help='Name for device site') -@click.option('--slug', help='Short name for device site') -def device_site(name: str, slug: str): - click.echo(f"Creating device site: name={name}") - device_site = nc.create_device_site(nbclient, name, slug) - print(device_site) - - -create.add_command(device) -create.add_command(interface) -create.add_command(manufacturer) -create.add_command(device_type) -create.add_command(device_role) -create.add_command(device_site) - - -# Define list commands here -@cli.group() -def list(): - pass - - -@list.command() -@click.option('--fqdn', help='Device name to list interfaces') -@click.option('--type', default="10gbase-t", help='Interface type to list interfaces') -def interfaces(fqdn: str, type: str): - click.echo(f"Listing all interfaces for: device with fqdn={fqdn}, type={type}") - interface_list = nc.get_interfaces_by_device(nbclient, fqdn) - display_fields = ["name", "enabled", "type", "parent", "lag", "speed"] - iface_list = [] - for iface in interface_list: - iface_list.append(dict(iface)) - - table = convert_to_table(iface_list, display_fields) - print(table) - - -@list.command() -def devices(): - click.echo("Listing all devices:") - device_list = nc.get_all_devices(nbclient) - display_fields = ["name", "device_type"] - devices = [] - for device in device_list: - devices.append(dict(device)) - - table = convert_to_table(devices, display_fields) - print(table) - - -list.add_command(interfaces) -list.add_command(devices) - - -# Define here attach command -@cli.group() -def attach(): - pass - - -@attach.command() -@click.option('--fqdn', help='Device name where to attach interface to lag') -@click.option('--iface', help='Interface name to attach to lag') -@click.option('--lag', help='LAG name to attach interface') -def interface(fqdn: str, iface: str, lag: str): - click.echo(f"Attaching interface to lag: device ={fqdn}, interface name={iface} to lag={lag}") - new_iface = nc.attach_interface_to_lag(nbclient, fqdn, lag, iface) - print(new_iface) - - -attach.add_command(interface) - - -if __name__ == '__main__': - cli() +""" +Command line tool to communicate with +the NetBox API +""" +import click +import pandas as pd + +from gso.services import netbox_client as nc + +# Define url and token for netbox API +# TODO move to config file +token = "428b68e7cb7afc6e7441879f21c3de6583081e40" +api = 'http://127.0.0.1:8000' +nbclient = nc.connect(api, token) + + +def convert_to_table(data, fields): + if len(data) == 0: + print("No data is available for your request") + exit() + + df = pd.DataFrame(data) + df = df[fields] # Selecting specific fields to display + return df + + +@click.group() +def cli(): + pass + + +@cli.group() +def create(): + pass + + +@create.command() +@click.option('--fqdn', prompt='Enter device name', help='Device name') +@click.option('--model', default="vmx", help='Device model') +@click.option('--role', default="router", help='Device role') +@click.option('--site', default="Amsterdam", help='Device role') +def device(fqdn: str, model: str, role: str, site: str): + click.echo(f"Creating device: fqdn={fqdn}, model={model}, role={role}") + new_device = nc.create_device(nbclient, fqdn, model, role, site) + print(new_device) + + +@create.command() +@click.option('--name', help='Interfacename') +@click.option('--type', default='10gbase-t', help='Interface type, default is 10GBASE-T') +@click.option('--fqdn', help='Device where to create interface') +def interface(name: str, type: str, fqdn: str): + click.echo(f"Creating interface: name={name}, type={type}, fqdn={fqdn}") + new_interface = nc.create_interface(nbclient, name, type, fqdn) + print(new_interface) + + +@create.command() +@click.option('--name', help='Manufacturer name') +@click.option('--slug', help='Short name for manufacturer') +def manufacturer(name: str, slug: str): + click.echo(f"Creating manufacturer: name={name}") + manufacturer = nc.create_device_manufacturer(nbclient, name, slug) + print(manufacturer) + + +@create.command() +@click.option('--manufacturer', help='Manufacturer for device') +@click.option('--model', help='Model for device') +@click.option('--slug', help='Short name for manufacturer') +def device_type(manufacturer: str, model: str, slug: str): + click.echo(f"Creating device type: manufacturer={manufacturer} model = {model}") + device_type = nc.create_device_type(nbclient, manufacturer, model, slug) + print(device_type) + + +@create.command() +@click.option('--name', help='Name for device role') +@click.option('--slug', help='Short name for device role') +def device_role(name: str, slug: str): + click.echo(f"Creating device role: name={name}") + device_role = nc.create_device_role(nbclient, name, slug) + print(device_role) + + +@create.command() +@click.option('--name', help='Name for device site') +@click.option('--slug', help='Short name for device site') +def device_site(name: str, slug: str): + click.echo(f"Creating device site: name={name}") + device_site = nc.create_device_site(nbclient, name, slug) + print(device_site) + + +create.add_command(device) +create.add_command(interface) +create.add_command(manufacturer) +create.add_command(device_type) +create.add_command(device_role) +create.add_command(device_site) + + +# Define list commands here +@cli.group() +def list(): + pass + + +@list.command() +@click.option('--fqdn', help='Device name to list interfaces') +@click.option('--type', default="10gbase-t", help='Interface type to list interfaces') +def interfaces(fqdn: str, type: str): + click.echo(f"Listing all interfaces for: device with fqdn={fqdn}, type={type}") + interface_list = nc.get_interfaces_by_device(nbclient, fqdn) + display_fields = ["name", "enabled", "mark_connected", "custom_fields", "lag", "speed"] + iface_list = [] + for iface in interface_list: + iface_list.append(dict(iface)) + + table = convert_to_table(iface_list, display_fields) + print(table) + + +@list.command() +def devices(): + click.echo("Listing all devices:") + device_list = nc.get_all_devices(nbclient) + display_fields = ["name", "device_type"] + devices = [] + for device in device_list: + devices.append(dict(device)) + + table = convert_to_table(devices, display_fields) + print(table) + + +list.add_command(interfaces) +list.add_command(devices) + + +# Define here attach command +@cli.group() +def attach(): + pass + + +@attach.command() +@click.option('--fqdn', help='Device name where to attach interface to lag') +@click.option('--iface', help='Interface name to attach to lag') +@click.option('--lag', help='LAG name to attach interface') +def interface(fqdn: str, iface: str, lag: str): + click.echo(f"Attaching interface to lag: device ={fqdn}, interface name={iface} to lag={lag}") + new_iface = nc.attach_interface_to_lag(nbclient, fqdn, lag, iface) + print(new_iface) + + +attach.add_command(interface) + + +if __name__ == '__main__': + cli()