Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VMware: Add disk_mode parameter in vmware_guest_disk #60406

Merged
merged 7 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/ansible/modules/cloud/vmware/vmware_guest_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
- ' - C(eagerzeroedthick) eagerzeroedthick disk'
- ' - C(thick) thick disk'
- ' Default: C(thick) thick disk, no eagerzero.'
- ' - C(disk_mode) (string): Type of disk mode. Valid values are:'
- ' - C(persistent) Changes are immediately and permanently written to the virtual disk. This is default.'
- ' - C(independent_persistent) Same as persistent, but not affected by snapshots.'
- ' - C(independent_nonpersistent) Changes to virtual disk are made to a redo log and discarded at power off, but not affected by snapshots.'
- ' - C(datastore) (string): Name of datastore or datastore cluster to be used for the disk.'
- ' - C(autoselect_datastore) (bool): Select the less used datastore. Specify only if C(datastore) is not specified.'
- ' - C(scsi_controller) (integer): SCSI controller number. Valid value range from 0 to 3.'
Expand Down Expand Up @@ -127,20 +131,23 @@
scsi_controller: 1
unit_number: 1
scsi_type: 'paravirtual'
disk_mode: 'persistent'
- size_gb: 10
type: eagerzeroedthick
state: present
autoselect_datastore: True
scsi_controller: 2
scsi_type: 'buslogic'
unit_number: 12
disk_mode: 'independent_persistent'
- size: 10Gb
type: eagerzeroedthick
state: present
autoselect_datastore: True
scsi_controller: 2
scsi_type: 'buslogic'
unit_number: 1
disk_mode: 'independent_nonpersistent'
delegate_to: localhost
register: disk_facts

Expand Down Expand Up @@ -244,7 +251,7 @@ def create_scsi_controller(self, scsi_type, scsi_bus_number):
return scsi_ctl

@staticmethod
def create_scsi_disk(scsi_ctl_key, disk_index):
def create_scsi_disk(scsi_ctl_key, disk_index, disk_mode):
"""
Create Virtual Device Spec for virtual disk
Args:
Expand All @@ -259,7 +266,7 @@ def create_scsi_disk(scsi_ctl_key, disk_index):
disk_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create
disk_spec.device = vim.vm.device.VirtualDisk()
disk_spec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
disk_spec.device.backing.diskMode = 'persistent'
disk_spec.device.backing.diskMode = disk_mode
disk_spec.device.controllerKey = scsi_ctl_key
disk_spec.device.unitNumber = disk_index
return disk_spec
Expand Down Expand Up @@ -340,7 +347,7 @@ def ensure_disks(self, vm_obj=None):
scsi_controller = disk['scsi_controller'] + 1000 # VMware auto assign 1000 + SCSI Controller
if disk['disk_unit_number'] not in current_scsi_info[scsi_controller]['disks'] and disk['state'] == 'present':
# Add new disk
disk_spec = self.create_scsi_disk(scsi_controller, disk['disk_unit_number'])
disk_spec = self.create_scsi_disk(scsi_controller, disk['disk_unit_number'], disk['disk_mode'])
disk_spec.device.capacityInKB = disk['size']
if disk['disk_type'] == 'thin':
disk_spec.device.backing.thinProvisioned = True
Expand Down Expand Up @@ -416,7 +423,8 @@ def sanitize_disk_inputs(self):
datastore=None,
autoselect_datastore=True,
disk_unit_number=0,
scsi_controller=0)
scsi_controller=0,
disk_mode='persistent')
# Check state
if 'state' in disk:
if disk['state'] not in ['absent', 'present']:
Expand Down Expand Up @@ -569,6 +577,13 @@ def sanitize_disk_inputs(self):
" 'disk_type' value from ['thin', 'thick', 'eagerzeroedthick']." % disk_index)
current_disk['disk_type'] = disk_type

# Mode of Disk
temp_disk_mode = disk.get('disk_mode', 'persistent').lower()
if temp_disk_mode not in ['persistent', 'independent_persistent', 'independent_nonpersistent']:
self.module.fail_json(msg="Invalid 'disk_mode' specified for disk index [%s]. Please specify"
" 'disk_mode' value from ['persistent', 'independent_persistent', 'independent_nonpersistent']." % disk_index)
current_disk['disk_mode'] = temp_disk_mode

# SCSI Controller Type
scsi_contrl_type = disk.get('scsi_type', 'paravirtual').lower()
if scsi_contrl_type not in self.scsi_device_type.keys():
Expand Down
3 changes: 3 additions & 0 deletions test/integration/targets/vmware_guest_disk/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud/vcenter
shippable/vcenter/group1
needs/target/prepare_vmware_tests
79 changes: 79 additions & 0 deletions test/integration/targets/vmware_guest_disk/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Test code for the vmware_guest_disk_disk module.

- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_virtualmachines: true

- name: create new disk with invalid disk mode
vmware_guest_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
name: "{{ virtual_machines[0].name }}"
disk:
- datastore: "{{ ds1 }}"
disk_mode: "invalid_disk_mode"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 2
register: test_create_disk1
ignore_errors: True

- debug:
msg: "{{ test_create_disk1 }}"

- name: assert that changes were not made
assert:
that:
- not(test_create_disk1 is changed)

- name: create new disk(s) with valid disk mode
vmware_guest_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
name: "{{ virtual_machines[0].name }}"
disk:
- datastore: "{{ ds1 }}"
disk_mode: "independent_persistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 2
- datastore: "{{ ds1 }}"
disk_mode: "independent_nonpersistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 3
- datastore: "{{ ds1 }}"
disk_mode: "persistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 4
register: test_create_disk2

- debug:
msg: "{{ test_create_disk2 }}"

- name: assert that changes were made
assert:
that:
- test_create_disk2 is changed