Completed
Push — master ( f45574...b7e403 )
by Tomaz
21s
created

VMAddSCSIController.run()   C

Complexity

Conditions 8

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
c 0
b 0
f 0
dl 0
loc 52
rs 5.5452

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
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from pyVmomi import vim
17
18
from vmwarelib import inventory
19
from vmwarelib import checkinputs
20
from vmwarelib.actions import BaseAction
21
22
23
class VMAddSCSIController(BaseAction):
24
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