Skip to content

Commit

Permalink
Use create_disk_controller() from vm_device_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mariolenz committed Jul 1, 2021
1 parent 6899712 commit 81ca35c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
15 changes: 8 additions & 7 deletions plugins/module_utils/vm_device_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ def __init__(self, module):
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,
'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 @@ -64,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 @@ -102,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_typekeys():
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
23 changes: 7 additions & 16 deletions plugins/modules/vmware_guest_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,29 +318,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.device_helper.sata_device_type()
disk_ctl.device.key = -randint(15000, 19999)
elif ctl_type == 'nvme':
disk_ctl.device = self.device_helper.nvme_device_type()
disk_ctl.device.key = -randint(31000, 39999)
elif ctl_type in self.device_helper.scsi_device_type.keys():
disk_ctl.device = self.device_helper.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
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

0 comments on commit 81ca35c

Please sign in to comment.