Skip to content

Commit

Permalink
bugfix for datacenter contents lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Sep 11, 2024
1 parent 2f8b34d commit 7e33fd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/529-manual-test-play-for-lookups.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 8 additions & 10 deletions plugins/plugin_utils/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
"""
Expand All @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions tests/manual/vmware_lookup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7e33fd0

Please sign in to comment.