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