def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.masakari_obj_get_changes() if 'uuid' not in updates: updates['uuid'] = uuidutils.generate_uuid() LOG.debug('Generated uuid %(uuid)s for failover segment', dict(uuid=updates['uuid'])) api_utils.notify_about_segment_api( self._context, self, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.START) db_segment = db.failover_segment_create(self._context, updates) api_utils.notify_about_segment_api( self._context, self, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_segment)
def create_segment(self, context, segment_data): """Create segment""" segment = objects.FailoverSegment(context=context) # Populate segment object for create segment.name = segment_data.get('name') segment.description = segment_data.get('description') segment.recovery_method = segment_data.get('recovery_method') segment.service_type = segment_data.get('service_type') segment.enabled = strutils.bool_from_string(segment_data.get( 'enabled', True), strict=True) try: segment.create() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_segment_api( context, segment, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return segment
def save(self): updates = self.masakari_obj_get_changes() updates.pop('id', None) api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_UPDATE, phase=fields.EventNotificationPhase.START) db_segment = db.failover_segment_update(self._context, self.uuid, updates) api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_UPDATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_segment)
def delete_segment(self, context, uuid): """Deletes the segment.""" segment = objects.FailoverSegment.get_by_uuid(context, uuid) if is_failover_segment_under_recovery(segment): msg = _("Failover segment (%s) can't be deleted as " "it is in-use to process notifications.") % uuid LOG.error(msg) raise exception.FailoverSegmentInUse(msg) try: segment.destroy() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_segment_api(context, segment, action=fields.EventNotificationAction.SEGMENT_DELETE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb)
def create_segment(self, context, segment_data): """Create segment""" segment = objects.FailoverSegment(context=context) # Populate segment object for create segment.name = segment_data.get('name') segment.description = segment_data.get('description') segment.recovery_method = segment_data.get('recovery_method') segment.service_type = segment_data.get('service_type') try: segment.create() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_segment_api(context, segment, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return segment
def destroy(self): if not self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='destroy', reason='already destroyed') if not self.obj_attr_is_set('uuid'): raise exception.ObjectActionError(action='destroy', reason='no uuid') api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_DELETE, phase=fields.EventNotificationPhase.START) db.failover_segment_delete(self._context, self.uuid) api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_DELETE, phase=fields.EventNotificationPhase.END) delattr(self, base.get_attrname('id'))
def update_segment(self, context, uuid, segment_data): """Update the properties of a failover segment.""" segment = objects.FailoverSegment.get_by_uuid(context, uuid) if is_failover_segment_under_recovery(segment): msg = _("Failover segment %s can't be updated as " "it is in-use to process notifications.") % uuid LOG.error(msg) raise exception.FailoverSegmentInUse(msg) try: segment.update(segment_data) segment.save() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_segment_api(context, segment, action=fields.EventNotificationAction.SEGMENT_UPDATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return segment
def test_notify_about_segment_api( self, mock_from_exception, mock_SegmentApiPayload, mock_SegmentApiNotification, mock_NotificationPublisher, mock_EventType): mock_fault = mock.Mock() mock_from_exception.return_value = mock_fault mock_payload = mock.Mock() mock_SegmentApiPayload.return_value = mock_payload mock_api_notification = mock.Mock() mock_SegmentApiNotification.return_value = mock_api_notification mock_api_notification.emit.return_value = None mock_publisher = mock.Mock() mock_NotificationPublisher.return_value = mock_publisher mock_event_type = mock.Mock() mock_EventType.return_value = mock_event_type mock_context = mock.Mock() segment = objects.FailoverSegment() action = fields.EventNotificationAction.SEGMENT_CREATE phase = fields.EventNotificationPhase.ERROR e = Exception() api_utils.notify_about_segment_api(mock_context, segment, action=action, phase=phase, exception=e) mock_from_exception.assert_called_once_with(e, None) mock_SegmentApiPayload.assert_called_once_with( segment=segment, fault=mock_fault) mock_SegmentApiNotification.assert_called_once_with( context=mock_context, priority=fields.EventNotificationPriority.ERROR, publisher=mock_publisher, event_type=mock_event_type, payload=mock_payload) mock_NotificationPublisher.assert_called_once_with( context=mock_context, host=socket.gethostname(), binary='masakari-api') mock_EventType.assert_called_once_with( action=action, phase=phase) mock_api_notification.emit.assert_called_once_with(mock_context)
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.masakari_obj_get_changes() if 'uuid' not in updates: updates['uuid'] = uuidutils.generate_uuid() LOG.debug('Generated uuid %(uuid)s for failover segment', dict(uuid=updates['uuid'])) api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.START) db_segment = db.failover_segment_create(self._context, updates) api_utils.notify_about_segment_api(self._context, self, action=fields.EventNotificationAction.SEGMENT_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_segment)