| 1 |
# Copyright (c) 2013 Red Hat, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
from oslo_utils import uuidutils |
| 17 |
from zaqarclient.queues.v2 import core |
| 18 |
from zaqarclient.queues.v2 import flavor |
| 19 |
from zaqarclient.queues.v2 import iterator |
| 20 |
from zaqarclient.queues.v2 import pool |
| 21 |
from zaqarclient.queues.v2 import queues |
| 22 |
from zaqarclient.queues.v2 import subscription |
| 23 |
from zaqarclient import transport |
| 24 |
from zaqarclient.transport import request |
| 25 |
|
| 26 |
|
| 27 |
class Client: |
| 28 |
"""Client base class |
| 29 |
|
| 30 |
:param url: Zaqar's instance base url. |
| 31 |
:type url: str |
| 32 |
:param version: API Version pointing to. |
| 33 |
:type version: `int` |
| 34 |
:param conf: CONF object. |
| 35 |
:type conf: `oslo_config.cfg.CONF` |
| 36 |
""" |
| 37 |
|
| 38 |
queues_module = queues |
| 39 |
|
| 40 |
def __init__(self, url=None, version=2, conf=None, session=None): |
| 41 |
self.conf = conf or {} |
| 42 |
|
| 43 |
self.api_url = url |
| 44 |
self.api_version = version |
| 45 |
self.auth_opts = self.conf.get('auth_opts', {}) |
| 46 |
self.client_uuid = self.conf.get('client_uuid', |
| 47 |
uuidutils.generate_uuid(dashed=False)) |
| 48 |
self.session = session |
| 49 |
|
| 50 |
def _get_transport(self, request): |
| 51 |
"""Gets a transport and caches its instance |
| 52 |
|
| 53 |
This method gets a transport instance based on |
| 54 |
the request's endpoint and caches that for later |
| 55 |
use. The transport instance is invalidated whenever |
| 56 |
a session expires. |
| 57 |
|
| 58 |
:param request: The request to use to load the |
| 59 |
transport instance. |
| 60 |
:type request: :class:`zaqarclient.transport.request.Request` |
| 61 |
""" |
| 62 |
|
| 63 |
return transport.get_transport_for(request, |
| 64 |
version=self.api_version, |
| 65 |
options=self.conf) |
| 66 |
|
| 67 |
def _request_and_transport(self): |
| 68 |
req = request.prepare_request(self.auth_opts, |
| 69 |
endpoint=self.api_url, |
| 70 |
api=self.api_version, |
| 71 |
session=self.session) |
| 72 |
|
| 73 |
req.headers['Client-ID'] = self.client_uuid |
| 74 |
|
| 75 |
trans = self._get_transport(req) |
| 76 |
return req, trans |
| 77 |
|
| 78 |
def transport(self): |
| 79 |
"""Gets a transport based the api url and version. |
| 80 |
|
| 81 |
:rtype: :class:`zaqarclient.transport.base.Transport` |
| 82 |
""" |
| 83 |
return transport.get_transport_for(self.api_url, |
| 84 |
version=self.api_version) |
| 85 |
|
| 86 |
def queue(self, ref, **kwargs): |
| 87 |
"""Returns a queue instance |
| 88 |
|
| 89 |
:param ref: Queue's reference id. |
| 90 |
:type ref: str |
| 91 |
|
| 92 |
:returns: A queue instance |
| 93 |
:rtype: `queues.Queue` |
| 94 |
""" |
| 95 |
return queues.Queue(self, ref, **kwargs) |
| 96 |
|
| 97 |
def queues(self, **params): |
| 98 |
"""Gets a list of queues from the server |
| 99 |
|
| 100 |
:returns: A list of queues |
| 101 |
:rtype: `list` |
| 102 |
""" |
| 103 |
req, trans = self._request_and_transport() |
| 104 |
|
| 105 |
queue_list = core.queue_list(trans, req, **params) |
| 106 |
|
| 107 |
count = None |
| 108 |
if params.get("with_count"): |
| 109 |
count = queue_list.get("count", None) |
| 110 |
|
| 111 |
list_iter = iterator._Iterator(self, queue_list, 'queues', |
| 112 |
self.queues_module.create_object(self)) |
| 113 |
return (list_iter, count) |
| 114 |
|
| 115 |
def follow(self, ref): |
| 116 |
"""Follows ref. |
| 117 |
|
| 118 |
This method instanciates a new request instance and requests |
| 119 |
`ref`. It is intended to be used to follow a reference href |
| 120 |
gotten from `links` sections in responses like queues' lists. |
| 121 |
|
| 122 |
:params ref: The reference path. |
| 123 |
:type ref: str |
| 124 |
""" |
| 125 |
req, trans = self._request_and_transport() |
| 126 |
req.ref = ref |
| 127 |
|
| 128 |
return trans.send(req).deserialized_content |
| 129 |
|
| 130 |
# ADMIN API |
| 131 |
def pool(self, ref, **kwargs): |
| 132 |
"""Returns a pool instance |
| 133 |
|
| 134 |
:param ref: Pool's reference name. |
| 135 |
:type ref: str |
| 136 |
|
| 137 |
:returns: A pool instance |
| 138 |
:rtype: `pool.Pool` |
| 139 |
""" |
| 140 |
return pool.Pool(self, ref, **kwargs) |
| 141 |
|
| 142 |
def pools(self, **params): |
| 143 |
"""Gets a list of pools from the server |
| 144 |
|
| 145 |
:param params: Filters to use for getting pools |
| 146 |
:type params: dict. |
| 147 |
|
| 148 |
:returns: A list of pools |
| 149 |
:rtype: `list` |
| 150 |
""" |
| 151 |
req, trans = self._request_and_transport() |
| 152 |
|
| 153 |
pool_list = core.pool_list(trans, req, **params) |
| 154 |
|
| 155 |
return iterator._Iterator(self, |
| 156 |
pool_list, |
| 157 |
'pools', |
| 158 |
pool.create_object(self)) |
| 159 |
|
| 160 |
def flavor(self, ref, **kwargs): |
| 161 |
"""Returns a flavor instance |
| 162 |
|
| 163 |
:param ref: Flavor's reference name. |
| 164 |
:type ref: str |
| 165 |
|
| 166 |
:returns: A flavor instance |
| 167 |
:rtype: `flavor.Flavor` |
| 168 |
""" |
| 169 |
return flavor.Flavor(self, ref, **kwargs) |
| 170 |
|
| 171 |
def flavors(self, **params): |
| 172 |
"""Gets a list of flavors from the server |
| 173 |
|
| 174 |
:param params: Filters to use for getting flavors |
| 175 |
:type params: dict. |
| 176 |
|
| 177 |
:returns: A list of flavors |
| 178 |
:rtype: `list` |
| 179 |
""" |
| 180 |
req, trans = self._request_and_transport() |
| 181 |
|
| 182 |
flavor_list = core.flavor_list(trans, req, **params) |
| 183 |
|
| 184 |
return iterator._Iterator(self, |
| 185 |
flavor_list, |
| 186 |
'flavors', |
| 187 |
flavor.create_object(self)) |
| 188 |
|
| 189 |
def subscription(self, queue_name, **kwargs): |
| 190 |
"""Returns a subscription instance |
| 191 |
|
| 192 |
:param queue_name: Name of the queue to subscribe to. |
| 193 |
:type queue_name: str |
| 194 |
|
| 195 |
:returns: A subscription instance |
| 196 |
:rtype: `subscription.Subscription` |
| 197 |
""" |
| 198 |
return subscription.Subscription(self, queue_name, **kwargs) |
| 199 |
|
| 200 |
def subscriptions(self, queue_name, **params): |
| 201 |
"""Gets a list of subscriptions from the server |
| 202 |
|
| 203 |
:param params: Filters to use for getting subscriptions |
| 204 |
:type params: dict. |
| 205 |
|
| 206 |
:returns: A list of subscriptions |
| 207 |
:rtype: `list` |
| 208 |
""" |
| 209 |
req, trans = self._request_and_transport() |
| 210 |
|
| 211 |
subscription_list = core.subscription_list(trans, req, queue_name, |
| 212 |
**params) |
| 213 |
|
| 214 |
return iterator._Iterator(self, |
| 215 |
subscription_list, |
| 216 |
'subscriptions', |
| 217 |
subscription.create_object(self)) |
| 218 |
|
| 219 |
def ping(self): |
| 220 |
"""Gets the health status of Zaqar server.""" |
| 221 |
req, trans = self._request_and_transport() |
| 222 |
return core.ping(trans, req) |
| 223 |
|
| 224 |
def health(self): |
| 225 |
"""Gets the detailed health status of Zaqar server.""" |
| 226 |
req, trans = self._request_and_transport() |
| 227 |
return core.health(trans, req) |
| 228 |
|
| 229 |
def homedoc(self): |
| 230 |
"""Get the detailed resource doc of Zaqar server""" |
| 231 |
req, trans = self._request_and_transport() |
| 232 |
return core.homedoc(trans, req) |