Completed
Pull Request — master (#487)
by
unknown
02:57
created

StartDiscovery   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 101
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 100 10
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from lib.actions import OrionBaseAction
17
from lib.utils import send_user_error, only_one
18
19
20
class StartDiscovery(OrionBaseAction):
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