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 vmwarelib import inventory |
17
|
|
|
from vmwarelib.actions import BaseAction |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class GetVMMoid(BaseAction): |
21
|
|
|
|
22
|
|
|
def run(self, vm_names, vsphere=None): |
23
|
|
|
""" |
24
|
|
|
Return moid values for VMs listed within the vsphere. |
25
|
|
|
|
26
|
|
|
Args: |
27
|
|
|
- vm_names: list of names as shown in vsphere |
28
|
|
|
|
29
|
|
|
Returns: |
30
|
|
|
- dict: key value pair of vm_name and vm moid. |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
results = {} |
34
|
|
|
self.establish_connection(vsphere) |
35
|
|
|
|
36
|
|
|
vmlist = inventory.get_virtualmachines(self.si_content) |
37
|
|
|
|
38
|
|
|
for vm in vmlist.view: |
39
|
|
|
if vm_names: |
40
|
|
|
if vm.name in vm_names: |
41
|
|
|
results[vm.name] = str(vm).split(':')[-1].replace("'", "") |
42
|
|
|
else: |
43
|
|
|
results[vm.name] = str(vm).split(':')[-1].replace("'", "") |
44
|
|
|
|
45
|
|
|
return results |
46
|
|
|
|