ListNodesStatus.run()   F
last analyzed

Complexity

Conditions 10

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 37
rs 3.1304
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ListNodesStatus.run() 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 lib.actions import OrionBaseAction
17
18
19
class ListNodesStatus(OrionBaseAction):
20
    def run(self, whitelist=None, fail_on_unknowns=False):
21
        """
22
        List the Status of Solarwinds Orion Nodes.
23
        """
24
25
        # Set up the results
26
        results = {"nodes_down": [],
27
                   "nodes_unknown": [],
28
                   "nodes_up": []}
29
30
        self.connect()
31
32
        swql = "SELECT Caption,Status FROM Orion.Nodes"
33
        kargs = {}
34
        orion_data = self.query(swql, **kargs)
35
36
        for node in orion_data['results']:
37
            if whitelist is not None:
38
                if not node['Caption'] in whitelist:
39
                    continue
40
41
            # Duplicate names will cause issues, so skip
42
            if orion_data['results'].count(node) > 1:
43
                continue
44
45
            if node["Status"] == 1:
46
                results['nodes_up'].append(node['Caption'])
47
            elif node["Status"] == 2:
48
                results['nodes_down'].append(node['Caption'])
49
            elif node["Status"] == 0:
50
                results['nodes_unknown'].append(node['Caption'])
51
52
        if fail_on_unknowns:
53
            if len(results['nodes_unknown']) > 0:
54
                raise Exception("There are nodes with an Unknown status!")
55
56
        return results
57