
This patch adds the support for subscription create, which includes the CLI support and the lib support. Besides, to make the functional test work for subscription create, this patch also adds the lib support for subscription get, delete, update and list to make things easier. Change-Id: Ie3ac27b731f41ea6023976aab3c1afbc283d659d
85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
# Copyright (c) 2015 Catalyst IT Ltd.
|
|
#
|
|
# 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 zaqarclient.queues.v2 import core
|
|
from zaqarclient.transport import errors
|
|
|
|
|
|
class Subscription(object):
|
|
|
|
def __init__(self, client, queue_name, subscriber=None, ttl=60, id=None,
|
|
auto_create=True, **kwargs):
|
|
self.client = client
|
|
|
|
self.id = id
|
|
self.queue_name = queue_name,
|
|
self.subscriber = subscriber
|
|
self.ttl = ttl
|
|
self.options = kwargs.get('options', {})
|
|
|
|
if auto_create:
|
|
self.ensure_exists()
|
|
|
|
def ensure_exists(self):
|
|
"""Ensures subscription exists
|
|
|
|
This method is not race safe, the subscription could've been deleted
|
|
right after it was called.
|
|
"""
|
|
req, trans = self.client._request_and_transport()
|
|
|
|
if not self.id and self.subscriber:
|
|
subscription_data = {'subscriber': self.subscriber,
|
|
'ttl': self.ttl,
|
|
'options': self.options
|
|
}
|
|
try:
|
|
subscription = core.subscription_create(trans, req,
|
|
self.queue_name,
|
|
subscription_data)
|
|
|
|
if subscription and 'subscription_id' in subscription:
|
|
self.id = subscription['subscription_id']
|
|
except errors.ConflictError:
|
|
# ConflictError means the subscription already exists.
|
|
print('The subscriber has been existed already.')
|
|
|
|
if self.id:
|
|
sub = core.subscription_get(trans, req, self.queue_name, self.id)
|
|
self.subscriber = sub.get('subscriber')
|
|
self.ttl = sub.get('ttl')
|
|
self.options = sub.get('options')
|
|
|
|
def update(self, subscription_data):
|
|
req, trans = self.client._request_and_transport()
|
|
core.subscription_update(trans, req, self.queue_name,
|
|
self.id, subscription_data)
|
|
|
|
for key, value in subscription_data.items():
|
|
setattr(self, key, value)
|
|
|
|
def delete(self):
|
|
req, trans = self.client._request_and_transport()
|
|
core.subscription_delete(trans, req, self.queue_name, self.id)
|
|
|
|
|
|
def create_object(parent):
|
|
return lambda kwargs: Subscription(parent, kwargs["queue_name"],
|
|
subscriber=kwargs['subscriber'],
|
|
ttl=kwargs['ttl'],
|
|
id=kwargs['id'],
|
|
auto_create=False,
|
|
**kwargs)
|