Conditions | 7 |
Total Lines | 59 |
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:
1 | #!/usr/bin/env python |
||
24 | def run(self, |
||
25 | node, |
||
26 | platform, |
||
27 | ip_address, |
||
28 | engineID, |
||
29 | mon_protocol, |
||
30 | std_community, |
||
31 | community, |
||
32 | status): |
||
33 | """ |
||
34 | Create an node in an Orion monitoring platform. |
||
35 | """ |
||
36 | results = {} |
||
37 | |||
38 | # Sort out which platform & poller to create the node on. |
||
39 | if platform is None: |
||
40 | try: |
||
41 | platform = self.config['defaults']['platform'] |
||
42 | except IndexError: |
||
43 | raise ValueError("No default Orion platform.") |
||
44 | |||
45 | self.logger.info("Connecting to Orion platform: {}".format(platform)) |
||
46 | self.connect(platform) |
||
47 | results['platform'] = platform |
||
48 | |||
49 | # Check node / ip is not already on platform. |
||
50 | self.logger.info( |
||
51 | "Checking node ({}) is not on Orion platform: {}".format(node, |
||
52 | platform)) |
||
53 | |||
54 | # The API allows addition of duplicate nodes, so check and raise |
||
55 | # exception if it's already monitored (by name or IP address). |
||
56 | # FIX ME - Do check of node caption here... |
||
57 | # FIX ME - Do check of ip caption here... |
||
58 | |||
59 | kargs = {'Caption': node, |
||
60 | 'EngineID': engineID, |
||
61 | 'IPAddress': ip_address |
||
62 | } |
||
63 | |||
64 | if mon_protocol == "snmpv2": |
||
65 | kargs['ObjectSubType'] = "SNMP" |
||
66 | kargs['SNMPVersion'] = 2 |
||
67 | |||
68 | if community is not None: |
||
69 | kargs['Community'] = community |
||
70 | elif std_community is not None: |
||
71 | kargs['Community'] = self.config['defaults']['snmp'][std_community] |
||
72 | elif std_community is None: |
||
73 | raise ValueError("Need one of community or std_community") |
||
74 | |||
75 | self.logger.info("Creating Orion Node: {}".format(kargs)) |
||
76 | orion_data = self.create('Orion.Nodes', **kargs) |
||
77 | |||
78 | results['node_id'] = re.search('(\d+)$', orion_data).group(0) |
||
79 | |||
80 | self.logger.info("Created Orion Node: {}".format(results['node_id'])) |
||
81 | |||
82 | return results |
||
83 |