Conditions | 10 |
Total Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
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 |