Skip to content

Commit 3335df2

Browse files
committed
Move set comparison/conversion to utils for a more generic approach
1 parent 001d241 commit 3335df2

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Take a more generic approach for set comparison. Other models have object_types too

plugins/module_utils/netbox_users.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
NB_TOKENS = "tokens"
1616
NB_USERS = "users"
1717

18-
# These suboptions are lists, but need to be modeled as sets for comparison purposes.
19-
LIST_AS_SET_KEYS = set(["permissions", "groups", "actions", "object_types"])
20-
2118

2219
class NetboxUsersModule(NetboxModule):
2320
def __init__(self, module, endpoint):
@@ -73,27 +70,18 @@ def run(self):
7370
self.module.exit_json(**self.result)
7471

7572
def _update_netbox_object(self, data):
76-
if self.endpoint == NB_TOKENS:
77-
return self._update_netbox_token(data)
73+
if self.endpoint == "users":
74+
return self._update_netbox_user(data)
7875
else:
79-
return self.__update_netbox_object__(data)
80-
81-
def _update_netbox_token(self, data):
82-
if "key" in data:
83-
del data["key"]
84-
return self.__update_netbox_object__(data)
76+
if self.endpoint == "tokens" and "key" in data:
77+
del data["key"]
78+
return super()._update_netbox_object(data)
8579

86-
def __update_netbox_object__(self, data):
80+
def _update_netbox_user(self, data):
8781
serialized_nb_obj = self.nb_object.serialize()
8882
updated_obj = serialized_nb_obj.copy()
8983
updated_obj.update(data)
9084

91-
if serialized_nb_obj:
92-
for key in LIST_AS_SET_KEYS:
93-
if serialized_nb_obj.get(key) and data.get(key):
94-
serialized_nb_obj[key] = set(serialized_nb_obj[key])
95-
updated_obj[key] = set(data[key])
96-
9785
if serialized_nb_obj == updated_obj:
9886
return serialized_nb_obj, None
9987
else:

plugins/module_utils/netbox_utils.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,27 @@
727727
"virtualization.clustergroup": "cluster_groups",
728728
}
729729

730+
# keys (for all endpoints) that should be converted from list to set
731+
LIST_AS_SET_KEYS = set(["tags"])
732+
733+
# keys (for given endpoints) that should be converted from list to set
734+
LIST_AS_SET_ENDPOINT_KEYS = {
735+
# tenancy
736+
"contacts": set(["contact_groups"]),
737+
# extra
738+
"custom_fields": set(["object_types"]),
739+
"custom_links": set(["object_types"]),
740+
"export_templates": set(["object_types"]),
741+
# users
742+
"permissions": set(["actions", "object_types"]),
743+
"groups": set(["permissions"]),
744+
"users": set(["groups", "permissions"]),
745+
# ipam
746+
"vlan_groups": set(["vid_ranges"]),
747+
# dcim, virtualization
748+
"interfaces": set(["tagged_vlans"]),
749+
}
750+
730751
NETBOX_ARG_SPEC = dict(
731752
netbox_url=dict(type="str", required=True),
732753
netbox_token=dict(type="str", required=True, no_log=True),
@@ -1491,9 +1512,15 @@ def _update_netbox_object(self, data):
14911512
updated_obj = serialized_nb_obj.copy()
14921513
updated_obj.update(data)
14931514

1494-
if serialized_nb_obj.get("tags") and data.get("tags"):
1495-
serialized_nb_obj["tags"] = set(serialized_nb_obj["tags"])
1496-
updated_obj["tags"] = set(data["tags"])
1515+
# these fields are considerd a set and should be converted
1516+
for k in LIST_AS_SET_KEYS:
1517+
if serialized_nb_obj.get(k) and data.get(k):
1518+
serialized_nb_obj[k] = set(serialized_nb_obj[k])
1519+
updated_obj[k] = set(data[k])
1520+
for k in LIST_AS_SET_ENDPOINT_KEYS.get(self.endpoint):
1521+
if serialized_nb_obj.get(k) and data.get(k):
1522+
serialized_nb_obj[k] = set(serialized_nb_obj[k])
1523+
updated_obj[k] = set(data[k])
14971524

14981525
# Ensure idempotency for site on older netbox versions
14991526
version_pre_30 = self._version_check_greater("3.0", self.version)

0 commit comments

Comments
 (0)