Skip to content

Commit 090ae9b

Browse files
authored
Various 4.1 updates (#1316)
* Version check * Logic * Linting * Fix filters * Adjust for type/form_factor change in netbox_rack * Adjust for rack type * Fix sanity test * Update test for virtual machine * Update user test * Adjust user group * Adjust permission test * Update netbox_token tests * Update main.yml * Add changelog * Sanity * pep8
1 parent 5cc4340 commit 090ae9b

File tree

10 files changed

+115
-50
lines changed

10 files changed

+115
-50
lines changed

changelogs/fragments/41_updates.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
minor_changes:
2+
- Adjust tests for various modules
3+
- Fix the form_factor option on netbox_rack

plugins/module_utils/netbox_dcim.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def run(self):
125125

126126
data = self.data
127127

128+
# Handle rack and form_factor
129+
if endpoint_name == "rack":
130+
if Version(self.full_version) >= Version("4.1.0"):
131+
if "type" in data:
132+
data["form_factor"] = self._to_slug(data["type"])
133+
del data["type"]
134+
128135
# Used for msg output
129136
if data.get("name"):
130137
name = data["name"]

plugins/module_utils/netbox_utils.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,10 +1085,18 @@ def _build_query_params(
10851085

10861086
elif parent == "rear_port_template" and self.endpoint == "front_port_templates":
10871087
if isinstance(module_data.get("rear_port_template"), str):
1088-
rear_port_template = {
1089-
"devicetype_id": module_data.get("device_type"),
1090-
"name": module_data.get("rear_port_template"),
1091-
}
1088+
if self._version_check_greater(
1089+
self.version, "4.0", greater_or_equal=True
1090+
):
1091+
rear_port_template = {
1092+
"device_type_id": module_data.get("device_type"),
1093+
"name": module_data.get("rear_port_template"),
1094+
}
1095+
else:
1096+
rear_port_template = {
1097+
"devicetype_id": module_data.get("device_type"),
1098+
"name": module_data.get("rear_port_template"),
1099+
}
10921100
query_dict.update(rear_port_template)
10931101

10941102
elif parent == "power_port" and self.endpoint == "power_outlets":
@@ -1104,10 +1112,18 @@ def _build_query_params(
11041112
and self.endpoint == "power_outlet_templates"
11051113
):
11061114
if isinstance(module_data.get("power_port_template"), str):
1107-
power_port_template = {
1108-
"devicetype_id": module_data.get("device_type"),
1109-
"name": module_data.get("power_port_template"),
1110-
}
1115+
if self._version_check_greater(
1116+
self.version, "4.0", greater_or_equal=True
1117+
):
1118+
power_port_template = {
1119+
"device_type_id": module_data.get("device_type"),
1120+
"name": module_data.get("power_port_template"),
1121+
}
1122+
else:
1123+
power_port_template = {
1124+
"devicetype_id": module_data.get("device_type"),
1125+
"name": module_data.get("power_port_template"),
1126+
}
11111127
query_dict.update(power_port_template)
11121128
elif parent == "l2vpn_termination":
11131129
query_param_mapping = {
@@ -1131,7 +1147,12 @@ def _build_query_params(
11311147
)
11321148
elif "_template" in parent:
11331149
if query_dict.get("device_type"):
1134-
query_dict["devicetype_id"] = query_dict.pop("device_type")
1150+
if self._version_check_greater(
1151+
self.version, "4.0", greater_or_equal=True
1152+
):
1153+
query_dict["device_type_id"] = query_dict.pop("device_type")
1154+
else:
1155+
query_dict["devicetype_id"] = query_dict.pop("device_type")
11351156

11361157
if not query_dict:
11371158
provided_kwargs = child.keys() if child else module_data.keys()

tests/integration/targets/v4.1/tasks/main.yml

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
ansible.builtin.include_tasks: netbox_tenant_group.yml
3434

3535
- name: NETBOX_RACK TESTS
36-
ansible.builtin.include_tasks: netbox_rack.yml
36+
ansible.builtin.include_tasks:
37+
file: netbox_rack.yml
38+
apply:
39+
tags:
40+
- netbox_rack
41+
tags:
42+
- netbox_rack
3743

3844
- name: NETBOX_RACK_ROLE TESTS
3945
ansible.builtin.include_tasks: netbox_rack_role.yml
@@ -84,7 +90,13 @@
8490
ansible.builtin.include_tasks: netbox_inventory_item.yml
8591

8692
- name: NETBOX_VIRTUAL_MACHINE TESTS
87-
ansible.builtin.include_tasks: netbox_virtual_machine.yml
93+
ansible.builtin.include_tasks:
94+
file: netbox_virtual_machine.yml
95+
apply:
96+
tags:
97+
- netbox_virtual_machine
98+
tags:
99+
- netbox_virtual_machine
88100

89101
- name: NETBOX_CLUSTER TESTS
90102
ansible.builtin.include_tasks: netbox_cluster.yml
@@ -158,17 +170,35 @@
158170
- name: NETBOX_VIRTUAL_CHASSIS TESTS
159171
ansible.builtin.include_tasks: netbox_virtual_chassis.yml
160172

161-
- name: NETBOX_USER TESTS
162-
ansible.builtin.include_tasks: netbox_user.yml
173+
- name: NETBOX_USER_TESTS
174+
ansible.builtin.include_tasks:
175+
file: netbox_user.yml
176+
apply:
177+
tags:
178+
- netbox_user
179+
tags:
180+
- netbox_user
163181

164-
- name: NETBOX_USER_GROUP TESTS
165-
ansible.builtin.include_tasks: netbox_user_group.yml
182+
- name: NETBOX_USER_GROUP_TESTS
183+
ansible.builtin.include_tasks:
184+
file: netbox_user_group.yml
185+
apply:
186+
tags:
187+
- netbox_user_group
188+
tags:
189+
- netbox_user_group
166190

167191
- name: NETBOX_PERMISSION TESTS
168192
ansible.builtin.include_tasks: netbox_permission.yml
169193

170-
- name: NETBOX_TOKEN TESTS
171-
ansible.builtin.include_tasks: netbox_token.yml
194+
- name: NETBOX_TOKEN_TESTS
195+
ansible.builtin.include_tasks:
196+
file: netbox_token.yml
197+
apply:
198+
tags:
199+
- netbox_token
200+
tags:
201+
- netbox_token
172202

173203
# Module has to be updated for 3.3
174204
# - name: "NETBOX_CABLE TESTS"
@@ -337,7 +367,11 @@
337367
- netbox_config_template
338368

339369
- name: NETBOX_VIRTUAL_DISK
340-
ansible.builtin.include_tasks: netbox_virtual_disk.yml
370+
ansible.builtin.include_tasks:
371+
file: netbox_virtual_disk.yml
372+
apply:
373+
tags:
374+
- netbox_virtual_disk
341375
tags:
342376
- netbox_virtual_disk
343377

tests/integration/targets/v4.1/tasks/netbox_permission.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
netbox_token: "0123456789abcdef0123456789abcdef01234567"
124124
data:
125125
username: TestUser
126-
password: TestPassword
126+
password: TestPassword2
127127
permissions:
128128
- Test Permission 2
129129
state: present

tests/integration/targets/v4.1/tasks/netbox_rack.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
- test_five['diff']['after']['status'] == "available"
120120
- test_five['diff']['after']['tenant'] == 1
121121
- test_five['diff']['after']['tags'][0] == 4
122-
- test_five['diff']['after']['type'] == "2-post-frame"
122+
- test_five['diff']['after']['form_factor'] == "2-post-frame"
123123
- test_five['diff']['after']['u_height'] == 48
124124
- test_five['diff']['after']['width'] == 23
125125
- test_five['rack']['name'] == "Test rack one"
@@ -136,7 +136,7 @@
136136
- test_five['rack']['status'] == "available"
137137
- test_five['rack']['tenant'] == 1
138138
- test_five['rack']['tags'][0] == 4
139-
- test_five['rack']['type'] == "2-post-frame"
139+
- test_five['rack']['form_factor'] == "2-post-frame"
140140
- test_five['rack']['u_height'] == 48
141141
- test_five['rack']['width'] == 23
142142
- test_five['msg'] == "rack Test rack one updated"
@@ -185,7 +185,7 @@
185185
- test_six['rack']['status'] == "available"
186186
- test_six['rack']['tenant'] == 1
187187
- test_six['rack']['tags'][0] == 4
188-
- test_six['rack']['type'] == "2-post-frame"
188+
- test_six['rack']['form_factor'] == "2-post-frame"
189189
- test_six['rack']['u_height'] == 48
190190
- test_six['rack']['width'] == 23
191191

tests/integration/targets/v4.1/tasks/netbox_token.yml

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
netbox_token: "0123456789abcdef0123456789abcdef01234567"
1111
data:
1212
username: TestUser
13-
password: TestPassword
13+
password: TestPassword2
1414
state: present
1515

1616
- name: "TOKEN 1: Necessary info creation"
@@ -48,30 +48,27 @@
4848
- not test_two['changed']
4949
- test_two['msg'] == "token ******** already exists"
5050

51-
# TODO: https://github.com/netbox-community/netbox/issues/17279
52-
# TODO: A netbox bug is causing the key to change when updating tokens
53-
# TODO: just skip the update test for now
54-
# - name: "TOKEN 3: Update"
55-
# netbox.netbox.netbox_token:
56-
# netbox_url: http://localhost:32768
57-
# netbox_token: "0123456789abcdef0123456789abcdef01234567"
58-
# data:
59-
# user: TestUser
60-
# key: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
61-
# description: The test token
62-
# write_enabled: false
63-
# expires: 2024-08-26T14:49:01.345000+00:00
64-
# state: present
65-
# register: test_three
66-
#
67-
# - name: "TOKEN 3: ASSERT - Update"
68-
# ansible.builtin.assert:
69-
# that:
70-
# - test_three is changed
71-
# - test_three['token']['description'] == "The test token"
72-
# - test_three['token']['write_enabled'] == false
73-
# - test_three['token']['expires'] == "2024-08-26T14:49:01.345000+00:00"
74-
# - test_three['msg'] == "token ******** updated"
51+
- name: "TOKEN 3: Update"
52+
netbox.netbox.netbox_token:
53+
netbox_url: http://localhost:32768
54+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
55+
data:
56+
user: TestUser
57+
key: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
58+
description: The test token
59+
write_enabled: false
60+
expires: 2024-08-26T14:49:01.345000+00:00
61+
state: present
62+
register: test_three
63+
64+
- name: "TOKEN 3: ASSERT - Update"
65+
ansible.builtin.assert:
66+
that:
67+
- test_three is changed
68+
- test_three['token']['description'] == "The test token"
69+
- test_three['token']['write_enabled'] == false
70+
- test_three['token']['expires'] == "2024-08-26T14:49:01.345000+00:00"
71+
- test_three['msg'] == "token ******** updated"
7572

7673
- name: "TOKEN 4: Delete"
7774
netbox.netbox.netbox_token:

tests/integration/targets/v4.1/tasks/netbox_user.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
netbox_token: "0123456789abcdef0123456789abcdef01234567"
1111
data:
1212
username: TestUser
13-
password: TestPassword
13+
password: TestPassword1
1414
state: present
1515
register: test_one
1616

@@ -48,7 +48,7 @@
4848
netbox_token: "0123456789abcdef0123456789abcdef01234567"
4949
data:
5050
username: TestUser
51-
password: TestPassword
51+
password: TestPassword1
5252
email: test@user.com
5353
first_name: Test
5454
last_name: User

tests/integration/targets/v4.1/tasks/netbox_user_group.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
netbox_token: "0123456789abcdef0123456789abcdef01234567"
8282
data:
8383
username: TestUser
84-
password: TestPassword
84+
password: TestPassword2
8585
groups:
8686
- Test User Group
8787
state: present

tests/integration/targets/v4.1/tasks/netbox_virtual_machine.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
data:
5050
name: Test VM One
5151
cluster: Test Cluster
52+
serial: 12345678
5253
vcpus: 8.5
5354
memory: 8
5455
status: Planned
@@ -66,13 +67,15 @@
6667
- test_three['diff']['after']['memory'] == 8
6768
- test_three['diff']['after']['status'] == "planned"
6869
- test_three['diff']['after']['role'] == 2
70+
- test_three['diff']['after']['serial'] == "12345678"
6971
- test_three['diff']['after']['tags'][0] == 4
7072
- test_three['virtual_machine']['name'] == "Test VM One"
7173
- test_three['virtual_machine']['cluster'] == 1
7274
- test_three['virtual_machine']['vcpus'] == 8.5
7375
- test_three['virtual_machine']['memory'] == 8
7476
- test_three['virtual_machine']['status'] == "planned"
7577
- test_three['virtual_machine']['role'] == 2
78+
- test_three['virtual_machine']['serial'] == "12345678"
7679
- test_three['virtual_machine']['tags'][0] == 4
7780
- test_three['msg'] == "virtual_machine Test VM One updated"
7881

0 commit comments

Comments
 (0)