| Conditions | 13 |
| Total Lines | 112 |
| 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 | ip_address, |
||
| 25 | platform, |
||
| 26 | poller, |
||
| 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 | ip_address, |
||
| 53 | platform) |
||
| 54 | ) |
||
| 55 | |||
| 56 | self.send_user_error("Node and/or IP is already in Orion!") |
||
| 57 | raise Exception("Node and/or IP already exists!") |
||
| 58 | else: |
||
| 59 | self.logger.info( |
||
| 60 | "Checking node ({}) is not on Orion platform: {}".format( |
||
| 61 | node, |
||
| 62 | platform) |
||
| 63 | ) |
||
| 64 | |||
| 65 | # engineID if happens to be None, default to the primary. |
||
| 66 | if poller is not None: |
||
| 67 | engineID = self.get_engine_id(poller) |
||
| 68 | else: |
||
| 69 | engineID = 1 |
||
| 70 | |||
| 71 | kargs = {'Caption': node, |
||
| 72 | 'EngineID': engineID, |
||
| 73 | 'IPAddress': ip_address |
||
| 74 | } |
||
| 75 | |||
| 76 | if mon_protocol == "snmpv2": |
||
| 77 | kargs['ObjectSubType'] = "SNMP" |
||
| 78 | kargs['SNMPVersion'] = 2 |
||
| 79 | |||
| 80 | if community is not None: |
||
| 81 | kargs['Community'] = community |
||
| 82 | elif std_community is not None: |
||
| 83 | kargs['Community'] = self.config['defaults']['snmp'][std_community] |
||
| 84 | elif std_community is None: |
||
| 85 | raise ValueError("Need one of community or std_community") |
||
| 86 | |||
| 87 | self.logger.info("Creating Orion Node: {}".format(kargs)) |
||
| 88 | orion_data = self.create('Orion.Nodes', **kargs) |
||
| 89 | |||
| 90 | node_id = re.search('(\d+)$', orion_data).group(0) |
||
| 91 | results['node_id'] = node_id |
||
| 92 | |||
| 93 | self.logger.info("Created Orion Node: {}".format(results['node_id'])) |
||
| 94 | |||
| 95 | pollers_to_add = { |
||
| 96 | 'N.Details.SNMP.Generic': True, |
||
| 97 | 'N.Uptime.SNMP.Generic': True, |
||
| 98 | 'N.Cpu.SNMP.HrProcessorLoad': True, |
||
| 99 | 'N.Memory.SNMP.NetSnmpReal': True, |
||
| 100 | 'N.AssetInventory.Snmp.Generic': True, |
||
| 101 | 'N.Topology_Layer3.SNMP.ipNetToMedia': True, |
||
| 102 | 'N.Routing.SNMP.Ipv4CidrRoutingTable': False |
||
| 103 | } |
||
| 104 | |||
| 105 | if status == 'icmp': |
||
| 106 | pollers_to_add['N.Status.ICMP.Native'] = True |
||
| 107 | pollers_to_add['N.Status.SNMP.Native'] = False |
||
| 108 | pollers_to_add['N.ResponseTime.ICMP.Native'] = True |
||
| 109 | pollers_to_add['N.ResponseTime.SNMP.Native'] = False |
||
| 110 | elif status == 'snmp': |
||
| 111 | pollers_to_add['N.Status.ICMP.Native'] = False |
||
| 112 | pollers_to_add['N.Status.SNMP.Native'] = True |
||
| 113 | pollers_to_add['N.ResponseTime.ICMP.Native'] = False |
||
| 114 | pollers_to_add['N.ResponseTime.SNMP.Native'] = True |
||
| 115 | |||
| 116 | pollers = [] |
||
| 117 | for p in pollers_to_add: |
||
| 118 | pollers.append({ |
||
| 119 | 'PollerType': p, |
||
| 120 | 'NetObject': 'N:{}'.format(node_id), |
||
| 121 | 'NetObjectType': 'N', |
||
| 122 | 'NetObjectID': node_id, |
||
| 123 | 'Enabled': pollers_to_add[p] |
||
| 124 | }) |
||
| 125 | |||
| 126 | for poller in pollers: |
||
| 127 | response = self.create('Orion.Pollers', **poller) |
||
| 128 | self.logger.info("Added {} ({}) poller: {}".format( |
||
| 129 | poller['PollerType'], |
||
| 130 | poller['Enabled'], |
||
| 131 | response)) |
||
| 132 | |||
| 133 | return results |
||
| 134 |