Completed
Push — master ( 062836...3184f1 )
by Edward
06:10 queued 02:54
created

ListNodeByPoller.run()   B

Complexity

Conditions 6

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 46
rs 7.5748
c 0
b 0
f 0
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 status_text_to_code
18
19
20
class ListNodeByPoller(OrionBaseAction):
21
    def run(self, poller="primary", status="Up", whitelist=None):
22
        """
23
        Lists the Orion Nodes (optionally by State) on an Orion Poller.
24
25
        Args:
26
        - poller: The Orion poller to list nodes on (default: primary)
27
28
        Returns:
29
        - dict: Of data from Orion.
30
31
        Raises:
32
        - UserWarning: If poller does not exist.
33
        """
34
35
        results = {'nodes': []}
36
37
        self.connect()
38
39
        engine_id = self.get_engine_id(poller)
40
41
        swql = """SELECT Caption
42
        FROM Orion.Nodes
43
        WHERE EngineID=@EngineID
44
        """
45
46
        kargs = {'EngineID': engine_id}
47
48
        if not status == "Any":
49
            status_id = status_text_to_code(status)
50
            swql += " and Status=@Status"
51
            kargs['Status'] = status_id
52
53
        orion_data = self.query(swql, **kargs)
54
55
        for node in orion_data['results']:
56
            if not whitelist:
57
                results['nodes'].append(node['Caption'])
58
            else:
59
                if node['Caption'] in whitelist:
60
                    results['nodes'].append(node['Caption'])
61
        else:
62
            results['count'] = len(results['nodes'])
63
64
        # Will check whitelist & regex here
65
66
        return results
67