Completed
Pull Request — master (#2836)
by Edward
08:46
created

Client.packs()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
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
import os
17
import logging
18
19
import six
20
21
from st2client import models
22
from st2client.models.core import ResourceManager
23
from st2client.models.core import ActionAliasResourceManager
24
from st2client.models.core import LiveActionResourceManager
25
from st2client.models.core import TriggerInstanceResourceManager
26
27
28
LOG = logging.getLogger(__name__)
29
30
# Default values for the options not explicitly specified by the user
31
DEFAULT_API_PORT = 9101
32
DEFAULT_AUTH_PORT = 9100
33
34
DEFAULT_BASE_URL = 'http://127.0.0.1'
35
DEFAULT_API_VERSION = 'v1'
36
37
38
class Client(object):
39
    def __init__(self, base_url=None, auth_url=None, api_url=None, api_version=None, cacert=None,
40
                 debug=False, token=None, api_key=None):
41
        # Get CLI options. If not given, then try to get it from the environment.
42
        self.endpoints = dict()
43
44
        # Populate the endpoints
45
        if base_url:
46
            self.endpoints['base'] = base_url
47
        else:
48
            self.endpoints['base'] = os.environ.get('ST2_BASE_URL', DEFAULT_BASE_URL)
49
50
        api_version = api_version or os.environ.get('ST2_API_VERSION', DEFAULT_API_VERSION)
51
52
        if api_url:
53
            self.endpoints['api'] = api_url
54
        else:
55
            self.endpoints['api'] = os.environ.get(
56
                'ST2_API_URL', '%s:%s/%s' % (self.endpoints['base'], DEFAULT_API_PORT, api_version))
57
58
        if auth_url:
59
            self.endpoints['auth'] = auth_url
60
        else:
61
            self.endpoints['auth'] = os.environ.get(
62
                'ST2_AUTH_URL', '%s:%s' % (self.endpoints['base'], DEFAULT_AUTH_PORT))
63
64
        if cacert is not None:
65
            self.cacert = cacert
66
        else:
67
            self.cacert = os.environ.get('ST2_CACERT', None)
68
69
        # Note: boolean is also a valid value for "cacert"
70
        is_cacert_string = isinstance(self.cacert, six.string_types)
71
        if (self.cacert and is_cacert_string and not os.path.isfile(self.cacert)):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after if.
Loading history...
72
            raise ValueError('CA cert file "%s" does not exist.' % (self.cacert))
73
74
        self.debug = debug
75
76
        # Note: This is a nasty hack for now, but we need to get rid of the decrator abuse
77
        if token:
78
            os.environ['ST2_AUTH_TOKEN'] = token
79
80
        self.token = token
81
82
        if api_key:
83
            os.environ['ST2_API_KEY'] = api_key
84
85
        self.api_key = api_key
86
87
        # Instantiate resource managers and assign appropriate API endpoint.
88
        self.managers = dict()
89
        self.managers['Token'] = ResourceManager(
90
            models.Token, self.endpoints['auth'], cacert=self.cacert, debug=self.debug)
91
        self.managers['RunnerType'] = ResourceManager(
92
            models.RunnerType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
93
        self.managers['Action'] = ResourceManager(
94
            models.Action, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
95
        self.managers['ActionAlias'] = ActionAliasResourceManager(
96
            models.ActionAlias, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
97
        self.managers['ApiKey'] = ResourceManager(
98
            models.ApiKey, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
99
        self.managers['LiveAction'] = LiveActionResourceManager(
100
            models.LiveAction, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
101
        self.managers['Pack'] = ResourceManager(
102
            models.Pack, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
103
        self.managers['Policy'] = ResourceManager(
104
            models.Policy, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
105
        self.managers['PolicyType'] = ResourceManager(
106
            models.PolicyType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
107
        self.managers['Rule'] = ResourceManager(
108
            models.Rule, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
109
        self.managers['Sensor'] = ResourceManager(
110
            models.Sensor, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
111
        self.managers['TriggerType'] = ResourceManager(
112
            models.TriggerType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
113
        self.managers['Trigger'] = ResourceManager(
114
            models.Trigger, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
115
        self.managers['TriggerInstance'] = TriggerInstanceResourceManager(
116
            models.TriggerInstance, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
117
        self.managers['KeyValuePair'] = ResourceManager(
118
            models.KeyValuePair, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
119
        self.managers['Webhook'] = ResourceManager(
120
            models.Webhook, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
121
        self.managers['Trace'] = ResourceManager(
122
            models.Trace, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
123
        self.managers['RuleEnforcement'] = ResourceManager(
124
            models.RuleEnforcement, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
125
126
    @property
127
    def actions(self):
128
        return self.managers['Action']
129
130
    @property
131
    def apikeys(self):
132
        return self.managers['ApiKey']
133
134
    @property
135
    def keys(self):
136
        return self.managers['KeyValuePair']
137
138
    @property
139
    def liveactions(self):
140
        return self.managers['LiveAction']
141
142
    @property
143
    def packs(self):
144
        return self.managers['Pack']
145
146
    @property
147
    def policies(self):
148
        return self.managers['Policy']
149
150
    @property
151
    def policytypes(self):
152
        return self.managers['PolicyType']
153
154
    @property
155
    def rules(self):
156
        return self.managers['Rule']
157
158
    @property
159
    def runners(self):
160
        return self.managers['RunnerType']
161
162
    @property
163
    def sensors(self):
164
        return self.managers['Sensor']
165
166
    @property
167
    def tokens(self):
168
        return self.managers['Token']
169
170
    @property
171
    def triggertypes(self):
172
        return self.managers['TriggerType']
173
174
    @property
175
    def triggerinstances(self):
176
        return self.managers['TriggerInstance']
177
178
    @property
179
    def trace(self):
180
        return self.managers['Trace']
181
182
    @property
183
    def ruleenforcements(self):
184
        return self.managers['RuleEnforcement']
185