~ubuntu-branches/ubuntu/vivid/python-heatclient/vivid

« back to all changes in this revision

Viewing changes to heatclient/common/http.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-03-12 10:43:34 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20150312104334-8e2j6r81g24r0h3y
Tags: 0.3.0-0ubuntu1
* New upstream release
* debian/control: Adjust dependencies. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import six
24
24
from six.moves.urllib import parse
25
25
 
 
26
from oslo.serialization import jsonutils
 
27
from oslo.utils import encodeutils
 
28
from oslo.utils import importutils
 
29
 
26
30
from heatclient import exc
27
 
from heatclient.openstack.common import importutils
28
 
from heatclient.openstack.common import jsonutils
29
 
from heatclient.openstack.common import strutils
 
31
from heatclient.openstack.common._i18n import _
 
32
from heatclient.openstack.common._i18n import _LE
 
33
from heatclient.openstack.common._i18n import _LW
30
34
 
31
35
LOG = logging.getLogger(__name__)
32
36
USER_AGENT = 'python-heatclient'
50
54
        if os.path.exists(ca):
51
55
            LOG.debug("Using ca file %s", ca)
52
56
            return ca
53
 
    LOG.warn("System ca file could not be found.")
 
57
    LOG.warn(_LW("System ca file could not be found."))
54
58
 
55
59
 
56
60
class HTTPClient(object):
83
87
            else:
84
88
                self.verify_cert = kwargs.get('ca_file', get_system_ca_file())
85
89
 
 
90
        # FIXME(shardy): We need this for compatibility with the oslo apiclient
 
91
        # we should move to inheriting this class from the oslo HTTPClient
 
92
        self.last_request_id = None
 
93
 
86
94
    def safe_header(self, name, value):
87
95
        if name in SENSITIVE_HEADERS:
88
96
            # because in python3 byte string handling is ... ug
89
97
            v = value.encode('utf-8')
90
98
            h = hashlib.sha1(v)
91
99
            d = h.hexdigest()
92
 
            return strutils.safe_decode(name), "{SHA1}%s" % d
 
100
            return encodeutils.safe_decode(name), "{SHA1}%s" % d
93
101
        else:
94
 
            return strutils.safe_decode(name), strutils.safe_decode(value)
 
102
            return (encodeutils.safe_decode(name),
 
103
                    encodeutils.safe_decode(value))
95
104
 
96
105
    def log_curl_request(self, method, url, kwargs):
97
 
        curl = ['curl -i -X %s' % method]
 
106
        curl = ['curl -g -i -X %s' % method]
98
107
 
99
108
        for (key, value) in kwargs['headers'].items():
100
109
            header = '-H \'%s: %s\'' % self.safe_header(key, value)
186
195
                allow_redirects=allow_redirects,
187
196
                **kwargs)
188
197
        except socket.gaierror as e:
189
 
            message = ("Error finding address for %(url)s: %(e)s" %
 
198
            message = (_("Error finding address for %(url)s: %(e)s") %
190
199
                       {'url': self.endpoint_url + url, 'e': e})
191
200
            raise exc.InvalidEndpoint(message=message)
192
201
        except (socket.error, socket.timeout) as e:
193
202
            endpoint = self.endpoint
194
 
            message = ("Error communicating with %(endpoint)s %(e)s" %
 
203
            message = (_("Error communicating with %(endpoint)s %(e)s") %
195
204
                       {'endpoint': endpoint, 'e': e})
196
205
            raise exc.CommunicationError(message=message)
197
206
 
200
209
        if not 'X-Auth-Key' in kwargs['headers'] and \
201
210
                (resp.status_code == 401 or
202
211
                 (resp.status_code == 500 and "(HTTP 401)" in resp.content)):
203
 
            raise exc.HTTPUnauthorized("Authentication failed. Please try"
204
 
                                       " again with option "
205
 
                                       "--include-password or export "
206
 
                                       "HEAT_INCLUDE_PASSWORD=1\n%s"
207
 
                                       % resp.content)
 
212
            raise exc.HTTPUnauthorized(_("Authentication failed. Please try"
 
213
                                         " again with option %(option)s or "
 
214
                                         "export %(var)s\n%(content)s") %
 
215
                                       {
 
216
                                           'option': '--include-password',
 
217
                                           'var': 'HEAT_INCLUDE_PASSWORD=1',
 
218
                                           'content': resp.content
 
219
                                       })
208
220
        elif 400 <= resp.status_code < 600:
209
221
            raise exc.from_response(resp)
210
222
        elif resp.status_code in (301, 302, 305):
221
233
 
222
234
    def strip_endpoint(self, location):
223
235
        if location is None:
224
 
            message = "Location not returned with 302"
 
236
            message = _("Location not returned with 302")
225
237
            raise exc.InvalidEndpoint(message=message)
226
238
        elif location.lower().startswith(self.endpoint.lower()):
227
239
            return location[len(self.endpoint):]
228
240
        else:
229
 
            message = "Prohibited endpoint redirect %s" % location
 
241
            message = _("Prohibited endpoint redirect %s") % location
230
242
            raise exc.InvalidEndpoint(message=message)
231
243
 
232
244
    def credentials_headers(self):
257
269
            try:
258
270
                body = resp.json()
259
271
            except ValueError:
260
 
                LOG.error('Could not decode response body as JSON')
 
272
                LOG.error(_LE('Could not decode response body as JSON'))
261
273
        else:
262
274
            body = None
263
275
 
337
349
        # just return the redirect response.  Useful for using stacks:lookup.
338
350
        follow_redirects = kwargs.pop('follow_redirects', True)
339
351
 
 
352
        # If the endpoint is passed in, make sure keystone uses it
 
353
        # instead of looking up the endpoint in the auth plugin.
 
354
        if self.endpoint:
 
355
            kwargs['endpoint_override'] = self.endpoint
 
356
 
340
357
        resp = self.session.request(url, method, redirect=follow_redirects,
341
358
                                    raise_exc=False, **kwargs)
342
359