Send soft_delete from context manager
This patch moves soft_delete notification sending to the notify_about_instance_delete context manager to unify the delete notification sending code practice. Implements: bp versioned-notification-transformation-stein Change-Id: Ib6f95c22ffd3ea235b60db4da32094d49c2efa2a
This commit is contained in:
parent
d3097e52b3
commit
e83dbe1205
@ -1833,7 +1833,7 @@ class API(base.Base):
|
|||||||
# FIXME: When the instance context is targeted,
|
# FIXME: When the instance context is targeted,
|
||||||
# we can remove this
|
# we can remove this
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, cctxt, instance):
|
self.notifier, cctxt, instance, CONF.host):
|
||||||
instance.destroy()
|
instance.destroy()
|
||||||
else:
|
else:
|
||||||
instance.destroy()
|
instance.destroy()
|
||||||
@ -1891,7 +1891,7 @@ class API(base.Base):
|
|||||||
try:
|
try:
|
||||||
# Now destroy the instance from the cell it lives in.
|
# Now destroy the instance from the cell it lives in.
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, context, instance):
|
self.notifier, context, instance, CONF.host):
|
||||||
instance.destroy()
|
instance.destroy()
|
||||||
except exception.InstanceNotFound:
|
except exception.InstanceNotFound:
|
||||||
pass
|
pass
|
||||||
@ -1962,7 +1962,7 @@ class API(base.Base):
|
|||||||
if not instance.host and not may_have_ports_or_volumes:
|
if not instance.host and not may_have_ports_or_volumes:
|
||||||
try:
|
try:
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, context, instance,
|
self.notifier, context, instance, CONF.host,
|
||||||
delete_type
|
delete_type
|
||||||
if delete_type != 'soft_delete'
|
if delete_type != 'soft_delete'
|
||||||
else 'delete'):
|
else 'delete'):
|
||||||
@ -2111,7 +2111,7 @@ class API(base.Base):
|
|||||||
LOG.warning("instance's host %s is down, deleting from "
|
LOG.warning("instance's host %s is down, deleting from "
|
||||||
"database", instance.host, instance=instance)
|
"database", instance.host, instance=instance)
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, context, instance,
|
self.notifier, context, instance, CONF.host,
|
||||||
delete_type if delete_type != 'soft_delete' else 'delete'):
|
delete_type if delete_type != 'soft_delete' else 'delete'):
|
||||||
|
|
||||||
elevated = context.elevated()
|
elevated = context.elevated()
|
||||||
|
@ -2824,11 +2824,8 @@ class ComputeManager(manager.Manager):
|
|||||||
def soft_delete_instance(self, context, instance):
|
def soft_delete_instance(self, context, instance):
|
||||||
"""Soft delete an instance on this host."""
|
"""Soft delete an instance on this host."""
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, context, instance, 'soft_delete',
|
self.notifier, context, instance, self.host, 'soft_delete',
|
||||||
fields.NotificationSource.COMPUTE):
|
source=fields.NotificationSource.COMPUTE):
|
||||||
compute_utils.notify_about_instance_action(context, instance,
|
|
||||||
self.host, action=fields.NotificationAction.SOFT_DELETE,
|
|
||||||
phase=fields.NotificationPhase.START)
|
|
||||||
try:
|
try:
|
||||||
self.driver.soft_delete(instance)
|
self.driver.soft_delete(instance)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
@ -2839,10 +2836,6 @@ class ComputeManager(manager.Manager):
|
|||||||
instance.vm_state = vm_states.SOFT_DELETED
|
instance.vm_state = vm_states.SOFT_DELETED
|
||||||
instance.task_state = None
|
instance.task_state = None
|
||||||
instance.save(expected_task_state=[task_states.SOFT_DELETING])
|
instance.save(expected_task_state=[task_states.SOFT_DELETING])
|
||||||
compute_utils.notify_about_instance_action(
|
|
||||||
context, instance, self.host,
|
|
||||||
action=fields.NotificationAction.SOFT_DELETE,
|
|
||||||
phase=fields.NotificationPhase.END)
|
|
||||||
|
|
||||||
@wrap_exception()
|
@wrap_exception()
|
||||||
@reverts_task_state
|
@reverts_task_state
|
||||||
|
@ -1156,32 +1156,32 @@ class UnlimitedSemaphore(object):
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def notify_about_instance_delete(notifier, context, instance,
|
def notify_about_instance_delete(notifier, context, instance, host,
|
||||||
delete_type='delete',
|
delete_type='delete',
|
||||||
source=fields.NotificationSource.API):
|
source=fields.NotificationSource.API):
|
||||||
try:
|
try:
|
||||||
notify_about_instance_usage(notifier, context, instance,
|
notify_about_instance_usage(notifier, context, instance,
|
||||||
"%s.start" % delete_type)
|
"%s.start" % delete_type)
|
||||||
# Note(gibi): soft_delete and force_delete types will be handled in a
|
# Note(gibi): force_delete types will be handled in a
|
||||||
# subsequent patch
|
# subsequent patch
|
||||||
if delete_type == 'delete':
|
if delete_type in ['delete', 'soft_delete']:
|
||||||
notify_about_instance_action(
|
notify_about_instance_action(
|
||||||
context,
|
context,
|
||||||
instance,
|
instance,
|
||||||
host=CONF.host,
|
host=host,
|
||||||
source=source,
|
source=source,
|
||||||
action=fields.NotificationAction.DELETE,
|
action=delete_type,
|
||||||
phase=fields.NotificationPhase.START)
|
phase=fields.NotificationPhase.START)
|
||||||
|
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
notify_about_instance_usage(notifier, context, instance,
|
notify_about_instance_usage(notifier, context, instance,
|
||||||
"%s.end" % delete_type)
|
"%s.end" % delete_type)
|
||||||
if delete_type == 'delete':
|
if delete_type in ['delete', 'soft_delete']:
|
||||||
notify_about_instance_action(
|
notify_about_instance_action(
|
||||||
context,
|
context,
|
||||||
instance,
|
instance,
|
||||||
host=CONF.host,
|
host=host,
|
||||||
source=source,
|
source=source,
|
||||||
action=fields.NotificationAction.DELETE,
|
action=delete_type,
|
||||||
phase=fields.NotificationPhase.END)
|
phase=fields.NotificationPhase.END)
|
||||||
|
@ -1419,7 +1419,7 @@ class ComputeTaskManager(base.Base):
|
|||||||
# bdm, tags and instance record.
|
# bdm, tags and instance record.
|
||||||
with obj_target_cell(instance, cell) as cctxt:
|
with obj_target_cell(instance, cell) as cctxt:
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
self.notifier, cctxt, instance,
|
self.notifier, cctxt, instance, CONF.host,
|
||||||
source=fields.NotificationSource.CONDUCTOR):
|
source=fields.NotificationSource.CONDUCTOR):
|
||||||
try:
|
try:
|
||||||
instance.destroy()
|
instance.destroy()
|
||||||
|
@ -1712,7 +1712,7 @@ class _ComputeAPIUnitTestMixIn(object):
|
|||||||
_lookup_instance.assert_called_once_with(
|
_lookup_instance.assert_called_once_with(
|
||||||
self.context, instance.uuid)
|
self.context, instance.uuid)
|
||||||
notify_mock.assert_called_once_with(
|
notify_mock.assert_called_once_with(
|
||||||
self.compute_api.notifier, self.context, instance)
|
self.compute_api.notifier, self.context, instance, 'fake-mini')
|
||||||
destroy_mock.assert_called_once_with()
|
destroy_mock.assert_called_once_with()
|
||||||
|
|
||||||
@mock.patch.object(context, 'target_cell')
|
@mock.patch.object(context, 'target_cell')
|
||||||
|
@ -4818,20 +4818,23 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
|||||||
def test_instance_soft_delete_notification(self):
|
def test_instance_soft_delete_notification(self):
|
||||||
inst_obj = fake_instance.fake_instance_obj(self.context,
|
inst_obj = fake_instance.fake_instance_obj(self.context,
|
||||||
vm_state=vm_states.ACTIVE)
|
vm_state=vm_states.ACTIVE)
|
||||||
|
inst_obj.system_metadata = {}
|
||||||
with test.nested(
|
with test.nested(
|
||||||
mock.patch.object(nova.compute.utils,
|
mock.patch.object(nova.compute.utils,
|
||||||
'notify_about_instance_action'),
|
'notify_about_instance_action'),
|
||||||
mock.patch.object(nova.compute.utils,
|
mock.patch.object(nova.compute.utils,
|
||||||
'notify_about_instance_delete'),
|
'notify_about_instance_usage'),
|
||||||
mock.patch.object(objects.Instance, 'save'),
|
mock.patch.object(objects.Instance, 'save'),
|
||||||
mock.patch.object(self.compute.driver, 'soft_delete')
|
mock.patch.object(self.compute.driver, 'soft_delete')
|
||||||
) as (fake_notify, fake_notify_usage, fake_save, fake_soft_delete):
|
) as (fake_notify, fake_legacy_notify, fake_save, fake_soft_delete):
|
||||||
self.compute.soft_delete_instance(self.context, inst_obj)
|
self.compute.soft_delete_instance(self.context, inst_obj)
|
||||||
fake_notify.assert_has_calls([
|
fake_notify.assert_has_calls([
|
||||||
mock.call(self.context, inst_obj, 'fake-mini',
|
mock.call(self.context, inst_obj, action='soft_delete',
|
||||||
action='soft_delete', phase='start'),
|
source='nova-compute', host='fake-mini',
|
||||||
mock.call(self.context, inst_obj, 'fake-mini',
|
phase='start'),
|
||||||
action='soft_delete', phase='end')])
|
mock.call(self.context, inst_obj, action='soft_delete',
|
||||||
|
source='nova-compute', host='fake-mini',
|
||||||
|
phase='end')])
|
||||||
|
|
||||||
def test_get_scheduler_hints(self):
|
def test_get_scheduler_hints(self):
|
||||||
# 1. No hints and no request_spec.
|
# 1. No hints and no request_spec.
|
||||||
|
@ -1107,7 +1107,7 @@ class ComputeUtilsTestCase(test.NoDBTestCase):
|
|||||||
instance = fake_instance.fake_instance_obj(
|
instance = fake_instance.fake_instance_obj(
|
||||||
self.context, expected_attrs=('system_metadata',))
|
self.context, expected_attrs=('system_metadata',))
|
||||||
with compute_utils.notify_about_instance_delete(
|
with compute_utils.notify_about_instance_delete(
|
||||||
mock.sentinel.notifier, self.context, instance):
|
mock.sentinel.notifier, self.context, instance, "fake-mini"):
|
||||||
instance.destroy()
|
instance.destroy()
|
||||||
expected_notify_calls = [
|
expected_notify_calls = [
|
||||||
mock.call(mock.sentinel.notifier, self.context, instance,
|
mock.call(mock.sentinel.notifier, self.context, instance,
|
||||||
|
Loading…
Reference in New Issue
Block a user