Skip to content

Commit 74a7e4c

Browse files
Bugfix: netbox_utils - If query_dict is None, fail and provide meaningful error message (#419)
1 parent d7e95cd commit 74a7e4c

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

plugins/module_utils/netbox_ipam.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ def run(self):
185185
else:
186186
first_available = False
187187

188-
object_query_params = self._build_query_params(
189-
endpoint_name, data, user_query_params
190-
)
191188
if data.get("prefix") and self.endpoint == "ip_addresses":
192189
object_query_params = self._build_query_params("prefix", data)
193190
self.nb_object = self._nb_endpoint_get(
194191
nb_app.prefixes, object_query_params, name
195192
)
196193
else:
194+
object_query_params = self._build_query_params(
195+
endpoint_name, data, user_query_params
196+
)
197197
self.nb_object = self._nb_endpoint_get(
198198
nb_endpoint, object_query_params, name
199199
)

plugins/module_utils/netbox_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,15 @@ def _build_query_params(
741741
if query_dict.get("device_type"):
742742
query_dict["devicetype_id"] = query_dict.pop("device_type")
743743

744+
if not query_dict:
745+
provided_kwargs = child.keys() if child else module_data.keys()
746+
acceptable_query_params = (
747+
user_query_params if user_query_params else query_params
748+
)
749+
self._handle_errors(
750+
f"One or more of the kwargs provided are invalid for {parent}, provided kwargs: {', '.join(sorted(provided_kwargs))}. Acceptable kwargs: {', '.join(sorted(acceptable_query_params))}"
751+
)
752+
744753
query_dict = self._convert_identical_keys(query_dict)
745754
return query_dict
746755

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,24 @@
195195
- query_params_372['diff']['after']['assigned_object'] == 3
196196
- query_params_372['diff']['after']['assigned_object_id'] == 3
197197
- query_params_372['diff']['after']['assigned_object_type'] == 'dcim.interface'
198+
199+
- name: "Validate failure due to invalid child params provided by user"
200+
netbox.netbox.netbox_cable:
201+
netbox_url: "http://localhost:32768"
202+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
203+
data:
204+
termination_a_type: "dcim.interface"
205+
termination_a:
206+
device: "test100"
207+
name: "GigabitEthernet1"
208+
termination_b_type: "circuits.circuittermination"
209+
termination_b:
210+
name: "XYZ987"
211+
ignore_errors: "yes"
212+
register: "test_results"
213+
214+
- name: "Issue #415 - Assert failure message shows the allowed params and what the user provided"
215+
assert:
216+
that:
217+
- test_results is failed
218+
- 'test_results["msg"] == "One or more of the kwargs provided are invalid for circuits.circuittermination, provided kwargs: name. Acceptable kwargs: circuit, term_side"'

tests/integration/targets/regression-v2.9/tasks/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,24 @@
195195
- query_params_372['diff']['after']['assigned_object'] == 3
196196
- query_params_372['diff']['after']['assigned_object_id'] == 3
197197
- query_params_372['diff']['after']['assigned_object_type'] == 'dcim.interface'
198+
199+
- name: "Validate failure due to invalid child params provided by user"
200+
netbox.netbox.netbox_cable:
201+
netbox_url: "http://localhost:32768"
202+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
203+
data:
204+
termination_a_type: "dcim.interface"
205+
termination_a:
206+
device: "test100"
207+
name: "GigabitEthernet1"
208+
termination_b_type: "circuits.circuittermination"
209+
termination_b:
210+
name: "XYZ987"
211+
ignore_errors: "yes"
212+
register: "test_results"
213+
214+
- name: "Issue #415 - Assert failure message shows the allowed params and what the user provided"
215+
assert:
216+
that:
217+
- test_results is failed
218+
- 'test_results["msg"] == "One or more of the kwargs provided are invalid for circuits.circuittermination, provided kwargs: name. Acceptable kwargs: circuit, term_side"'

0 commit comments

Comments
 (0)