Skip to content

Commit

Permalink
add support for resource pools
Browse files Browse the repository at this point in the history
  • Loading branch information
nebb00 committed Jul 26, 2024
1 parent 1ed1069 commit 2a5a47c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 18 additions & 5 deletions plugins/module_utils/vcenter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ def establish_vcenter_connection(module, vCenter_host, username, password, ignor
module.fail_json(msg="Caught vmodl fault while connecting to vCenter: " + error.msg)
return service_instance.RetrieveContent()


def get_resource_id_from_name(module, vCenter_host, username, password,
resource_type, resource_name, ignore_ssl_verification):
"""
params:
- resource_type: Type of vCenter resource. Accepted values 'host', 'cluster', 'storage', 'network', and 'folder'.
- resource_type: Type of vCenter resource. Accepted values 'host', 'cluster', 'storage', 'network', 'folder', and 'compute'.
- resource_name: Name of the resource.
result:
- moref id of the resource name and type given.
Expand All @@ -104,11 +103,17 @@ def get_resource_id_from_name(module, vCenter_host, username, password,
elif resource_type == 'folder':
objview = content.viewManager.CreateContainerView(content.rootFolder,
[vim.Folder], True)
elif resource_type == 'compute':
# For 'compute', we need to look for both clusters and resource pools
objview = content.viewManager.CreateContainerView(content.rootFolder,
[vim.ClusterComputeResource, vim.ResourcePool], True)
else:
module.fail_json(msg='Resource type provided by user either doesn\'t'
' exist or is not supported')

all_resources = objview.view
objview.Destroy()

for resource in all_resources:
if resource_type == 'folder' and resource.name == resource_name.split('/')[-1]:
folder_path = resource_name.split('/')
Expand All @@ -121,13 +126,21 @@ def get_resource_id_from_name(module, vCenter_host, username, password,
return resource._moId
elif resource.name == resource_name:
return resource._moId
module.fail_json(msg='%s doesn\'t exist in %s' % (resource_name,
resource_type))

if resource_type == 'compute':
# If we're here, we didn't find a matching cluster or top-level resource pool
# Let's search for nested resource pools
for resource in all_resources:
if isinstance(resource, vim.ClusterComputeResource):
for rp in resource.resourcePool.resourcePool: # Search immediate child resource pools
if rp.name == resource_name:
return rp._moId

module.fail_json(msg='%s doesn\'t exist in %s' % (resource_name, resource_type))
except vmodl.MethodFault as error:
print("Caught vmodl fault while fetching info from vCenter: " + error.msg)
return -1


def get_data_network_id_from_name(module, vCenter_host, username, password,
data_network_name_list, ignore_ssl_verification):
"""
Expand Down
13 changes: 9 additions & 4 deletions plugins/modules/nsxt_transport_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,15 @@ def inject_vcenter_info(module, manager_url, mgr_username, mgr_password, validat
'storage', storage, ignore_ssl_verification)
transport_node_params['node_deployment_info']['deployment_config']['vm_deployment_config']['storage_id'] = str(storage_id)

cluster = vm_deployment_config.pop('compute')
cluster_id = get_resource_id_from_name(module, vc_ip, vc_username, vc_password,
'cluster', cluster, ignore_ssl_verification)
transport_node_params['node_deployment_info']['deployment_config']['vm_deployment_config']['compute_id'] = str(cluster_id)
cluster_or_rp = vm_deployment_config.pop('compute')
# Try to get the cluster ID or resource pool ID
compute_id = get_resource_id_from_name(module, vc_ip, vc_username, vc_password,
'compute', cluster_or_rp, ignore_ssl_verification)

if compute_id == -1:
module.fail_json(msg=f"Neither cluster nor resource pool with name '{cluster_or_rp}' found.")

transport_node_params['node_deployment_info']['deployment_config']['vm_deployment_config']['compute_id'] = str(compute_id)

management_network = vm_deployment_config.pop('management_network')
management_network_id = get_resource_id_from_name(module, vc_ip, vc_username, vc_password,
Expand Down

0 comments on commit 2a5a47c

Please sign in to comment.