summaryrefslogtreecommitdiff
path: root/troveclient/compat/client.py
blob: b04e0086d538d45a06d7f906cbdc8637be013a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# Copyright (c) 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import httplib2
import logging
import os
import sys
import time

try:
    import json
except ImportError:
    import simplejson as json


from troveclient.compat import auth
from troveclient.compat import exceptions


LOG = logging.getLogger(__name__)
RDC_PP = os.environ.get("RDC_PP", "False") == "True"


expected_errors = (400, 401, 403, 404, 408, 409, 413, 422, 500, 501)


def log_to_streamhandler(stream=None):
    stream = stream or sys.stderr
    ch = logging.StreamHandler(stream)
    LOG.setLevel(logging.DEBUG)
    LOG.addHandler(ch)


if 'REDDWARFCLIENT_DEBUG' in os.environ and os.environ['REDDWARFCLIENT_DEBUG']:
    log_to_streamhandler()


class TroveHTTPClient(httplib2.Http):

    USER_AGENT = 'python-troveclient'

    def __init__(self, user, password, tenant, auth_url, service_name,
                 service_url=None,
                 auth_strategy=None, insecure=False,
                 timeout=None, proxy_tenant_id=None,
                 proxy_token=None, region_name=None,
                 endpoint_type='publicURL', service_type=None,
                 timings=False):

        super(TroveHTTPClient, self).__init__(timeout=timeout)

        self.username = user
        self.password = password
        self.tenant = tenant
        if auth_url:
            self.auth_url = auth_url.rstrip('/')
        else:
            self.auth_url = None
        self.region_name = region_name
        self.endpoint_type = endpoint_type
        self.service_url = service_url
        self.service_type = service_type
        self.service_name = service_name
        self.timings = timings

        self.times = []  # [("item", starttime, endtime), ...]

        self.auth_token = None
        self.proxy_token = proxy_token
        self.proxy_tenant_id = proxy_tenant_id

        # httplib2 overrides
        self.force_exception_to_status_code = True
        self.disable_ssl_certificate_validation = insecure

        auth_cls = auth.get_authenticator_cls(auth_strategy)

        self.authenticator = auth_cls(self, auth_strategy,
                                      self.auth_url, self.username,
                                      self.password, self.tenant,
                                      region=region_name,
                                      service_type=service_type,
                                      service_name=service_name,
                                      service_url=service_url)
        if hasattr(self.authenticator, 'auth'):
            self.auth = self.authenticator.auth

    def get_timings(self):
        return self.times

    def http_log(self, args, kwargs, resp, body):
        if not RDC_PP:
            self.simple_log(args, kwargs, resp, body)
        else:
            self.pretty_log(args, kwargs, resp, body)

    def simple_log(self, args, kwargs, resp, body):
        if not LOG.isEnabledFor(logging.DEBUG):
            return

        string_parts = ['curl -i']
        for element in args:
            if element in ('GET', 'POST'):
                string_parts.append(' -X %s' % element)
            else:
                string_parts.append(' %s' % element)

        for element in kwargs['headers']:
            header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
            string_parts.append(header)

        LOG.debug("REQ: %s\n", "".join(string_parts))
        if 'body' in kwargs:
            LOG.debug("REQ BODY: %s\n", kwargs['body'])
        LOG.debug("RESP:%s %s\n", resp, body)

    def pretty_log(self, args, kwargs, resp, body):
        if not LOG.isEnabledFor(logging.DEBUG):
            return

        string_parts = ['curl -i']
        for element in args:
            if element in ('GET', 'POST'):
                string_parts.append(' -X %s' % element)
            else:
                string_parts.append(' %s' % element)

        for element in kwargs['headers']:
            header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
            string_parts.append(header)

        curl_cmd = "".join(string_parts)
        LOG.debug("REQUEST:")
        if 'body' in kwargs:
            LOG.debug("%s -d '%s'", curl_cmd, kwargs['body'])
            try:
                req_body = json.dumps(json.loads(kwargs['body']),
                                      sort_keys=True, indent=4)
            except Exception:
                req_body = kwargs['body']
            LOG.debug("BODY: %s\n", req_body)
        else:
            LOG.debug(curl_cmd)

        try:
            resp_body = json.dumps(json.loads(body), sort_keys=True, indent=4)
        except Exception:
            resp_body = body
        LOG.debug("RESPONSE HEADERS: %s", resp)
        LOG.debug("RESPONSE BODY   : %s", resp_body)

    def request(self, *args, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        kwargs['headers']['User-Agent'] = self.USER_AGENT
        self.morph_request(kwargs)

        resp, body = super(TroveHTTPClient, self).request(*args, **kwargs)
        # compat between requests and httplib2
        resp.status_code = resp.status

        # Save this in case anyone wants it.
        self.last_response = (resp, body)
        self.http_log(args, kwargs, resp, body)

        if body:
            try:
                body = self.morph_response_body(body)
            except exceptions.ResponseFormatError:
                # Acceptable only if the response status is an error code.
                # Otherwise its the API or client misbehaving.
                self.raise_error_from_status(resp, None)
                raise  # Not accepted!
        else:
            body = None

        if resp.status in expected_errors:
            raise exceptions.from_response(resp, body)

        return resp, body

    def raise_error_from_status(self, resp, body):
        if resp.status in expected_errors:
            raise exceptions.from_response(resp, body)

    def morph_request(self, kwargs):
        kwargs['headers']['Accept'] = 'application/json'
        kwargs['headers']['Content-Type'] = 'application/json'
        if 'body' in kwargs:
            kwargs['body'] = json.dumps(kwargs['body'])

    def morph_response_body(self, body_string):
        try:
            return json.loads(body_string)
        except ValueError:
            raise exceptions.ResponseFormatError()

    def _time_request(self, url, method, **kwargs):
        start_time = time.time()
        resp, body = self.request(url, method, **kwargs)
        self.times.append(("%s %s" % (method, url),
                           start_time, time.time()))
        return resp, body

    def _cs_request(self, url, method, **kwargs):
        def request():
            kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token
            if self.tenant:
                kwargs['headers']['X-Auth-Project-Id'] = self.tenant

            resp, body = self._time_request(self.service_url + url, method,
                                            **kwargs)
            return resp, body

        if not self.auth_token or not self.service_url:
            self.authenticate()

        # Perform the request once. If we get a 401 back then it
        # might be because the auth token expired, so try to
        # re-authenticate and try again. If it still fails, bail.
        try:
            return request()
        except exceptions.Unauthorized:
            self.authenticate()
            return request()

    def get(self, url, **kwargs):
        return self._cs_request(url, 'GET', **kwargs)

    def patch(self, url, **kwargs):
        return self._cs_request(url, 'PATCH', **kwargs)

    def post(self, url, **kwargs):
        return self._cs_request(url, 'POST', **kwargs)

    def put(self, url, **kwargs):
        return self._cs_request(url, 'PUT', **kwargs)

    def delete(self, url, **kwargs):
        return self._cs_request(url, 'DELETE', **kwargs)

    def authenticate(self):
        """Auths the client and gets a token. May optionally set a service url.

        The client will get auth errors until the authentication step
        occurs. Additionally, if a service_url was not explicitly given in
        the clients __init__ method, one will be obtained from the auth
        service.

        """
        catalog = self.authenticator.authenticate()
        if self.service_url:
            possible_service_url = None
        else:
            if self.endpoint_type == "publicURL":
                possible_service_url = catalog.get_public_url()
            elif self.endpoint_type == "adminURL":
                possible_service_url = catalog.get_management_url()
        self.authenticate_with_token(catalog.get_token(), possible_service_url)

    def authenticate_with_token(self, token, service_url=None):
        self.auth_token = token
        if not self.service_url:
            if not service_url:
                raise exceptions.ServiceUrlNotGiven()
            else:
                self.service_url = service_url


class Dbaas(object):
    """Top-level object to access the Rackspace Database as a Service API.

    Create an instance with your creds::

        >> red = Dbaas(USERNAME, API_KEY, TENANT, AUTH_URL, SERVICE_NAME, \
                        SERVICE_URL)

    Then call methods on its managers::

        >> red.instances.list()
        ...
        >> red.flavors.list()
        ...

    &c.
    """

    def __init__(self, username, api_key, tenant=None, auth_url=None,
                 service_type='database', service_name=None,
                 service_url=None, insecure=False, auth_strategy='keystone',
                 region_name=None, client_cls=TroveHTTPClient):

        from troveclient.compat import versions
        from troveclient.v1 import accounts
        from troveclient.v1 import backups
        from troveclient.v1 import clusters
        from troveclient.v1 import configurations
        from troveclient.v1 import databases
        from troveclient.v1 import datastores
        from troveclient.v1 import diagnostics
        from troveclient.v1 import flavors
        from troveclient.v1 import hosts
        from troveclient.v1 import instances
        from troveclient.v1 import limits
        from troveclient.v1 import management
        from troveclient.v1 import metadata
        from troveclient.v1 import modules
        from troveclient.v1 import quota
        from troveclient.v1 import root
        from troveclient.v1 import security_groups
        from troveclient.v1 import storage
        from troveclient.v1 import users

        self.client = client_cls(username, api_key, tenant, auth_url,
                                 service_type=service_type,
                                 service_name=service_name,
                                 service_url=service_url,
                                 insecure=insecure,
                                 auth_strategy=auth_strategy,
                                 region_name=region_name)
        self.versions = versions.Versions(self)
        self.databases = databases.Databases(self)
        self.flavors = flavors.Flavors(self)
        self.instances = instances.Instances(self)
        self.limits = limits.Limits(self)
        self.users = users.Users(self)
        self.root = root.Root(self)
        self.hosts = hosts.Hosts(self)
        self.quota = quota.Quotas(self)
        self.backups = backups.Backups(self)
        self.clusters = clusters.Clusters(self)
        self.security_groups = security_groups.SecurityGroups(self)
        self.security_group_rules = security_groups.SecurityGroupRules(self)
        self.datastores = datastores.Datastores(self)
        self.datastore_versions = datastores.DatastoreVersions(self)
        self.datastore_version_members = (datastores.
                                          DatastoreVersionMembers(self))
        self.storage = storage.StorageInfo(self)
        self.management = management.Management(self)
        self.mgmt_cluster = management.MgmtClusters(self)
        self.mgmt_flavor = management.MgmtFlavors(self)
        self.accounts = accounts.Accounts(self)
        self.diagnostics = diagnostics.DiagnosticsInterrogator(self)
        self.hwinfo = diagnostics.HwInfoInterrogator(self)
        self.configurations = configurations.Configurations(self)
        config_parameters = configurations.ConfigurationParameters(self)
        self.configuration_parameters = config_parameters
        self.metadata = metadata.Metadata(self)
        self.modules = modules.Modules(self)
        self.mgmt_configs = management.MgmtConfigurationParameters(self)
        self.mgmt_datastore_versions = management.MgmtDatastoreVersions(self)

        class Mgmt(object):
            def __init__(self, dbaas):
                self.instances = dbaas.management
                self.hosts = dbaas.hosts
                self.accounts = dbaas.accounts
                self.storage = dbaas.storage
                self.datastore_version = dbaas.mgmt_datastore_versions

        self.mgmt = Mgmt(self)

    def set_management_url(self, url):
        self.client.management_url = url

    def get_timings(self):
        return self.client.get_timings()

    def authenticate(self):
        """Authenticate against the server.

        This is called to perform an authentication to retrieve a token.

        Returns on success; raises :exc:`exceptions.Unauthorized` if the
        credentials are wrong.
        """
        self.client.authenticate()