Added Strategy model
In this changeset, I add the Strategy model as well as the DB functionalities we need to manipulate strategies. This changeset implies a DB schema update. Partially Implements: blueprint get-goal-from-strategy Change-Id: I438a8788844fbc514edfe1e9e3136f46ba5a82f2
This commit is contained in:
@@ -130,6 +130,96 @@ class BaseConnection(object):
|
||||
:raises: :py:class:`~.Invalid`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_strategy_list(self, context, filters=None, limit=None,
|
||||
marker=None, sort_key=None, sort_dir=None):
|
||||
"""Get specific columns for matching strategies.
|
||||
|
||||
Return a list of the specified columns for all strategies that
|
||||
match the specified filters.
|
||||
|
||||
:param context: The security context
|
||||
:param columns: List of column names to return.
|
||||
Defaults to 'id' column when columns == None.
|
||||
:param filters: Filters to apply. Defaults to None.
|
||||
|
||||
:param limit: Maximum number of strategies to return.
|
||||
:param marker: The last item of the previous page; we return the next
|
||||
result set.
|
||||
:param sort_key: Attribute by which results should be sorted.
|
||||
:param sort_dir: Direction in which results should be sorted.
|
||||
(asc, desc)
|
||||
:returns: A list of tuples of the specified columns.
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def create_strategy(self, values):
|
||||
"""Create a new strategy.
|
||||
|
||||
:param values: A dict containing items used to identify
|
||||
and track the strategy. For example:
|
||||
|
||||
::
|
||||
|
||||
{
|
||||
'id': 1,
|
||||
'uuid': utils.generate_uuid(),
|
||||
'name': 'my_strategy',
|
||||
'display_name': 'My strategy',
|
||||
'goal_uuid': utils.generate_uuid(),
|
||||
}
|
||||
:returns: A strategy
|
||||
:raises: :py:class:`~.StrategyAlreadyExists`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_strategy_by_id(self, context, strategy_id):
|
||||
"""Return a strategy given its ID.
|
||||
|
||||
:param context: The security context
|
||||
:param strategy_id: The ID of a strategy
|
||||
:returns: A strategy
|
||||
:raises: :py:class:`~.StrategyNotFound`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_strategy_by_uuid(self, context, strategy_uuid):
|
||||
"""Return a strategy given its UUID.
|
||||
|
||||
:param context: The security context
|
||||
:param strategy_uuid: The UUID of a strategy
|
||||
:returns: A strategy
|
||||
:raises: :py:class:`~.StrategyNotFound`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_strategy_by_name(self, context, strategy_name):
|
||||
"""Return a strategy given its name.
|
||||
|
||||
:param context: The security context
|
||||
:param strategy_name: The name of a strategy
|
||||
:returns: A strategy
|
||||
:raises: :py:class:`~.StrategyNotFound`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def destroy_strategy(self, strategy_uuid):
|
||||
"""Destroy a strategy.
|
||||
|
||||
:param strategy_uuid: The UUID of a strategy
|
||||
:raises: :py:class:`~.StrategyNotFound`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def update_strategy(self, strategy_uuid, values):
|
||||
"""Update properties of a strategy.
|
||||
|
||||
:param strategy_uuid: The UUID of a strategy
|
||||
:returns: A strategy
|
||||
:raises: :py:class:`~.StrategyNotFound`
|
||||
:raises: :py:class:`~.Invalid`
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_audit_template_list(self, context, columns=None, filters=None,
|
||||
limit=None, marker=None, sort_key=None,
|
||||
|
||||
@@ -270,6 +270,14 @@ class Connection(api.BaseConnection):
|
||||
query=query, model=models.Goal, filters=filters,
|
||||
plain_fields=plain_fields)
|
||||
|
||||
def _add_strategies_filters(self, query, filters):
|
||||
plain_fields = ['uuid', 'name', 'display_name', 'goal_id']
|
||||
join_fieldmap = {'goal_uuid': models.Goal}
|
||||
|
||||
return self._add_filters(
|
||||
query=query, model=models.Strategy, filters=filters,
|
||||
plain_fields=plain_fields, join_fieldmap=join_fieldmap)
|
||||
|
||||
def _add_audit_templates_filters(self, query, filters):
|
||||
if filters is None:
|
||||
filters = []
|
||||
@@ -441,6 +449,72 @@ class Connection(api.BaseConnection):
|
||||
except exception.ResourceNotFound:
|
||||
raise exception.GoalNotFound(goal=goal_id)
|
||||
|
||||
# ### STRATEGIES ### #
|
||||
|
||||
def get_strategy_list(self, context, filters=None, limit=None,
|
||||
marker=None, sort_key=None, sort_dir=None):
|
||||
|
||||
query = model_query(models.Strategy)
|
||||
query = self._add_strategies_filters(query, filters)
|
||||
if not context.show_deleted:
|
||||
query = query.filter_by(deleted_at=None)
|
||||
return _paginate_query(models.Strategy, limit, marker,
|
||||
sort_key, sort_dir, query)
|
||||
|
||||
def create_strategy(self, values):
|
||||
# ensure defaults are present for new strategies
|
||||
if not values.get('uuid'):
|
||||
values['uuid'] = utils.generate_uuid()
|
||||
|
||||
strategy = models.Strategy()
|
||||
strategy.update(values)
|
||||
|
||||
try:
|
||||
strategy.save()
|
||||
except db_exc.DBDuplicateEntry:
|
||||
raise exception.StrategyAlreadyExists(uuid=values['uuid'])
|
||||
return strategy
|
||||
|
||||
def _get_strategy(self, context, fieldname, value):
|
||||
try:
|
||||
return self._get(context, model=models.Strategy,
|
||||
fieldname=fieldname, value=value)
|
||||
except exception.ResourceNotFound:
|
||||
raise exception.StrategyNotFound(strategy=value)
|
||||
|
||||
def get_strategy_by_id(self, context, strategy_id):
|
||||
return self._get_strategy(context, fieldname="id", value=strategy_id)
|
||||
|
||||
def get_strategy_by_uuid(self, context, strategy_uuid):
|
||||
return self._get_strategy(
|
||||
context, fieldname="uuid", value=strategy_uuid)
|
||||
|
||||
def get_strategy_by_name(self, context, strategy_name):
|
||||
return self._get_strategy(
|
||||
context, fieldname="name", value=strategy_name)
|
||||
|
||||
def destroy_strategy(self, strategy_id):
|
||||
try:
|
||||
return self._destroy(models.Strategy, strategy_id)
|
||||
except exception.ResourceNotFound:
|
||||
raise exception.StrategyNotFound(strategy=strategy_id)
|
||||
|
||||
def update_strategy(self, strategy_id, values):
|
||||
if 'uuid' in values:
|
||||
raise exception.Invalid(
|
||||
message=_("Cannot overwrite UUID for an existing Strategy."))
|
||||
|
||||
try:
|
||||
return self._update(models.Strategy, strategy_id, values)
|
||||
except exception.ResourceNotFound:
|
||||
raise exception.StrategyNotFound(strategy=strategy_id)
|
||||
|
||||
def soft_delete_strategy(self, strategy_id):
|
||||
try:
|
||||
self._soft_delete(models.Strategy, strategy_id)
|
||||
except exception.ResourceNotFound:
|
||||
raise exception.GoalNotFound(strategy=strategy_id)
|
||||
|
||||
# ### AUDIT TEMPLATES ### #
|
||||
|
||||
def get_audit_template_list(self, context, filters=None, limit=None,
|
||||
|
||||
@@ -110,6 +110,21 @@ class WatcherBase(models.SoftDeleteMixin,
|
||||
Base = declarative_base(cls=WatcherBase)
|
||||
|
||||
|
||||
class Strategy(Base):
|
||||
"""Represents a strategy."""
|
||||
|
||||
__tablename__ = 'strategies'
|
||||
__table_args__ = (
|
||||
schema.UniqueConstraint('uuid', name='uniq_strategies0uuid'),
|
||||
table_args()
|
||||
)
|
||||
id = Column(Integer, primary_key=True)
|
||||
uuid = Column(String(36))
|
||||
name = Column(String(63), nullable=False)
|
||||
display_name = Column(String(63), nullable=False)
|
||||
goal_id = Column(Integer, ForeignKey('goals.id'), nullable=False)
|
||||
|
||||
|
||||
class Goal(Base):
|
||||
"""Represents a goal."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user