VMware: Autoselect datastore only from the given cluster/ESXi (#43435)

* Autoselect DS only from the given cluster/esxi
* Run tests only on real vCenter
This commit is contained in:
Jiri Tyr 2019-12-02 16:23:08 +00:00 committed by Abhijeet Kasurde
parent 5942e7f2a2
commit a8ef5d5034
3 changed files with 88 additions and 72 deletions

View file

@ -139,7 +139,7 @@ def find_object_by_name(content, name, obj_type, folder=None, recurse=True):
def find_cluster_by_name(content, cluster_name, datacenter=None):
if datacenter:
if datacenter and hasattr(datacenter, 'hostFolder'):
folder = datacenter.hostFolder
else:
folder = content.rootFolder

View file

@ -2158,10 +2158,24 @@ class PyVmomiHelper(PyVmomi):
if len(self.params['disk']) != 0:
# TODO: really use the datastore for newly created disks
if 'autoselect_datastore' in self.params['disk'][0] and self.params['disk'][0]['autoselect_datastore']:
datastores = self.cache.get_all_objs(self.content, [vim.Datastore])
datastores = [x for x in datastores if self.cache.get_parent_datacenter(x).name == self.params['datacenter']]
if datastores is None or len(datastores) == 0:
self.module.fail_json(msg="Unable to find a datastore list when autoselecting")
datastores = []
if self.params['cluster']:
cluster = self.find_cluster_by_name(self.params['cluster'], self.content)
for host in cluster.host:
for mi in host.configManager.storageSystem.fileSystemVolumeInfo.mountInfo:
if mi.volume.type == "VMFS":
datastores.append(self.cache.find_obj(self.content, [vim.Datastore], mi.volume.name))
elif self.params['esxi_hostname']:
host = self.find_hostsystem_by_name(self.params['esxi_hostname'])
for mi in host.configManager.storageSystem.fileSystemVolumeInfo.mountInfo:
if mi.volume.type == "VMFS":
datastores.append(self.cache.find_obj(self.content, [vim.Datastore], mi.volume.name))
else:
datastores = self.cache.get_all_objs(self.content, [vim.Datastore])
datastores = [x for x in datastores if self.cache.get_parent_datacenter(x).name == self.params['datacenter']]
datastore_freespace = 0
for ds in datastores:

View file

@ -2,74 +2,76 @@
# Copyright: (c) 2019, Noe Gonzalez <noe.a.gonzalez@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- name: create new VM
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_original
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
hardware:
num_cpus: 1
memory_mb: 128
guest_id: centos7_64Guest
disk:
- size_gb: 1
type: thin
datastore: "{{ rw_datastore }}"
state: poweredoff
- when: vcsim is not defined
block:
- name: create new VM
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_original
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
hardware:
num_cpus: 1
memory_mb: 128
guest_id: centos7_64Guest
disk:
- size_gb: 1
type: thin
datastore: "{{ rw_datastore }}"
state: poweredoff
- name: convert to VM template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_original
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
is_template: True
- name: convert to VM template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_original
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
is_template: True
- name: clone template and modify disks
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_clone
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
disk:
- size_gb: 2
type: thin
datastore: "{{ rw_datastore }}"
- size_gb: 3
type: thin
datastore: "{{ rw_datastore }}"
template: clone_resize_disks_original
state: poweredoff
register: l_clone_template_modify_disks
- name: clone template and modify disks
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: clone_resize_disks_clone
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
disk:
- size_gb: 2
type: thin
datastore: "{{ rw_datastore }}"
- size_gb: 3
type: thin
datastore: "{{ rw_datastore }}"
template: clone_resize_disks_original
state: poweredoff
register: l_clone_template_modify_disks
- assert:
that:
- l_clone_template_modify_disks.changed | bool
- assert:
that:
- l_clone_template_modify_disks.changed | bool
- name: delete VM clone & original template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ item }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
state: absent
with_items:
- clone_resize_disks_original
- clone_resize_disks_clone
- name: delete VM clone & original template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ item }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
folder: "{{ f0 }}"
state: absent
with_items:
- clone_resize_disks_original
- clone_resize_disks_clone