Refactored Strategy selector to select from DB

In this changeset, I refactored the strategy selector to now
look into the Watcher DB instead of looking into the configuration
file.

Partially Implements: blueprint get-goal-from-strategy

Change-Id: I2bcb63542f6237f26796a3e5a781c8b62820cf6f
This commit is contained in:
Vincent Françoise
2016-04-12 10:26:06 +02:00
parent 81765b9aa5
commit e67b532110
10 changed files with 111 additions and 60 deletions

View File

@@ -30,26 +30,20 @@ class DefaultStrategyContext(base.BaseStrategyContext):
def __init__(self):
super(DefaultStrategyContext, self).__init__()
LOG.debug("Initializing Strategy Context")
self._strategy_selector = default.DefaultStrategySelector()
self._collector_manager = manager.CollectorManager()
@property
def collector(self):
return self._collector_manager
@property
def strategy_selector(self):
return self._strategy_selector
def execute_strategy(self, audit_uuid, request_context):
audit = objects.Audit.get_by_uuid(request_context, audit_uuid)
# Retrieve the Audit Template
audit_template = objects.\
AuditTemplate.get_by_id(request_context, audit.audit_template_id)
audit_template = objects.AuditTemplate.get_by_id(
request_context, audit.audit_template_id)
osc = clients.OpenStackClients()
# todo(jed) retrieve in audit_template parameters (threshold,...)
# todo(jed) create ActionPlan
collector_manager = self.collector.get_cluster_model_collector(osc=osc)
@@ -57,8 +51,13 @@ class DefaultStrategyContext(base.BaseStrategyContext):
# todo(jed) remove call to get_latest_cluster_data_model
cluster_data_model = collector_manager.get_latest_cluster_data_model()
selected_strategy = self.strategy_selector.define_from_goal(
audit_template.goal, osc=osc)
strategy_selector = default.DefaultStrategySelector(
goal_name=objects.Goal.get_by_name(
request_context, audit_template.goal).name,
strategy_name=None,
osc=osc)
selected_strategy = strategy_selector.select()
# todo(jed) add parameters and remove cluster_data_model
return selected_strategy.execute(cluster_data_model)

View File

@@ -22,6 +22,7 @@ import six
@six.add_metaclass(abc.ABCMeta)
class BaseSelector(object):
@abc.abstractmethod
def define_from_goal(self, goal_name):
def select(self):
raise NotImplementedError()

View File

@@ -44,19 +44,48 @@ CONF.register_opts(WATCHER_GOALS_OPTS, goals_opt_group)
class DefaultStrategySelector(base.BaseSelector):
def __init__(self):
def __init__(self, goal_name, strategy_name=None, osc=None):
"""Default strategy selector
:param goal_name: Name of the goal
:param strategy_name: Name of the strategy
:param osc: an OpenStackClients instance
"""
super(DefaultStrategySelector, self).__init__()
self.goal_name = goal_name
self.strategy_name = strategy_name
self.osc = osc
self.strategy_loader = default.DefaultStrategyLoader()
def define_from_goal(self, goal_name, osc=None):
""":param osc: an OpenStackClients instance"""
def select(self):
"""Selects a strategy
:raises: :py:class:`~.LoadingError` if it failed to load a strategy
:returns: A :py:class:`~.BaseStrategy` instance
"""
strategy_to_load = None
try:
strategy_to_load = CONF.watcher_goals.goals[goal_name]
return self.strategy_loader.load(strategy_to_load, osc=osc)
except KeyError as exc:
if self.strategy_name:
strategy_to_load = self.strategy_name
else:
available_strategies = self.strategy_loader.list_available()
available_strategies_for_goal = list(
key for key, strat in available_strategies.items()
if strat.get_goal_name() == self.goal_name)
if not available_strategies_for_goal:
raise exception.NoAvailableStrategyForGoal(
goal=self.goal_name)
# TODO(v-francoise): We should do some more work here to select
# a strategy out of a given goal instead of just choosing the
# 1st one
strategy_to_load = available_strategies_for_goal[0]
return self.strategy_loader.load(strategy_to_load, osc=self.osc)
except exception.NoAvailableStrategyForGoal:
raise
except Exception as exc:
LOG.exception(exc)
raise exception.WatcherException(
_("Incorrect mapping: could not find "
"associated strategy for '%s'") % goal_name
)
raise exception.LoadingError(
_("Could not load any strategy for goal %(goal)s"),
goal=self.goal_name)