Skip to content

Commit

Permalink
Fix cable creation in NetBox v3.3 (netbox-community#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-konts committed Feb 10, 2023
1 parent cc3816e commit c1b054a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugins/module_utils/netbox_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,25 @@ 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:
Expand Down

0 comments on commit c1b054a

Please sign in to comment.