Skip to content
Snippets Groups Projects
Commit 17c74a1f authored by JORGE SASIAIN's avatar JORGE SASIAIN Committed by Neda Moeini
Browse files

NAT-244: create method to delete interface in NetBox

parent ac53b2e9
No related branches found
No related tags found
1 merge request!94Feature/Netbox integration terminate ip trunk
......@@ -131,7 +131,7 @@ class NetboxClient:
return int("".join(filter(str.isdigit, type_parts[0]))) * 1000000
return None
def create_device(self, router_name: str, site_tier: str) -> Devices:
def create_device(self, device_name: str, site_tier: str) -> Devices:
"""Create a new device in Netbox."""
# Get device type id
......@@ -146,7 +146,7 @@ class NetboxClient:
# Create new device
device = self.netbox.dcim.devices.create(
name=router_name, device_type=device_type.id, role=device_role.id, site=device_site.id
name=device_name, device_type=device_type.id, role=device_role.id, site=device_site.id
)
module_bays = list(self.netbox.dcim.module_bays.filter(device_id=device.id))
card_type = self.netbox.dcim.module_types.get(model=tier_info.module_type)
......@@ -168,8 +168,9 @@ class NetboxClient:
return device
def delete_device(self, router_name: str) -> None:
self.netbox.dcim.devices.get(name=router_name).delete()
def delete_device(self, device_name: str) -> None:
"""Delete device by name"""
self.netbox.dcim.devices.get(name=device_name).delete()
return
def attach_interface_to_lag(
......@@ -191,7 +192,8 @@ class NetboxClient:
# Assign interface to LAG, ensuring it doesn't already belong to a LAG
if iface.lag:
raise WorkflowStateError(
f"The interface: {iface_name} on device: {device_name} already belongs to a LAG: {iface.lag.name}.")
f"The interface: {iface_name} on device: {device_name} already belongs to a LAG: {iface.lag.name}."
)
iface.lag = lag.id
......@@ -305,4 +307,4 @@ class NetboxClient:
speed_bps = self.calculate_speed_bits_per_sec(speed)
return self.netbox.dcim.interfaces.filter(
device=device.name, enabled=False, mark_connected=False, speed=speed_bps
)
\ No newline at end of file
)
......@@ -131,9 +131,9 @@ list.add_command(interfaces)
list.add_command(devices)
# Define here attach command
# Define delete commands here
@cli.group()
def attach() -> None:
def delete() -> None:
pass
......@@ -147,7 +147,16 @@ def interface_to_lag(fqdn: str, iface: str, lag: str) -> None:
click.echo(new_iface)
attach.add_command(interface_to_lag)
@delete.command()
@click.option("--fqdn", help="Device name from where to get interface to delete")
@click.option("--iface", help="Name of interface name to delete")
def interface(fqdn: str, iface: str) -> None:
click.echo(f"Deleting interface: device={fqdn}, interface name={iface}")
NetboxClient().delete_interface(fqdn, iface)
delete.add_command(device)
delete.add_command(interface)
# The action command
......@@ -188,7 +197,7 @@ def allocate_interface(fqdn: str, iface: str) -> None:
@click.option("--iface", help="Interface name to edit")
def deallocate_interface(fqdn: str, iface: str) -> None:
click.echo(f"Deallocating interface: device={fqdn}, interface name={iface}")
deallocated_iface = NetboxClient().deallocate_interface(fqdn, iface)
deallocated_iface = NetboxClient().free_interface(fqdn, iface)
click.echo(deallocated_iface)
......@@ -198,26 +207,15 @@ def deallocate_interface(fqdn: str, iface: str) -> None:
@click.option("--iface", help="Interface name to attach to LAG")
def attach_interface_to_lag(fqdn: str, lag: str, iface: str) -> None:
click.echo(f"Attaching LAG to physical interface: device={fqdn}, LAG name={lag}, interface name={iface}")
attached_iface = NetBoxClient().attach_interface_to_lag(fqdn, lag, iface)
attached_iface = NetboxClient().attach_interface_to_lag(fqdn, lag, iface)
click.echo(attached_iface)
@action.command()
@click.option("--fqdn", help="Device name from where to get physical interface to unattach LAG")
@click.option("--lag", help="LAG name to unattach from physical interface")
@click.option("--iface", help="Interface name to unattach LAG from")
def unattach_interface_from_lag(fqdn: str, lag: str, iface: str) -> None:
click.echo(f"Unattaching LAG from physical interface: device={fqdn}, LAG name={lag}, interface name={iface}")
unattached_iface = NetBoxClient().unattach_interface_from_lag(fqdn, lag, iface)
click.echo(unattached_iface)
action.add_command(reserve_interface)
action.add_command(free_interface)
action.add_command(allocate_interface)
action.add_command(deallocate_interface)
action.add_command(attach_interface_to_lag)
action.add_command(unattach_interface_from_lag)
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment