diff --git a/gso/services/netbox_client.py b/gso/services/netbox_client.py index e227032be3b6c2d1fe824d6d8b539383ddf784d6..974bb7ba9d51578178e3979714c4da3ec0c33f62 100644 --- a/gso/services/netbox_client.py +++ b/gso/services/netbox_client.py @@ -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")))