Conditions | 8 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
25 | def run(self, vm_id, vm_name, controller_type, scsi_sharing, vsphere=None): |
||
26 | """ |
||
27 | Add SCSI controller to Virtual Machine |
||
28 | |||
29 | Args: |
||
30 | - vm_id: Moid of Virtual Machine to edit |
||
31 | - vm_name: Name of Virtual Machine to edit |
||
32 | - controller_type: Type of Controller to add |
||
33 | - scsi_sharing: type of sharing for scsi adapter |
||
34 | - vsphere: Pre-configured vsphere connection details (config.yaml) |
||
35 | |||
36 | Returns: |
||
37 | - dict: state true/false |
||
38 | """ |
||
39 | |||
40 | # VM name or ID given? |
||
41 | checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") |
||
42 | |||
43 | self.establish_connection(vsphere) |
||
44 | |||
45 | # Create object for VM |
||
46 | vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) |
||
47 | |||
48 | # Create SCSI Controller Object |
||
49 | configspec = vim.vm.ConfigSpec() |
||
50 | scsictrl = vim.vm.device.VirtualDeviceSpec() |
||
51 | scsictrl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
||
52 | |||
53 | # Select object type |
||
54 | if controller_type == 'ParaVirtual': |
||
55 | scsictrl.device = vim.vm.device.ParaVirtualSCSIController() |
||
56 | elif controller_type == 'BusLogic': |
||
57 | scsictrl.device = vim.vm.device.VirtualBusLogicController() |
||
58 | elif controller_type == 'LSILogic': |
||
59 | scsictrl.device = vim.vm.device.VirtualLsiLogicController() |
||
60 | elif controller_type == 'LSILogicSAS': |
||
61 | scsictrl.device = vim.vm.device.VirtualLsiLogicSASController() |
||
62 | |||
63 | # Set SCSI Bus Sharing type |
||
64 | if scsi_sharing == 'None': |
||
65 | scsictrl.device.sharedBus = 'noSharing' |
||
66 | elif scsi_sharing == 'Physical': |
||
67 | scsictrl.device.sharedBus = 'physicalSharing' |
||
68 | elif scsi_sharing == 'Virtual': |
||
69 | scsictrl.device.sharedBus = 'virtualSharing' |
||
70 | |||
71 | # Create Task to add to VM |
||
72 | configspec.deviceChange = [scsictrl] |
||
73 | add_ctrl_task = vm.ReconfigVM_Task(configspec) |
||
74 | successfully_added_ctrl = self._wait_for_task(add_ctrl_task) |
||
75 | |||
76 | return {'state': successfully_added_ctrl} |
||
77 |