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 host', dict(uuid=updates['uuid'])) if 'failover_segment' not in updates: raise exception.ObjectActionError(action='create', reason='failover segment ' 'not assigned') segment = updates.pop('failover_segment') updates['failover_segment_id'] = segment.uuid api_utils.notify_about_host_api( self._context, self, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.START) db_host = db.host_create(self._context, updates) api_utils.notify_about_host_api( self._context, self, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_host)
def update_host(self, context, segment_uuid, id, host_data): """Update the host""" host = objects.Host.get_by_uuid(context, id, segment_uuid=segment_uuid) if is_failover_segment_under_recovery(host.failover_segment): msg = _("Host %s can't be updated as " "it is in-use to process notifications.") % host.uuid LOG.error(msg) raise exception.HostInUse(msg) if 'name' in host_data: self._is_valid_host_name(context, host_data.get('name')) if 'on_maintenance' in host_data: host_data['on_maintenance'] = strutils.bool_from_string( host_data['on_maintenance'], strict=True) if 'reserved' in host_data: host_data['reserved'] = strutils.bool_from_string( host_data['reserved'], strict=True) try: host.update(host_data) host.save() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_host_api( context, host, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return host
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 host', dict(uuid=updates['uuid'])) if 'failover_segment' in updates: raise exception.ObjectActionError(action='create', reason='failover segment ' 'assigned') api_utils.notify_about_host_api(self._context, self, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.START) db_host = db.host_create(self._context, updates) api_utils.notify_about_host_api(self._context, self, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_host)
def create_host(self, context, segment_uuid, host_data): """Create host""" segment = objects.FailoverSegment.get_by_uuid(context, segment_uuid) host = objects.Host(context=context) # Populate host object for create host.name = host_data.get('name') host.failover_segment = segment host.type = host_data.get('type') host.control_attributes = host_data.get('control_attributes') host.on_maintenance = strutils.bool_from_string(host_data.get( 'on_maintenance', False), strict=True) host.reserved = strutils.bool_from_string(host_data.get( 'reserved', False), strict=True) self._is_valid_host_name(context, host.name) try: host.create() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_host_api( context, host, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return host
def update_host(self, context, segment_uuid, id, host_data): """Update the host""" segment = objects.FailoverSegment.get_by_uuid(context, segment_uuid) host = objects.Host.get_by_uuid(context, id) if is_failover_segment_under_recovery(segment): msg = _("Host %s can't be updated as " "it is in-use to process notifications.") % host.uuid LOG.error(msg) raise exception.HostInUse(msg) if 'name' in host_data: self._is_valid_host_name(context, host_data.get('name')) if 'on_maintenance' in host_data: host_data['on_maintenance'] = strutils.bool_from_string( host_data['on_maintenance'], strict=True) if 'reserved' in host_data: host_data['reserved'] = strutils.bool_from_string( host_data['reserved'], strict=True) try: host.update(host_data) host.save() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_host_api(context, host, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return host
def create_host(self, context, segment_uuid, host_data): """Create host""" segment = objects.FailoverSegment.get_by_uuid(context, segment_uuid) host = objects.Host(context=context) # Populate host object for create host.name = host_data.get('name') host.failover_segment_id = segment.uuid host.type = host_data.get('type') host.control_attributes = host_data.get('control_attributes') host.on_maintenance = strutils.bool_from_string( host_data.get('on_maintenance', False), strict=True) host.reserved = strutils.bool_from_string( host_data.get('reserved', False), strict=True) self._is_valid_host_name(context, host.name) try: host.create() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_host_api(context, host, action=fields.EventNotificationAction.HOST_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return host
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_host_api(self._context, self, action=fields.EventNotificationAction.HOST_DELETE, phase=fields.EventNotificationPhase.START) db.host_delete(self._context, self.uuid) api_utils.notify_about_host_api(self._context, self, action=fields.EventNotificationAction.HOST_DELETE, phase=fields.EventNotificationPhase.END) delattr(self, base.get_attrname('id'))
def save(self): updates = self.masakari_obj_get_changes() if 'failover_segment' in updates: raise exception.ObjectActionError(action='save', reason='failover segment ' 'changed') updates.pop('id', None) api_utils.notify_about_host_api(self._context, self, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.START) db_host = db.host_update(self._context, self.uuid, updates) api_utils.notify_about_host_api(self._context, self, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_host)
def delete_host(self, context, segment_uuid, id): """Delete the host""" segment = objects.FailoverSegment.get_by_uuid(context, segment_uuid) host = objects.Host.get_by_uuid(context, id, segment_uuid=segment_uuid) if is_failover_segment_under_recovery(segment): msg = _("Host %s can't be deleted as " "it is in-use to process notifications.") % host.uuid LOG.error(msg) raise exception.HostInUse(msg) try: host.destroy() except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_host_api(context, host, action=fields.EventNotificationAction.HOST_DELETE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb)
def test_notify_about_host_api( self, mock_from_exception, mock_HostApiPayload, mock_HostApiNotification, mock_NotificationPublisher, mock_EventType): mock_fault = mock.Mock() mock_from_exception.return_value = mock_fault mock_payload = mock.Mock() mock_HostApiPayload.return_value = mock_payload mock_api_notification = mock.Mock() mock_HostApiNotification.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() host = host_obj.Host() action = fields.EventNotificationAction.HOST_CREATE phase = fields.EventNotificationPhase.ERROR e = Exception() api_utils.notify_about_host_api(mock_context, host, action=action, phase=phase, exception=e) mock_from_exception.assert_called_once_with(e, None) mock_HostApiPayload.assert_called_once_with( host=host, fault=mock_fault) mock_HostApiNotification.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_api_notification.emit.assert_called_once_with(mock_context) mock_EventType.assert_called_once_with( action=action, phase=phase)
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_host_api( self._context, self, action=fields.EventNotificationAction.HOST_DELETE, phase=fields.EventNotificationPhase.START) db.host_delete(self._context, self.uuid) api_utils.notify_about_host_api( self._context, self, action=fields.EventNotificationAction.HOST_DELETE, phase=fields.EventNotificationPhase.END) delattr(self, base.get_attrname('id'))
def save(self): updates = self.masakari_obj_get_changes() if 'failover_segment' in updates: raise exception.ObjectActionError(action='save', reason='failover segment ' 'changed') updates.pop('id', None) api_utils.notify_about_host_api( self._context, self, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.START) db_host = db.host_update(self._context, self.uuid, updates) api_utils.notify_about_host_api( self._context, self, action=fields.EventNotificationAction.HOST_UPDATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_host)