Completed
Pull Request — master (#474)
by
unknown
03:25
created

ICSPBaseActions.__init__()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
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
import requests
17
from st2actions.runners.pythonrunner import Action
18
19
20
class ICSPBaseActions(Action):
21
22
    def __init__(self, config):
23
        super(ICSPBaseActions, self).__init__(config)
24
        self.icsp_host = config['host']
25
        self.icsp_user = config['user']
26
        self.icsp_pass = config['pass']
27
        self.icsp_apiv = config['apiv']
28
        self.icsp_sslverify = config['sslverify']
29
30
    def checkResults(self, results):
31
        # Check for errorCode json element
32
        if 'errorCode' in results:
33
            raise Exception("Error: %s" % (results["recommendedActions"]))
34
35
    def setConnection(self, connection):
36
        if 'host' in connection:
37
            self.icsp_host = connection['host']
38
        if 'user' in connection:
39
            self.icsp_user = connection['user']
40
        if 'pass' in connection:
41
            self.icsp_pass = connection['pass']
42
        if 'apiv' in connection:
43
            self.icsp_apiv = connection['apiv']
44
        if 'sslverify' in connection:
45
            if connection['sslverify'].lower() == "false":
46
                self.icsp_sslverify = False
47
            else:
48
                self.icsp_sslverify = True
49
50
    def getSessionID(self):
51
        url = 'https://%s/rest/login-sessions' % self.icsp_host
52
        payload = {'userName': self.icsp_user, 'password': self.icsp_pass}
53
        headers = {'accept': 'application/json',
54
                   'accept-language': 'en-us',
55
                   'Content-Type': 'application/json'}
56
        p = requests.post(url, headers=headers,
57
                          json=payload, verify=self.icsp_sslverify)
58
        results = p.json()
59
        self.checkResults(results)
60
        self.icsp_sessionid = results["sessionID"]
0 ignored issues
show
Coding Style introduced by
The attribute icsp_sessionid was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
61
        return results["sessionID"]
62
63
    def icspGET(self, endpoint):
64
        url = 'https://%s%s' % (self.icsp_host, endpoint)
65
        headers = {'Auth': self.icsp_sessionid,
66
                   'X-Api-Version': self.icsp_apiv}
67
        p = requests.get(url, headers=headers, verify=self.icsp_sslverify)
68
        results = p.json()
69
        self.checkResults(results)
70
        return results
71
72
    def icspPUT(self, endpoint, payload):
73
        url = 'https://%s%s' % (self.icsp_host, endpoint)
74
        headers = {'Auth': self.icsp_sessionid,
75
                   'X-Api-Version': self.icsp_apiv}
76
        p = requests.put(url, headers=headers,
77
                         json=payload, verify=self.icsp_sslverify)
78
        results = p.json()
79
        self.checkResults(results)
80
        return
81
82
    def icspPOST(self, endpoint, payload):
83
        url = 'https://%s%s' % (self.icsp_host, endpoint)
84
        headers = {'Auth': self.icsp_sessionid,
85
                   'X-Api-Version': self.icsp_apiv,
86
                   'Content-type': "application/json"}
87
        p = requests.post(url, headers=headers,
88
                          data=payload, verify=self.icsp_sslverify)
89
        results = p.json()
90
        self.checkResults(results)
91
        return results
92
93
    def icspDELETE(self, endpoint):
94
        url = 'https://%s%s' % (self.icsp_host, endpoint)
95
        headers = {'Auth': self.icsp_sessionid,
96
                   'X-Api-Version': self.icsp_apiv}
97
        requests.delete(url, headers=headers, verify=self.icsp_sslverify)
98
        return
99