RemediatePoolFailure.run()   F
last analyzed

Complexity

Conditions 10

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Complexity   

Complexity

Complex classes like RemediatePoolFailure.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
#! /usr/bin/python
2
3
import json
4
from st2actions.runners.pythonrunner import Action
5
from lib.vadc import Vtm
6
from lib.vadc import Bsd
7
8
9
class RemediatePoolFailure(Action):
10
11
    def run(self, errors, error_level):
12
13
        errors = json.loads(errors, encoding="utf-8")
14
        if "name" not in errors:
15
            raise Exception("Error: Instance name not provided in errors. Can not continue!")
16
17
        instance = errors["name"]
18
        vtm = Vtm(self.config, self.logger, instance)
19
        bsd = Bsd(self.config, self.logger)
20
        status = bsd.getStatus(instance)[0]
21
22
        nodes = errors["traffic_health"]["failed_nodes"]
23
        failedPools = {pool: [node["node"] for node in nodes for pool in node["pools"]]
24
            for node in nodes for pool in node["pools"]}
25
26
        for pool in failedPools.keys():
27
            nodes = vtm.getPoolNodes(pool)
28
            if set(nodes["active"]).issubset(failedPools[pool]):
29
                self.logger.debug("Pool Dead")
30
                for vs in status["traffic_health"]["virtual_servers"]:
31
                    if vs["pool"] == pool:
32
                        self.logger.debug("Putting VS: {} into maintenance.".format(vs["name"]))
33
                        vtm.enableMaintenance(vs["name"], "maintenance")
34
            else:
35
                self.logger.debug("Pool not dead")
36