Skip to content

Commit bccfd3c

Browse files
Enhancement: Allow id for api_filter that uses .get instead of .filter (#376)
1 parent 72f8221 commit bccfd3c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

plugins/lookup/nb_lookup.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,20 @@ def run(self, terms, variables=None, **kwargs):
330330
if netbox_api_filter:
331331
filter = build_filters(netbox_api_filter)
332332

333+
if "id" in filter:
334+
Display().vvvv(
335+
u"Filter is: %s and includes id, will use .get instead of .filter"
336+
% (filter)
337+
)
338+
try:
339+
id = int(filter["id"][0])
340+
nb_data = endpoint.get(id)
341+
data = dict(nb_data)
342+
Display().vvvvv(pformat(data))
343+
return [data]
344+
except pynetbox.RequestError as e:
345+
raise AnsibleError(e.error)
346+
333347
Display().vvvv("filter is %s" % filter)
334348

335349
# Make call to NetBox API and capture any failures
@@ -338,13 +352,14 @@ def run(self, terms, variables=None, **kwargs):
338352
)
339353

340354
for data in nb_data:
341-
Display().vvvvv(pformat(dict(data)))
355+
data = dict(data)
356+
Display().vvvvv(pformat(data))
342357

343358
if netbox_raw_return:
344-
results.append(dict(data))
359+
results.append(data)
345360
else:
346-
key = dict(data)["id"]
347-
result = {key: dict(data)}
361+
key = data["id"]
362+
result = {key: data}
348363
results.extend(self._flatten_hash_to_list(result))
349364

350365
return results

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@
7575
- "'L2' in {{ query_result |json_query('[*].display_name') }}"
7676
vars:
7777
query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='role=core-switch site=test-site site=test-site2', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567', raw_data=True) }}"
78+
79+
- name: "NETBOX_LOOKUP 10: Device query by ID"
80+
assert:
81+
that:
82+
- "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}"

tests/integration/targets/v2.8/tasks/netbox_lookup.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@
7575
- "'L2' in {{ query_result |json_query('[*].display_name') }}"
7676
vars:
7777
query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='role=core-switch site=test-site site=test-site2', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567', raw_data=True) }}"
78+
79+
- name: "NETBOX_LOOKUP 10: Device query by ID"
80+
assert:
81+
that:
82+
- "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}"

0 commit comments

Comments
 (0)