| 1 |
# Copyright (c) 2013 Rackspace, Inc. |
| 2 |
# Copyright (c) 2013 Red Hat, Inc. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# 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 |
| 13 |
# implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
import json |
| 18 |
from stevedore import driver |
| 19 |
|
| 20 |
from zaqarclient import auth |
| 21 |
from zaqarclient import errors |
| 22 |
|
| 23 |
|
| 24 |
def prepare_request(auth_opts=None, data=None, **kwargs): |
| 25 |
"""Prepares a request |
| 26 |
|
| 27 |
This method takes care of authentication |
| 28 |
and all other steps in the preparation chain. |
| 29 |
|
| 30 |
The request returned by this call is ready to |
| 31 |
be sent to the server. |
| 32 |
|
| 33 |
:param auth_opts: Auth parameters |
| 34 |
:type auth_opts: `dict` |
| 35 |
:param data: Optional data to send along with the |
| 36 |
request. If data is not None, it'll be serialized. |
| 37 |
:type data: Any primitive type that is json-serializable. |
| 38 |
:param kwargs: Anything accepted by `Request` |
| 39 |
|
| 40 |
:returns: A `Request` instance ready to be sent. |
| 41 |
:rtype: `Request` |
| 42 |
""" |
| 43 |
|
| 44 |
req = Request(**kwargs) |
| 45 |
auth_backend = auth.get_backend(**(auth_opts or {})) |
| 46 |
req = auth_backend.authenticate(kwargs.get('api'), req) |
| 47 |
|
| 48 |
option = auth_opts.get('options', {}) |
| 49 |
# TODO(wangxiyuan): To keep backwards compatibility, we leave |
| 50 |
# "os_project_id" here. Remove it in the next release. |
| 51 |
project_id = option.get('os_project_id', option.get('project_id')) |
| 52 |
|
| 53 |
# Let's add project id header, only if it will have non-empty value. |
| 54 |
if project_id: |
| 55 |
req.headers['X-Project-Id'] = project_id |
| 56 |
|
| 57 |
# In case of noauth backend and no specified project id, the default |
| 58 |
# project id will be added as header. |
| 59 |
if ('X-Project-Id' not in req.headers and |
| 60 |
auth_opts.get("backend") == "noauth"): |
| 61 |
req.headers['X-Project-Id'] = "fake_project_id_for_noauth" |
| 62 |
|
| 63 |
if data is not None: |
| 64 |
req.content = json.dumps(data) |
| 65 |
return req |
| 66 |
|
| 67 |
|
| 68 |
class Request: |
| 69 |
"""General data for a Zaqar request |
| 70 |
|
| 71 |
The idea is to be declarative i.e. specify *what* is desired. It's up to |
| 72 |
the respective transport to turn this into a layer-specific request. |
| 73 |
|
| 74 |
*NOTE:* This implementation is not definitive and may change. |
| 75 |
|
| 76 |
:param endpoint: Server's endpoint |
| 77 |
:type endpoint: str |
| 78 |
:param operation: Operation to issue on the endpoint, i.e: |
| 79 |
- get_queues |
| 80 |
- get_messages |
| 81 |
:type operation: str |
| 82 |
:param content: Request's body. Default: None |
| 83 |
:type content: str |
| 84 |
:param params: Query string params. Default: None |
| 85 |
:type params: dict |
| 86 |
:param headers: Request headers. Default: None |
| 87 |
:type headers: dict |
| 88 |
:param api: Api entry point. i.e: 'queues.v2' |
| 89 |
:type api: str. |
| 90 |
:param verify: If verify the SSL cert |
| 91 |
:type verify: bool |
| 92 |
:param cert: certificate of SSL |
| 93 |
:type cert: str |
| 94 |
:param session: Keystone session |
| 95 |
:type session: keystone session object |
| 96 |
""" |
| 97 |
|
| 98 |
def __init__(self, endpoint='', operation='', |
| 99 |
ref='', content=None, params=None, |
| 100 |
headers=None, api=None, verify=True, cert=None, session=None): |
| 101 |
|
| 102 |
self._api = None |
| 103 |
# ensure that some values like "v2.0" could work as "v2" |
| 104 |
self._api_mod = None |
| 105 |
if api and int(api) == api: |
| 106 |
self._api_mod = 'queues.v' + str(int(api)) |
| 107 |
elif api: |
| 108 |
self._api_mod = 'queues.v' + str(api) |
| 109 |
|
| 110 |
self.endpoint = endpoint |
| 111 |
self.operation = operation |
| 112 |
self.ref = ref |
| 113 |
self.content = content |
| 114 |
self.params = params or {} |
| 115 |
self.headers = headers or {} |
| 116 |
self.verify = verify |
| 117 |
self.cert = cert |
| 118 |
self.session = session |
| 119 |
|
| 120 |
@property |
| 121 |
def api(self): |
| 122 |
if not self._api and self._api_mod: |
| 123 |
try: |
| 124 |
namespace = 'zaqarclient.api' |
| 125 |
mgr = driver.DriverManager(namespace, |
| 126 |
self._api_mod, |
| 127 |
invoke_on_load=True) |
| 128 |
self._api = mgr.driver |
| 129 |
except RuntimeError as ex: |
| 130 |
raise errors.DriverLoadFailure(self._api_mod, ex) |
| 131 |
return self._api |
| 132 |
|
| 133 |
def validate(self): |
| 134 |
"""Validation shortcut |
| 135 |
|
| 136 |
Refer to `transport.api.Api.validate` for |
| 137 |
more information about this method. |
| 138 |
""" |
| 139 |
return self.api.validate(params=self.params, |
| 140 |
content=self.content) |