Completed
Pull Request — master (#460)
by Manas
02:13
created

get_managed_entity()   B

Complexity

Conditions 6

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
dl 0
loc 8
rs 8
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
    container = content.viewManager.CreateContainerView(content.rootFolder, [vimtype], True)
21
    for entity in container.view:
22
        # verify if this works.
23
        if moid and entity._moId == moid:
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _moId was declared protected and should not be accessed from this context.

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:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
24
            return entity
25
        elif name and entity.name == name:
26
            return entity
27
28
29
def get_datacenter(content, moid=None, name=None):
30
    return get_managed_entity(content, vim.Datacenter, moid=moid, name=name)
31
32
33
def get_folder(content, moid=None, name=None):
34
    return get_managed_entity(content, vim.Folder, moid=moid, name=name)
35
36
37
def get_resource_pool(content, moid=None, name=None):
38
    return get_managed_entity(content, vim.ResourcePool, moid=moid, name=name)
39
40
41
def get_datastore(content, moid=None, name=None):
42
    return get_managed_entity(content, vim.Datastore, moid=moid, name=name)
43
44
45
def get_network(content, moid=None, name=None):
46
    return get_managed_entity(content, vim.Network, moid=moid, name=name)
47
48
49
def get_virtualmachine(content, moid=None, name=None):
50
    return get_managed_entity(content, vim.VirtualMachine, moid=moid, name=name)
51
52
53
def get_task(content, moid=None):
54
    return get_managed_entity(content, vim.Task, moid=moid, name=None)
55