From e0bbe2a4cdf68bd9c29c1911f7abd2b801a300a0 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 15 Aug 2022 15:41:05 +0200 Subject: [PATCH 1/5] Created methods to retrieve and create rear and front ports --- netbox/dcim.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/netbox/dcim.py b/netbox/dcim.py index 7911cca..4fc8aa1 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -769,3 +769,45 @@ def get_power_ports(self, **kwargs): def get_power_connections(self, **kwargs): """Return power connections """ return self.netbox_con.get('/dcim/power-connections/', **kwargs) + + def create_rear_port(self, name, device_id, type, **kwargs): + """Create a new rear port for a device + + :param name: rear port name + :param device_id: device id for the device + :param type: the type for the rear port (for instance: 8p8c) + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "name": name, + "device": device_id, + "type": type + } + return self.netbox_con.post('/dcim/rear-ports/', required_fields, **kwargs) + + def get_rear_ports(self, **kwargs): + """Return rear ports""" + return self.netbox_con.get('/dcim/rear-ports/', **kwargs) + + def create_front_port(self, name, device_id, type, rear_port_id, **kwargs): + """Create a new front port for a device + + :param name: rear port name + :param device_id: device id for the device + :param type: the type for the rear port (for instance: 8p8c) + :param rear_port_id: the id for the connected rear port + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "name": name, + "device": device_id, + "rear_port": rear_port_id, + "type": type + } + return self.netbox_con.post('/dcim/front-ports/', required_fields, **kwargs) + + def get_front_ports(self, **kwargs): + """Return front ports""" + return self.netbox_con.get('/dcim/front-ports/', **kwargs) \ No newline at end of file From afc5b6ad506b239beaf8818da4836964b64ac0a7 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 15 Aug 2022 16:56:31 +0200 Subject: [PATCH 2/5] Added a method to create a connection between two resources --- netbox/dcim.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 4fc8aa1..9be6107 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -810,4 +810,22 @@ def create_front_port(self, name, device_id, type, rear_port_id, **kwargs): def get_front_ports(self, **kwargs): """Return front ports""" - return self.netbox_con.get('/dcim/front-ports/', **kwargs) \ No newline at end of file + return self.netbox_con.get('/dcim/front-ports/', **kwargs) + + def create_cable(self, termination_a_type, termination_a_id, termination_b_type, termination_b_id, **kwargs): + """Create a new front port for a device + + :param termination_a_type: the type for the first termination (dcim.interface, dcim.frontport, etc.) + :param termination_a_id: the id for the first termination + :param termination_b_type: the type for the second termination (dcim.interface, dcim.frontport, etc.) + :param termination_b_id: the id for the second termination + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "termination_a_type": termination_a_type, + "termination_a_id": termination_a_id, + "termination_b_type": termination_b_type, + "termination_b_id": termination_b_id + } + return self.netbox_con.post('/dcim/cables/', required_fields, **kwargs) \ No newline at end of file From 9e32ad6796638b784e4aa6f24a19138daf5621e2 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Wed, 24 Aug 2022 11:38:22 +0200 Subject: [PATCH 3/5] Adjusted for the new API in Netbox 3.3.0 --- netbox/dcim.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 9be6107..606d1b6 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -823,9 +823,17 @@ def create_cable(self, termination_a_type, termination_a_id, termination_b_type, :return: netbox object if successful otherwise exception raised """ required_fields = { - "termination_a_type": termination_a_type, - "termination_a_id": termination_a_id, - "termination_b_type": termination_b_type, - "termination_b_id": termination_b_id + "a_terminations": [ + { + "object_type": termination_a_type, + "object_id": termination_a_id + } + ], + "b_terminations": [ + { + "object_type": termination_b_type, + "object_id": termination_b_id + } + ], } return self.netbox_con.post('/dcim/cables/', required_fields, **kwargs) \ No newline at end of file From 51b697c54c2e57c4ba41ea52c00da15753316d7e Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 5 Sep 2022 14:40:11 +0200 Subject: [PATCH 4/5] Created methods to create, retrieve, update and delete virtual chassis. Adding a device to a Virtual Chassis is a field for the device-model and should be done by updating the device (). --- netbox/dcim.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/netbox/dcim.py b/netbox/dcim.py index 7911cca..4dff4b9 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -250,6 +250,42 @@ def update_rack_group_by_id(self, rack_group_id, **kwargs): """ return self.netbox_con.patch('/dcim/rack-groups/', rack_group_id, **kwargs) + def create_virtual_chassis(self, name, **kwargs): + """Create a new virtual chassis + + :param name: Name of the virtual chassis + :param kwargs: Optional arguments + :return: netbox object if successful otherwise raise CreateException + """ + required_fields = {"name": name} + return self.netbox_con.post('/dcim/virtual-chassis/', required_fields, **kwargs) + + def get_virtual_chassis(self, **kwargs): + """Get all virtual chassis""" + return self.netbox_con.get('/dcim/virtual-chassis/', **kwargs) + + def update_virtual_chassis(self, virtual_chassis_name, **kwargs): + """Update virtual chassis by virtual chassis name + + :param virtual_chassis_name: virtual chassis name to update + :param kwargs: requests body dict + :return: bool True if successful otherwise raise UpdateException + """ + device_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] + return self.netbox_con.patch('/dcim/virtual-chassis/', device_id, **kwargs) + + def delete_virtual_chassis(self, virtual_chassis_name): + """Delete virtual chassis by virtual chassis name name + + :param virtual_chassis_name: Virtual chassis to delete + :return: bool True if successful otherwise raise DeleteException + """ + try: + device_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] + except IndexError: + raise exceptions.NotFoundException({"detail": "device: {}".format(virtual_chassis_name)}) from None + return self.netbox_con.delete('/dcim/virtual-chassis/', device_id) + def get_devices(self, **kwargs): """Get all devices""" return self.netbox_con.get('/dcim/devices/', **kwargs) From 08df3501457c02d55290d337856b4ee4f4c978c5 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Thu, 6 Oct 2022 07:43:43 +0200 Subject: [PATCH 5/5] Updated the variable for the `virtual_chassis_id` --- netbox/dcim.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 4dff4b9..8748b34 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -271,8 +271,8 @@ def update_virtual_chassis(self, virtual_chassis_name, **kwargs): :param kwargs: requests body dict :return: bool True if successful otherwise raise UpdateException """ - device_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] - return self.netbox_con.patch('/dcim/virtual-chassis/', device_id, **kwargs) + virtual_chassis_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] + return self.netbox_con.patch('/dcim/virtual-chassis/', virtual_chassis_id, **kwargs) def delete_virtual_chassis(self, virtual_chassis_name): """Delete virtual chassis by virtual chassis name name