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

Refactor vmware guest controller #926

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions plugins/module_utils/vm_device_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ def __init__(self, module):
'buslogic': vim.vm.device.VirtualBusLogicController,
'lsilogicsas': vim.vm.device.VirtualLsiLogicSASController,
}
self.sata_device_type = vim.vm.device.VirtualAHCIController
self.nvme_device_type = vim.vm.device.VirtualNVMEController
self.usb_device_type = {
'usb2': vim.vm.device.VirtualUSBController,
'usb3': vim.vm.device.VirtualUSBXHCIController,
}

def create_scsi_controller(self, scsi_type, bus_number):
def create_scsi_controller(self, scsi_type, bus_number, bus_sharing='noSharing'):
"""
Create SCSI Controller with given SCSI Type and SCSI Bus Number
Args:
scsi_type: Type of SCSI
scsi_bus_number: SCSI Bus number to be assigned
bus_sharing: noSharing, virtualSharing, physicalSharing

Returns: Virtual device spec for SCSI Controller

Expand All @@ -58,7 +65,7 @@ def create_scsi_controller(self, scsi_type, bus_number):
# should be unique negative integers
scsi_ctl.device.key = -randint(1000, 9999)
scsi_ctl.device.hotAddRemove = True
scsi_ctl.device.sharedBus = 'noSharing'
scsi_ctl.device.sharedBus = bus_sharing
scsi_ctl.device.scsiCtlrUnitNumber = 7

return scsi_ctl
Expand Down Expand Up @@ -96,10 +103,10 @@ def create_nvme_controller(bus_number):
def is_nvme_controller(device):
return isinstance(device, vim.vm.device.VirtualNVMEController)

def create_disk_controller(self, ctl_type, ctl_number):
def create_disk_controller(self, ctl_type, ctl_number, bus_sharing='noSharing'):
disk_ctl = None
if ctl_type in ['buslogic', 'paravirtual', 'lsilogic', 'lsilogicsas']:
disk_ctl = self.create_scsi_controller(ctl_type, ctl_number)
if ctl_type in self.scsi_device_type.keys():
disk_ctl = self.create_scsi_controller(ctl_type, ctl_number, bus_sharing)
if ctl_type == 'sata':
disk_ctl = self.create_sata_controller(ctl_number)
if ctl_type == 'nvme':
Expand Down
69 changes: 27 additions & 42 deletions plugins/modules/vmware_guest_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,21 @@
except ImportError:
pass

from random import randint
import time
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec, wait_for_task, TaskError
from ansible_collections.community.vmware.plugins.module_utils.vm_device_helper import PyVmomiDeviceHelper


class PyVmomiHelper(PyVmomi):
def __init__(self, module):
super(PyVmomiHelper, self).__init__(module)
self.device_helper = PyVmomiDeviceHelper(self.module)
self.sleep_time = 10
self.scsi_device_type = dict(lsilogic=vim.vm.device.VirtualLsiLogicController,
paravirtual=vim.vm.device.ParaVirtualSCSIController,
buslogic=vim.vm.device.VirtualBusLogicController,
lsilogicsas=vim.vm.device.VirtualLsiLogicSASController)
self.sata_device_type = vim.vm.device.VirtualAHCIController
self.nvme_device_type = vim.vm.device.VirtualNVMEController
self.usb_device_type = dict(usb2=vim.vm.device.VirtualUSBController,
usb3=vim.vm.device.VirtualUSBXHCIController)
self.controller_types = dict(self.scsi_device_type, sata=self.sata_device_type, nvme=self.nvme_device_type)
self.controller_types.update(self.usb_device_type)
self.controller_types = self.device_helper.scsi_device_type.copy()
self.controller_types.update(self.device_helper.usb_device_type)
self.controller_types.update({'sata': self.device_helper.sata_device_type, 'nvme': self.device_helper.nvme_device_type})
self.config_spec = vim.vm.ConfigSpec()
self.config_spec.deviceChange = []
self.change_detected = False
Expand All @@ -281,13 +275,13 @@ def get_unused_ctl_bus_number(self):
Get gid of occupied bus numbers of each type of disk controller, update the available bus number list
"""
for device in self.current_vm_obj.config.hardware.device:
if isinstance(device, self.sata_device_type):
if isinstance(device, self.device_helper.sata_device_type):
if len(self.disk_ctl_bus_num_list['sata']) != 0:
self.disk_ctl_bus_num_list['sata'].remove(device.busNumber)
if isinstance(device, self.nvme_device_type):
if isinstance(device, self.device_helper.nvme_device_type):
if len(self.disk_ctl_bus_num_list['nvme']) != 0:
self.disk_ctl_bus_num_list['nvme'].remove(device.busNumber)
if isinstance(device, tuple(self.scsi_device_type.values())):
if isinstance(device, tuple(self.device_helper.scsi_device_type.values())):
if len(self.disk_ctl_bus_num_list['scsi']) != 0:
self.disk_ctl_bus_num_list['scsi'].remove(device.busNumber)

Expand Down Expand Up @@ -322,29 +316,20 @@ def create_controller(self, ctl_type, bus_sharing, bus_number=0):

