Completed
Push — master ( ca2760...4c7c7c )
by Manas
15s
created

VMCreateFromTemplate.run()   B

Complexity

Conditions 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 26
rs 8.8571
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 VMCreateFromTemplate(BaseAction):
23
24
    def run(self, name, template_id, datacenter_id, resourcepool_id, datastore_id):
25
        # convert ids to stubs
26
        template = inventory.get_virtualmachine(self.si_content, template_id)
27
        datacenter = inventory.get_datacenter(self.si_content, datacenter_id)
28
        resourcepool = inventory.get_resource_pool(self.si_content, resourcepool_id)
29
        datastore = inventory.get_datastore(self.si_content, datastore_id)
30
        # prep objects for consumption
31
        target_folder = datacenter.vmFolder
32
33
        # relocate spec
34
        relocatespec = vim.vm.RelocateSpec()
35
        relocatespec.datastore = datastore
36
        relocatespec.pool = resourcepool
37
38
        # clone spec
39
        clonespec = vim.vm.CloneSpec()
40
        clonespec.location = relocatespec
41
        clonespec.powerOn = False
42
        clonespec.template = False
43
44
        task = template.CloneVM_Task(folder=target_folder, name=name, spec=clonespec)
45
        self._wait_for_task(task)
46
        if task.info.state != vim.TaskInfo.State.success:
47
            raise Exception(task.info.error.msg)
48
49
        return {'task_id': task._moId, 'vm_id': task.info.result._moId}
50