Added action_plan ObjectField for Action

In this changeset, I added the "action_plan" ObjectField which can
either be loaded by setting the new "eager" parameter as True
or not loaded (as before) by setting it to False. The advantage of
introducing this eager parameter is that this way,
we can reduce to a minimum the overhead of DB queries whenever the
related object fields is not actually needed.

Change-Id: Iae255d9a0a919e2b6db710be24dbf9033b72166e
Partially-Implements: blueprint watcher-versioned-objects
This commit is contained in:
Vincent Françoise
2016-10-04 11:29:38 +02:00
parent 060c369838
commit b4b17ba395
7 changed files with 203 additions and 109 deletions

View File

@@ -16,7 +16,8 @@
from watcher.common import exception
from watcher.common import utils
from watcher.db import api as dbapi
from watcher.db import api as db_api
from watcher import objects
from watcher.objects import base
from watcher.objects import fields as wfields
@@ -35,60 +36,69 @@ class Action(base.WatcherPersistentObject, base.WatcherObject,
base.WatcherObjectDictCompat):
# Version 1.0: Initial version
VERSION = '1.0'
# Version 1.1: Added 'action_plan' object field
VERSION = '1.1'
dbapi = dbapi.get_instance()
dbapi = db_api.get_instance()
fields = {
'id': wfields.IntegerField(),
'uuid': wfields.UUIDField(),
'action_plan_id': wfields.IntegerField(nullable=True),
'action_plan_id': wfields.IntegerField(),
'action_type': wfields.StringField(nullable=True),
'input_parameters': wfields.DictField(nullable=True),
'state': wfields.StringField(nullable=True),
'next': wfields.IntegerField(nullable=True),
'action_plan': wfields.ObjectField('ActionPlan', nullable=True),
}
object_fields = {
'action_plan': (objects.ActionPlan, 'action_plan_id'),
}
@base.remotable_classmethod
def get(cls, context, action_id):
def get(cls, context, action_id, eager=False):
"""Find a action based on its id or uuid and return a Action object.
:param action_id: the id *or* uuid of a action.
:param eager: Load object fields if True (Default: False)
:returns: a :class:`Action` object.
"""
if utils.is_int_like(action_id):
return cls.get_by_id(context, action_id)
return cls.get_by_id(context, action_id, eager=eager)
elif utils.is_uuid_like(action_id):
return cls.get_by_uuid(context, action_id)
return cls.get_by_uuid(context, action_id, eager=eager)
else:
raise exception.InvalidIdentity(identity=action_id)
@base.remotable_classmethod
def get_by_id(cls, context, action_id):
def get_by_id(cls, context, action_id, eager=False):
"""Find a action based on its integer id and return a Action object.
:param action_id: the id of a action.
:param eager: Load object fields if True (Default: False)
:returns: a :class:`Action` object.
"""
db_action = cls.dbapi.get_action_by_id(context, action_id)
action = Action._from_db_object(cls(context), db_action)
db_action = cls.dbapi.get_action_by_id(context, action_id, eager=eager)
action = cls._from_db_object(cls(context), db_action, eager=eager)
return action
@base.remotable_classmethod
def get_by_uuid(cls, context, uuid):
def get_by_uuid(cls, context, uuid, eager=False):
"""Find a action based on uuid and return a :class:`Action` object.
:param uuid: the uuid of a action.
:param context: Security context
:param eager: Load object fields if True (Default: False)
:returns: a :class:`Action` object.
"""
db_action = cls.dbapi.get_action_by_uuid(context, uuid)
action = Action._from_db_object(cls(context), db_action)
db_action = cls.dbapi.get_action_by_uuid(context, uuid, eager=eager)
action = cls._from_db_object(cls(context), db_action, eager=eager)
return action
@base.remotable_classmethod
def list(cls, context, limit=None, marker=None, filters=None,
sort_key=None, sort_dir=None):
sort_key=None, sort_dir=None, eager=False):
"""Return a list of Action objects.
:param context: Security context.
@@ -97,6 +107,7 @@ class Action(base.WatcherPersistentObject, base.WatcherObject,
:param filters: Filters to apply. Defaults to None.
:param sort_key: column to sort results by.
:param sort_dir: direction to sort. "asc" or "desc".
:param eager: Load object fields if True (Default: False)
:returns: a list of :class:`Action` object.
"""
db_actions = cls.dbapi.get_action_list(context,
@@ -104,17 +115,23 @@ class Action(base.WatcherPersistentObject, base.WatcherObject,
marker=marker,
filters=filters,
sort_key=sort_key,
sort_dir=sort_dir)
sort_dir=sort_dir,
eager=eager)
return [cls._from_db_object(cls(context), obj)
return [cls._from_db_object(cls(context), obj, eager=eager)
for obj in db_actions]
@base.remotable
def create(self):
"""Create a Action record in the DB"""
"""Create an :class:`Action` record in the DB.
:returns: An :class:`Action` object.
"""
values = self.obj_get_changes()
db_action = self.dbapi.create_action(values)
self._from_db_object(self, db_action)
# Note(v-francoise): Always load eagerly upon creation so we can send
# notifications containing information about the related relationships
self._from_db_object(self, db_action, eager=True)
def destroy(self):
"""Delete the Action from the DB"""
@@ -134,14 +151,15 @@ class Action(base.WatcherPersistentObject, base.WatcherObject,
self.obj_reset_changes()
@base.remotable
def refresh(self):
def refresh(self, eager=False):
"""Loads updates for this Action.
Loads a action with the same uuid from the database and
checks for updated attributes. Updates are applied from
the loaded action column by column, if there are any updates.
:param eager: Load object fields if True (Default: False)
"""
current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
current = self.get_by_uuid(self._context, uuid=self.uuid, eager=eager)
self.obj_refresh(current)
@base.remotable