Completed
Pull Request — master (#466)
by
unknown
02:37
created

NodeDiscoverAndAddInterfaces.run()   C

Complexity

Conditions 7

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 48
rs 5.5
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
18
19
class NodeDiscoverAndAddInterfaces(OrionBaseAction):
20
    def run(self, node, platform, whitelist=[], blacklist=[]):
21
        """
22
        Discover and add interfaces on an Orion node
23
        """
24
        results = {'added': [], 'existing': []}
25
26
        self.connect(platform)
27
28
        NodeId = self.get_node_id(node)
29
30
        Discoverdinterfaces = self.invoke('Orion.NPM.Interfaces',
31
                                          'DiscoverInterfacesOnNode',
32
                                          NodeId)
33
34
        add_interfaces = []
35
        for interface in Discoverdinterfaces['DiscoveredInterfaces']:
36
            # Unmonitored interfaces have an InterfaceID of 0.
37
            if not interface['InterfaceID'] == 0:
38
                self.logger.info("Skipping {} as monitored (I:{})".format(
39
                    interface['Caption'],
40
                    interface['InterfaceID']))
41
                results['existing'].append(
42
                    {interface['Caption']: interface['InterfaceID']})
43
                continue
44
45
            if interface['Caption'] in blacklist:
46
                self.logger.info("Skipping {} as in blacklist".format(
47
                    interface['Caption']))
48
                continue
49
            elif interface['Caption'] in whitelist:
50
                self.logger.info("Adding {} as in whitelist".format(
51
                    interface['Caption']))
52
                add_interfaces.append(interface)
53
            elif not whitelist:
54
                add_interfaces.append(interface)
55
            else:
56
                continue
57
58
        additions = self.invoke('Orion.NPM.Interfaces',
59
                                'AddInterfacesOnNode',
60
                                NodeId,
61
                                add_interfaces,
62
                                'AddDefaultPollers')
63
64
        for i in additions['DiscoveredInterfaces']:
65
            results['added'].append({i['Caption']: i['InterfaceID']})
66
67
        return results
68