virt: Remove 'change_instance_metadata' API

This was used to propagate the metadata changes to the hypervisor. For
all non-XenAPI drivers, we still allow updating instance metadata via
the API (i.e. '/servers/{server_id}/metadata') but this simply changes
what is exposed via the metadata API.

Change-Id: Ibd0ffd9906e7d7f22a9233539091d450e8023f07
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2020-10-23 14:37:27 +01:00
parent e13e8c8cd4
commit 30067be9bd
8 changed files with 5 additions and 121 deletions

View File

@ -267,23 +267,6 @@ def reject_vtpm_instances(operation):
return outer return outer
def _diff_dict(orig, new):
"""Return a dict describing how to change orig to new. The keys
correspond to values that have changed; the value will be a list
of one or two elements. The first element of the list will be
either '+' or '-', indicating whether the key was updated or
deleted; if the key was updated, the list will contain a second
element, giving the updated value.
"""
# Figure out what keys went away
result = {k: ['-'] for k in set(orig.keys()) - set(new.keys())}
# Compute the updates
for key, value in new.items():
if key not in orig or value != orig[key]:
result[key] = ['+', value]
return result
def load_cells(): def load_cells():
global CELLS global CELLS
if not CELLS: if not CELLS:
@ -5004,9 +4987,6 @@ class API(base.Base):
def delete_instance_metadata(self, context, instance, key): def delete_instance_metadata(self, context, instance, key):
"""Delete the given metadata item from an instance.""" """Delete the given metadata item from an instance."""
instance.delete_metadata_key(key) instance.delete_metadata_key(key)
self.compute_rpcapi.change_instance_metadata(context,
instance=instance,
diff={key: ['-']})
@check_instance_lock @check_instance_lock
@check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.PAUSED, @check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.PAUSED,
@ -5020,7 +5000,6 @@ class API(base.Base):
`metadata` argument will be deleted. `metadata` argument will be deleted.
""" """
orig = dict(instance.metadata)
if delete: if delete:
_metadata = metadata _metadata = metadata
else: else:
@ -5030,10 +5009,7 @@ class API(base.Base):
self._check_metadata_properties_quota(context, _metadata) self._check_metadata_properties_quota(context, _metadata)
instance.metadata = _metadata instance.metadata = _metadata
instance.save() instance.save()
diff = _diff_dict(orig, instance.metadata)
self.compute_rpcapi.change_instance_metadata(context,
instance=instance,
diff=diff)
return _metadata return _metadata
@block_accelerators() @block_accelerators()

View File

@ -4228,13 +4228,11 @@ class ComputeManager(manager.Manager):
self.host, action=fields.NotificationAction.UNRESCUE, self.host, action=fields.NotificationAction.UNRESCUE,
phase=fields.NotificationPhase.END) phase=fields.NotificationPhase.END)
# TODO(stephenfin): Remove this once we bump the compute API to v6.0
@wrap_exception() @wrap_exception()
@wrap_instance_fault @wrap_instance_fault
def change_instance_metadata(self, context, diff, instance): def change_instance_metadata(self, context, diff, instance):
"""Update the metadata published to the instance.""" raise NotImplementedError()
LOG.debug("Changing instance metadata according to %r",
diff, instance=instance)
self.driver.change_instance_metadata(context, instance, diff)
@wrap_exception() @wrap_exception()
@wrap_instance_event(prefix='compute') @wrap_instance_event(prefix='compute')

View File

@ -554,13 +554,6 @@ class ComputeAPI(object):
server=_compute_host(None, instance), version=version) server=_compute_host(None, instance), version=version)
cctxt.cast(ctxt, 'attach_volume', instance=instance, bdm=bdm) cctxt.cast(ctxt, 'attach_volume', instance=instance, bdm=bdm)
def change_instance_metadata(self, ctxt, instance, diff):
version = '5.0'
cctxt = self.router.client(ctxt).prepare(
server=_compute_host(None, instance), version=version)
cctxt.cast(ctxt, 'change_instance_metadata',
instance=instance, diff=diff)
def check_can_live_migrate_destination(self, ctxt, instance, destination, def check_can_live_migrate_destination(self, ctxt, instance, destination,
block_migration, disk_over_commit, block_migration, disk_over_commit,
migration, limits): migration, limits):

View File

