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

get_managed_entity()   F

Complexity

Conditions 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
dl 0
loc 35
rs 2.7855

How to fix   Complexity   

Complexity

Complex classes like get_managed_entity() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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:
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...
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_virtualmachine(content, moid=None, name=None):
97
    return get_managed_entity(content, vim.VirtualMachine,
98
                              moid=moid, name=name)
99
100
101
def get_virtualmachines(content):
102
    return get_managed_entities(content, vim.VirtualMachine)
103
104
105
def get_task(content, moid=None):
106
    return get_managed_entity(content, vim.Task,
107
                              moid=moid, name=None)
108