Skip to content

Commit

Permalink
fix datacenter loop issue with lookup plugins (ansible-collections#547)
Browse files Browse the repository at this point in the history
SUMMARY
This fixes an issue where the datacenter search filter is never actually set. In some cases this caused the lookup plugin to short circuit and return incorrect results, or return results that were outside of the search scope
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
All lookup plugins
ADDITIONAL INFORMATION
Before
    "lookup('vmware.vmware_rest.resource_pool_moid', '/' + vcenter_datacenter + '/' + vcenter_cluster + '/')": [
        "resgroup-3026",
        "resgroup-35",
        "resgroup-5060",
        "resgroup-55921",
        "resgroup-63368"
    ]
}

After
    "lookup('vmware.vmware_rest.resource_pool_moid', '/' + vcenter_datacenter + '/' + vcenter_cluster + '/')": [
        "resgroup-63368"
    ]
}

Reviewed-by: Danielle Barda
Reviewed-by: Anna Savina
  • Loading branch information
mikemorency authored Nov 13, 2024
1 parent 16768a8 commit b32b44f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/547-fix-datacenter-loop-in-lookups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- lookup plugins - Fixed issue where datacenter search filter was never properly set
12 changes: 9 additions & 3 deletions plugins/plugin_utils/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def __init__(self, options, session):
self.api = VcenterApi(options["vcenter_hostname"], session)
self.active_filters = {}
self.object_type = options["object_type"]
# this is an internal flag that indicates if we tried to find the datacenter or not
# if its true, we stop trying and save some api calls.
# see process_intermediate_path_part
self._searched_for_datacenter = False

@classmethod
async def entry_point(cls, terms, options):
Expand Down Expand Up @@ -164,12 +168,14 @@ async def process_intermediate_path_part(self, intermediate_object_name):
Returns:
str or None, a single MoID or none if nothing was found
"""
if not self.active_filters.get("datacenter"):
if not self._searched_for_datacenter:
self._searched_for_datacenter = True
result = await self.get_object_moid_by_name_and_type(
intermediate_object_name, "datacenter"
)
self.active_filters["datacenters"] = result
return result
if result:
self.active_filters["datacenters"] = result
return result

if self.object_type == "vm":
result = await self.get_object_moid_by_name_and_type(
Expand Down

0 comments on commit b32b44f

Please sign in to comment.