Completed
Pull Request — master (#462)
by
unknown
02:34
created

VMAddHDD.run()   B

Complexity

Conditions 5

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 60
rs 8.3554
cc 5

How to fix   Long Method   

Long Method

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:

1
from pyVmomi import vim
2
import sys
3
4
from vmwarelib import inventory
5
from vmwarelib import checkinputs
6
from vmwarelib.actions import BaseAction
7
8
9
class VMAddHDD(BaseAction):
10
    def run(self, vm_id, vm_name, datastore_cluster,
11
            datastore, disk_size, provision_type):
12
        # ensure that minimal inputs are provided
13
        checkinputs.vm_identifier(vm_id, vm_name)
14
15
        vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name)
16
        spec = vim.vm.ConfigSpec()
17
        hdd_unit_number = self.get_next_unit_number(vm)
18
        ctrl_key = self.get_controller_key(vm)
19
20
        #Prepare new Disk configuration
21
        disk_changes = []
22
        disk_spec = vim.vm.device.VirtualDeviceSpec()
23
        disk_spec.fileOperation = "create"
24
        disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
25
        disk_spec.device = vim.vm.device.VirtualDisk()
26
        disk_spec.device.backing =\
27
            vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
28
        disk_spec.device.backing.diskMode = "persistent"
29
30
        if provision_type == 'thin':
31
            disk_spec.device.backing.thinProvisioned = True
32
33
        disk_spec.device.unitNumber = hdd_unit_number
34
        disk_spec.device.capacityInKB = int(disk_size) * 1024 * 1024
35
        disk_spec.device.controllerKey = ctrl_key
36
37
        #If Datastore Cluster is provided attach Disk via that
38
        if datastore_cluster:
39
            ds_clust_obj = inventory.get_datastore_cluster(
40
                self.si_content, name=datastore_cluster)
41
            disk_changes.append(disk_spec)
42
            spec.deviceChange = disk_changes
43
            srm = self.si_content.storageResourceManager
44
45
            storage_placement_spec = self.get_storage_placement_spec(
46
                ds_clust_obj, vm, spec)
47
            datastores = srm.RecommendDatastores(
48
                storageSpec=storage_placement_spec)
49
50
            if not datastores.recommendations:
51
                sys.stderr.write('Skipping as No datastore Recommendations')
52
53
            add_disk_task = srm.ApplyStorageDrsRecommendation_Task(
54
                datastores.recommendations[0].key)
55
56
        elif datastore:
57
            datastore_obj = inventory.get_datastore(self.si_content,
58
                                                    name=datastore)
59
            disk_spec.device.backing.datastore = datastore_obj
60
            disk_changes.append(disk_spec)
61
            spec.deviceChange = disk_changes
62
            add_disk_task = vm.ReconfigVM_Task(spec)
63
        else:
64
            disk_changes.append(disk_spec)
65
            spec.deviceChange = disk_changes
66
            add_disk_task = vm.ReconfigVM_Task(spec)
67
68
        successfully_added_disk = self._wait_for_task(add_disk_task)
69
        return {'state': successfully_added_disk}
70
71
    def get_storage_placement_spec(self, ds_clust_obj, vm, vm_reconfig_spec):
72
        storage_placement_spec = vim.storageDrs.StoragePlacementSpec()
73
        storage_placement_spec.type =\
74
            vim.storageDrs.StoragePlacementSpec.PlacementType.reconfigure
75
        storage_placement_spec.configSpec = vm_reconfig_spec
76
        storage_placement_spec.podSelectionSpec =\
77
            vim.storageDrs.PodSelectionSpec()
78
        storage_placement_spec.vm = vm
79
80
        vm_pod_cfg = vim.storageDrs.PodSelectionSpec.VmPodConfig()
81
        vm_pod_cfg.storagePod = ds_clust_obj
82
        disk_locator = vim.storageDrs.PodSelectionSpec.DiskLocator()
83
        disk_locator.diskBackingInfo =\
84
            vm_reconfig_spec.deviceChange[0].device.backing
85
        vm_pod_cfg.disk = [disk_locator]
86
        storage_placement_spec.podSelectionSpec.initialVmConfig = [vm_pod_cfg]
87
        return storage_placement_spec
88
89
    def get_next_unit_number(self, vm):
90
        unit_number = 0
91
        #Cycle though devices on VM and find
92
        #entries with attribute "fileName".
93
        for device in vm.config.hardware.device:
94
            if hasattr(device.backing, 'fileName'):
95
                unit_number = int(device.unitNumber) + 1
96
                #unit number 7 is reserved
97
                if unit_number == 7:
98
                    unit_number += 1
99
        return unit_number
100
101
    def get_controller_key(self, vm):
102
        #1000 is the default used for hdd controllers
103
        key = 1000
104
        for device in vm.config.hardware.device:
105
            if isinstance(device, vim.vm.device.VirtualSCSIController):
106
                key = device.key
107
        return key
108