Skip to content

Commit

Permalink
Bugfix/lookup plugin regression (ansible-collections#552)
Browse files Browse the repository at this point in the history
SUMMARY
After refactoring one of the methods, lookup results broke. This was partially due to missing async keywords and improper dictionary cloning when creating a temporary set of search filters
This issue also made it clear that the filters do not need (nor should be) reset each time we search for an object. The method that performed that reset has been removed
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
all lookup plugins

Reviewed-by: Anna Savina
  • Loading branch information
mikemorency authored Nov 21, 2024
1 parent f845b55 commit 1efb395
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions plugins/plugin_utils/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,33 +182,35 @@ async def add_intermediate_path_part_to_filter_spec(self, intermediate_object_na
# the datacenter. If its not, continue the search as normal and dont search for the datacenter
# again
if not self._searched_for_datacenter:
if self.__add_datacenter_to_filter_spec_if_exists(intermediate_object_name):
if await self.__add_datacenter_to_filter_spec_if_exists(
intermediate_object_name
):
return

# Resource pools can only be in the vm filter spec
if self.object_type == "vm":
if self.__add_object_to_filter_spec_if_exists(
if await self.__add_object_to_filter_spec_if_exists(
intermediate_object_name, "resource_pool", "resource_pools"
):
return

# Clusters can be used in the vm, host, or resource pool filter specs
if self.object_type in ("vm", "host", "resource_pool"):
if self.__add_object_to_filter_spec_if_exists(
if await self.__add_object_to_filter_spec_if_exists(
intermediate_object_name, "cluster", "clusters"
):
return

# Hosts can be in the filter spec for vms, networks, datastores, or resource pools
if self.object_type in ("vm", "network", "datastore", "resource_pool"):
if self.__add_object_to_filter_spec_if_exists(
if await self.__add_object_to_filter_spec_if_exists(
intermediate_object_name, "host", "hosts"
):
return

# Folders can be used in the filter spec for everything except resource pools
if self.object_type != "resource_pool":
if self.__add_object_to_filter_spec_if_exists(
if await self.__add_object_to_filter_spec_if_exists(
intermediate_object_name, "folder", "parent_folders"
):
return
Expand Down Expand Up @@ -247,7 +249,7 @@ async def __add_object_to_filter_spec_if_exists(
"""
result = await self.get_object_moid_by_name_and_type(object_name, object_type)
if result:
self.set_new_filters_with_datacenter({filter_key: result})
self.active_filters[filter_key] = result
return result

async def get_object_moid_by_name_and_type(self, object_name, _object_type=None):
Expand All @@ -266,7 +268,7 @@ async def get_object_moid_by_name_and_type(self, object_name, _object_type=None)
if _object_type == "datacenter":
_filters = {"folders": "group-d1"}
else:
_filters = self.active_filters
_filters = self.active_filters.copy()

_filters["names"] = object_name
_result = await self.api.fetch_object_with_filters(_object_type, _filters)
Expand Down Expand Up @@ -322,15 +324,3 @@ async def get_all_children_in_object(self):
return [result[self.object_type] for result in results]
except KeyError:
return None

def set_new_filters_with_datacenter(self, new_filters):
"""
Deletes filter key value pairs from the active filter dict and replaces them with the new filters.
It will leave the datacenter filter since that is always used.
Params:
new_filters: dict, The new filters you want to apply as active
"""
_dc = self.active_filters.get("datacenters")
self.active_filters = new_filters
if _dc:
self.active_filters["datacenters"] = _dc

0 comments on commit 1efb395

Please sign in to comment.