From b58b3ab5298eaf49558831e473a25ae5219dad2d Mon Sep 17 00:00:00 2001 From: pfallasro Date: Tue, 7 Jul 2026 18:32:05 +0200 Subject: [PATCH] fix: don't treat empty list response as a found resource in get_resource get_resource() stored the empty asset.*.List response wrapper (a dict containing an empty Results key) in api_response. Callers that guard on `if not api_response` then saw a truthy value and skipped their action; intersight_target_claim never sent the /asset/DeviceClaims POST, so unclaimed devices were never claimed (reported ok/changed:false). Only store a no-Results dict when the response has no 'Results' key at all (a GET by Moid). When a filtered list query returns an empty Results, clear api_response instead. Fixes #342 Signed-off-by: pfallasro --- changelogs/fragments/342-get-resource-empty-list.yml | 2 ++ plugins/module_utils/intersight.py | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/342-get-resource-empty-list.yml diff --git a/changelogs/fragments/342-get-resource-empty-list.yml b/changelogs/fragments/342-get-resource-empty-list.yml new file mode 100644 index 0000000..17cabbc --- /dev/null +++ b/changelogs/fragments/342-get-resource-empty-list.yml @@ -0,0 +1,2 @@ +bugfixes: + - module_utils - ``get_resource`` no longer stores an empty list-response wrapper as a found object. A filtered query with no matches now clears ``api_response`` instead of keeping the ``asset.*.List`` dict, so callers such as ``intersight_target_claim`` correctly detect an absent resource and perform the create/claim (https://github.com/CiscoDevNet/intersight-ansible/issues/342). diff --git a/plugins/module_utils/intersight.py b/plugins/module_utils/intersight.py index 6162993..2a8beed 100644 --- a/plugins/module_utils/intersight.py +++ b/plugins/module_utils/intersight.py @@ -407,8 +407,13 @@ def get_resource(self, resource_path, query_params, return_list=False): # return the 1st list element self.result['api_response'] = response['Results'][0] else: - # Return a dict if no Results array (handles case where resource_path contains a Moid) - if isinstance(response, dict): + # A GET by Moid returns the object directly (a dict with no + # 'Results' key). A filtered list query with no matches returns a + # dict that DOES contain an (empty) 'Results' key; that is not a + # hit and must not be stored, otherwise callers that guard on + # `if not api_response` (e.g. intersight_target_claim) treat the + # empty result as an existing object and skip their action. + if isinstance(response, dict) and 'Results' not in response: self.result['api_response'] = response else: # Clear api_response when no results found to prevent returning stale data