Skip to content

Commit

Permalink
Added UUID check to anywhere checking for ints (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
FragmentedPacket authored Mar 14, 2021
1 parent 1c3b40a commit 28366ac
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 75 deletions.
22 changes: 15 additions & 7 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def is_valid_uuid(self, match):
"""Determine if the match is already UUID."""
try:
uuid_obj = UUID(match)
except ValueError:
except (ValueError, AttributeError):
return False
return str(uuid_obj) == match

Expand Down Expand Up @@ -704,7 +704,9 @@ def _build_query_params(
"Link Aggregation Group (LAG)", "interfaces"
)
query_dict.update({"type": intf_type})
if isinstance(module_data["device"], int):
if isinstance(module_data["device"], int) or self.is_valid_uuid(
module_data["device"]
):
query_dict.update({"device_id": module_data["device"]})
else:
query_dict.update({"device": module_data["device"]})
Expand All @@ -713,7 +715,9 @@ def _build_query_params(
query_dict.update({"prefix": module_data["parent"]})

elif parent == "ip_addresses":
if isinstance(module_data["device"], int):
if isinstance(module_data["device"], int) or self.is_valid_uuid(
module_data["device"]
):
query_dict.update({"device_id": module_data["device"]})
else:
query_dict.update({"device": module_data["device"]})
Expand Down Expand Up @@ -800,7 +804,9 @@ def _change_choices_id(self, endpoint, data):
required_choices = REQUIRED_ID_FIND[endpoint]
for choice in required_choices:
if data.get(choice):
if isinstance(data[choice], int):
if isinstance(data[choice], int) or self.is_valid_uuid(
data[choice]
):
continue
choice_value = self._fetch_choice_value(data[choice], endpoint)
data[choice] = choice_value
Expand Down Expand Up @@ -858,7 +864,9 @@ def _find_ids(self, data, user_query_params):
)
# If user passes in an integer, add to ID list to id_list as user
# should have passed in a tag ID
elif isinstance(list_item, int):
elif isinstance(list_item, int) or self.is_valid_uuid(
list_item
):
id_list.append(list_item)
continue
else:
Expand All @@ -880,7 +888,7 @@ def _find_ids(self, data, user_query_params):

if isinstance(v, list):
data[k] = id_list
elif isinstance(v, int):
elif isinstance(v, int) or self.is_valid_uuid(v):
pass
elif query_id:
data[k] = query_id.id
Expand All @@ -896,7 +904,7 @@ def _to_slug(self, value):
"""
if value is None:
return value
elif isinstance(value, int):
elif isinstance(value, int) or self.is_valid_uuid(value):
return value
else:
removed_chars = re.sub(r"[^\-\.\w\s]", "", value)
Expand Down
Loading

0 comments on commit 28366ac

Please sign in to comment.