Skip to content

Commit 0c327f2

Browse files
committed
Move set comparison/conversion to utils for a more generic approach
1 parent d32fa03 commit 0c327f2

File tree

3 files changed

+79
-21
lines changed

3 files changed

+79
-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: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,68 @@
737737
"virtualization.clustergroup": "cluster_groups",
738738
}
739739

740+
# keys (for all endpoints) that should be converted from list to set
741+
LIST_AS_SET_KEYS = set(["tags"])
742+
743+
# keys (for given endpoints) that should be converted from list to set
744+
LIST_AS_SET_ENDPOINT_KEYS = {
745+
# tenancy
746+
"contacts": set(["contact_groups"]),
747+
# extra
748+
"config_contexts": set(
749+
[
750+
"regions",
751+
"site_groups",
752+
"sites",
753+
"roles",
754+
"device_types",
755+
"platforms",
756+
"cluster_types",
757+
"cluster_groups",
758+
"clusters",
759+
"tenant_groups",
760+
"tenants",
761+
]
762+
),
763+
"custom_fields": set(["object_types"]),
764+
"custom_links": set(["object_types"]),
765+
"export_templates": set(["object_types"]),
766+
# users
767+
"groups": set(["permissions"]),
768+
"permissions": set(
769+
[
770+
"actions",
771+
"object_types",
772+
]
773+
),
774+
"users": set(
775+
[
776+
"groups",
777+
"permissions",
778+
]
779+
),
780+
# ipam
781+
"l2vpns": set(
782+
[
783+
"import_targets",
784+
"export_targets",
785+
]
786+
),
787+
"services": set(["ports"]),
788+
"service_templates": set(["ports"]),
789+
"vlan_groups": set(["vid_ranges"]),
790+
"vrfs": set(
791+
[
792+
"import_targets",
793+
"export_targets",
794+
]
795+
),
796+
# dcim, virtualization
797+
"interfaces": set(["tagged_vlans"]),
798+
}
799+
800+
config_context
801+
740802
NETBOX_ARG_SPEC = dict(
741803
netbox_url=dict(type="str", required=True),
742804
netbox_token=dict(type="str", required=True, no_log=True),
@@ -1524,9 +1586,15 @@ def _update_netbox_object(self, data):
15241586
updated_obj = serialized_nb_obj.copy()
15251587
updated_obj.update(data)
15261588

1527-
if serialized_nb_obj.get("tags") and data.get("tags"):
1528-
serialized_nb_obj["tags"] = set(serialized_nb_obj["tags"])
1529-
updated_obj["tags"] = set(data["tags"])
1589+
# these fields are considerd a set and should be converted
1590+
for k in LIST_AS_SET_KEYS:
1591+
if serialized_nb_obj.get(k) and data.get(k):
1592+
serialized_nb_obj[k] = set(serialized_nb_obj[k])
1593+
updated_obj[k] = set(data[k])
1594+
for k in LIST_AS_SET_ENDPOINT_KEYS.get(self.endpoint, []):
1595+
if serialized_nb_obj.get(k) and data.get(k):
1596+
serialized_nb_obj[k] = set(serialized_nb_obj[k])
1597+
updated_obj[k] = set(data[k])
15301598

15311599
# Ensure idempotency for site on older netbox versions
15321600
version_pre_30 = self._version_check_greater("3.0", self.api_version)

0 commit comments

Comments
 (0)