Replace create / update with upsert

This patch replaces both methods with a single method upsert. The idea
behind it, as discussed, is to have a single method for handling queue
creation and update.

The method is expected to return Ture when a queue was created and False
when a queue was updated.

Implements blueprint storage-base
Fixes bug 1155657

Change-Id: I367c5a29038acc89dbf60a3df9d8dcbf436037a7
This commit is contained in:
Flaper Fesp 2013-03-15 16:28:27 +01:00
parent 3ee717576d
commit a23643ec3c
3 changed files with 26 additions and 44 deletions

View File

@ -99,32 +99,19 @@ class QueueBase(ControllerBase):
pass
@abc.abstractmethod
def create(self, name, tenant=None, ttl=MIN_TTL, **metadata):
def upsert(self, name, tenant=None, metadata=None):
"""
Base method for queue creation.
This methods handles both creates and updates
operations for queues.
:param name: The queue name
:param tenant: Tenant id
:param ttl: Number of seconds the server
will keep messages for this queue.
:param metadata: Arbitrary metadata
:returns: True if a queue was created and False
if it was updated.
"""
m = (_("TTL must be an integer between %(min)s and %(max)s") %
dict(min=MIN_TTL, max=MAX_TTL))
assert (ttl >= MIN_TTL and ttl <= MAX_TTL), m
@abc.abstractmethod
def update(self, name, tenant=None, **metadata):
"""
Base method for queue update.
:param name: The queue name
:param tenant: Tenant id
:param metadata: Extra parameters defining
the new metadata for this queue.
"""
pass
msg = _("Metadata should be an instance of dict")
assert isinstance(metadata, dict), msg
@abc.abstractmethod
def delete(self, name, tenant=None):

View File

@ -46,26 +46,29 @@ class QueueControllerTest(ControllerBaseTest):
def test_queue_lifecycle(self):
# Test Queue Creation
self.controller.create("test", tenant=self.tenant,
ttl=60, topic="test_queue")
created = self.controller.upsert("test", tenant=self.tenant,
metadata=dict(topic="test_queue"))
self.assertTrue(created)
# Test Queue retrieval
queue = self.controller.get("test", tenant=self.tenant)
self.assertEqual(queue["name"], "test")
self.assertEqual(queue["ttl"], 60)
# Test Queue Update
self.controller.update("tests", name="test1", tenant=self.tenant)
queue = self.controller.get("test1", tenant=self.tenant)
self.assertEqual(queue["ttl"], 60)
created = self.controller.upsert("test", tenant=self.tenant,
metadata=dict(meta="test_meta"))
self.assertFalse(created)
queue = self.controller.get("test", tenant=self.tenant)
self.assertEqual(queue["metadata"]["meta"], "test_meta")
# Test Queue Deletion
self.controller.delete("test1", tenant=self.tenant)
self.controller.delete("test", tenant=self.tenant)
# Test DoesNotExist Exception
self.assertRaises(storage.exceptions.DoesNotExist,
self.controller.get, "test1",
tenant=self.tenant)
self.controller.get, "test", tenant=self.tenant)
class MessageControllerTest(ControllerBaseTest):

View File

@ -41,12 +41,9 @@ class QueueController(storage.QueueBase):
def get(self, name, tenant=None):
super(QueueController, self).get(name, tenant=tenant)
def create(self, name, tenant=None, ttl=None, **metadata):
super(QueueController, self).create(name, tenant=tenant,
ttl=ttl, **metadata)
def update(self, name, tenant=None, **metadata):
super(QueueController, self).update(name, tenant=tenant, **metadata)
def upsert(self, name, tenant=None, metadata=None):
super(QueueController, self).upsert(name, tenant=tenant,
metadata=metadata)
def delete(self, name, tenant=None):
super(QueueController, self).delete(name, tenant=tenant)
@ -66,13 +63,8 @@ class TestQueueBase(suite.TestSuite):
self.driver = Driver()
self.controller = self.driver.queue_controller
def test_create(self):
self.assertRaises(AssertionError, self.controller.create,
"test", ttl=30)
def test_upsert(self):
self.assertRaises(AssertionError, self.controller.upsert,
"test", metadata=[])
self.assertRaises(AssertionError, self.controller.create,
"test", ttl=1209601)
self.assertIsNone(self.controller.create("test", ttl=60))
self.assertIsNone(self.controller.create("test", ttl=120))
self.assertIsNone(self.controller.create("test", ttl=1209600))
self.assertIsNone(self.controller.upsert("test", metadata={}))