Added action_plan.create|update|delete notifs
In this changeset, I added 3 notifications: - action_plan.create - action_plan.update - action_plan.delete Partially Implements: blueprint action-plan-versioned-notifications-api Change-Id: I8821fc6f47e7486037839d81bed9e28020b02fdd
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
# need to be changed after we moved these function inside the package
|
||||
# Todo(gibi): remove these imports after legacy notifications using these are
|
||||
# transformed to versioned notifications
|
||||
from watcher.notifications import action_plan # noqa
|
||||
from watcher.notifications import audit # noqa
|
||||
from watcher.notifications import exception # noqa
|
||||
from watcher.notifications import goal # noqa
|
||||
|
||||
267
watcher/notifications/action_plan.py
Normal file
267
watcher/notifications/action_plan.py
Normal file
@@ -0,0 +1,267 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2017 b<>com
|
||||
#
|
||||
# Authors: Vincent FRANCOISE <vincent.francoise@b-com.com>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
|
||||
from watcher.common import context as wcontext
|
||||
from watcher.common import exception
|
||||
from watcher.notifications import audit as audit_notifications
|
||||
from watcher.notifications import base as notificationbase
|
||||
from watcher.notifications import strategy as strategy_notifications
|
||||
from watcher import objects
|
||||
from watcher.objects import base
|
||||
from watcher.objects import fields as wfields
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanPayload(notificationbase.NotificationPayloadBase):
|
||||
SCHEMA = {
|
||||
'uuid': ('action_plan', 'uuid'),
|
||||
|
||||
'state': ('action_plan', 'state'),
|
||||
'global_efficacy': ('action_plan', 'global_efficacy'),
|
||||
'audit_uuid': ('audit', 'uuid'),
|
||||
'strategy_uuid': ('strategy', 'uuid'),
|
||||
|
||||
'created_at': ('action_plan', 'created_at'),
|
||||
'updated_at': ('action_plan', 'updated_at'),
|
||||
'deleted_at': ('action_plan', 'deleted_at'),
|
||||
}
|
||||
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'uuid': wfields.UUIDField(),
|
||||
'state': wfields.StringField(),
|
||||
'global_efficacy': wfields.FlexibleDictField(nullable=True),
|
||||
'audit_uuid': wfields.UUIDField(),
|
||||
'strategy_uuid': wfields.UUIDField(),
|
||||
'audit': wfields.ObjectField('TerseAuditPayload'),
|
||||
'strategy': wfields.ObjectField('StrategyPayload'),
|
||||
|
||||
'created_at': wfields.DateTimeField(nullable=True),
|
||||
'updated_at': wfields.DateTimeField(nullable=True),
|
||||
'deleted_at': wfields.DateTimeField(nullable=True),
|
||||
}
|
||||
|
||||
def __init__(self, action_plan, audit, strategy, **kwargs):
|
||||
super(ActionPlanPayload, self).__init__(
|
||||
audit=audit, strategy=strategy, **kwargs)
|
||||
self.populate_schema(
|
||||
action_plan=action_plan, audit=audit, strategy=strategy)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanStateUpdatePayload(notificationbase.NotificationPayloadBase):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'old_state': wfields.StringField(nullable=True),
|
||||
'state': wfields.StringField(nullable=True),
|
||||
}
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanCreatePayload(ActionPlanPayload):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
fields = {}
|
||||
|
||||
def __init__(self, action_plan, audit, strategy):
|
||||
super(ActionPlanCreatePayload, self).__init__(
|
||||
action_plan=action_plan,
|
||||
audit=audit,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanUpdatePayload(ActionPlanPayload):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
fields = {
|
||||
'state_update': wfields.ObjectField('ActionPlanStateUpdatePayload'),
|
||||
}
|
||||
|
||||
def __init__(self, action_plan, state_update, audit, strategy):
|
||||
super(ActionPlanUpdatePayload, self).__init__(
|
||||
action_plan=action_plan,
|
||||
state_update=state_update,
|
||||
audit=audit,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanActionPayload(ActionPlanPayload):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
fields = {
|
||||
'fault': wfields.ObjectField('ExceptionPayload', nullable=True),
|
||||
}
|
||||
|
||||
def __init__(self, action_plan, audit, strategy, **kwargs):
|
||||
super(ActionPlanActionPayload, self).__init__(
|
||||
action_plan=action_plan,
|
||||
audit=audit,
|
||||
strategy=strategy,
|
||||
**kwargs)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanDeletePayload(ActionPlanPayload):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
fields = {}
|
||||
|
||||
def __init__(self, action_plan, audit, strategy):
|
||||
super(ActionPlanDeletePayload, self).__init__(
|
||||
action_plan=action_plan,
|
||||
audit=audit,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
@notificationbase.notification_sample('action_plan-create.json')
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanCreateNotification(notificationbase.NotificationBase):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'payload': wfields.ObjectField('ActionPlanCreatePayload')
|
||||
}
|
||||
|
||||
|
||||
@notificationbase.notification_sample('action_plan-update.json')
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanUpdateNotification(notificationbase.NotificationBase):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'payload': wfields.ObjectField('ActionPlanUpdatePayload')
|
||||
}
|
||||
|
||||
|
||||
@notificationbase.notification_sample('action_plan-delete.json')
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class ActionPlanDeleteNotification(notificationbase.NotificationBase):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'payload': wfields.ObjectField('ActionPlanDeletePayload')
|
||||
}
|
||||
|
||||
|
||||
def _get_common_payload(action_plan):
|
||||
audit = None
|
||||
strategy = None
|
||||
try:
|
||||
audit = action_plan.audit
|
||||
strategy = action_plan.strategy
|
||||
except NotImplementedError:
|
||||
raise exception.EagerlyLoadedActionPlanRequired(
|
||||
action_plan=action_plan.uuid)
|
||||
|
||||
goal = objects.Goal.get(
|
||||
wcontext.make_context(show_deleted=True), audit.goal_id)
|
||||
audit_payload = audit_notifications.TerseAuditPayload(
|
||||
audit=audit, goal_uuid=goal.uuid)
|
||||
|
||||
strategy_payload = strategy_notifications.StrategyPayload(
|
||||
strategy=strategy)
|
||||
|
||||
return audit_payload, strategy_payload
|
||||
|
||||
|
||||
def send_create(context, action_plan, service='infra-optim', host=None):
|
||||
"""Emit an action_plan.create notification."""
|
||||
audit_payload, strategy_payload = _get_common_payload(action_plan)
|
||||
|
||||
versioned_payload = ActionPlanCreatePayload(
|
||||
action_plan=action_plan,
|
||||
audit=audit_payload,
|
||||
strategy=strategy_payload,
|
||||
)
|
||||
|
||||
notification = ActionPlanCreateNotification(
|
||||
priority=wfields.NotificationPriority.INFO,
|
||||
event_type=notificationbase.EventType(
|
||||
object='action_plan',
|
||||
action=wfields.NotificationAction.CREATE),
|
||||
publisher=notificationbase.NotificationPublisher(
|
||||
host=host or CONF.host,
|
||||
binary=service),
|
||||
payload=versioned_payload)
|
||||
|
||||
notification.emit(context)
|
||||
|
||||
|
||||
def send_update(context, action_plan, service='infra-optim',
|
||||
host=None, old_state=None):
|
||||
"""Emit an action_plan.update notification."""
|
||||
audit_payload, strategy_payload = _get_common_payload(action_plan)
|
||||
|
||||
state_update = ActionPlanStateUpdatePayload(
|
||||
old_state=old_state,
|
||||
state=action_plan.state if old_state else None)
|
||||
|
||||
versioned_payload = ActionPlanUpdatePayload(
|
||||
action_plan=action_plan,
|
||||
state_update=state_update,
|
||||
audit=audit_payload,
|
||||
strategy=strategy_payload,
|
||||
)
|
||||
|
||||
notification = ActionPlanUpdateNotification(
|
||||
priority=wfields.NotificationPriority.INFO,
|
||||
event_type=notificationbase.EventType(
|
||||
object='action_plan',
|
||||
action=wfields.NotificationAction.UPDATE),
|
||||
publisher=notificationbase.NotificationPublisher(
|
||||
host=host or CONF.host,
|
||||
binary=service),
|
||||
payload=versioned_payload)
|
||||
|
||||
notification.emit(context)
|
||||
|
||||
|
||||
def send_delete(context, action_plan, service='infra-optim', host=None):
|
||||
"""Emit an action_plan.delete notification."""
|
||||
audit_payload, strategy_payload = _get_common_payload(action_plan)
|
||||
|
||||
versioned_payload = ActionPlanDeletePayload(
|
||||
action_plan=action_plan,
|
||||
audit=audit_payload,
|
||||
strategy=strategy_payload,
|
||||
)
|
||||
|
||||
notification = ActionPlanDeleteNotification(
|
||||
priority=wfields.NotificationPriority.INFO,
|
||||
event_type=notificationbase.EventType(
|
||||
object='action_plan',
|
||||
action=wfields.NotificationAction.DELETE),
|
||||
publisher=notificationbase.NotificationPublisher(
|
||||
host=host or CONF.host,
|
||||
binary=service),
|
||||
payload=versioned_payload)
|
||||
|
||||
notification.emit(context)
|
||||
@@ -30,7 +30,7 @@ CONF = cfg.CONF
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class AuditPayload(notificationbase.NotificationPayloadBase):
|
||||
class TerseAuditPayload(notificationbase.NotificationPayloadBase):
|
||||
SCHEMA = {
|
||||
'uuid': ('audit', 'uuid'),
|
||||
|
||||
@@ -57,19 +57,54 @@ class AuditPayload(notificationbase.NotificationPayloadBase):
|
||||
'scope': wfields.FlexibleListOfDictField(nullable=True),
|
||||
'goal_uuid': wfields.UUIDField(),
|
||||
'strategy_uuid': wfields.UUIDField(nullable=True),
|
||||
'goal': wfields.ObjectField('GoalPayload'),
|
||||
'strategy': wfields.ObjectField('StrategyPayload', nullable=True),
|
||||
|
||||
'created_at': wfields.DateTimeField(nullable=True),
|
||||
'updated_at': wfields.DateTimeField(nullable=True),
|
||||
'deleted_at': wfields.DateTimeField(nullable=True),
|
||||
}
|
||||
|
||||
def __init__(self, audit, **kwargs):
|
||||
super(AuditPayload, self).__init__(**kwargs)
|
||||
def __init__(self, audit, goal_uuid, strategy_uuid=None, **kwargs):
|
||||
super(TerseAuditPayload, self).__init__(
|
||||
goal_uuid=goal_uuid, strategy_uuid=strategy_uuid, **kwargs)
|
||||
self.populate_schema(audit=audit)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class AuditPayload(TerseAuditPayload):
|
||||
SCHEMA = {
|
||||
'uuid': ('audit', 'uuid'),
|
||||
|
||||
'audit_type': ('audit', 'audit_type'),
|
||||
'state': ('audit', 'state'),
|
||||
'parameters': ('audit', 'parameters'),
|
||||
'interval': ('audit', 'interval'),
|
||||
'scope': ('audit', 'scope'),
|
||||
|
||||
'created_at': ('audit', 'created_at'),
|
||||
'updated_at': ('audit', 'updated_at'),
|
||||
'deleted_at': ('audit', 'deleted_at'),
|
||||
}
|
||||
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'goal': wfields.ObjectField('GoalPayload'),
|
||||
'strategy': wfields.ObjectField('StrategyPayload', nullable=True),
|
||||
}
|
||||
|
||||
def __init__(self, audit, goal, strategy=None, **kwargs):
|
||||
if not kwargs.get('goal_uuid'):
|
||||
kwargs['goal_uuid'] = goal.uuid
|
||||
|
||||
if strategy and not kwargs.get('strategy_uuid'):
|
||||
kwargs['strategy_uuid'] = strategy.uuid
|
||||
|
||||
super(AuditPayload, self).__init__(
|
||||
audit=audit, goal=goal,
|
||||
strategy=strategy, **kwargs)
|
||||
|
||||
|
||||
@base.WatcherObjectRegistry.register_notification
|
||||
class AuditStateUpdatePayload(notificationbase.NotificationPayloadBase):
|
||||
# Version 1.0: Initial version
|
||||
@@ -91,6 +126,7 @@ class AuditCreatePayload(AuditPayload):
|
||||
super(AuditCreatePayload, self).__init__(
|
||||
audit=audit,
|
||||
goal=goal,
|
||||
goal_uuid=goal.uuid,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
@@ -107,6 +143,7 @@ class AuditUpdatePayload(AuditPayload):
|
||||
audit=audit,
|
||||
state_update=state_update,
|
||||
goal=goal,
|
||||
goal_uuid=goal.uuid,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
@@ -122,6 +159,7 @@ class AuditActionPayload(AuditPayload):
|
||||
super(AuditActionPayload, self).__init__(
|
||||
audit=audit,
|
||||
goal=goal,
|
||||
goal_uuid=goal.uuid,
|
||||
strategy=strategy,
|
||||
**kwargs)
|
||||
|
||||
@@ -136,6 +174,7 @@ class AuditDeletePayload(AuditPayload):
|
||||
super(AuditDeletePayload, self).__init__(
|
||||
audit=audit,
|
||||
goal=goal,
|
||||
goal_uuid=goal.uuid,
|
||||
strategy=strategy)
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
|
||||
from watcher.common import exception
|
||||
from watcher.common import rpc
|
||||
@@ -20,6 +21,7 @@ from watcher.objects import base
|
||||
from watcher.objects import fields as wfields
|
||||
|
||||
CONF = cfg.CONF
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
# Definition of notification levels in increasing order of severity
|
||||
NOTIFY_LEVELS = {
|
||||
@@ -59,7 +61,8 @@ class EventType(NotificationObject):
|
||||
# Version 1.0: Initial version
|
||||
# Version 1.1: Added STRATEGY action in NotificationAction enum
|
||||
# Version 1.2: Added PLANNER action in NotificationAction enum
|
||||
VERSION = '1.2'
|
||||
# Version 1.3: Added EXECUTION action in NotificationAction enum
|
||||
VERSION = '1.3'
|
||||
|
||||
fields = {
|
||||
'object': wfields.StringField(),
|
||||
@@ -171,6 +174,7 @@ class NotificationBase(NotificationObject):
|
||||
def _emit(self, context, event_type, publisher_id, payload):
|
||||
notifier = rpc.get_notifier(publisher_id)
|
||||
notify = getattr(notifier, self.priority)
|
||||
LOG.debug("Emitting notification `%s`", event_type)
|
||||
notify(context, event_type=event_type, payload=payload)
|
||||
|
||||
def emit(self, context):
|
||||
|
||||
Reference in New Issue
Block a user