Completed
Pull Request — master (#462)
by
unknown
02:34
created

get_managed_entity()   F

Complexity

Conditions 14

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 2.7581
cc 14

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
from pyVmomi import vim
2
3
4
def get_managed_entity(content, vimtype, moid=None, name=None):
5
    container = content.viewManager.CreateContainerView(
6
        content.rootFolder, [vimtype], True)
7
    for entity in container.view:
8
        # verify if this works.
9
        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...
10
            return entity
11
        elif name and entity.name == name:
12
            return entity
13
14
    #if this area is reached no object has been found
15
    #if no arguments were passed just return with empty object
16
    if ((name is None or name == "") and (moid is None or moid == "")):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after if.
Loading history...
17
        return
18
    #if a name was passed error
19
    elif (name is not None and name != ""):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after elif.
Loading history...
20
        raise Exception("Inventory Error: Unable to Find Object (%s): %s"
21
                        % (vimtype, name))
22
    #if a moid was passed error
23
    elif (moid is not None and moid != ""):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after elif.
Loading history...
24
        raise Exception("Inventory Error: Unable to Find Object (%s): %s"
25
                        % (vimtype, moid))
26
    #catch all error
27
    else:
28
        raise Exception("Inventory Error: No Name or moid provided (%s)"
29
                        % vimtype)
30
31
32
def get_managed_entities(content, vimtype):
33
    container = content.viewManager.CreateContainerView(
34
        content.rootFolder, [vimtype], True)
35
    return container
36
37
38
def get_datacenter(content, moid=None, name=None):
39
    return get_managed_entity(content, vim.Datacenter, moid=moid, name=name)
40
41
42
def get_cluster(content, moid=None, name=None):
43
    return get_managed_entity(content, vim.ClusterComputeResource,
44
                              moid=moid, name=name)
45
46
47
def get_folder(content, moid=None, name=None):
48
    return get_managed_entity(content, vim.Folder,
49
                              moid=moid, name=name)
50
51
52
def get_resource_pool(content, moid=None, name=None):
53
    return get_managed_entity(content, vim.ResourcePool,
54
                              moid=moid, name=name)
55
56
57
def get_datastore_cluster(content, moid=None, name=None):
58
    return get_managed_entity(content, vim.StoragePod,
59
                              moid=moid, name=name)
60
61
62
def get_datastore(content, moid=None, name=None):
63
    return get_managed_entity(content, vim.Datastore,
64
                              moid=moid, name=name)
65
66
67
def get_network(content, moid=None, name=None):
68
    return get_managed_entity(content, vim.Network,
69
                              moid=moid, name=name)
70
71
72
def get_virtualmachine(content, moid=None, name=None):
73
    return get_managed_entity(content, vim.VirtualMachine,
74
                              moid=moid, name=name)
75
76
77
def get_virtualmachines(content):
78
    return get_managed_entities(content, vim.VirtualMachine)
79
80
81
def get_task(content, moid=None):
82
    return get_managed_entity(content, vim.Task,
83
                              moid=moid, name=None)
84