Skip to content

Commit

Permalink
CR fixes and adding filter hosts to vm list
Browse files Browse the repository at this point in the history
  • Loading branch information
bardielle committed May 16, 2024
1 parent 79f5133 commit 7c438d5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
42 changes: 22 additions & 20 deletions plugins/modules/vm_list_group_by_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@
author:
- Ansible Cloud Team (@ansible-collections)
options:
detailed_vms:
default: true
description:
- Wither or not get the vms detailed.
type: bool
detailed_vms:
default: true
description:
- If I(true) gather detailed information about virtual machines.
type: bool
extends_documentation_fragment:
- vmware.vmware.vmware_rest_client.documentation
'''

EXAMPLES = r'''
- name: Gather list of VMs
- name: Gather list of VMs group by clusters and folders
vmware.vmware.vm_list_group_by_clusters:
hostname: "https://vcenter"
username: "username"
password: "password"
- name: Gather list of VMs in clusterA
vmware.vmware.vm_list_group_by_clusters:
hostname: "https://vcenter"
username: "username"
password: "password"
detailed_vms: False
'''

RETURN = r'''
Expand Down Expand Up @@ -132,24 +125,34 @@ def get_all_folders(self):
type=self.api_client.vcenter.Folder.Type.VIRTUAL_MACHINE)
return self.api_client.vcenter.Folder.list(folder_filter_spec)

def get_all_vms_in_cluster_and_folder(self, cluster, folder):
def get_all_vms_in_cluster_and_folder(self, cluster, folder, host):
vms_filter = self.api_client.vcenter.VM.FilterSpec(clusters=set([cluster]),
folders=set([folder]))
folders=set([folder]),
hosts=set([host]))

return self.api_client.vcenter.VM.list(vms_filter)

def get_vm_detailed(self, vm):
return self.api_client.vcenter.VM.get(vm=vm)

def get_cluster_hosts(self, cluster):
host_filter = self.api_client.vcenter.Host.FilterSpec(
clusters=set([cluster]))
return self.api_client.vcenter.Host.list(host_filter)

def get_vm_list_group_by_clusters(self):
clusters = self.get_all_clusters()
folders = self.get_all_folders()

result_dict = {}
for cluster in clusters:
vm_list_group_by_folder_dict = {}
empty = True
hosts = self.get_cluster_hosts(cluster.cluster)
for folder in folders:
vms = self.get_all_vms_in_cluster_and_folder(cluster.cluster, folder.folder)
vms = []
# iterate each host due to an error too_many_matches when looking at all vms on a cluster
# https://github.com/vmware/vsphere-automation-sdk-python/issues/142
for host in hosts:
vms.extend(self.get_all_vms_in_cluster_and_folder(cluster.cluster, folder.folder, host.host))
vms_detailed = []
for vm in vms:
if self.detailed_vms:
Expand All @@ -158,10 +161,9 @@ def get_vm_list_group_by_clusters(self):
vms_detailed.append(self._vvars(vm))

if vms_detailed:
empty = False
vm_list_group_by_folder_dict[folder.name] = vms_detailed

if not empty:
if vm_list_group_by_folder_dict:
result_dict[cluster.name] = vm_list_group_by_folder_dict

return result_dict
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/targets/vm_list_group_by_clusters/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
"body": {
"type": "JSON",
"matchType": "PARTIAL",
"json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.VM\",\"operationId\":\"list\"}}"
"json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.host\",\"operationId\":\"list\"}}"
}
},
"httpResponseTemplate": {
"template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.VM\": {\"name\": \"vm1\", \"power_state\": \"POWERED_OFF\", \"vm\": \"vm1\"}}}]}, 'id': '2'}}",
"template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.host\": {\"connection_state\": \"CONNECTED\", \"host\": \"host1\", \"name\": \"host1\", \"power_state\": {\"OPTIONAL\": \"POWERED_ON\"}}}}]}, 'id': '2'}}",
"templateType": "VELOCITY"
},
"priority": 1
Expand All @@ -77,11 +77,11 @@
"body": {
"type": "JSON",
"matchType": "PARTIAL",
"json": "{\"params\":{\"serviceId\":\"com.vmware.cis.session\",\"operationId\":\"get\"}}"
"json": "{\"params\":{\"serviceId\":\"com.vmware.vcenter.VM\",\"operationId\":\"list\"}}"
}
},
"httpResponseTemplate": {
"template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": 'hostname.example.com'}, 'id': '${json.parse($!request.body)['id']}'}}",
"template": "{\"statusCode\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"body\": {\"jsonrpc\": \"2.0\", \"result\": {\"output\": [{\"STRUCTURE\": {\"com.vmware.vcenter.VM\": {\"name\": \"vm1\", \"power_state\": \"POWERED_OFF\", \"vm\": \"vm1\"}}}]}, 'id': '3'}}",
"templateType": "VELOCITY"
},
"priority": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
ansible.builtin.assert:
that:
- __res.changed == False
- __res.vm_list_group_by_clusters | length == 1
- __res.vm_list_group_by_clusters | length == 1
- __res.vm_list_group_by_clusters['cluster1'] | length == 1
- __res.vm_list_group_by_clusters['cluster1']['folder1'] | length == 1
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ vcenter_username: "user"
vcenter_password: "pass"
vcenter_port: 1080

mock_file: "vm_list_group_by_clusters"
mock_file: "vm_list_group_by_clusters"

0 comments on commit 7c438d5

Please sign in to comment.