Merge "xenapi: Add efficient impl of instance_exists()"

This commit is contained in:
Jenkins 2013-09-05 21:34:18 +00:00 committed by Gerrit Code Review
commit 3119dafeb5
3 changed files with 30 additions and 0 deletions

View File

@ -377,6 +377,20 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
self.conn.init_host(None)
self.assertEquals(set(xenapi_fake.get_all('VBD')), set([vbd0, vbd2]))
def test_instance_exists(self):
self.mox.StubOutWithMock(vm_utils, 'lookup')
vm_utils.lookup(mox.IgnoreArg(), 'foo').AndReturn(True)
self.mox.ReplayAll()
self.assertTrue(self.conn.instance_exists('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'))
def test_list_instances_0(self):
instances = self.conn.list_instances()
self.assertEquals(instances, [])

View File

@ -163,6 +163,19 @@ class XenAPIDriver(driver.ComputeDriver):
except Exception:
LOG.exception(_('Failure while cleaning up attached VDIs'))
def instance_exists(self, instance_name):
"""Checks existence of an instance on the host.
:param instance_name: The name of the instance to lookup
Returns True if an instance with the supplied name 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)
def list_instances(self):
"""List VM instances."""
return self._vmops.list_instances()

View File

@ -211,6 +211,9 @@ class VMOps(object):
instance, vm_ref)
raise exception.NovaException(_("Error: Agent is disabled"))
def instance_exists(self, name_label):
return vm_utils.lookup(self._session, name_label) is not None
def list_instances(self):
"""List VM instances."""
# TODO(justinsb): Should we just always use the details method?