Skip to content

Commit d20a949

Browse files
Bugfix: Fix passing in integers and custom_fields/local_context_data from being changed to null (#270)
1 parent 2521d5d commit d20a949

File tree

9 files changed

+41
-83
lines changed

9 files changed

+41
-83
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -780,27 +780,6 @@ def _to_slug(self, value):
780780
convert_chars = re.sub(r"[\-\.\s]+", "-", removed_chars)
781781
return convert_chars.strip().lower()
782782

783-
def _normalize_to_integer(self, key, value):
784-
"""
785-
:returns value (str/int): Returns either the original value or the
786-
converted value (int) if able to make the conversion.
787-
788-
:params (str/int): Value that needs to be tested whether or not it
789-
needs to be converted to an integer.
790-
"""
791-
DO_NOT_CONVERT_TO_INT = {"asset_tag"}
792-
if key in DO_NOT_CONVERT_TO_INT:
793-
return value
794-
elif isinstance(value, int):
795-
return value
796-
797-
try:
798-
value = int(value)
799-
except ValueError:
800-
return value
801-
except TypeError:
802-
return value
803-
804783
def _normalize_data(self, data):
805784
"""
806785
:returns data (dict): Normalized module data to formats accepted by Netbox searches
@@ -809,22 +788,24 @@ def _normalize_data(self, data):
809788
:params data (dict): Original data from Netbox module
810789
"""
811790
for k, v in data.items():
812-
if k == "local_context_data":
813-
pass
814-
elif isinstance(v, dict):
815-
for subk, subv in v.items():
816-
sub_data_type = QUERY_TYPES.get(subk, "q")
817-
if sub_data_type == "slug":
818-
data[k][subk] = self._to_slug(subv)
819-
data[k][subk] = self._normalize_to_integer(subk, data[k].get(subk))
791+
if isinstance(v, dict):
792+
if v.get("id"):
793+
try:
794+
data[k] = int(v["id"])
795+
except (ValueError, TypeError):
796+
pass
797+
else:
798+
for subk, subv in v.items():
799+
sub_data_type = QUERY_TYPES.get(subk, "q")
800+
if sub_data_type == "slug":
801+
data[k][subk] = self._to_slug(subv)
820802
else:
821803
data_type = QUERY_TYPES.get(k, "q")
822804
if data_type == "slug":
823805
data[k] = self._to_slug(v)
824806
elif data_type == "timezone":
825807
if " " in v:
826808
data[k] = v.replace(" ", "_")
827-
data[k] = self._normalize_to_integer(k, data.get(k))
828809
if k == "description":
829810
data[k] = v.strip()
830811

tests/integration/netbox-deploy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"manufacturer": cisco_manu.id,
136136
"subdevice_role": False,
137137
},
138+
{"model": "1841", "slug": "1841", "manufacturer": cisco_manu.id,},
138139
]
139140
created_device_types = nb.dcim.device_types.create(device_types)
140141
### Device type variables to be used later on

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
netbox_token: "0123456789abcdef0123456789abcdef01234567"
1111
data:
1212
name: "R1"
13-
device_type: "Cisco Test"
13+
device_type:
14+
id: "1"
1415
device_role: "Core Switch"
1516
site: "Test Site"
1617
status: "Staged"
@@ -122,7 +123,7 @@
122123
netbox_token: "0123456789abcdef0123456789abcdef01234567"
123124
data:
124125
name: "TestR1"
125-
device_type: "Arista Test"
126+
device_type: "1841"
126127
device_role: "Core Switch"
127128
site: "Test Site2"
128129
rack: "Test Rack"
@@ -143,7 +144,7 @@
143144
- test_four['diff']['after']['state'] == "present"
144145
- test_four['device']['name'] == "TestR1"
145146
- test_four['device']['device_role'] == 1
146-
- test_four['device']['device_type'] == 2
147+
- test_four['device']['device_type'] == 5
147148
- test_four['device']['site'] == 2
148149
- test_four['device']['status'] == "active"
149150
- test_four['device']['rack'] == 1
@@ -218,7 +219,8 @@
218219
netbox_token: "0123456789abcdef0123456789abcdef01234567"
219220
data:
220221
name: ""
221-
device_type: "Cisco Test"
222+
device_type:
223+
id: 1
222224
device_role: "Core Switch"
223225
site: "Test Site"
224226
status: "Staged"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- test_one['diff']['before']['state'] == "absent"
2222
- test_one['diff']['after']['state'] == "present"
2323
- test_one['device_bay_template']['name'] == "Device Bay Template One"
24-
- test_one['device_bay_template']['device_type'] == 6
24+
- test_one['device_bay_template']['device_type'] == 7
2525
- test_one['msg'] == "device_bay_template Device Bay Template One created"
2626

2727
- name: "DEVICE_BAY_TEMPLATE 2: Create duplicate"
@@ -39,7 +39,7 @@
3939
that:
4040
- not test_two['changed']
4141
- test_two['device_bay_template']['name'] == "Device Bay Template One"
42-
- test_two['device_bay_template']['device_type'] == 6
42+
- test_two['device_bay_template']['device_type'] == 7
4343
- test_two['msg'] == "device_bay_template Device Bay Template One already exists"
4444

4545
- name: "DEVICE_BAY_TEMPLATE 3: ASSERT - Create Device Bay Template for Delete Test"
@@ -59,7 +59,7 @@
5959
- test_three['diff']['before']['state'] == "absent"
6060
- test_three['diff']['after']['state'] == "present"
6161
- test_three['device_bay_template']['name'] == "Device Bay Template Two"
62-
- test_three['device_bay_template']['device_type'] == 6
62+
- test_three['device_bay_template']['device_type'] == 7
6363
- test_three['msg'] == "device_bay_template Device Bay Template Two created"
6464

