Files
python-zaqarclient/marconiclient/queues/v1/queues.py
Flavio Percoco 6d0391ceb3 Implement queue's API methods
This patch implements methods for every queue operation. The 2
operations left out in this patch are: queue_list and queue_stats.

This patch makes the client, officially, usable as a library. An example
of how this could be use:

    client = Client()
    queue = client.queue('my_queue') # Creates the queue automatically
    queue.metadata() # Get's metadata from the queue
    queue.metadata(dict(new_meta='Yo yo!'))
    queue.delete()

Metadata was implemented as method instead of as a property to make it
explicit that both get and set do something in the remote server.

The client API was implemented in 2 separate levels. The highest level
is the one shown in the example above, which allows users to just query
Marconi instances without worrying about the transport, request build
process, references, deserealization and what not. The lowest instead,
allows the user to control every bit of the communication process - the
user can pick a specific transport, or pass custom params. For example -
which will make it possible to have other implementations around that
API in addition to the asynchronous support. Example of the API:

    transport = http.HttpTransport(cfg.CONF)
    request = request.prepare_request(cfg.CONF,
                                      endpoint='http://localhost:8888')
    core.queue_create(transport, request, 1, callback=my_callback)

There are tons of things to do and improve. For example:

    * The way config params are registered may work when using
    marconiclient as a library but they won't when using it as a CLI
    tool.
    * This code lacks of logging.
    * Handling of 20(1|4) is missing.

Partially-Implements blueprint python-marconiclient-v1
Implements blueprint queues-management

Change-Id: I8bdc8a4aff8ea22b5673bc7440e07796ecaf34cc
2013-10-29 16:43:03 +01:00

104 lines
3.3 KiB
Python

# Copyright (c) 2013 Red Hat, Inc.
#
# 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.
from marconiclient.queues.v1 import core
from marconiclient import transport
from marconiclient.transport import request
class Queue(object):
def __init__(self, client, queue_id, auto_create=True):
self.client = client
# NOTE(flaper87) Queue Info
self._id = queue_id
self._metadata = None
if auto_create:
self.ensure_exists()
def _get_transport(self, request):
"""Gets a transport and caches its instance
This method gets a transport instance based on
the request's endpoint and caches that for later
use. The transport instance is invalidated whenever
a session expires.
:param request: The request to use to load the
transport instance.
:type request: `transport.request.Request`
"""
trans = transport.get_transport_for(self.client.conf, request)
return (trans or self.client.transport)
def _request_and_transport(self):
api = 'queues.v' + str(self.client.api_version)
req = request.prepare_request(self.client.conf,
endpoint=self.client.api_url,
api=api)
trans = self._get_transport(req)
return req, trans
def exists(self):
"""Checks if the queue exists."""
req, trans = self._request_and_transport()
return core.queue_exists(trans, req, self._id)
def ensure_exists(self):
"""Ensures a queue exists
This method is not race safe,
the queue could've been deleted
right after it was called.
"""
req, trans = self._request_and_transport()
core.queue_create(trans, req, self._id)
def metadata(self, new_meta=None, force_reload=False):
"""Get metadata and return it
:param new_meta: A dictionary containing
an updated metadata object. If present
the queue metadata will be updated in
remote server.
:type new_meta: `dict`
:param force_reload: Whether to ignored the
cached metadata and reload it from the
server.
:type force_reload: `bool`
:returns: The queue metadata.
"""
req, trans = self._request_and_transport()
if new_meta:
core.queue_set_metadata(trans, req, self._id, new_meta)
self._metadata = new_meta
# TODO(flaper87): Cache with timeout
if self._metadata and not force_reload:
return self._metadata
self._metadata = core.queue_get_metadata(trans, req, self._id)
return self._metadata
def delete(self):
req, trans = self._request_and_transport()
core.queue_delete(trans, req, self._id)