diff --git a/watcher/decision_engine/command/__init__.py b/watcher/decision_engine/audit/__init__.py similarity index 100% rename from watcher/decision_engine/command/__init__.py rename to watcher/decision_engine/audit/__init__.py diff --git a/watcher/decision_engine/messaging/command/base.py b/watcher/decision_engine/audit/base.py similarity index 95% rename from watcher/decision_engine/messaging/command/base.py rename to watcher/decision_engine/audit/base.py index 8d66dc380..8a516deab 100644 --- a/watcher/decision_engine/messaging/command/base.py +++ b/watcher/decision_engine/audit/base.py @@ -21,7 +21,7 @@ import six @six.add_metaclass(abc.ABCMeta) -class BaseDecisionEngineCommand(object): +class BaseAuditHandler(object): @abc.abstractmethod def execute(self): raise NotImplementedError() diff --git a/watcher/decision_engine/command/audit.py b/watcher/decision_engine/audit/default.py similarity index 70% rename from watcher/decision_engine/command/audit.py rename to watcher/decision_engine/audit/default.py index 96fc2f9bb..34924dfc7 100644 --- a/watcher/decision_engine/command/audit.py +++ b/watcher/decision_engine/audit/default.py @@ -16,8 +16,8 @@ from oslo_log import log from watcher.common.messaging.events.event import Event -from watcher.decision_engine.messaging.command.base import \ - BaseDecisionEngineCommand +from watcher.decision_engine.audit.base import \ + BaseAuditHandler from watcher.decision_engine.messaging.events import Events from watcher.decision_engine.planner.default import DefaultPlanner from watcher.decision_engine.strategy.context.default import StrategyContext @@ -28,8 +28,9 @@ from watcher.objects.audit_template import AuditTemplate LOG = log.getLogger(__name__) -class TriggerAuditCommand(BaseDecisionEngineCommand): +class DefaultAuditHandler(BaseAuditHandler): def __init__(self, messaging, model_collector): + super(DefaultAuditHandler, self).__init__() self.messaging = messaging self.model_collector = model_collector self.strategy_context = StrategyContext() @@ -43,8 +44,8 @@ class TriggerAuditCommand(BaseDecisionEngineCommand): self.messaging.topic_status.publish_event(event.get_type().name, payload) - def update_audit(self, request_context, audit_uuid, state): - LOG.debug("update audit {0} ".format(state)) + def update_audit_state(self, request_context, audit_uuid, state): + LOG.debug("Update audit state:{0} ".format(state)) audit = Audit.get_by_uuid(request_context, audit_uuid) audit.state = state audit.save() @@ -53,31 +54,32 @@ class TriggerAuditCommand(BaseDecisionEngineCommand): def execute(self, audit_uuid, request_context): try: - LOG.debug("Execute TriggerAuditCommand ") + LOG.debug("Trigger audit %s" % audit_uuid) - # 1 - change status to ONGOING - audit = self.update_audit(request_context, audit_uuid, - AuditStatus.ONGOING) + # change state to ONGOING + audit = self.update_audit_state(request_context, audit_uuid, + AuditStatus.ONGOING) - # 3 - Retrieve cluster-data-model + # Retrieve cluster-data-model cluster = self.model_collector.get_latest_cluster_data_model() - # 4 - Select appropriate strategy + # Select appropriate strategy audit_template = AuditTemplate.get_by_id(request_context, audit.audit_template_id) self.strategy_context.set_goal(audit_template.goal) - # 5 - compute change requests + # compute change requests solution = self.strategy_context.execute_strategy(cluster) - # 6 - create an action plan + # create an action plan planner = DefaultPlanner() planner.schedule(request_context, audit.id, solution) - # 7 - change status to SUCCEEDED and notify - self.update_audit(request_context, audit_uuid, - AuditStatus.SUCCEEDED) + # change state to SUCCEEDED and notify + self.update_audit_state(request_context, audit_uuid, + AuditStatus.SUCCEEDED) except Exception as e: - self.update_audit(request_context, audit_uuid, AuditStatus.FAILED) - LOG.error("Execute audit command {0} ".format(unicode(e))) + LOG.exception(e) + self.update_audit_state(request_context, audit_uuid, + AuditStatus.FAILED) diff --git a/watcher/decision_engine/messaging/audit_endpoint.py b/watcher/decision_engine/messaging/audit_endpoint.py index 0d75b5cae..6de6cbbc9 100644 --- a/watcher/decision_engine/messaging/audit_endpoint.py +++ b/watcher/decision_engine/messaging/audit_endpoint.py @@ -18,7 +18,7 @@ # from oslo_log import log -from watcher.decision_engine.command.audit import TriggerAuditCommand +from watcher.decision_engine.audit.default import DefaultAuditHandler from watcher.metrics_engine.cluster_model_collector.manager import \ CollectorManager @@ -33,7 +33,7 @@ class AuditEndpoint(object): def do_trigger_audit(self, context, audit_uuid): model_collector = self.manager.get_cluster_model_collector() - audit = TriggerAuditCommand(self.de, model_collector) + audit = DefaultAuditHandler(self.de, model_collector) audit.execute(audit_uuid, context) def trigger_audit(self, context, audit_uuid): diff --git a/watcher/decision_engine/messaging/command/__init__.py b/watcher/tests/decision_engine/audit/__init__.py similarity index 100% rename from watcher/decision_engine/messaging/command/__init__.py rename to watcher/tests/decision_engine/audit/__init__.py diff --git a/watcher/tests/decision_engine/command/test_trigger_audit_command.py b/watcher/tests/decision_engine/audit/test_default_audit_handler.py similarity index 75% rename from watcher/tests/decision_engine/command/test_trigger_audit_command.py rename to watcher/tests/decision_engine/audit/test_default_audit_handler.py index 1e783ef9c..3932333e7 100644 --- a/watcher/tests/decision_engine/command/test_trigger_audit_command.py +++ b/watcher/tests/decision_engine/audit/test_default_audit_handler.py @@ -15,7 +15,7 @@ # limitations under the License. from mock import call from mock import MagicMock -from watcher.decision_engine.command.audit import TriggerAuditCommand +from watcher.decision_engine.audit.default import DefaultAuditHandler from watcher.decision_engine.messaging.events import Events from watcher.objects.audit import Audit from watcher.objects.audit import AuditStatus @@ -25,9 +25,9 @@ from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state \ from watcher.tests.objects import utils as obj_utils -class TestTriggerAuditCommand(DbTestCase): +class TestDefaultAuditHandler(DbTestCase): def setUp(self): - super(TestTriggerAuditCommand, self).setUp() + super(TestDefaultAuditHandler, self).setUp() self.audit_template = obj_utils.create_test_audit_template( self.context) self.audit = obj_utils.create_test_audit( @@ -36,24 +36,24 @@ class TestTriggerAuditCommand(DbTestCase): def test_trigger_audit_without_errors(self): model_collector = FakerModelCollector() - command = TriggerAuditCommand(MagicMock(), model_collector) - command.execute(self.audit.uuid, self.context) + audit_handler = DefaultAuditHandler(MagicMock(), model_collector) + audit_handler.execute(self.audit.uuid, self.context) def test_trigger_audit_state_success(self): model_collector = FakerModelCollector() - command = TriggerAuditCommand(MagicMock(), model_collector) - command.strategy_context.execute_strategy = MagicMock() - command.execute(self.audit.uuid, self.context) + audit_handler = DefaultAuditHandler(MagicMock(), model_collector) + audit_handler.strategy_context.execute_strategy = MagicMock() + audit_handler.execute(self.audit.uuid, self.context) audit = Audit.get_by_uuid(self.context, self.audit.uuid) self.assertEqual(AuditStatus.SUCCEEDED, audit.state) def test_trigger_audit_send_notification(self): messaging = MagicMock() model_collector = FakerModelCollector() - command = TriggerAuditCommand(messaging, model_collector) - command.strategy_context.execute_strategy = MagicMock() + audit_handler = DefaultAuditHandler(messaging, model_collector) + audit_handler.strategy_context.execute_strategy = MagicMock() - command.execute(self.audit.uuid, self.context) + audit_handler.execute(self.audit.uuid, self.context) call_on_going = call(Events.TRIGGER_AUDIT.name, { 'audit_status': AuditStatus.ONGOING, diff --git a/watcher/tests/decision_engine/command/test_event_consumer_factory.py b/watcher/tests/decision_engine/audit/test_event_consumer_factory.py similarity index 100% rename from watcher/tests/decision_engine/command/test_event_consumer_factory.py rename to watcher/tests/decision_engine/audit/test_event_consumer_factory.py diff --git a/watcher/tests/decision_engine/command/__init__.py b/watcher/tests/decision_engine/command/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/watcher/tests/decision_engine/messaging/test_audit_endpoint.py b/watcher/tests/decision_engine/messaging/test_audit_endpoint.py index 67378e968..028a4365a 100644 --- a/watcher/tests/decision_engine/messaging/test_audit_endpoint.py +++ b/watcher/tests/decision_engine/messaging/test_audit_endpoint.py @@ -15,9 +15,8 @@ # limitations under the License. import mock from mock import MagicMock - from watcher.common import utils -from watcher.decision_engine.command.audit import TriggerAuditCommand +from watcher.decision_engine.audit.default import DefaultAuditHandler from watcher.decision_engine.messaging.audit_endpoint import AuditEndpoint from watcher.metrics_engine.cluster_model_collector.manager import \ CollectorManager @@ -26,9 +25,9 @@ from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state impor FakerModelCollector -class TriggerAuditCommandWithExecutor(TriggerAuditCommand): +class DefaultAuditHandlerMock(DefaultAuditHandler): def setUp(self): - super(TriggerAuditCommand, self).setUp() + super(DefaultAuditHandlerMock, self).setUp() def executor(self): pass @@ -42,28 +41,28 @@ class TestAuditEndpoint(base.TestCase): def test_do_trigger_audit(self): audit_uuid = utils.generate_uuid() model_collector = FakerModelCollector() - command = TriggerAuditCommand(MagicMock(), model_collector) - endpoint = AuditEndpoint(command) + audit_handler = DefaultAuditHandler(MagicMock(), model_collector) + endpoint = AuditEndpoint(audit_handler) with mock.patch.object(CollectorManager, 'get_cluster_model_collector') \ as mock_call2: mock_call2.return_value = 0 - with mock.patch.object(TriggerAuditCommand, 'execute') \ + with mock.patch.object(DefaultAuditHandler, 'execute') \ as mock_call: mock_call.return_value = 0 - endpoint.do_trigger_audit(command, audit_uuid) + endpoint.do_trigger_audit(audit_handler, audit_uuid) # mock_call.assert_called_once_with() mock_call2.assert_called_once_with() def test_trigger_audit(self): audit_uuid = utils.generate_uuid() model_collector = FakerModelCollector() - command = TriggerAuditCommandWithExecutor(MagicMock(), - model_collector) - endpoint = AuditEndpoint(command) + audit_handler = DefaultAuditHandlerMock(MagicMock(), + model_collector) + endpoint = AuditEndpoint(audit_handler) - with mock.patch.object(TriggerAuditCommandWithExecutor, 'executor') \ + with mock.patch.object(DefaultAuditHandlerMock, 'executor') \ as mock_call: mock_call.return_value = 0 - endpoint.trigger_audit(command, audit_uuid) + endpoint.trigger_audit(audit_handler, audit_uuid)