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