Conditions | 12 |
Total Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like NodeCreate.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 |
||
22 | def run(self, |
||
23 | node, |
||
24 | platform, |
||
25 | ip_address, |
||
26 | engineID, |
||
27 | mon_protocol, |
||
28 | std_community, |
||
29 | community, |
||
30 | status): |
||
31 | """ |
||
32 | Create an node in an Orion monitoring platform. |
||
33 | """ |
||
34 | results = {} |
||
35 | |||
36 | # Sort out which platform & poller to create the node on. |
||
37 | if platform is None: |
||
38 | try: |
||
39 | platform = self.config['defaults']['platform'] |
||
40 | except IndexError: |
||
41 | self.send_user_error("No default Orion platform.") |
||
42 | raise ValueError("No default Orion platform.") |
||
43 | |||
44 | self.logger.info("Connecting to Orion platform: {}".format(platform)) |
||
45 | self.connect(platform) |
||
46 | results['platform'] = platform |
||
47 | |||
48 | if self.node_exists(node, ip_address): |
||
49 | self.logger.error( |
||
50 | "Node ({}) or IP ({}) already in Orion platform: {}".format( |
||
51 | node, |
||
52 | platform) |
||
53 | ) |
||
54 | |||
55 | self.send_user_error("Node and/or IP is already in Orion!") |
||
56 | raise Exception("Node and/or IP already exists!") |
||
57 | else: |
||
58 | self.logger.info( |
||
59 | "Checking node ({}) is not on Orion platform: {}".format( |
||
60 | node, |
||
61 | platform) |
||
62 | ) |
||
63 | |||
64 | kargs = {'Caption': node, |
||
65 | 'EngineID': engineID, |
||
66 | 'IPAddress': ip_address |
||
67 | } |
||
68 | |||
69 | if mon_protocol == "snmpv2": |
||
70 | kargs['ObjectSubType'] = "SNMP" |
||
71 | kargs['SNMPVersion'] = 2 |
||
72 | |||
73 | if community is not None: |
||
74 | kargs['Community'] = community |
||
75 | elif std_community is not None: |
||
76 | kargs['Community'] = self.config['defaults']['snmp'][std_community] |
||
77 | elif std_community is None: |
||
78 | raise ValueError("Need one of community or std_community") |
||
79 | |||
80 | self.logger.info("Creating Orion Node: {}".format(kargs)) |
||
81 | orion_data = self.create('Orion.Nodes', **kargs) |
||
82 | |||
83 | node_id = re.search('(\d+)$', orion_data).group(0) |
||
84 | results['node_id'] = node_id |
||
85 | |||
86 | self.logger.info("Created Orion Node: {}".format(results['node_id'])) |
||
87 | |||
88 | pollers_to_add = { |
||
89 | 'N.Details.SNMP.Generic': True, |
||
90 | 'N.Uptime.SNMP.Generic': True, |
||
91 | 'N.Cpu.SNMP.HrProcessorLoad': True, |
||
92 | 'N.Memory.SNMP.NetSnmpReal': True, |
||
93 | 'N.AssetInventory.Snmp.Generic': True, |
||
94 | 'N.Topology_Layer3.SNMP.ipNetToMedia': True, |
||
95 | 'N.Routing.SNMP.Ipv4CidrRoutingTable': False |
||
96 | } |
||
97 | |||
98 | if status == 'icmp': |
||
99 | pollers_to_add['N.Status.ICMP.Native'] = True |
||
100 | pollers_to_add['N.Status.SNMP.Native'] = False |
||
101 | pollers_to_add['N.ResponseTime.ICMP.Native'] = True |
||
102 | pollers_to_add['N.ResponseTime.SNMP.Native'] = False |
||
103 | elif status == 'snmp': |
||
104 | pollers_to_add['N.Status.ICMP.Native'] = False |
||
105 | pollers_to_add['N.Status.SNMP.Native'] = True |
||
106 | pollers_to_add['N.ResponseTime.ICMP.Native'] = False |
||
107 | pollers_to_add['N.ResponseTime.SNMP.Native'] = True |
||
108 | |||
109 | pollers = [] |
||
110 | for p in pollers_to_add: |
||
111 | pollers.append({ |
||
112 | 'PollerType': p, |
||
113 | 'NetObject': 'N:{}'.format(node_id), |
||
114 | 'NetObjectType': 'N', |
||
115 | 'NetObjectID': node_id, |
||
116 | 'Enabled': pollers_to_add[p] |
||
117 | }) |
||
118 | |||
119 | for poller in pollers: |
||
120 | response = self.create('Orion.Pollers', **poller) |
||
121 | self.logger.info("Added {} ({}) poller: {}".format( |
||
122 | poller['PollerType'], |
||
123 | poller['Enabled'], |
||
124 | response)) |
||
125 | |||
126 | return results |
||
127 |