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
|
|
|
|
19
|
|
|
def get_managed_entity(content, vimtype, moid=None, name=None): |
20
|
|
|
if not name and not moid: |
21
|
|
|
return |
22
|
|
|
container = content.viewManager.CreateContainerView( |
23
|
|
|
content.rootFolder, [vimtype], True) |
24
|
|
|
count = 0 |
25
|
|
|
for entity in container.view: |
26
|
|
|
# Find matches in the results |
27
|
|
|
if moid and entity._moId == moid: |
|
|
|
|
28
|
|
|
results = entity |
29
|
|
|
count += 1 |
30
|
|
|
elif name and entity.name == name: |
31
|
|
|
results = entity |
32
|
|
|
count += 1 |
33
|
|
|
# check to see if multiple matches were found |
34
|
|
|
if count >= 2: |
35
|
|
|
raise Exception("Multiple Managed Objects found,\ |
36
|
|
|
Check Names or IDs provided are unique") |
37
|
|
|
elif count == 1: |
38
|
|
|
# Single Match found |
39
|
|
|
return results |
40
|
|
|
|
41
|
|
|
# if this area is reached no object has been found |
42
|
|
|
# if a name was passed error |
43
|
|
|
if name: |
44
|
|
|
raise Exception("Inventory Error: Unable to Find Object (%s): %s" |
45
|
|
|
% (vimtype, name)) |
46
|
|
|
# if a moid was passed error |
47
|
|
|
elif moid: |
48
|
|
|
raise Exception("Inventory Error: Unable to Find Object (%s): %s" |
49
|
|
|
% (vimtype, moid)) |
50
|
|
|
# catch all error |
51
|
|
|
else: |
52
|
|
|
raise Exception("Inventory Error: No Name or moid provided (%s)" |
53
|
|
|
% vimtype) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def get_managed_entities(content, vimtype): |
57
|
|
|
container = content.viewManager.CreateContainerView( |
58
|
|
|
content.rootFolder, [vimtype], True) |
59
|
|
|
return container |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def get_datacenter(content, moid=None, name=None): |
63
|
|
|
return get_managed_entity(content, vim.Datacenter, moid=moid, name=name) |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def get_cluster(content, moid=None, name=None): |
67
|
|
|
return get_managed_entity(content, vim.ClusterComputeResource, |
68
|
|
|
moid=moid, name=name) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def get_folder(content, moid=None, name=None): |
72
|
|
|
return get_managed_entity(content, vim.Folder, |
73
|
|
|
moid=moid, name=name) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def get_resource_pool(content, moid=None, name=None): |
77
|
|
|
return get_managed_entity(content, vim.ResourcePool, |
78
|
|
|
moid=moid, name=name) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def get_datastore_cluster(content, moid=None, name=None): |
82
|
|
|
return get_managed_entity(content, vim.StoragePod, |
83
|
|
|
moid=moid, name=name) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def get_datastore(content, moid=None, name=None): |
87
|
|
|
return get_managed_entity(content, vim.Datastore, |
88
|
|
|
moid=moid, name=name) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def get_network(content, moid=None, name=None): |
92
|
|
|
return get_managed_entity(content, vim.Network, |
93
|
|
|
moid=moid, name=name) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def get_distributedportgroup(content, moid=None, name=None): |
97
|
|
|
return get_managed_entity(content, vim.dvs.DistributedVirtualPortgroup, |
98
|
|
|
moid=moid, name=name) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def get_virtualmachine(content, moid=None, name=None): |
102
|
|
|
return get_managed_entity(content, vim.VirtualMachine, |
103
|
|
|
moid=moid, name=name) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def get_virtualmachines(content): |
107
|
|
|
return get_managed_entities(content, vim.VirtualMachine) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def get_task(content, moid=None): |
111
|
|
|
return get_managed_entity(content, vim.Task, |
112
|
|
|
moid=moid, name=None) |
113
|
|
|
|
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: