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

NcmConfigDownload   A

Complexity

Total Complexity 11

Size/Duplication

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 40 6
B transfer_complete() 0 28 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
            #self.logger.info(transfer_data)
36
            status = transfer_data['results'][0]['Status']
37
38
            if status == 1:
39
                time.sleep(10)
40
            elif status == 2:
41
                ts['status'] = "Complete"
42
                break
43
            elif status == 3:
44
                ts['status'] = "Error"
45
                ts['ErrorMessage'] = transfer_data['results']
46
                [0]['ErrorMessage']
47
                break
48
            else:
49
                ts['status'] = "Unknown"
50
                ts['ErrorMessage'] = "Invalid stauts: {}".format(status)
51
                break
52
53
        return ts
54
55
    def run(self, node, platform, configs):
56
        """
57
        Mostly completed! Download configurations via Solarwinds Orion NCM ....
58
59
        see https://github.com/solarwinds/OrionSDK/wiki/NCM-Config-Transfer
60
        """
61
62
        # Set up the results
63
        results = {}
64
65
        try:
66
            swis = SwisClient(self.config['orion'][platform]['host'],
67
                              self.config['orion'][platform]['user'],
68
                              self.config['orion'][platform]['password'])
69
        except KeyError:
70
            raise ValueError("Orion host details not in the config.yaml")
71
72
        node_data = swis.query(
73
            "SELECT NodeID FROM Cirrus.Nodes WHERE NodeCaption=@node",
74
            node=node)
75
        node_ids = []
76
77
        try:
78
            node_ids.append(node_data['results'][0]['NodeID'])
79
        except IndexError:
80
            for config in configs:
81
                results[config] = {'status': "Error",
82
                                   'ErrorMessage': "Invalid NodeID"}
83
        else:
84
            for config in configs:
85
                orion_data = swis.invoke("Cirrus.ConfigArchive",
86
                                         "DownloadConfig",
87
                                         node_ids,
88
                                         config)
89
                self.logger.info("transfer_id: " + orion_data[0])
90
                transfer_id = orion_data[0]
91
92
                results[config] = self.transfer_complete(transfer_id, swis)
93
94
        return results
95