From 906a89f61dc8a0165f9f1777575d2e22c01648fb Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Wed, 14 Jan 2015 08:59:34 -0500 Subject: [PATCH] Add data model entities for OrderRetryTask Change-Id: I108dca2d6e1ef5be5bafd54e307836d66d789958 --- .../aa2cf96a1d5_add_orderretrytask.py | 34 ++++++++++++++ barbican/model/models.py | 22 +++++++++ barbican/tests/model/test_models.py | 45 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 barbican/model/migration/alembic_migrations/versions/aa2cf96a1d5_add_orderretrytask.py diff --git a/barbican/model/migration/alembic_migrations/versions/aa2cf96a1d5_add_orderretrytask.py b/barbican/model/migration/alembic_migrations/versions/aa2cf96a1d5_add_orderretrytask.py new file mode 100644 index 00000000..ccde1a93 --- /dev/null +++ b/barbican/model/migration/alembic_migrations/versions/aa2cf96a1d5_add_orderretrytask.py @@ -0,0 +1,34 @@ +"""Add OrderRetryTask + +Revision ID: aa2cf96a1d5 +Revises: 256da65e0c5f +Create Date: 2015-01-19 10:27:19.179196 + +""" + +# revision identifiers, used by Alembic. +revision = "aa2cf96a1d5" +down_revision = "256da65e0c5f" + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.create_table( + "order_retry_tasks", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("order_id", sa.String(length=36), nullable=False), + sa.Column("retry_task", sa.Text(), nullable=False), + sa.Column("retry_at", sa.DateTime(), nullable=False), + sa.Column("retry_args", sa.Text(), nullable=False), + sa.Column("retry_kwargs", sa.Text(), nullable=False), + sa.Column("retry_count", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(["order_id"], ["orders.id"]), + sa.PrimaryKeyConstraint("id"), + mysql_engine="InnoDB" + ) + + +def downgrade(): + op.drop_table("order_retry_tasks") diff --git a/barbican/model/models.py b/barbican/model/models.py index 8cda2751..5e75730e 100644 --- a/barbican/model/models.py +++ b/barbican/model/models.py @@ -548,6 +548,28 @@ class OrderPluginMetadatum(BASE, ModelBase): 'value': self.value} +class OrderRetryTask(BASE): + + __tablename__ = "order_retry_tasks" + __table_args__ = {"mysql_engine": "InnoDB"} + __table_initialized__ = False + + id = sa.Column( + sa.String(36), primary_key=True, default=utils.generate_uuid, + ) + order_id = sa.Column( + sa.String(36), sa.ForeignKey("orders.id"), nullable=False, + ) + retry_task = sa.Column(sa.Text, nullable=False) + retry_at = sa.Column(sa.DateTime, default=None, nullable=False) + retry_args = sa.Column(sa.Text, nullable=False) + retry_kwargs = sa.Column(sa.Text, nullable=False) + retry_count = sa.Column(sa.Integer, nullable=False, default=0) + + def get_retry_params(self): + return json.loads(self.retry_args), json.loads(self.retry_kwargs) + + class Container(BASE, ModelBase): """Represents a Container for Secrets in the datastore. diff --git a/barbican/tests/model/test_models.py b/barbican/tests/model/test_models.py index 7cef9546..0d38eb07 100644 --- a/barbican/tests/model/test_models.py +++ b/barbican/tests/model/test_models.py @@ -16,6 +16,7 @@ import datetime from barbican.model import models +from barbican.openstack.common import jsonutils as json from barbican.tests import utils @@ -175,3 +176,47 @@ class WhenProcessingJsonBlob(utils.BaseTestCase): def test_process_result_value_w_json_str(self): res = self.json_blob.process_result_value('{"test": true}', None) self.assertTrue(res.get('test')) + + +class WhenCreatingOrderRetryTask(utils.BaseTestCase): + + def test_create_new_order_task(self): + order = models.Order({ + 'type': 'certificate', + 'meta': { + 'email': 'email@email.com' + }, + 'sub_status': 'Pending', + 'sub_status_message': 'Waiting for instructions...' + }) + at = datetime.datetime.utcnow() + order_retry_task = models.OrderRetryTask( + order_id=order.id, + retry_task="foobar", + retry_at=at, + retry_args=json.dumps(["one", "two"]), + retry_kwargs=json.dumps({"three": "four"}), + ) + + self.assertEqual(order_retry_task.order_id, order.id) + self.assertEqual(order_retry_task.retry_task, "foobar") + self.assertEqual(order_retry_task.retry_at, at) + self.assertEqual( + order_retry_task.retry_args, + json.dumps(["one", "two"]), + ) + self.assertEqual( + order_retry_task.retry_kwargs, + json.dumps({"three": "four"}), + ) + + def test_get_retry_params(self): + order_retry_task = models.OrderRetryTask( + retry_args=json.dumps(["one", "two"]), + retry_kwargs=json.dumps({"three": "four"}), + ) + + self.assertEqual( + order_retry_task.get_retry_params(), + (["one", "two"], {"three": "four"}), + )