Skip to content

Commit 142bafe

Browse files
Bugfix: Change tag diff to sets to assert uniqueness (#247)
1 parent 8cbbfde commit 142bafe

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- cd ..
3131
- pip install -U pip "setuptools>=46.1.3"
3232
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
33-
- pip install pynetbox cryptography codecov jmespath jsondiff ansible
33+
- pip install pynetbox==4.3.3 cryptography codecov jmespath jsondiff ansible
3434

3535
# Stick to python 3.7 instead of 3.8, as ansible-test sanity is not compatible with 3.7
3636
# https://github.com/ansible/ansible/issues/67118
@@ -49,7 +49,7 @@ jobs:
4949
- cd ..
5050
- pip install -U pip "setuptools>=46.1.3"
5151
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
52-
- pip install pynetbox cryptography codecov jmespath jsondiff ansible
52+
- pip install pynetbox==4.3.3 cryptography codecov jmespath jsondiff ansible
5353

5454
# Latest development versions of Netbox and Ansible, newest Python
5555
# This may be broken sometimes by changes in the netbox & ansible projects
@@ -67,7 +67,7 @@ jobs:
6767
- cd ..
6868
- pip install -U pip "setuptools>=46.1.3"
6969
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
70-
- pip install pynetbox cryptography jmespath jsondiff
70+
- pip install pynetbox==4.3.3 cryptography jmespath jsondiff
7171
- git clone https://github.com/ansible/ansible.git
7272
- cd ansible
7373
- source hacking/env-setup

plugins/module_utils/netbox_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,12 @@ def _update_netbox_object(self, data):
805805
"""
806806
serialized_nb_obj = self.nb_object.serialize()
807807
updated_obj = serialized_nb_obj.copy()
808-
updated_obj.update(data)
808+
if serialized_nb_obj.get("tags") and data.get("tags"):
809+
serialized_nb_obj["tags"] = set(serialized_nb_obj["tags"])
810+
updated_obj["tags"] = set(data["tags"])
811+
else:
812+
updated_obj.update(data)
813+
809814
if serialized_nb_obj == updated_obj:
810815
return serialized_nb_obj, None
811816
else:

tests/integration/targets/latest/tasks/netbox_power_outlet.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']['name'] == "Power Outlet"
27-
- test_one['power_outlet']['device'] == 9
27+
- test_one['power_outlet']['device'] == 10
2828
- test_one['msg'] == "power_outlet Power Outlet created"
2929

3030
- name: "POWER_OUTLET 2: Create duplicate"
@@ -42,7 +42,7 @@
4242
that:
4343
- not test_two['changed']
4444
- test_two['power_outlet']['name'] == "Power Outlet"
45-
- test_two['power_outlet']['device'] == 9
45+
- test_two['power_outlet']['device'] == 10
4646
- test_two['msg'] == "power_outlet Power Outlet already exists"
4747

4848
- name: "POWER_OUTLET 3: Update power_outlet with other fields"
@@ -68,7 +68,7 @@
6868
- test_three['diff']['after']['feed_leg'] == "B"
6969
- test_three['diff']['after']['description'] == "test description"
7070
- test_three['power_outlet']['name'] == "Power Outlet"
71-
- test_three['power_outlet']['device'] == 9
71+
- test_three['power_outlet']['device'] == 10
7272
- test_three['power_outlet']['type'] == "ita-e"
7373
- test_three['power_outlet']['power_port'] == 1
7474
- test_three['power_outlet']['feed_leg'] == "B"
@@ -92,7 +92,7 @@
9292
- test_four['diff']['before']['state'] == "absent"
9393
- test_four['diff']['after']['state'] == "present"
9494
- test_four['power_outlet']['name'] == "Power Outlet 2"
95-
- test_four['power_outlet']['device'] == 9
95+
- test_four['power_outlet']['device'] == 10
9696
- test_four['msg'] == "power_outlet Power Outlet 2 created"
9797

9898
- name: "POWER_OUTLET 5: Delete Power Outlet"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- test_one['diff']['before']['state'] == "absent"
3636
- test_one['diff']['after']['state'] == "present"
3737
- test_one['power_port']['name'] == "Power Port"
38-
- test_one['power_port']['device'] == 9
38+
- test_one['power_port']['device'] == 10
3939
- test_one['msg'] == "power_port Power Port created"
4040

4141
- name: "POWER_PORT 2: Create duplicate"
@@ -53,7 +53,7 @@
5353
that:
5454
- not test_two['changed']
5555
- test_two['power_port']['name'] == "Power Port"
56-
- test_two['power_port']['device'] == 9
56+
- test_two['power_port']['device'] == 10
5757
- test_two['msg'] == "power_port Power Port already exists"
5858

5959
- name: "POWER_FEED 3: Update power_port with other fields"
@@ -79,7 +79,7 @@
7979
- test_three['diff']['after']['maximum_draw'] == 20
8080
- test_three['diff']['after']['description'] == "test description"
8181
- test_three['power_port']['name'] == "Power Port"
82-
- test_three['power_port']['device'] == 9
82+
- test_three['power_port']['device'] == 10
8383
- test_three['power_port']['type'] == "ita-e"
8484
- test_three['power_port']['allocated_draw'] == 10
8585
- test_three['power_port']['maximum_draw'] == 20
@@ -103,7 +103,7 @@
103103
- test_four['diff']['before']['state'] == "absent"
104104
- test_four['diff']['after']['state'] == "present"
105105
- test_four['power_port']['name'] == "Power Port 2"
106-
- test_four['power_port']['device'] == 9
106+
- test_four['power_port']['device'] == 10
107107
- test_four['msg'] == "power_port Power Port 2 created"
108108

109109
- name: "POWER_PORT 5: Delete Power Port"

tests/integration/targets/regression/tasks/main.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,62 @@
4444
that:
4545
- test_two is changed
4646
- test_two['msg'] == "New prefix created within 10.10.0.0/16"
47+
48+
- name: "3 - Add device with tags - Setup device to test #242"
49+
netbox.netbox.netbox_device:
50+
netbox_url: "http://localhost:32768"
51+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
52+
data:
53+
name: "issue-242"
54+
device_type: "Cisco Test"
55+
device_role: "Core Switch"
56+
site: "Test Site"
57+
status: "Staged"
58+
tags:
59+
- first
60+
- second
61+
62+
- name: "4 - Add device with tags out of order - shouldn't change - Tests #242 is fixed"
63+
netbox.netbox.netbox_device:
64+
netbox_url: "http://localhost:32768"
65+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
66+
data:
67+
name: "issue-242"
68+
device_type: "Cisco Test"
69+
device_role: "Core Switch"
70+
site: "Test Site"
71+
status: "Staged"
72+
tags:
73+
- second
74+
- first
75+
register: test_four
76+
diff: yes
77+
78+
- name: "4 - Assert not changed - Tests #242 is fixed"
79+
assert:
80+
that:
81+
- not test_four["changed"]
82+
83+
- name: "5 - Add device with extra tag - Tests #242 is fixed"
84+
netbox.netbox.netbox_device:
85+
netbox_url: "http://localhost:32768"
86+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
87+
data:
88+
name: "issue-242"
89+
device_type: "Cisco Test"
90+
device_role: "Core Switch"
91+
site: "Test Site"
92+
status: "Staged"
93+
tags:
94+
- second
95+
- third
96+
- first
97+
register: test_five
98+
diff: yes
99+
100+
- name: "5 - Assert added tag - Tests #242 is fixed"
101+
assert:
102+
that:
103+
- test_five is changed
104+
- test_five["diff"]["after"]["tags"] is defined
105+
- test_five["device"]["tags"] is defined

0 commit comments

Comments
 (0)