From 7e33fd0ac33c73220c774f17eeb6107aecb75212 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Wed, 11 Sep 2024 12:57:34 -0400 Subject: [PATCH] bugfix for datacenter contents lookup --- .../529-manual-test-play-for-lookups.yml | 3 +++ plugins/plugin_utils/lookup.py | 18 ++++++++---------- tests/manual/vmware_lookup/tasks/main.yml | 12 ++++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/changelogs/fragments/529-manual-test-play-for-lookups.yml b/changelogs/fragments/529-manual-test-play-for-lookups.yml index c5830b7a..ea99b717 100644 --- a/changelogs/fragments/529-manual-test-play-for-lookups.yml +++ b/changelogs/fragments/529-manual-test-play-for-lookups.yml @@ -1,3 +1,6 @@ --- trivial: - tests - added manual test playbook for lookup plugins + +bugfixes: + - lookup - fixed issue where searching for datacenter contents would throw an exception instead of returning expected results diff --git a/plugins/plugin_utils/lookup.py b/plugins/plugin_utils/lookup.py index cc458784..3a0185a3 100644 --- a/plugins/plugin_utils/lookup.py +++ b/plugins/plugin_utils/lookup.py @@ -129,15 +129,6 @@ async def search_for_object_moid_top_down(self): path_parts = [_part for _part in object_path.split("/") if _part] for index, path_part in enumerate(path_parts): - if not self.active_filters.get("datacenters"): - datacenter_moid = await self.get_object_moid_by_name_and_type( - path_part, "datacenter" - ) - if self.object_type == "datacenter" or not datacenter_moid: - return datacenter_moid - self.active_filters["datacenters"] = datacenter_moid - continue - if index == len(path_parts) - 1: # were at the end of the object path. Either return the object, or return # all of the objects it contains (for example, the children inside of a folder) @@ -153,7 +144,7 @@ async def search_for_object_moid_top_down(self): await self.process_intermediate_path_part(path_part) continue - raise Exception("here4") + raise AnsibleLookupError("No objects could be found due to an invalid search path") async def process_intermediate_path_part(self, intermediate_object_name): """ @@ -171,6 +162,13 @@ 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'): + result = await self.get_object_moid_by_name_and_type( + intermediate_object_name, "datacenter" + ) + self.active_filters["datacenters"] = result + return result + if self.object_type == "vm": result = await self.get_object_moid_by_name_and_type( intermediate_object_name, "resource_pool" diff --git a/tests/manual/vmware_lookup/tasks/main.yml b/tests/manual/vmware_lookup/tasks/main.yml index 8ff8ab08..4e3298f1 100644 --- a/tests/manual/vmware_lookup/tasks/main.yml +++ b/tests/manual/vmware_lookup/tasks/main.yml @@ -3,13 +3,17 @@ ansible.builtin.assert: that: lookup('vmware.vmware_rest.datacenter_moid', '/' + vcenter_datacenter, **connection_args) -- name: Lookup Cluster +- name: Lookup Clusters In Datacenter ansible.builtin.assert: - that: lookup('vmware.vmware_rest.cluster_moid', '/' + vcenter_datacenter + '/' + vcenter_cluster, **connection_args) + that: lookup('vmware.vmware_rest.cluster_moid', '/' + vcenter_datacenter + '/', **connection_args) + +- name: Lookup Datastores In Datacenter + ansible.builtin.assert: + that: lookup('vmware.vmware_rest.datastore_moid', '/' + vcenter_datacenter + '/', **connection_args) -- name: Lookup Cluster Contents +- name: Lookup Cluster ansible.builtin.assert: - that: lookup('vmware.vmware_rest.cluster_moid', '/' + vcenter_datacenter + '/' + vcenter_cluster + '/', **connection_args) + that: lookup('vmware.vmware_rest.cluster_moid', '/' + vcenter_datacenter + '/' + vcenter_cluster, **connection_args) - name: Lookup Datastores ansible.builtin.assert: