Conditions | 10 |
Total Lines | 100 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 StartDiscovery.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 |
||
21 | def run(self, |
||
22 | name, |
||
23 | platform, |
||
24 | poller, |
||
25 | snmp_communities, |
||
26 | nodes=None, |
||
27 | subnets=None, |
||
28 | ip_ranges=None, |
||
29 | no_icmp_only=True, |
||
30 | auto_import=False): |
||
31 | """ |
||
32 | Create and Start Discovery process in Orion. |
||
33 | |||
34 | Returns: |
||
35 | - ProfileID that was created (or error from Orion). |
||
36 | """ |
||
37 | results = {} |
||
38 | |||
39 | # Orion must have the un-used varabiles to be certain values. |
||
40 | BulkList = None |
||
41 | IpRanges = [] |
||
42 | Subnets = None |
||
43 | |||
44 | results['platform'] = self.connect(platform) |
||
45 | |||
46 | if not only_one(nodes, subnets, ip_ranges): |
||
47 | msg = "Need only one out of nodes, ip_ranges or subnets!" |
||
48 | send_user_error(msg) |
||
49 | raise ValueError(msg) |
||
50 | |||
51 | if nodes is not None: |
||
52 | BulkList = [] |
||
53 | for node in nodes: |
||
54 | BulkList.append({'Address': node}) |
||
55 | elif ip_ranges is not None: |
||
56 | for ip_range in ip_ranges: |
||
57 | (start_ip, end_ip) = ip_range.split(':') |
||
58 | IpRanges.append({'StartAddress': start_ip, |
||
59 | 'EndAddress': end_ip}) |
||
60 | elif subnets is not None: |
||
61 | Subnets = [] |
||
62 | for subnet in subnets: |
||
63 | (SubnetIP, SubnetMask) = subnet.split('/') |
||
64 | Subnets.append({'SubnetIP': SubnetIP, |
||
65 | 'SubnetMask': SubnetMask}) |
||
66 | |||
67 | CredID_order = 1 |
||
68 | CredIDs = [] |
||
69 | for snmp in snmp_communities: |
||
70 | CredIDs.append( |
||
71 | {'CredentialID': self.get_snmp_cred_id(snmp), |
||
72 | 'Order': CredID_order} |
||
73 | ) |
||
74 | CredID_order += 1 |
||
75 | |||
76 | CorePluginConfiguration = self.invoke('Orion.Discovery', |
||
77 | 'CreateCorePluginConfiguration', |
||
78 | {'BulkList': BulkList, |
||
79 | 'IpRanges': IpRanges, |
||
80 | 'Subnets': Subnets, |
||
81 | 'Credentials': CredIDs, |
||
82 | 'WmiRetriesCount': 0, |
||
83 | 'WmiRetryIntervalMiliseconds': |
||
84 | 1000}) |
||
85 | |||
86 | # engineID if happens to be None, default to the primary (aka 1). |
||
87 | if poller is not None: |
||
88 | engineID = self.get_engine_id(poller) |
||
89 | else: |
||
90 | engineID = 1 |
||
91 | |||
92 | self.logger.info( |
||
93 | "Adding '{}' Discovery profile to Orion Platform {}".format( |
||
94 | name, platform)) |
||
95 | |||
96 | disco = self.invoke('Orion.Discovery', 'StartDiscovery', |
||
97 | { |
||
98 | 'Name': name, |
||
99 | 'EngineId': engineID, |
||
100 | 'JobTimeoutSeconds': 3600, |
||
101 | 'SearchTimeoutMiliseconds': 2000, |
||
102 | 'SnmpTimeoutMiliseconds': 2000, |
||
103 | 'SnmpRetries': 4, |
||
104 | 'RepeatIntervalMiliseconds': 1800, |
||
105 | 'SnmpPort': 161, |
||
106 | 'HopCount': 0, |
||
107 | 'PreferredSnmpVersion': 'SNMP2c', |
||
108 | 'DisableIcmp': no_icmp_only, |
||
109 | 'AllowDuplicateNodes': False, |
||
110 | 'IsAutoImport': auto_import, |
||
111 | 'IsHidden': False, |
||
112 | 'PluginConfigurations': [ |
||
113 | {'PluginConfigurationItem': |
||
114 | CorePluginConfiguration} |
||
115 | ] |
||
116 | }) |
||
117 | |||
118 | # FIX ME Check job created.... |
||
119 | |||
120 | return disco |
||
121 |