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