Make instance_exists() take an instance, not instance_name

Some virt drivers may not support/need to use instance.name and have
a different way to find out if an instance exists. NOTE: this still
uses dict access on instance, as the _run_instance() path is not
converted to Instance objects yet.

NOTE: This also removes a duplicate call to self._lookup_by_name()
in libvirt's resume_state_on_host_boot(). It was calling
self.instance_exists() and then self._lookup_by_name(), but instance_exists()
calls _lookup_by_name() itself.

Change-Id: I701cb5416ec47262268d1a088018b6be2774d1c5
This commit is contained in:
Chris Behrens 2014-04-21 12:19:42 -07:00
parent 816483592b
commit ccf423333a
8 changed files with 29 additions and 24 deletions

View File

@ -1507,7 +1507,7 @@ class ComputeManager(manager.Manager):
def _check_instance_exists(self, context, instance):
"""Ensure an instance with the same name is not already present."""
if self.driver.instance_exists(instance['name']):
if self.driver.instance_exists(instance):
raise exception.InstanceExists(name=instance['name'])
def _start_building(self, context, instance):
@ -4199,7 +4199,7 @@ class ComputeManager(manager.Manager):
if connection_info and 'serial' not in connection_info:
connection_info['serial'] = volume_id
try:
if not self.driver.instance_exists(instance.name):
if not self.driver.instance_exists(instance):
LOG.warn(_('Detaching volume from unknown instance'),
context=context, instance=instance)

View File

@ -8782,7 +8782,7 @@ class ComputeAPITestCase(BaseTestCase):
'source_type': 'snapshot', 'destination_type': 'volume',
'connection_info': '{"test": "test"}'})
def fake_libvirt_driver_instance_exists(*args, **kwargs):
def fake_libvirt_driver_instance_exists(_instance):
called['fake_libvirt_driver_instance_exists'] = True
return False

View File

@ -5308,14 +5308,14 @@ class LibvirtConnTestCase(test.TestCase):
called = {'count': 0}
instance = {'name': 'test'}
def fake_instance_exists(name):
return False
def fake_lookup_by_name(instance_name):
raise exception.InstanceNotFound(instance_id='fake')
def fake_hard_reboot(*args):
called['count'] += 1
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
self.stubs.Set(conn, 'instance_exists', fake_instance_exists)
self.stubs.Set(conn, '_lookup_by_name', fake_lookup_by_name)
self.stubs.Set(conn, '_hard_reboot', fake_hard_reboot)
conn.resume_state_on_host_boot(self.context, instance, network_info=[],
block_device_info=None)

View File

@ -368,14 +368,14 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
vm_utils.lookup(mox.IgnoreArg(), 'foo').AndReturn(True)
self.mox.ReplayAll()
self.assertTrue(self.conn.instance_exists('foo'))
self.assertTrue(self.conn.instance_exists(dict(name='foo')))
def test_instance_not_exists(self):
self.mox.StubOutWithMock(vm_utils, 'lookup')
vm_utils.lookup(mox.IgnoreArg(), 'bar').AndReturn(None)
self.mox.ReplayAll()
self.assertFalse(self.conn.instance_exists('bar'))
self.assertFalse(self.conn.instance_exists(dict(name='bar')))
def test_list_instances_0(self):
instances = self.conn.list_instances()

View File

@ -186,10 +186,10 @@ class ComputeDriver(object):
"""
return len(self.list_instances())
def instance_exists(self, instance_id):
def instance_exists(self, instance):
"""Checks existence of an instance on the host.
:param instance_id: The ID / name of the instance to lookup
:param instance: The instance to lookup
Returns True if an instance with the supplied ID exists on
the host, False otherwise.
@ -201,7 +201,10 @@ class ComputeDriver(object):
encouraged to override this method with something more
efficient.
"""
return instance_id in self.list_instances()
try:
return instance['uuid'] in self.list_instance_uuids()
except NotImplementedError:
return instance['name'] in self.list_instances()
def estimate_instance_overhead(self, instance_info):
"""Estimate the virtualization overhead required to build an instance

View File

@ -72,9 +72,10 @@ def restore_nodes():
class FakeInstance(object):
def __init__(self, name, state):
def __init__(self, name, state, uuid):
self.name = name
self.state = state
self.uuid = uuid
def __getitem__(self, key):
return getattr(self, key)
@ -116,6 +117,9 @@ class FakeDriver(driver.ComputeDriver):
def list_instances(self):
return self.instances.keys()
def list_instance_uuids(self):
return [self.instances[name].uuid for name in self.instances.keys()]
def plug_vifs(self, instance, network_info):
"""Plug VIFs into networks."""
pass
@ -128,7 +132,7 @@ class FakeDriver(driver.ComputeDriver):
admin_password, network_info=None, block_device_info=None):
name = instance['name']
state = power_state.RUNNING
fake_instance = FakeInstance(name, state)
fake_instance = FakeInstance(name, state, instance['uuid'])
self.instances[name] = fake_instance
def snapshot(self, context, instance, name, update_task_state):
@ -463,9 +467,6 @@ class FakeDriver(driver.ComputeDriver):
def instance_on_disk(self, instance):
return False
def list_instance_uuids(self):
return []
class FakeVirtAPI(virtapi.VirtAPI):
def instance_update(self, context, instance_uuid, updates):

View File

@ -799,10 +799,10 @@ class LibvirtDriver(driver.ComputeDriver):
"""Efficient override of base instance_exists method."""
return self._conn.numOfDomains()
def instance_exists(self, instance_name):
def instance_exists(self, instance):
"""Efficient override of base instance_exists method."""
try:
self._lookup_by_name(instance_name)
self._lookup_by_name(instance['name'])
return True
except exception.NovaException:
return False
@ -2153,7 +2153,7 @@ class LibvirtDriver(driver.ComputeDriver):
"""resume guest state when a host is booted."""
# Check if the instance is running already and avoid doing
# anything if it is.
if self.instance_exists(instance['name']):
try:
domain = self._lookup_by_name(instance['name'])
state = LIBVIRT_POWER_STATE[domain.info()[0]]
@ -2164,6 +2164,8 @@ class LibvirtDriver(driver.ComputeDriver):
if state in ignored_states:
return
except exception.NovaException:
pass
# Instance is not up and could be in an unknown state.
# Be as absolute as possible about getting it back into

View File

@ -177,18 +177,17 @@ class XenAPIDriver(driver.ComputeDriver):
except Exception:
LOG.exception(_('Failure while cleaning up attached VDIs'))
def instance_exists(self, instance_name):
def instance_exists(self, instance):
"""Checks existence of an instance on the host.
:param instance_name: The name of the instance to lookup
:param instance: The instance to lookup
Returns True if an instance with the supplied name exists on
the host, False otherwise.
Returns True if supplied instance exists on the host, False otherwise.
NOTE(belliott): This is an override of the base method for
efficiency.
"""
return self._vmops.instance_exists(instance_name)
return self._vmops.instance_exists(instance['name'])
def estimate_instance_overhead(self, instance_info):
"""Get virtualization overhead required to build an instance of the