Completed
Push — master ( f419ff...edacad )
by Manas
02:43
created

VMCreateBareBones   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %
Metric Value
dl 0
loc 81
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 80 5
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 VMCreateBareBones(BaseAction):
24
    def run(self, vm_name, cluster, datastore_cluster,
25
            datastore, resourcepool, cpu_size, ram_size,
26
            guestos, version, description):
27
        # Setup Identifiers for objects
28
        si = self.si
29
        si_content = si.RetrieveContent()
30
        # checkinputs.vm_storage(datastore_cluster, datastore)
31
        checkinputs.one_of_two_strings(datastore_cluster,
32
                                       datastore,
33
                                       "Datastore Cluster or Datastore")
34
35
        data_center = self.si_content.rootFolder.childEntity[0]
36
        cluster = inventory.get_cluster(self.si_content, name=cluster)
37
        data_store_cluster = inventory.get_datastore_cluster(
38
            self.si_content,
39
            name=datastore_cluster)
40
        # data_store = inventory.get_datastore(self.si_content, name=datastore)
41
        target_folder = data_center.vmFolder
42
43
        # If No Resource Pool issued the Default one for
44
        # the Cluster will be selected.
45
        if resourcepool:
46
            resource_pool = inventory.get_resource_pool(self.si_content,
47
                                                        name=resourcepool)
48
        else:
49
            resource_pool = cluster.resourcePool
50
51
        # Config created that is required for DS selection
52
        # Config is BareBones, CPU RAM no more.
53
        config = vim.vm.ConfigSpec(name=vm_name,
54
                                   memoryMB=(ram_size * 1024),
55
                                   numCPUs=cpu_size,
56
                                   guestId=guestos,
57
                                   version=version,
58
                                   cpuHotAddEnabled=True,
59
                                   memoryHotAddEnabled=True,
60
                                   annotation=description)
61
62
        # if Datastore cluster is provided it will find the
63
        # recommended Datastore to store VM files
64
        if datastore_cluster:
65
            podsel = vim.storageDrs.PodSelectionSpec()
66
            podsel.storagePod = data_store_cluster
67
68
            storage_spec = vim.storageDrs.StoragePlacementSpec(
69
                type='create',
70
                configSpec=config,
71
                resourcePool=resource_pool,
72
                podSelectionSpec=podsel,
73
                folder=target_folder)
74
75
            # Create Instance of Storage Resource Manager -
76
            # This is used to identify a Recommended Datastore from a Cluster
77
            srm = si_content.storageResourceManager
78
            results = srm.RecommendDatastores(storageSpec=storage_spec)
79
            rec_ds = results.recommendations[0].action[0]\
80
                .relocateSpec.datastore
81
            datastore_path = '[' + rec_ds.name + '] ' + vm_name
82
        else:
83
            # No Datastore Cluster has been offered so using the D
84
            if datastore:
85
                datastore_path = '[' + datastore + '] ' + vm_name
86
            else:
87
                raise Exception('Error No Storage Data Provided')
88
89
        # Now Datastore is known the remaining
90
        # of VM Config can be setup and added
91
        vmx_file = vim.vm.FileInfo(logDirectory=None,
92
                                   snapshotDirectory=None,
93
                                   suspendDirectory=None,
94
                                   vmPathName=datastore_path)
95
        config.files = vmx_file
96
97
        # Create task to Build actual Machine
98
        task = target_folder.CreateVM_Task(config=config, pool=resource_pool)
99
        self._wait_for_task(task)
100
        if task.info.state != vim.TaskInfo.State.success:
101
            raise Exception(task.info.error.msg)
102
103
        return {'vm_id': task.info.result._moId}
104