@ -82,10 +82,6 @@ def return_server_nonexistent(context, server_id,
raise exception.InstanceNotFound(instance_id=server_id) raise exception.InstanceNotFound(instance_id=server_id)
def fake_change_instance_metadata(self, context, instance, diff):
pass
class ServerMetaDataTestV21(test.TestCase): class ServerMetaDataTestV21(test.TestCase):
validation_ex = exception.ValidationError validation_ex = exception.ValidationError
validation_ex_large = validation_ex validation_ex_large = validation_ex
@ -104,9 +100,6 @@ class ServerMetaDataTestV21(test.TestCase):
self.stub_out('nova.db.api.instance_metadata_get', self.stub_out('nova.db.api.instance_metadata_get',
return_server_metadata) return_server_metadata)
self.stub_out(
'nova.compute.rpcapi.ComputeAPI.change_instance_metadata',
fake_change_instance_metadata)
self._set_up_resources() self._set_up_resources()
def _set_up_resources(self): def _set_up_resources(self):
@ -655,9 +648,6 @@ class BadStateServerMetaDataTestV21(test.TestCase):
super(BadStateServerMetaDataTestV21, self).setUp() super(BadStateServerMetaDataTestV21, self).setUp()
self.stub_out('nova.db.api.instance_metadata_get', self.stub_out('nova.db.api.instance_metadata_get',
return_server_metadata) return_server_metadata)
self.stub_out(
'nova.compute.rpcapi.ComputeAPI.change_instance_metadata',
fake_change_instance_metadata)
self.stub_out('nova.compute.api.API.get', self.stub_out('nova.compute.api.API.get',
fakes.fake_compute_get( fakes.fake_compute_get(
**{'uuid': '0cc3346e-9fef-4445-abe6-5d2b2690ec64', **{'uuid': '0cc3346e-9fef-4445-abe6-5d2b2690ec64',

View File

@ -7634,35 +7634,3 @@ class ComputeAPIUnitTestCase(_ComputeAPIUnitTestMixIn, test.NoDBTestCase):
exception.MixedInstanceNotSupportByComputeService, exception.MixedInstanceNotSupportByComputeService,
self.compute_api._check_compute_service_for_mixed_instance, self.compute_api._check_compute_service_for_mixed_instance,
fake_numa_topo) fake_numa_topo)
class DiffDictTestCase(test.NoDBTestCase):
"""Unit tests for _diff_dict()."""
def test_no_change(self):
old = dict(a=1, b=2, c=3)
new = dict(a=1, b=2, c=3)
diff = compute_api._diff_dict(old, new)
self.assertEqual(diff, {})
def test_new_key(self):
old = dict(a=1, b=2, c=3)
new = dict(a=1, b=2, c=3, d=4)
diff = compute_api._diff_dict(old, new)
self.assertEqual(diff, dict(d=['+', 4]))
def test_changed_key(self):
old = dict(a=1, b=2, c=3)
new = dict(a=1, b=4, c=3)
diff = compute_api._diff_dict(old, new)
self.assertEqual(diff, dict(b=['+', 4]))
def test_removed_key(self):
old = dict(a=1, b=2, c=3)
new = dict(a=1, c=3)
diff = compute_api._diff_dict(old, new)
self.assertEqual(diff, dict(b=['-']))

View File

@ -9457,16 +9457,8 @@ class ComputeAPITestCase(BaseTestCase):
self.assertEqual(len(instances), 3) self.assertEqual(len(instances), 3)
def test_instance_metadata(self): def test_instance_metadata(self):
meta_changes = [None]
self.flags(notify_on_state_change='vm_state', group='notifications') self.flags(notify_on_state_change='vm_state', group='notifications')
def fake_change_instance_metadata(inst, ctxt, diff, instance=None,
instance_uuid=None):
meta_changes[0] = diff
self.stub_out('nova.compute.rpcapi.ComputeAPI.'
'change_instance_metadata',
fake_change_instance_metadata)
_context = context.get_admin_context() _context = context.get_admin_context()
instance = self._create_fake_instance_obj({'metadata': instance = self._create_fake_instance_obj({'metadata':
{'key1': 'value1'}}) {'key1': 'value1'}})
@ -9478,7 +9470,6 @@ class ComputeAPITestCase(BaseTestCase):
{'key2': 'value2'}) {'key2': 'value2'})
metadata = self.compute_api.get_instance_metadata(_context, instance) metadata = self.compute_api.get_instance_metadata(_context, instance)
self.assertEqual(metadata, {'key1': 'value1', 'key2': 'value2'}) self.assertEqual(metadata, {'key1': 'value1', 'key2': 'value2'})
self.assertEqual(meta_changes, [{'key2': ['+', 'value2']}])
self.assertEqual(len(fake_notifier.NOTIFICATIONS), 1) self.assertEqual(len(fake_notifier.NOTIFICATIONS), 1)
msg = fake_notifier.NOTIFICATIONS[0] msg = fake_notifier.NOTIFICATIONS[0]
@ -9487,15 +9478,10 @@ class ComputeAPITestCase(BaseTestCase):
self.assertEqual(payload['metadata'], metadata) self.assertEqual(payload['metadata'], metadata)
new_metadata = {'key2': 'bah', 'key3': 'value3'} new_metadata = {'key2': 'bah', 'key3': 'value3'}
self.compute_api.update_instance_metadata(_context, instance, self.compute_api.update_instance_metadata(
new_metadata, delete=True) _context, instance, new_metadata, delete=True)
metadata = self.compute_api.get_instance_metadata(_context, instance) metadata = self.compute_api.get_instance_metadata(_context, instance)
self.assertEqual(metadata, new_metadata) self.assertEqual(metadata, new_metadata)
self.assertEqual(meta_changes, [{
'key1': ['-'],
'key2': ['+', 'bah'],
'key3': ['+', 'value3'],
}])
self.assertEqual(len(fake_notifier.NOTIFICATIONS), 2) self.assertEqual(len(fake_notifier.NOTIFICATIONS), 2)
msg = fake_notifier.NOTIFICATIONS[1] msg = fake_notifier.NOTIFICATIONS[1]
@ -9506,7 +9492,6 @@ class ComputeAPITestCase(BaseTestCase):
self.compute_api.delete_instance_metadata(_context, instance, 'key2') self.compute_api.delete_instance_metadata(_context, instance, 'key2')
metadata = self.compute_api.get_instance_metadata(_context, instance) metadata = self.compute_api.get_instance_metadata(_context, instance)
self.assertEqual(metadata, {'key3': 'value3'}) self.assertEqual(metadata, {'key3': 'value3'})
self.assertEqual(meta_changes, [{'key2': ['-']}])
self.assertEqual(len(fake_notifier.NOTIFICATIONS), 3) self.assertEqual(len(fake_notifier.NOTIFICATIONS), 3)
msg = fake_notifier.NOTIFICATIONS[2] msg = fake_notifier.NOTIFICATIONS[2]
@ -9515,13 +9500,6 @@ class ComputeAPITestCase(BaseTestCase):
self.assertEqual(payload['metadata'], {'key3': 'value3'}) self.assertEqual(payload['metadata'], {'key3': 'value3'})
def test_disallow_metadata_changes_during_building(self): def test_disallow_metadata_changes_during_building(self):
def fake_change_instance_metadata(inst, ctxt, diff, instance=None,
instance_uuid=None):
pass
self.stub_out('nova.compute.rpcapi.ComputeAPI.'
'change_instance_metadata',
fake_change_instance_metadata)
instance = self._create_fake_instance_obj( instance = self._create_fake_instance_obj(
{'vm_state': vm_states.BUILDING}) {'vm_state': vm_states.BUILDING})

View File

@ -213,10 +213,6 @@ class ComputeRpcAPITestCase(test.NoDBTestCase):
instance=self.fake_instance_obj, bdm=self.fake_volume_bdm, instance=self.fake_instance_obj, bdm=self.fake_volume_bdm,
version='5.0') version='5.0')
def test_change_instance_metadata(self):
self._test_compute_api('change_instance_metadata', 'cast',
instance=self.fake_instance_obj, diff={}, version='5.0')
def test_check_instance_shared_storage(self): def test_check_instance_shared_storage(self):
self._test_compute_api('check_instance_shared_storage', 'call', self._test_compute_api('check_instance_shared_storage', 'call',
instance=self.fake_instance_obj, data='foo', instance=self.fake_instance_obj, data='foo',

View File

@ -1362,21 +1362,6 @@ class ComputeDriver(object):
""" """
raise NotImplementedError() raise NotImplementedError()
# TODO(stephenfin): This was only implemented (properly) for XenAPI and
# should be removed.
def change_instance_metadata(self, context, instance, diff):
"""Applies a diff to the instance metadata.
This is an optional driver method which is used to publish
changes to the instance's metadata to the hypervisor. If the
hypervisor has no means of publishing the instance metadata to
the instance, then this method should not be implemented.
:param context: security context
:param instance: nova.objects.instance.Instance
"""
pass
def inject_network_info(self, instance, nw_info): def inject_network_info(self, instance, nw_info):
"""inject network info for specified instance.""" """inject network info for specified instance."""
# TODO(Vek): Need to pass context in for access to auth_token # TODO(Vek): Need to pass context in for access to auth_token