6565
- name: "DEVICE_BAY_TEMPLATE 4: ASSERT - Delete"
@@ -77,5 +77,5 @@
7777
that:
7878
- test_four is changed
7979
- test_four['device_bay_template']['name'] == "Device Bay Template Two"
80-
- test_four['device_bay_template']['device_type'] == 6
80+
- test_four['device_bay_template']['device_type'] == 7
8181
- test_four['msg'] == "device_bay_template Device Bay Template Two deleted"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
- test_one['diff']['before']['state'] == "absent"
2525
- test_one['diff']['after']['state'] == "present"
2626
- test_one['power_outlet_template']['name'] == "Power Outlet Template"
27-
- test_one['power_outlet_template']['device_type'] == 7
27+
- test_one['power_outlet_template']['device_type'] == 8
2828
- test_one['msg'] == "power_outlet_template Power Outlet Template created"
2929

3030
- name: "POWER_OUTLET_TEMPLATE 2: Create duplicate"
@@ -42,7 +42,7 @@
4242
that:
4343
- not test_two['changed']
4444
- test_two['power_outlet_template']['name'] == "Power Outlet Template"
45-
- test_two['power_outlet_template']['device_type'] == 7
45+
- test_two['power_outlet_template']['device_type'] == 8
4646
- test_two['msg'] == "power_outlet_template Power Outlet Template already exists"
4747

4848
- name: "POWER_OUTLET_TEMPLATE 3: Update power_outlet_template with other fields"
@@ -66,7 +66,7 @@
6666
- test_three['diff']['after']['power_port'] == 1
6767
- test_three['diff']['after']['feed_leg'] == "B"
6868
- test_three['power_outlet_template']['name'] == "Power Outlet Template"
69-
- test_three['power_outlet_template']['device_type'] == 7
69+
- test_three['power_outlet_template']['device_type'] == 8
7070
- test_three['power_outlet_template']['type'] == "ita-e"
7171
- test_three['power_outlet_template']['power_port'] == 1
7272
- test_three['power_outlet_template']['feed_leg'] == "B"
@@ -89,7 +89,7 @@
8989
- test_four['diff']['before']['state'] == "absent"
9090
- test_four['diff']['after']['state'] == "present"
9191
- test_four['power_outlet_template']['name'] == "Power Outlet Template 2"
92-
- test_four['power_outlet_template']['device_type'] == 7
92+
- test_four['power_outlet_template']['device_type'] == 8
9393
- test_four['msg'] == "power_outlet_template Power Outlet Template 2 created"
9494

9595
- name: "POWER_OUTLET_TEMPLATE 5: Delete Power Outlet Template"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
- test_one['diff']['before']['state'] == "absent"
3434
- test_one['diff']['after']['state'] == "present"
3535
- test_one['power_port_template']['name'] == "Power Port Template"
36-
- test_one['power_port_template']['device_type'] == 7
36+
- test_one['power_port_template']['device_type'] == 8
3737
- test_one['msg'] == "power_port_template Power Port Template created"
3838

3939
- name: "POWER_PORT_TEMPLATE 2: Create duplicate"
@@ -51,7 +51,7 @@
5151
that:
5252
- not test_two['changed']
5353
- test_two['power_port_template']['name'] == "Power Port Template"
54-
- test_two['power_port_template']['device_type'] == 7
54+
- test_two['power_port_template']['device_type'] == 8
5555
- test_two['msg'] == "power_port_template Power Port Template already exists"
5656

5757
- name: "POWER_PORT_TEMPLATE 3: Update power_port_template with other fields"
@@ -75,7 +75,7 @@
7575
- test_three['diff']['after']['allocated_draw'] == 10
7676
- test_three['diff']['after']['maximum_draw'] == 20
7777
- test_three['power_port_template']['name'] == "Power Port Template"
78-
- test_three['power_port_template']['device_type'] == 7
78+
- test_three['power_port_template']['device_type'] == 8
7979
- test_three['power_port_template']['type'] == "ita-e"
8080
- test_three['power_port_template']['allocated_draw'] == 10
8181
- test_three['power_port_template']['maximum_draw'] == 20
@@ -98,7 +98,7 @@
9898
- test_four['diff']['before']['state'] == "absent"
9999
- test_four['diff']['after']['state'] == "present"
100100
- test_four['power_port_template']['name'] == "Power Port Template 2"
101-
- test_four['power_port_template']['device_type'] == 7
101+
- test_four['power_port_template']['device_type'] == 8
102102
- test_four['msg'] == "power_port_template Power Port Template 2 created"
103103

104104
- name: "POWER_PORT_TEMPLATE 5: Delete Power Port Template"

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@
295295
"interface": 1
296296
}
297297
},
298+
{
299+
"before": {
300+
"interface": {
301+
"id": "123"
302+
}
303+
},
304+
"after": {
305+
"interface": 123
306+
}
307+
},
298308
{
299309
"before": {
300310
"interface": {
@@ -335,4 +345,4 @@
335345
"mac_address": "AA:BB:CC:DD:EE:FF"
336346
}
337347
}
338-
]
348+
]

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@ 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-
172165
@pytest.mark.parametrize("data, expected", load_relative_test_data("arg_spec_default"))
173166
def test_remove_arg_spec_defaults(mock_netbox_module, data, expected):
174167
new_data = mock_netbox_module._remove_arg_spec_default(data)

0 commit comments

Comments
 (0)