Skip to content

Commit

Permalink
Added check for UUIDs, added tests for new method (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
FragmentedPacket authored Mar 14, 2021
1 parent eab1a69 commit 74e4ef2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
37 changes: 24 additions & 13 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import json

from uuid import UUID
from itertools import chain

from ansible.module_utils.common.text.converters import to_text
Expand Down Expand Up @@ -617,27 +618,37 @@ def _remove_arg_spec_default(self, data):

return new_dict

def is_valid_uuid(self, match):
"""Determine if the match is already UUID."""
try:
uuid_obj = UUID(match)
except ValueError:
return False
return str(uuid_obj) == match

def _get_query_param_id(self, match, data):
"""Used to find IDs of necessary searches when required under _build_query_params
:returns id (int) or data (dict): Either returns the ID or original data passed in
:params match (str): The key within the user defined data that is required to have an ID
:params data (dict): User defined data passed into the module
"""
if isinstance(data.get(match), int):
return data[match]
else:
endpoint = CONVERT_TO_ID[match]
app = self._find_app(endpoint)
nb_app = getattr(self.nb, app)
nb_endpoint = getattr(nb_app, endpoint)

query_params = {QUERY_TYPES.get(match): data[match]}
result = self._nb_endpoint_get(nb_endpoint, query_params, match)
match_value = data.get(match)
if isinstance(match_value, int) or self.is_valid_uuid(match_value):
return match_value

if result:
return result.id
else:
return data
endpoint = CONVERT_TO_ID[match]
app = self._find_app(endpoint)
nb_app = getattr(self.nb, app)
nb_endpoint = getattr(nb_app, endpoint)

query_params = {QUERY_TYPES.get(match): data[match]}
result = self._nb_endpoint_get(nb_endpoint, query_params, match)

if result:
return result.id
else:
return data

def _build_query_params(
self, parent, module_data, user_query_params=None, child=None
Expand Down
46 changes: 45 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ antsibull = "^0.25.0"
importlib-metadata = "1.7.0"
pylint = "^2.6.0"
sphinx_rtd_theme = "*"
hypothesis = "^6.8.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/module_utils/test_nautobot_base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
import os
from hypothesis import given, settings, HealthCheck, strategies as st
from functools import partial
from unittest.mock import patch, MagicMock, Mock
from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -355,3 +356,18 @@ def test_version_check_greater_equal_to_true(mock_module, obj_mock, version):
def test_version_check_greater_equal_to_false(mock_module, obj_mock, version):
mock_module.nb_object = obj_mock
assert not mock_module._version_check_greater(version, "2.7", greater_or_equal=True)


@given(st.uuids(version=4))
@settings(suppress_health_check=[HealthCheck(9)])
def test_get_query_param_id_return_uuid(mock_module, value):
string_value = str(value)
data = mock_module._get_query_param_id("test", {"test": string_value})
assert data == string_value


@given(st.integers())
@settings(suppress_health_check=[HealthCheck(9)])
def test_get_query_param_id_return_int(mock_module, value):
data = mock_module._get_query_param_id("test", {"test": value})
assert data == value

0 comments on commit 74e4ef2

Please sign in to comment.