Skip to content

Commit 4a132f8

Browse files
Updated normalize_data to try and convert strings to integers, added unit/integration tests (#233)
1 parent 35fc727 commit 4a132f8

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,6 @@ def _find_ids(self, data, user_query_params):
635635
query_params = {QUERY_TYPES.get(k, "q"): search}
636636
query_id = self._nb_endpoint_get(nb_endpoint, query_params, k)
637637

638-
# Code to work around Ansible templating converting all values to strings
639-
# This should allow users to pass in IDs obtained from previous tasks
640-
# without causing the module to fail
641-
if isinstance(v, str):
642-
try:
643-
v = int(v)
644-
except ValueError:
645-
pass
646-
647638
if isinstance(v, list):
648639
data[k] = id_list
649640
elif isinstance(v, int):
@@ -669,6 +660,27 @@ def _to_slug(self, value):
669660
convert_chars = re.sub(r"[\-\.\s]+", "-", removed_chars)
670661
return convert_chars.strip().lower()
671662

663+
def _normalize_to_integer(self, key, value):
664+
"""
665+
:returns value (str/int): Returns either the original value or the
666+
converted value (int) if able to make the conversion.
667+
668+
:params (str/int): Value that needs to be tested whether or not it
669+
needs to be converted to an integer.
670+
"""
671+
DO_NOT_CONVERT_TO_INT = {"asset_tag"}
672+
if key in DO_NOT_CONVERT_TO_INT:
673+
return value
674+
elif isinstance(value, int):
675+
return value
676+
677+
try:
678+
value = int(value)
679+
except ValueError:
680+
return value
681+
except TypeError:
682+
return value
683+
672684
def _normalize_data(self, data):
673685
"""
674686
:returns data (dict): Normalized module data to formats accepted by Netbox searches
@@ -682,13 +694,15 @@ def _normalize_data(self, data):
682694
sub_data_type = QUERY_TYPES.get(subk, "q")
683695
if sub_data_type == "slug":
684696
data[k][subk] = self._to_slug(subv)
697+
data[k][subk] = self._normalize_to_integer(subk, data[k].get(subk))
685698
else:
686699
data_type = QUERY_TYPES.get(k, "q")
687700
if data_type == "slug":
688701
data[k] = self._to_slug(v)
689702
elif data_type == "timezone":
690703
if " " in v:
691704
data[k] = v.replace(" ", "_")
705+
data[k] = self._normalize_to_integer(k, data.get(k))
692706

693707
return data
694708

tests/integration/targets/latest/tasks/netbox_ip_address.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@
160160
nat_inside:
161161
address: 172.16.1.20
162162
vrf: Test VRF
163-
interface:
164-
name: GigabitEthernet1
165-
device: test100
163+
interface: 3
166164
register: test_eight
167165

168166
- name: "8 - ASSERT"

tests/unit/module_utils/test_data/normalize_data/data.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,37 @@
286286
"after": {
287287
"vrf": "Test VRF"
288288
}
289+
},
290+
{
291+
"before": {
292+
"interface": 1
293+
},
294+
"after": {
295+
"interface": 1
296+
}
297+
},
298+
{
299+
"before": {
300+
"interface": {
301+
"vrf": "Test VRF"
302+
}
303+
},
304+
"after": {
305+
"interface": {
306+
"vrf": "Test VRF"
307+
}
308+
}
309+
},
310+
{
311+
"before": {
312+
"vlan": {
313+
"vlan_group": "Test VLAN Group"
314+
}
315+
},
316+
"after": {
317+
"vlan": {
318+
"vlan_group": "test-vlan-group"
319+
}
320+
}
289321
}
290322
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"data": [
4+
"site",
5+
"test-site"
6+
],
7+
"expected": "test-site"
8+
},
9+
{
10+
"data": [
11+
"interface",
12+
1
13+
],
14+
"expected": 1
15+
},
16+
{
17+
"data": [
18+
"interface",
19+
{
20+
"name": "ethernet1",
21+
"device": "test100"
22+
}
23+
],
24+
"expected": {
25+
"name": "ethernet1",
26+
"device": "test100"
27+
}
28+
}
29+
]

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ def test_normalize_data_returns_correct_data(mock_netbox_module, before, after):
162162
assert norm_data == after
163163

164164

165+
@pytest.mark.parametrize("data, expected", load_relative_test_data("normalize_integer"))
166+
def test_normalize_to_integer_returns_correct_data(mock_netbox_module, data, expected):
167+
value = mock_netbox_module._normalize_to_integer(*data)
168+
169+
assert value == expected
170+
171+
165172
@pytest.mark.parametrize("data, expected", load_relative_test_data("arg_spec_default"))
166173
def test_remove_arg_spec_defaults(mock_netbox_module, data, expected):
167174
new_data = mock_netbox_module._remove_arg_spec_default(data)

0 commit comments

Comments
 (0)