1
# Copyright 2010 Jacob Kaplan-Moss
2
# Copyright 2011 OpenStack Foundation
3
# Copyright 2011 Piston Cloud Computing, Inc.
4
# Copyright 2013 Alessio Ababilov
5
# Copyright 2013 Grid Dynamics
6
# Copyright 2013 OpenStack Foundation
9
# Licensed under the Apache License, Version 2.0 (the "License"); you may
10
# not use this file except in compliance with the License. You may obtain
11
# a copy of the License at
13
# http://www.apache.org/licenses/LICENSE-2.0
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
# License for the specific language governing permissions and limitations
22
OpenStack Client interface. Handles the REST calls and responses.
25
# E0202: An attribute inherited from %s hide this method
26
# pylint: disable=E0202
32
import simplejson as json
38
from ironic.openstack.common.apiclient import exceptions
39
from ironic.openstack.common.gettextutils import _
40
from ironic.openstack.common import importutils
43
_logger = logging.getLogger(__name__)
46
class HTTPClient(object):
47
"""This client handles sending HTTP requests to OpenStack servers.
51
- share authentication information between several clients to different
52
services (e.g., for compute and image clients);
53
- reissue authentication request for expired tokens;
54
- encode/decode JSON bodies;
55
- raise exceptions on HTTP errors;
56
- pluggable authentication;
57
- store authentication information in a keyring;
58
- store time spent for requests;
59
- register clients for particular services, so one can use
60
`http_client.identity` or `http_client.compute`;
61
- log requests and responses in a format that is easy to copy-and-paste
62
into terminal and send the same request with curl.
65
user_agent = "ironic.openstack.common.apiclient"
70
endpoint_type="publicURL",
80
self.auth_plugin = auth_plugin
82
self.endpoint_type = endpoint_type
83
self.region_name = region_name
85
self.original_ip = original_ip
86
self.timeout = timeout
90
self.keyring_saver = keyring_saver
92
self.user_agent = user_agent or self.user_agent
94
self.times = [] # [("item", starttime, endtime), ...]
95
self.timings = timings
97
# requests within the same session can reuse TCP connections from pool
98
self.http = http or requests.Session()
100
self.cached_token = None
102
def _http_log_req(self, method, url, kwargs):
112
for element in kwargs['headers']:
113
header = "-H '%s: %s'" % (element, kwargs['headers'][element])
114
string_parts.append(header)
116
_logger.debug("REQ: %s" % " ".join(string_parts))
118
_logger.debug("REQ BODY: %s\n" % (kwargs['data']))
120
def _http_log_resp(self, resp):
127
if resp._content_consumed:
132
def serialize(self, kwargs):
133
if kwargs.get('json') is not None:
134
kwargs['headers']['Content-Type'] = 'application/json'
135
kwargs['data'] = json.dumps(kwargs['json'])
141
def get_timings(self):
144
def reset_timings(self):
147
def request(self, method, url, **kwargs):
148
"""Send an http request with the specified characteristics.
150
Wrapper around `requests.Session.request` to handle tasks such as
151
setting headers, JSON encoding/decoding, and error handling.
153
:param method: method of HTTP request
154
:param url: URL of HTTP request
155
:param kwargs: any other parameter that can be passed to
156
requests.Session.request (such as `headers`) or `json`
157
that will be encoded as JSON and used as `data` argument
159
kwargs.setdefault("headers", kwargs.get("headers", {}))
160
kwargs["headers"]["User-Agent"] = self.user_agent
162
kwargs["headers"]["Forwarded"] = "for=%s;by=%s" % (
163
self.original_ip, self.user_agent)
164
if self.timeout is not None:
165
kwargs.setdefault("timeout", self.timeout)
166
kwargs.setdefault("verify", self.verify)
167
if self.cert is not None:
168
kwargs.setdefault("cert", self.cert)
169
self.serialize(kwargs)
171
self._http_log_req(method, url, kwargs)
173
start_time = time.time()
174
resp = self.http.request(method, url, **kwargs)
176
self.times.append(("%s %s" % (method, url),
177
start_time, time.time()))
178
self._http_log_resp(resp)
180
if resp.status_code >= 400:
182
"Request returned failure status: %s",
184
raise exceptions.from_response(resp, method, url)
189
def concat_url(endpoint, url):
190
"""Concatenate endpoint and final URL.
192
E.g., "http://keystone/v2.0/" and "/tokens" are concatenated to
193
"http://keystone/v2.0/tokens".
195
:param endpoint: the base URL
196
:param url: the final URL
198
return "%s/%s" % (endpoint.rstrip("/"), url.strip("/"))
200
def client_request(self, client, method, url, **kwargs):
201
"""Send an http request using `client`'s endpoint and specified `url`.
203
If request was rejected as unauthorized (possibly because the token is
204
expired), issue one authorization attempt and send the request once
207
:param client: instance of BaseClient descendant
208
:param method: method of HTTP request
209
:param url: URL of HTTP request
210
:param kwargs: any other parameter that can be passed to
215
"endpoint_type": client.endpoint_type or self.endpoint_type,
216
"service_type": client.service_type,
218
token, endpoint = (self.cached_token, client.cached_endpoint)
219
just_authenticated = False
220
if not (token and endpoint):
222
token, endpoint = self.auth_plugin.token_and_endpoint(
224
except exceptions.EndpointException:
226
if not (token and endpoint):
228
just_authenticated = True
229
token, endpoint = self.auth_plugin.token_and_endpoint(
231
if not (token and endpoint):
232
raise exceptions.AuthorizationFailure(
233
_("Cannot find endpoint or token for request"))
235
old_token_endpoint = (token, endpoint)
236
kwargs.setdefault("headers", {})["X-Auth-Token"] = token
237
self.cached_token = token
238
client.cached_endpoint = endpoint
239
# Perform the request once. If we get Unauthorized, then it
240
# might be because the auth token expired, so try to
241
# re-authenticate and try again. If it still fails, bail.
244
method, self.concat_url(endpoint, url), **kwargs)
245
except exceptions.Unauthorized as unauth_ex:
246
if just_authenticated:
248
self.cached_token = None
249
client.cached_endpoint = None
252
token, endpoint = self.auth_plugin.token_and_endpoint(
254
except exceptions.EndpointException:
256
if (not (token and endpoint) or
257
old_token_endpoint == (token, endpoint)):
259
self.cached_token = token
260
client.cached_endpoint = endpoint
261
kwargs["headers"]["X-Auth-Token"] = token
263
method, self.concat_url(endpoint, url), **kwargs)
265
def add_client(self, base_client_instance):
266
"""Add a new instance of :class:`BaseClient` descendant.
268
`self` will store a reference to `base_client_instance`.
272
>>> def test_clients():
273
... from keystoneclient.auth import keystone
274
... from openstack.common.apiclient import client
275
... auth = keystone.KeystoneAuthPlugin(
276
... username="user", password="pass", tenant_name="tenant",
277
... auth_url="http://auth:5000/v2.0")
278
... openstack_client = client.HTTPClient(auth)
279
... # create nova client
280
... from novaclient.v1_1 import client
281
... client.Client(openstack_client)
282
... # create keystone client
283
... from keystoneclient.v2_0 import client
284
... client.Client(openstack_client)
286
... openstack_client.identity.tenants.list()
287
... openstack_client.compute.servers.list()
289
service_type = base_client_instance.service_type
290
if service_type and not hasattr(self, service_type):
291
setattr(self, service_type, base_client_instance)
293
def authenticate(self):
294
self.auth_plugin.authenticate(self)
295
# Store the authentication results in the keyring for later requests
296
if self.keyring_saver:
297
self.keyring_saver.save(self)
300
class BaseClient(object):
301
"""Top-level object to access the OpenStack API.
303
This client uses :class:`HTTPClient` to send requests. :class:`HTTPClient`
304
will handle a bunch of issues such as authentication.
308
endpoint_type = None # "publicURL" will be used
309
cached_endpoint = None
311
def __init__(self, http_client, extensions=None):
312
self.http_client = http_client
313
http_client.add_client(self)
315
# Add in any extensions...
317
for extension in extensions:
318
if extension.manager_class:
319
setattr(self, extension.name,
320
extension.manager_class(self))
322
def client_request(self, method, url, **kwargs):
323
return self.http_client.client_request(
324
self, method, url, **kwargs)
326
def head(self, url, **kwargs):
327
return self.client_request("HEAD", url, **kwargs)
329
def get(self, url, **kwargs):
330
return self.client_request("GET", url, **kwargs)
332
def post(self, url, **kwargs):
333
return self.client_request("POST", url, **kwargs)
335
def put(self, url, **kwargs):
336
return self.client_request("PUT", url, **kwargs)
338
def delete(self, url, **kwargs):
339
return self.client_request("DELETE", url, **kwargs)
341
def patch(self, url, **kwargs):
342
return self.client_request("PATCH", url, **kwargs)
345
def get_class(api_name, version, version_map):
346
"""Returns the client class for the requested API version
348
:param api_name: the name of the API, e.g. 'compute', 'image', etc
349
:param version: the requested API version
350
:param version_map: a dict of client classes keyed by version
351
:rtype: a client class for the requested API version
354
client_path = version_map[str(version)]
355
except (KeyError, ValueError):
356
msg = _("Invalid %(api_name)s client version '%(version)s'. "
357
"Must be one of: %(version_map)s") % {
358
'api_name': api_name,
360
'version_map': ', '.join(version_map.keys())
362
raise exceptions.UnsupportedVersion(msg)
364
return importutils.import_class(client_path)