From 1001db6a2a7351ff04d55342d47c9ddda36fa5ee Mon Sep 17 00:00:00 2001 From: Andrii Konts Date: Fri, 10 Feb 2023 17:25:52 +0200 Subject: [PATCH] Fix cable creation in NetBox v3.3 (#927) --- plugins/module_utils/netbox_dcim.py | 14 ++++++++++++++ plugins/module_utils/netbox_utils.py | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/plugins/module_utils/netbox_dcim.py b/plugins/module_utils/netbox_dcim.py index 0a40a6a09..8f178bb28 100644 --- a/plugins/module_utils/netbox_dcim.py +++ b/plugins/module_utils/netbox_dcim.py @@ -188,6 +188,20 @@ def run(self): self.nb_object = cables[0] else: self._handle_errors(msg="More than one result returned for %s" % (name)) + + if Version(self.full_version) >= Version("3.3.0"): + data['a_terminations'] = [ + { + "object_id": data.pop("termination_a_id"), + "object_type": data.pop("termination_a_type") + } + ] + data['b_terminations'] = [ + { + "object_id": data.pop("termination_b_id"), + "object_type": data.pop("termination_b_type") + } + ] else: object_query_params = self._build_query_params( endpoint_name, data, user_query_params diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index 5aa1bb7a8..8f93deb6a 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -1315,6 +1315,29 @@ def _update_netbox_object(self, data): else: updated_obj["vcpus"] = float(data["vcpus"]) + # Ensure idempotency for cable on netbox versions later than 3.3 + version_post_33 = self._version_check_greater(self.version, "3.3", True) + if ( + serialized_nb_obj.get("a_terminations") and + serialized_nb_obj.get("b_terminations") and + data.get("a_terminations") and + data.get("b_terminations") and + version_post_33 + ): + def _convert_termination(termination): + object_app = self._find_app(termination.endpoint.name) + object_name = ENDPOINT_NAME_MAPPING[termination.endpoint.name] + return { + "object_id": termination.id, + "object_type": f"{object_app}.{object_name}" + } + serialized_nb_obj["a_terminations"] = list( + map(_convert_termination, self.nb_object.a_terminations) + ) + serialized_nb_obj["b_terminations"] = list( + map(_convert_termination, self.nb_object.b_terminations) + ) + if serialized_nb_obj == updated_obj: return serialized_nb_obj, None else: