Completed
Pull Request — master (#463)
by
unknown
02:24
created

NcmConfigDownload   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %
Metric Value
wmc 11
dl 0
loc 70
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 40 6
B transfer_complete() 0 27 5
1
#!/usr/bin/env python
2
3
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
4
# contributor license agreements.  See the NOTICE file distributed with
5
# this work for additional information regarding copyright ownership.
6
# The ASF licenses this file to You under the Apache License, Version 2.0
7
# (the "License"); you may not use this file except in compliance with
8
# the License.  You may obtain a copy of the License at
9
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
18
import time
19
20
from st2actions.runners.pythonrunner import Action
21
from orionsdk import SwisClient
22
23
24
class NcmConfigDownload(Action):
25
26
    def transfer_complete(self, transfer_id, swis):
27
        ts = {}
28
        while True:
29
            transfer_data = swis.query(
30
                """SELECT TransferID,
31
                Action, Status, ErrorMessage, DeviceOutput
32
                FROM NCM.TransferResults WHERE TransferID=@transfer_id""",
33
                transfer_id=transfer_id)
34
35
            status = transfer_data['results'][0]['Status']
36
37
            if status == 1:
38
                time.sleep(10)
39
            elif status == 2:
40
                ts['status'] = "Complete"
41
                break
42
            elif status == 3:
43
                ts['status'] = "Error"
44
                ts['ErrorMessage'] = transfer_data['results'][0][
45
                    'ErrorMessage']
46
                break
47
            else:
48
                ts['status'] = "Unknown"
49
                ts['ErrorMessage'] = "Invalid stauts: {}".format(status)
50
                break
51
52
        return ts
53
54
    def run(self, node, platform, configs):
55
        """
56
        Mostly completed! Download configurations via Solarwinds Orion NCM ....
57
58
        see https://github.com/solarwinds/OrionSDK/wiki/NCM-Config-Transfer
59
        """
60
61
        # Set up the results
62
        results = {}
63
64
        try:
65
            swis = SwisClient(self.config['orion'][platform]['host'],
66
                              self.config['orion'][platform]['user'],
67
                              self.config['orion'][platform]['password'])
68
        except KeyError:
69
            raise ValueError("Orion host details not in the config.yaml")
70
71
        node_data = swis.query(
72
            "SELECT NodeID FROM Cirrus.Nodes WHERE NodeCaption=@node",
73
            node=node)
74
        node_ids = []
75
76
        try:
77
            node_ids.append(node_data['results'][0]['NodeID'])
78
        except IndexError:
79
            for config in configs:
80
                results[config] = {'status': "Error",
81
                                   'ErrorMessage': "Invalid NodeID"}
82
        else:
83
            for config in configs:
84
                orion_data = swis.invoke("Cirrus.ConfigArchive",
85
                                         "DownloadConfig",
86
                                         node_ids,
87
                                         config)
88
                self.logger.info("transfer_id: " + orion_data[0])
89
                transfer_id = orion_data[0]
90
91
                results[config] = self.transfer_complete(transfer_id, swis)
92
93
        return results
94