Completed
Pull Request — master (#515)
by
unknown
03:08
created

ListNodeByPoller.run()   B

Complexity

Conditions 6

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
dl 0
loc 47
rs 7.6129
c 1
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, platform, 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
        - platform: The orion platform to act on.
28
29
        Returns:
30
        - dict: Of data from Orion.
31
32
        Raises:
33
        - UserWarning: If poller does not exist.
34
        """
35
36
        results = {'nodes': []}
37
38
        self.connect(platform)
39
40
        engine_id = self.get_engine_id(poller)
41
42
        swql = """SELECT Caption
43
        FROM Orion.Nodes
44
        WHERE EngineID=@EngineID
45
        """
46
47
        kargs = {'EngineID': engine_id}
48
49
        if not status == "Any":
50
            status_id = status_text_to_code(status)
51
            swql += " and Status=@Status"
52
            kargs['Status'] = status_id
53
54
        orion_data = self.query(swql, **kargs)
55
56
        for node in orion_data['results']:
57
            if not whitelist:
58
                results['nodes'].append(node['Caption'])
59
            else:
60
                if node['Caption'] in whitelist:
61
                    results['nodes'].append(node['Caption'])
62
        else:
63
            results['count'] = len(results['nodes'])
64
65
        # Will check whitelist & regex here
66
67
        return results
68