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.actions import BaseAction |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class VMDiskAdd(BaseAction): |
23
|
|
|
|
24
|
|
|
def run(self, vm_id, disk_size, disk_type): |
25
|
|
|
vm = inventory.get_virtualmachine(self.si_content, vm_id) |
26
|
|
|
spec = vim.vm.ConfigSpec() |
27
|
|
|
|
28
|
|
|
# disk spec |
29
|
|
|
disk_spec = vim.vm.device.VirtualDeviceSpec() |
30
|
|
|
disk_spec.fileOperation = "create" |
31
|
|
|
disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
32
|
|
|
disk_spec.device = vim.vm.device.VirtualDisk() |
33
|
|
|
disk_spec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo() |
34
|
|
|
if disk_type == 'thin': |
35
|
|
|
disk_spec.device.backing.thinProvisioned = True |
36
|
|
|
disk_spec.device.backing.diskMode = 'persistent' |
37
|
|
|
disk_spec.device.unitNumber = self.get_next_unit_number(vm) |
38
|
|
|
disk_spec.device.capacityInKB = disk_size * 1024 * 1024 |
39
|
|
|
disk_spec.device.controllerKey = 1000 |
40
|
|
|
|
41
|
|
|
spec.deviceChange = [disk_spec] |
42
|
|
|
|
43
|
|
|
# add disk and wait for task to complete |
44
|
|
|
add_disk_task = vm.ReconfigVM_Task(spec) |
45
|
|
|
successfully_added_disk = self._wait_for_task(add_disk_task) |
46
|
|
|
return {'state': successfully_added_disk} |
47
|
|
|
|
48
|
|
|
def get_next_unit_number(self, vm): |
49
|
|
|
# See https://github.com/whereismyjetpack/pyvmomi-community-samples/blob/ |
50
|
|
|
# add-disk/samples/add_disk_to_vm.py |
51
|
|
|
unit_number = 0 |
52
|
|
|
for dev in vm.config.hardware.device: |
53
|
|
|
if hasattr(dev.backing, 'fileName'): |
54
|
|
|
unit_number = int(dev.unitNumber) + 1 |
55
|
|
|
# unit_number 7 reserved for scsi controller |
56
|
|
|
if unit_number == 7: |
57
|
|
|
unit_number += 1 |
58
|
|
|
return unit_number |
59
|
|
|
|