Rename command to audit
This patchset is there to change the code structure. Some Python class and packages need to be renamed for a better compliance with the shared terminology which provides a better understanding of Watcher objects and components by every contributor. This patchset is there to rename the folder command to audit Partially implements: blueprint glossary-related-refactoring Change-Id: I76616fb58d5e79a7dc209b80e882d216850d18a4
This commit is contained in:
@@ -21,7 +21,7 @@ import six
|
|||||||
|
|
||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
class BaseDecisionEngineCommand(object):
|
class BaseAuditHandler(object):
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def execute(self):
|
def execute(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
@@ -16,8 +16,8 @@
|
|||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
|
|
||||||
from watcher.common.messaging.events.event import Event
|
from watcher.common.messaging.events.event import Event
|
||||||
from watcher.decision_engine.messaging.command.base import \
|
from watcher.decision_engine.audit.base import \
|
||||||
BaseDecisionEngineCommand
|
BaseAuditHandler
|
||||||
from watcher.decision_engine.messaging.events import Events
|
from watcher.decision_engine.messaging.events import Events
|
||||||
from watcher.decision_engine.planner.default import DefaultPlanner
|
from watcher.decision_engine.planner.default import DefaultPlanner
|
||||||
from watcher.decision_engine.strategy.context.default import StrategyContext
|
from watcher.decision_engine.strategy.context.default import StrategyContext
|
||||||
@@ -28,8 +28,9 @@ from watcher.objects.audit_template import AuditTemplate
|
|||||||
LOG = log.getLogger(__name__)
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TriggerAuditCommand(BaseDecisionEngineCommand):
|
class DefaultAuditHandler(BaseAuditHandler):
|
||||||
def __init__(self, messaging, model_collector):
|
def __init__(self, messaging, model_collector):
|
||||||
|
super(DefaultAuditHandler, self).__init__()
|
||||||
self.messaging = messaging
|
self.messaging = messaging
|
||||||
self.model_collector = model_collector
|
self.model_collector = model_collector
|
||||||
self.strategy_context = StrategyContext()
|
self.strategy_context = StrategyContext()
|
||||||
@@ -43,8 +44,8 @@ class TriggerAuditCommand(BaseDecisionEngineCommand):
|
|||||||
self.messaging.topic_status.publish_event(event.get_type().name,
|
self.messaging.topic_status.publish_event(event.get_type().name,
|
||||||
payload)
|
payload)
|
||||||
|
|
||||||
def update_audit(self, request_context, audit_uuid, state):
|
def update_audit_state(self, request_context, audit_uuid, state):
|
||||||
LOG.debug("update audit {0} ".format(state))
|
LOG.debug("Update audit state:{0} ".format(state))
|
||||||
audit = Audit.get_by_uuid(request_context, audit_uuid)
|
audit = Audit.get_by_uuid(request_context, audit_uuid)
|
||||||
audit.state = state
|
audit.state = state
|
||||||
audit.save()
|
audit.save()
|
||||||
@@ -53,31 +54,32 @@ class TriggerAuditCommand(BaseDecisionEngineCommand):
|
|||||||
|
|
||||||
def execute(self, audit_uuid, request_context):
|
def execute(self, audit_uuid, request_context):
|
||||||
try:
|
try:
|
||||||
LOG.debug("Execute TriggerAuditCommand ")
|
LOG.debug("Trigger audit %s" % audit_uuid)
|
||||||
|
|
||||||
# 1 - change status to ONGOING
|
# change state to ONGOING
|
||||||
audit = self.update_audit(request_context, audit_uuid,
|
audit = self.update_audit_state(request_context, audit_uuid,
|
||||||
AuditStatus.ONGOING)
|
AuditStatus.ONGOING)
|
||||||
|
|
||||||
# 3 - Retrieve cluster-data-model
|
# Retrieve cluster-data-model
|
||||||
cluster = self.model_collector.get_latest_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_template = AuditTemplate.get_by_id(request_context,
|
||||||
audit.audit_template_id)
|
audit.audit_template_id)
|
||||||
|
|
||||||
self.strategy_context.set_goal(audit_template.goal)
|
self.strategy_context.set_goal(audit_template.goal)
|
||||||
|
|
||||||
# 5 - compute change requests
|
# compute change requests
|
||||||
solution = self.strategy_context.execute_strategy(cluster)
|
solution = self.strategy_context.execute_strategy(cluster)
|
||||||
|
|
||||||
# 6 - create an action plan
|
# create an action plan
|
||||||
planner = DefaultPlanner()
|
planner = DefaultPlanner()
|
||||||
planner.schedule(request_context, audit.id, solution)
|
planner.schedule(request_context, audit.id, solution)
|
||||||
|
|
||||||
# 7 - change status to SUCCEEDED and notify
|
# change state to SUCCEEDED and notify
|
||||||
self.update_audit(request_context, audit_uuid,
|
self.update_audit_state(request_context, audit_uuid,
|
||||||
AuditStatus.SUCCEEDED)
|
AuditStatus.SUCCEEDED)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.update_audit(request_context, audit_uuid, AuditStatus.FAILED)
|
LOG.exception(e)
|
||||||
LOG.error("Execute audit command {0} ".format(unicode(e)))
|
self.update_audit_state(request_context, audit_uuid,
|
||||||
|
AuditStatus.FAILED)
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
#
|
#
|
||||||
from oslo_log import log
|
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 \
|
from watcher.metrics_engine.cluster_model_collector.manager import \
|
||||||
CollectorManager
|
CollectorManager
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ class AuditEndpoint(object):
|
|||||||
def do_trigger_audit(self, context, audit_uuid):
|
def do_trigger_audit(self, context, audit_uuid):
|
||||||
model_collector = self.manager.get_cluster_model_collector()
|
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)
|
audit.execute(audit_uuid, context)
|
||||||
|
|
||||||
def trigger_audit(self, context, audit_uuid):
|
def trigger_audit(self, context, audit_uuid):
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
from mock import call
|
from mock import call
|
||||||
from mock import MagicMock
|
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.decision_engine.messaging.events import Events
|
||||||
from watcher.objects.audit import Audit
|
from watcher.objects.audit import Audit
|
||||||
from watcher.objects.audit import AuditStatus
|
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
|
from watcher.tests.objects import utils as obj_utils
|
||||||
|
|
||||||
|
|
||||||
class TestTriggerAuditCommand(DbTestCase):
|
class TestDefaultAuditHandler(DbTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestTriggerAuditCommand, self).setUp()
|
super(TestDefaultAuditHandler, self).setUp()
|
||||||
self.audit_template = obj_utils.create_test_audit_template(
|
self.audit_template = obj_utils.create_test_audit_template(
|
||||||
self.context)
|
self.context)
|
||||||
self.audit = obj_utils.create_test_audit(
|
self.audit = obj_utils.create_test_audit(
|
||||||
@@ -36,24 +36,24 @@ class TestTriggerAuditCommand(DbTestCase):
|
|||||||
|
|
||||||
def test_trigger_audit_without_errors(self):
|
def test_trigger_audit_without_errors(self):
|
||||||
model_collector = FakerModelCollector()
|
model_collector = FakerModelCollector()
|
||||||
command = TriggerAuditCommand(MagicMock(), model_collector)
|
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
|
||||||
command.execute(self.audit.uuid, self.context)
|
audit_handler.execute(self.audit.uuid, self.context)
|
||||||
|
|
||||||
def test_trigger_audit_state_success(self):
|
def test_trigger_audit_state_success(self):
|
||||||
model_collector = FakerModelCollector()
|
model_collector = FakerModelCollector()
|
||||||
command = TriggerAuditCommand(MagicMock(), model_collector)
|
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
|
||||||
command.strategy_context.execute_strategy = MagicMock()
|
audit_handler.strategy_context.execute_strategy = MagicMock()
|
||||||
command.execute(self.audit.uuid, self.context)
|
audit_handler.execute(self.audit.uuid, self.context)
|
||||||
audit = Audit.get_by_uuid(self.context, self.audit.uuid)
|
audit = Audit.get_by_uuid(self.context, self.audit.uuid)
|
||||||
self.assertEqual(AuditStatus.SUCCEEDED, audit.state)
|
self.assertEqual(AuditStatus.SUCCEEDED, audit.state)
|
||||||
|
|
||||||
def test_trigger_audit_send_notification(self):
|
def test_trigger_audit_send_notification(self):
|
||||||
messaging = MagicMock()
|
messaging = MagicMock()
|
||||||
model_collector = FakerModelCollector()
|
model_collector = FakerModelCollector()
|
||||||
command = TriggerAuditCommand(messaging, model_collector)
|
audit_handler = DefaultAuditHandler(messaging, model_collector)
|
||||||
command.strategy_context.execute_strategy = MagicMock()
|
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, {
|
call_on_going = call(Events.TRIGGER_AUDIT.name, {
|
||||||
'audit_status': AuditStatus.ONGOING,
|
'audit_status': AuditStatus.ONGOING,
|
||||||
@@ -15,9 +15,8 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import mock
|
import mock
|
||||||
from mock import MagicMock
|
from mock import MagicMock
|
||||||
|
|
||||||
from watcher.common import utils
|
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.decision_engine.messaging.audit_endpoint import AuditEndpoint
|
||||||
from watcher.metrics_engine.cluster_model_collector.manager import \
|
from watcher.metrics_engine.cluster_model_collector.manager import \
|
||||||
CollectorManager
|
CollectorManager
|
||||||
@@ -26,9 +25,9 @@ from watcher.tests.decision_engine.strategy.strategies.faker_cluster_state impor
|
|||||||
FakerModelCollector
|
FakerModelCollector
|
||||||
|
|
||||||
|
|
||||||
class TriggerAuditCommandWithExecutor(TriggerAuditCommand):
|
class DefaultAuditHandlerMock(DefaultAuditHandler):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TriggerAuditCommand, self).setUp()
|
super(DefaultAuditHandlerMock, self).setUp()
|
||||||
|
|
||||||
def executor(self):
|
def executor(self):
|
||||||
pass
|
pass
|
||||||
@@ -42,28 +41,28 @@ class TestAuditEndpoint(base.TestCase):
|
|||||||
def test_do_trigger_audit(self):
|
def test_do_trigger_audit(self):
|
||||||
audit_uuid = utils.generate_uuid()
|
audit_uuid = utils.generate_uuid()
|
||||||
model_collector = FakerModelCollector()
|
model_collector = FakerModelCollector()
|
||||||
command = TriggerAuditCommand(MagicMock(), model_collector)
|
audit_handler = DefaultAuditHandler(MagicMock(), model_collector)
|
||||||
endpoint = AuditEndpoint(command)
|
endpoint = AuditEndpoint(audit_handler)
|
||||||
|
|
||||||
with mock.patch.object(CollectorManager, 'get_cluster_model_collector') \
|
with mock.patch.object(CollectorManager, 'get_cluster_model_collector') \
|
||||||
as mock_call2:
|
as mock_call2:
|
||||||
mock_call2.return_value = 0
|
mock_call2.return_value = 0
|
||||||
|
|
||||||
with mock.patch.object(TriggerAuditCommand, 'execute') \
|
with mock.patch.object(DefaultAuditHandler, 'execute') \
|
||||||
as mock_call:
|
as mock_call:
|
||||||
mock_call.return_value = 0
|
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_call.assert_called_once_with()
|
||||||
mock_call2.assert_called_once_with()
|
mock_call2.assert_called_once_with()
|
||||||
|
|
||||||
def test_trigger_audit(self):
|
def test_trigger_audit(self):
|
||||||
audit_uuid = utils.generate_uuid()
|
audit_uuid = utils.generate_uuid()
|
||||||
model_collector = FakerModelCollector()
|
model_collector = FakerModelCollector()
|
||||||
command = TriggerAuditCommandWithExecutor(MagicMock(),
|
audit_handler = DefaultAuditHandlerMock(MagicMock(),
|
||||||
model_collector)
|
model_collector)
|
||||||
endpoint = AuditEndpoint(command)
|
endpoint = AuditEndpoint(audit_handler)
|
||||||
|
|
||||||
with mock.patch.object(TriggerAuditCommandWithExecutor, 'executor') \
|
with mock.patch.object(DefaultAuditHandlerMock, 'executor') \
|
||||||
as mock_call:
|
as mock_call:
|
||||||
mock_call.return_value = 0
|
mock_call.return_value = 0
|
||||||
endpoint.trigger_audit(command, audit_uuid)
|
endpoint.trigger_audit(audit_handler, audit_uuid)
|
||||||
|
|||||||
Reference in New Issue
Block a user