Merge "Implement triggers table of db"

This commit is contained in:
Jenkins 2016-02-18 09:52:47 +00:00 committed by Gerrit Code Review
commit 4226f709d8
6 changed files with 179 additions and 7 deletions

View File

@ -127,6 +127,60 @@ def get_by_id(context, model, id, *args, **kwargs):
###################
def trigger_get(context, id):
"""Get a trigger by its id.
:param context: The security context
:param id: ID of the trigger
:returns: Dictionary-like object containing properties of the trigger
Raises TriggerNotFound if trigger with the given ID doesn't exist.
"""
return IMPL.trigger_get(context, id)
def trigger_create(context, values):
"""Create a trigger from the values dictionary.
:param context: The security context
:param values: Dictionary containing trigger properties
:returns: Dictionary-like object containing the properties of the created
trigger
"""
return IMPL.trigger_create(context, values)
def trigger_update(context, id, values):
"""Set the given properties on a trigger and update it.
:param context: The security context
:param id: ID of the trigger
:param values: Dictionary containing trigger properties to be updated
:returns: Dictionary-like object containing the properties of the updated
trigger
Raises TriggerNotFound if trigger with the given ID doesn't exist.
"""
return IMPL.trigger_update(context, id, values)
def trigger_delete(context, id):
"""Delete a trigger from the database.
:param context: The security context
:param id: ID of the trigger
Raises TriggerNotFound if trigger with the given ID doesn't exist.
"""
return IMPL.trigger_delete(context, id)
###################
def scheduled_operation_state_get(context, operation_id):
"""Get a scheduled operation state by its id.

View File

@ -331,6 +331,52 @@ def get_by_id(context, model, id, *args, **kwargs):
###################
def trigger_get(context, id):
return _trigger_get(context, id)
def _trigger_get(context, id, session=None):
result = model_query(context, models.Trigger,
session=session).filter_by(id=id)
result = result.first()
if not result:
raise exception.TriggerNotFound(id=id)
return result
def trigger_create(context, values):
trigger_ref = models.Trigger()
trigger_ref.update(values)
trigger_ref.save(get_session())
return trigger_ref
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
def trigger_update(context, id, values):
"""Update the Trigger record with the most recent data."""
session = get_session()
with session.begin():
trigger_ref = _trigger_get(context, id, session=session)
trigger_ref.update(values)
trigger_ref.save(session)
return trigger_ref
def trigger_delete(context, id):
"""Delete a Trigger record."""
session = get_session()
with session.begin():
trigger_ref = _trigger_get(context, id, session=session)
trigger_ref.delete(session=session)
###################
def scheduled_operation_state_get(context, operation_id):
return _scheduled_operation_state_get(context, operation_id)

View File

@ -81,15 +81,15 @@ def define_tables(meta):
triggers = Table(
'triggers', meta,
Column('id', String(36), primary_key=True, nullable=False),
Column('name', String(length=255)),
Column('project_id', String(length=255)),
Column('type', String(length=64)),
Column('properties', Text),
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('deleted_at', DateTime),
Column('deleted', Boolean),
Column('deleted', Boolean, nullable=False),
Column('id', String(length=36), primary_key=True, nullable=False),
Column('name', String(length=255), nullable=False),
Column('project_id', String(length=255), nullable=False),
Column('type', String(length=64), nullable=False),
Column('properties', Text, nullable=False),
mysql_engine='InnoDB'
)
@ -116,7 +116,7 @@ def define_tables(meta):
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('deleted_at', DateTime),
Column('deleted', Boolean),
Column('deleted', Boolean, nullable=False),
Column('id', Integer, primary_key=True, nullable=False,
autoincrement=True),
Column('operation_id', String(length=36),

View File

@ -61,6 +61,18 @@ class Service(BASE, SmaugBase):
rpc_available_version = Column(String(36))
class Trigger(BASE, SmaugBase):
"""Represents a trigger."""
__tablename__ = 'triggers'
id = Column(String(36), primary_key=True, nullable=False)
name = Column(String(255), nullable=False)
project_id = Column(String(255), nullable=False)
type = Column(String(64), nullable=False)
properties = Column(Text, nullable=False)
class ScheduledOperationState(BASE, SmaugBase):
"""Represents a scheduled operation state."""

View File

@ -180,6 +180,10 @@ class HostBinaryNotFound(NotFound):
message = _("Could not find binary %(binary)s on host %(host)s.")
class TriggerNotFound(NotFound):
message = _("Trigger %(id)s could not be found.")
class ScheduledOperationStateNotFound(NotFound):
message = _("Scheduled Operation State %(op_id)s could not be found.")

View File

@ -91,6 +91,62 @@ class ServicesDbTestCase(base.TestCase):
self.assertEqual(service_get_ref['host'], 'hosttest5')
class TriggerTestCase(base.TestCase):
"""Test cases for triggers table."""
def setUp(self):
super(TriggerTestCase, self).setUp()
self.ctxt = context.RequestContext(user_id='user_id',
project_id='project_id')
def _create_trigger(self):
values = {
'id': "0354ca9ddcd046b693340d78759fd274",
'name': 'first trigger',
'project_id': self.ctxt.tenant,
'type': 'time',
'properties': '{}',
}
return db.trigger_create(self.ctxt, values)
def test_trigger_create(self):
trigger_ref = self._create_trigger()
self.assertEqual('time', trigger_ref['type'])
def test_trigger_delete(self):
trigger_ref = self._create_trigger()
db.trigger_delete(self.ctxt, trigger_ref['id'])
self.assertRaises(exception.TriggerNotFound,
db.trigger_delete,
self.ctxt, trigger_ref['id'])
self.assertRaises(exception.TriggerNotFound,
db.trigger_get,
self.ctxt, trigger_ref['id'])
self.assertRaises(exception.TriggerNotFound,
db.trigger_delete, self.ctxt, '100')
def test_trigger_update(self):
trigger_ref = self._create_trigger()
id = trigger_ref['id']
trigger_ref = db.trigger_update(self.ctxt, id, {'type': 'event'})
self.assertEqual('event', trigger_ref['type'])
trigger_ref = db.trigger_get(self.ctxt, id)
self.assertEqual('event', trigger_ref['type'])
self.assertRaises(exception.TriggerNotFound,
db.trigger_update,
self.ctxt, '100', {"type": "event"})
def test_trigger_get(self):
trigger_ref = self._create_trigger()
trigger_ref = db.trigger_get(self.ctxt, trigger_ref['id'])
self.assertEqual('time', trigger_ref['type'])
class ScheduledOperationStateTestCase(base.TestCase):
"""Test cases for scheduled_operation_states table."""