Return: Virtual device spec for virtual controller
"""
disk_ctl = vim.vm.device.VirtualDeviceSpec()
disk_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
if ctl_type == 'sata':
disk_ctl.device = self.sata_device_type()
disk_ctl.device.key = -randint(15000, 19999)
elif ctl_type == 'nvme':
disk_ctl.device = self.nvme_device_type()
disk_ctl.device.key = -randint(31000, 39999)
elif ctl_type in self.scsi_device_type.keys():
disk_ctl.device = self.scsi_device_type.get(ctl_type)()
disk_ctl.device.key = -randint(1000, 6999)
disk_ctl.device.hotAddRemove = True
disk_ctl.device.sharedBus = bus_sharing
disk_ctl.device.scsiCtlrUnitNumber = 7
elif ctl_type in self.usb_device_type.keys():
disk_ctl.device = self.usb_device_type.get(ctl_type)()
if ctl_type == 'sata' or ctl_type == 'nvme' or ctl_type in self.device_helper.scsi_device_type.keys():
disk_ctl = self.device_helper.create_disk_controller(ctl_type, bus_number, bus_sharing)
elif ctl_type in self.device_helper.usb_device_type.keys():
disk_ctl = vim.vm.device.VirtualDeviceSpec()
disk_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
disk_ctl.device = self.device_helper.usb_device_type.get(ctl_type)()

if ctl_type == 'usb2':
disk_ctl.device.key = 7000
elif ctl_type == 'usb3':
disk_ctl.device.key = 14000

disk_ctl.device.deviceInfo = vim.Description()
disk_ctl.device.busNumber = bus_number
disk_ctl.device.deviceInfo = vim.Description()
disk_ctl.device.busNumber = bus_number

return disk_ctl

Expand Down Expand Up @@ -375,15 +360,15 @@ def gather_disk_controller_facts(self):
)
if hasattr(device, 'sharedBus'):
ctl_facts_dict[device.busNumber]['controller_bus_sharing'] = device.sharedBus
if isinstance(device, tuple(self.scsi_device_type.values())):
if isinstance(device, tuple(self.device_helper.scsi_device_type.values())):
disk_ctl_facts['scsi'].update(ctl_facts_dict)
if isinstance(device, self.nvme_device_type):
if isinstance(device, self.device_helper.nvme_device_type):
disk_ctl_facts['nvme'].update(ctl_facts_dict)
if isinstance(device, self.sata_device_type):
if isinstance(device, self.device_helper.sata_device_type):
disk_ctl_facts['sata'].update(ctl_facts_dict)
if isinstance(device, self.usb_device_type.get('usb2')):
if isinstance(device, self.device_helper.usb_device_type.get('usb2')):
disk_ctl_facts['usb2'].update(ctl_facts_dict)
if isinstance(device, self.usb_device_type.get('usb3')):
if isinstance(device, self.device_helper.usb_device_type.get('usb3')):
disk_ctl_facts['usb3'].update(ctl_facts_dict)

return disk_ctl_facts
Expand All @@ -403,7 +388,7 @@ def sanitize_disk_controller_config(self):
controller_config = self.params.get('controllers')
for ctl_config in controller_config:
if ctl_config:
if ctl_config['type'] not in self.usb_device_type.keys():
if ctl_config['type'] not in self.device_helper.usb_device_type.keys():
if ctl_config['state'] == 'absent' and ctl_config.get('controller_number') is None:
self.module.fail_json(msg="Disk controller number is required when removing it.")
if ctl_config['state'] == 'present' and not exec_get_unused_ctl_bus_number:
Expand All @@ -417,8 +402,8 @@ def sanitize_disk_controller_config(self):
" is '%s', not >= 13." % vm_hwv)
if exec_get_unused_ctl_bus_number:
for ctl_config in controller_config:
if ctl_config and ctl_config['state'] == 'present' and ctl_config['type'] not in self.usb_device_type.keys():
if ctl_config['type'] in self.scsi_device_type.keys():
if ctl_config and ctl_config['state'] == 'present' and ctl_config['type'] not in self.device_helper.usb_device_type.keys():
if ctl_config['type'] in self.device_helper.scsi_device_type.keys():
if len(self.disk_ctl_bus_num_list['scsi']) != 0:
ctl_config['controller_number'] = self.disk_ctl_bus_num_list['scsi'].pop(0)
else:
Expand Down Expand Up @@ -446,7 +431,7 @@ def configure_disk_controllers(self):
for disk_ctl_config in controller_config:
if disk_ctl_config and disk_ctl_config['state'] == 'present':
# create new USB controller, bus number is 0
if disk_ctl_config['type'] in self.usb_device_type.keys():
if disk_ctl_config['type'] in self.device_helper.usb_device_type.keys():
usb_exists, has_disks_attached = self.check_ctl_disk_exist(disk_ctl_config['type'])
if usb_exists:
self.module.warn("'%s' USB controller already exists, can not add more." % disk_ctl_config['type'])
Expand All @@ -465,7 +450,7 @@ def configure_disk_controllers(self):
self.config_spec.deviceChange.append(disk_controller_new)
self.change_detected = True
else:
if disk_ctl_config['type'] in self.scsi_device_type.keys():
if disk_ctl_config['type'] in self.device_helper.scsi_device_type.keys():
self.module.warn("Already 4 SCSI controllers, can not add new '%s' controller." % disk_ctl_config['type'])
else:
self.module.warn("Already 4 '%s' controllers, can not add new one." % disk_ctl_config['type'])
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/vmware_guest_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def __init__(self, module):
self.desired_disks = self.params['disk'] # Match with vmware_guest parameter
self.vm = None
self.ctl_device_type = self.device_helper.scsi_device_type.copy()
self.ctl_device_type.update({'sata': vim.vm.device.VirtualAHCIController, 'nvme': vim.vm.device.VirtualNVMEController})
self.ctl_device_type.update({'sata': self.device_helper.sata_device_type, 'nvme': self.device_helper.nvme_device_type})
self.config_spec = vim.vm.ConfigSpec()
self.config_spec.deviceChange = []

Expand Down