Skip to content
Snippets Groups Projects
Commit 53cbe8a2 authored by Hakan Calim's avatar Hakan Calim Committed by Neda Moeini
Browse files

NAT-243: added method to allocate interface

parent 38861584
No related branches found
No related tags found
1 merge request!77Netbox integration including intial CLI for populating base data and ...
......@@ -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")))
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