Skip to content
Snippets Groups Projects

Netbox integration including intial CLI for populating base data and ...

Merged Neda Moeini requested to merge netbox-integration into develop
1 file
+ 29
2
Compare changes
  • Side-by-side
  • Inline
  • 53cbe8a2
    NAT-243: added method to allocate interface · 53cbe8a2
    Hakan Calim authored
@@ -55,6 +55,12 @@ class NotFoundError(Exception):
pass
# An exception on a workflow error
class WorkflowStateException(Exception):
"""Exception raised on problems during workflow."""
pass
def connect(api, token):
"""
Creates a netbox client to communicate
@@ -222,10 +228,10 @@ def reserve_interface(nbclient, device_name: str, iface_name: str) -> dict:
# Reserve interface by enabling it
if interface is None:
raise NotFoundError(f"Interface: {iface_name} on device: {device_name} not found.")
# Check if interface is reserved
if interface.enabled:
print("Interface is reserved")
raise WorkflowStateException(f"The interface: {iface_name} on device: {device_name} is already reserved.")
# Reserve interface by enabling it
interface.enabled = True
@@ -234,6 +240,27 @@ def reserve_interface(nbclient, device_name: str, iface_name: str) -> dict:
return dict(interface)
def allocate_interface(nbclient, device_name: str, iface_name: str) -> dict:
# First get interface from device
device = get_device_by_name(nbclient, device_name)
interface = nbclient.dcim.interfaces.get(device_id=device.id,
name=iface_name)
# allocate interface by marking it as connected
# Check if interface is available
if interface is None:
raise NotFoundError(f"Interface: {iface_name} on device: {device_name} not found.")
# Check if interface is reserved
if interface.mark_connected:
raise WorkflowStateException(f"The interface: {iface_name} on device: {device_name} is already allocated.")
# allocate interface by mark as connected
interface.mark_connected = True
interface.save()
return dict(interface)
if __name__ == "__main__":
print(dict(create_device_manufacturer("Juniper", "juniper")))
Loading