From e0bbe2a4cdf68bd9c29c1911f7abd2b801a300a0 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 15 Aug 2022 15:41:05 +0200 Subject: [PATCH 1/7] 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/7] 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/7] 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 c670e2ce039373b68cd7e6f1f4f7c9a61fd94043 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 5 Sep 2022 14:46:52 +0200 Subject: [PATCH 4/7] Added methods to update and delete rear ports --- netbox/dcim.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/netbox/dcim.py b/netbox/dcim.py index 606d1b6..0ecd0dc 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -790,6 +790,32 @@ def get_rear_ports(self, **kwargs): """Return rear ports""" return self.netbox_con.get('/dcim/rear-ports/', **kwargs) + def update_rear_ports(self, rear_port_id, **kwargs): + """Update rear port + + :param rear_port_id: Rear port ID to delete + :param kwargs: Items to update + :return bool True if successful otherwise raise Exception + """ + return self.netbox_con.patch('/dcim/rear-ports/', rear_port_id, **kwargs) + + def update_rear_ports(self, rear_port_id, **kwargs): + """Update rear port + + :param rear_port_id: Rear port ID to update + :param kwargs: Items to update + :return bool True if successful otherwise raise Exception + """ + return self.netbox_con.patch('/dcim/rear-ports/', rear_port_id, **kwargs) + + def delete_rear_port(self, rear_port_id): + """Delete rear port + + :param rear_port_id: Rear port ID to delete + :return: bool True if successful otherwise raise DeleteException + """ + return self.netbox_con.delete('/dcim/rear-ports/', rear_port_id) + def create_front_port(self, name, device_id, type, rear_port_id, **kwargs): """Create a new front port for a device From c21b209510efb68cca76d26bd984c1ce1fa46fd2 Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 5 Sep 2022 14:48:25 +0200 Subject: [PATCH 5/7] Added methods to update and delete front ports --- netbox/dcim.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 0ecd0dc..9670a6f 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -790,7 +790,7 @@ def get_rear_ports(self, **kwargs): """Return rear ports""" return self.netbox_con.get('/dcim/rear-ports/', **kwargs) - def update_rear_ports(self, rear_port_id, **kwargs): + def update_rear_port(self, rear_port_id, **kwargs): """Update rear port :param rear_port_id: Rear port ID to delete @@ -799,7 +799,7 @@ def update_rear_ports(self, rear_port_id, **kwargs): """ return self.netbox_con.patch('/dcim/rear-ports/', rear_port_id, **kwargs) - def update_rear_ports(self, rear_port_id, **kwargs): + def update_rear_port(self, rear_port_id, **kwargs): """Update rear port :param rear_port_id: Rear port ID to update @@ -838,6 +838,23 @@ def get_front_ports(self, **kwargs): """Return front ports""" return self.netbox_con.get('/dcim/front-ports/', **kwargs) + def update_front_port(self, front_port_id, **kwargs): + """Update front port + + :param front_port_id: Front port ID to update + :param kwargs: Items to update + :return bool True if successful otherwise raise Exception + """ + return self.netbox_con.patch('/dcim/front-ports/', front_port_id, **kwargs) + + def delete_front_port(self, front_port_id): + """Delete front port + + :param front_port_id: Front port ID to delete + :return: bool True if successful otherwise raise DeleteException + """ + return self.netbox_con.delete('/dcim/front-ports/', front_port_id) + 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 From bd50ba821987f083d33173d2c039417b98ae351b Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Mon, 5 Sep 2022 14:50:12 +0200 Subject: [PATCH 6/7] Added methods to retrieve, update and delete cables --- netbox/dcim.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 9670a6f..9d1d9fb 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -879,4 +879,25 @@ def create_cable(self, termination_a_type, termination_a_id, termination_b_type, } ], } - return self.netbox_con.post('/dcim/cables/', required_fields, **kwargs) \ No newline at end of file + return self.netbox_con.post('/dcim/cables/', required_fields, **kwargs) + + def get_cables(self, **kwargs): + """Return cables""" + return self.netbox_con.get('/dcim/cables/', **kwargs) + + def update_cable(self, cable_id, **kwargs): + """Update cable + + :param cable_id: Cable ID to update + :param kwargs: Items to update + :return bool True if successful otherwise raise Exception + """ + return self.netbox_con.patch('/dcim/cable/', cable_id, **kwargs) + + def delete_cable(self, cable_id): + """Delete cable + + :param cable_id: Cable ID to delete + :return: bool True if successful otherwise raise DeleteException + """ + return self.netbox_con.delete('/dcim/cables/', cable_id) \ No newline at end of file From 7e7ed48c9a2c843d28be46cde48cbf2d3e806edc Mon Sep 17 00:00:00 2001 From: Daryl Stark Date: Thu, 6 Oct 2022 07:48:19 +0200 Subject: [PATCH 7/7] Removed duplicate method and added a newline --- netbox/dcim.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/netbox/dcim.py b/netbox/dcim.py index 9d1d9fb..86373cb 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -799,15 +799,6 @@ def update_rear_port(self, rear_port_id, **kwargs): """ return self.netbox_con.patch('/dcim/rear-ports/', rear_port_id, **kwargs) - def update_rear_port(self, rear_port_id, **kwargs): - """Update rear port - - :param rear_port_id: Rear port ID to update - :param kwargs: Items to update - :return bool True if successful otherwise raise Exception - """ - return self.netbox_con.patch('/dcim/rear-ports/', rear_port_id, **kwargs) - def delete_rear_port(self, rear_port_id): """Delete rear port @@ -900,4 +891,4 @@ def delete_cable(self, cable_id): :param cable_id: Cable ID to delete :return: bool True if successful otherwise raise DeleteException """ - return self.netbox_con.delete('/dcim/cables/', cable_id) \ No newline at end of file + return self.netbox_con.delete('/dcim/cables/', cable_id)