Support to delete claimed message

When deleting a claimed message, Zaqar v2 API requires client to
specify the claim_id as query parameter. However, this is not
RESTful. So this patch tries to address this issue by rebuilding
request URI only for deleting claimed message operation rather than
supporting query parameter for all resources' deleting operation.

Change-Id: I614014373c8fb14e13ff1f00837ea36c41c3fe2e
This commit is contained in:
yanyanhu 2016-10-19 02:16:58 -04:00
parent 32aece6268
commit e9f41bdd1c
4 changed files with 78 additions and 10 deletions

View File

@ -15,6 +15,7 @@ from openstack.message.v2 import message as _message
from openstack.message.v2 import queue as _queue
from openstack.message.v2 import subscription as _subscription
from openstack import proxy2
from openstack import resource2
class Proxy(proxy2.BaseProxy):
@ -125,12 +126,17 @@ class Proxy(proxy2.BaseProxy):
queue_name=queue_name)
return self._get(_message.Message, message)
def delete_message(self, queue_name, value, ignore_missing=True):
def delete_message(self, queue_name, value, claim=None,
ignore_missing=True):
"""Delete a message
:param queue_name: The name of target queue to delete message from.
:param value: The value can be either the name of a message or a
:class:`~openstack.message.v2.message.Message` instance.
:param claim: The value can be the ID or a
:class:`~openstack.message.v2.claim.Claim` instance of
the claim seizing the message. If None, the message has
not been claimed.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the message does not exist.
@ -141,6 +147,7 @@ class Proxy(proxy2.BaseProxy):
"""
message = self._get_resource(_message.Message, value,
queue_name=queue_name)
message.claim_id = resource2.Resource._get_id(claim)
return self._delete(_message.Message, message,
ignore_missing=ignore_missing)

View File

@ -131,6 +131,11 @@ class Message(resource2.Resource):
}
request.headers.update(headers)
# For Zaqar v2 API requires client to specify claim_id as query
# parameter when deleting a message that has been claimed, we
# rebuild the request URI if claim_id is not None.
if self.claim_id:
request.uri += '?claim_id=%s' % self.claim_id
response = session.delete(request.uri, endpoint_filter=self.service,
headers=headers)

View File

@ -181,7 +181,7 @@ class TestMessage(testtools.TestCase):
self.assertEqual(sot, res)
@mock.patch.object(uuid, 'uuid4')
def test_delete(self, mock_uuid):
def test_delete_unclaimed(self, mock_uuid):
sess = mock.Mock()
resp = mock.Mock()
sess.delete.return_value = resp
@ -189,6 +189,7 @@ class TestMessage(testtools.TestCase):
mock_uuid.return_value = 'NEW_CLIENT_ID'
sot = message.Message(**FAKE1)
sot.claim_id = None
sot._translate_response = mock.Mock()
sot.delete(sess)
@ -201,12 +202,36 @@ class TestMessage(testtools.TestCase):
sess.get_project_id.assert_called_once_with()
sot._translate_response.assert_called_once_with(resp, has_body=False)
@mock.patch.object(uuid, 'uuid4')
def test_delete_claimed(self, mock_uuid):
sess = mock.Mock()
resp = mock.Mock()
sess.delete.return_value = resp
sess.get_project_id.return_value = 'NEW_PROJECT_ID'
mock_uuid.return_value = 'NEW_CLIENT_ID'
sot = message.Message(**FAKE1)
sot.claim_id = 'CLAIM_ID'
sot._translate_response = mock.Mock()
sot.delete(sess)
url = 'queues/%(queue)s/messages/%(message)s?claim_id=%(cid)s' % {
'queue': FAKE1['queue_name'], 'message': FAKE1['id'],
'cid': 'CLAIM_ID'}
headers = {'Client-ID': 'NEW_CLIENT_ID',
'X-PROJECT-ID': 'NEW_PROJECT_ID'}
sess.delete.assert_called_with(url, endpoint_filter=sot.service,
headers=headers)
sess.get_project_id.assert_called_once_with()
sot._translate_response.assert_called_once_with(resp, has_body=False)
def test_delete_client_id_project_id_exist(self):
sess = mock.Mock()
resp = mock.Mock()
sess.delete.return_value = resp
sot = message.Message(**FAKE2)
sot.claim_id = None
sot._translate_response = mock.Mock()
sot.delete(sess)

View File

@ -72,20 +72,51 @@ class TestMessageProxy(test_proxy_base2.TestProxyBase):
@mock.patch.object(proxy_base.BaseProxy, '_get_resource')
def test_message_delete(self, mock_get_resource):
mock_get_resource.return_value = "resource_or_id"
self.verify_delete(self.proxy.delete_message,
message.Message, False,
["test_queue", "resource_or_id"])
fake_message = mock.Mock()
fake_message.id = "message_id"
mock_get_resource.return_value = fake_message
self._verify2("openstack.proxy2.BaseProxy._delete",
self.proxy.delete_message,
method_args=["test_queue", "resource_or_id", None,
False],
expected_args=[message.Message,
fake_message],
expected_kwargs={"ignore_missing": False})
self.assertIsNone(fake_message.claim_id)
mock_get_resource.assert_called_once_with(message.Message,
"resource_or_id",
queue_name="test_queue")
@mock.patch.object(proxy_base.BaseProxy, '_get_resource')
def test_message_delete_claimed(self, mock_get_resource):
fake_message = mock.Mock()
fake_message.id = "message_id"
mock_get_resource.return_value = fake_message
self._verify2("openstack.proxy2.BaseProxy._delete",
self.proxy.delete_message,
method_args=["test_queue", "resource_or_id", "claim_id",
False],
expected_args=[message.Message,
fake_message],
expected_kwargs={"ignore_missing": False})
self.assertEqual("claim_id", fake_message.claim_id)
mock_get_resource.assert_called_once_with(message.Message,
"resource_or_id",
queue_name="test_queue")
@mock.patch.object(proxy_base.BaseProxy, '_get_resource')
def test_message_delete_ignore(self, mock_get_resource):
mock_get_resource.return_value = "resource_or_id"
self.verify_delete(self.proxy.delete_message,
message.Message, True,
["test_queue", "resource_or_id"])
fake_message = mock.Mock()
fake_message.id = "message_id"
mock_get_resource.return_value = fake_message
self._verify2("openstack.proxy2.BaseProxy._delete",
self.proxy.delete_message,
method_args=["test_queue", "resource_or_id", None,
True],
expected_args=[message.Message,
fake_message],
expected_kwargs={"ignore_missing": True})
self.assertIsNone(fake_message.claim_id)
mock_get_resource.assert_called_once_with(message.Message,
"resource_or_id",
queue_name="test_queue")