Add Service.get_by_host_and_binary and ServiceList.get_by_binary
This adds query methods that don't depend on CONF, but still allow us to query for services by their function (binary instead of topic). It also allows for code at other layers to do this without needing details of the RPC layer. Also added here is a convenience method to search for nova-compute services specifically. Change-Id: Iee2b652b352a40bc8fdaa0a7265de86a0bef3cc0
This commit is contained in:
@@ -101,10 +101,15 @@ def service_get(context, service_id, use_slave=False):
|
||||
|
||||
|
||||
def service_get_by_host_and_topic(context, host, topic):
|
||||
"""Get a service by host it's on and topic it listens to."""
|
||||
"""Get a service by hostname and topic it listens to."""
|
||||
return IMPL.service_get_by_host_and_topic(context, host, topic)
|
||||
|
||||
|
||||
def service_get_by_host_and_binary(context, host, binary):
|
||||
"""Get a service by hostname and binary."""
|
||||
return IMPL.service_get_by_host_and_binary(context, host, binary)
|
||||
|
||||
|
||||
def service_get_all(context, disabled=None):
|
||||
"""Get all services."""
|
||||
return IMPL.service_get_all(context, disabled)
|
||||
@@ -115,6 +120,11 @@ def service_get_all_by_topic(context, topic):
|
||||
return IMPL.service_get_all_by_topic(context, topic)
|
||||
|
||||
|
||||
def service_get_all_by_binary(context, binary):
|
||||
"""Get all services for a given binary."""
|
||||
return IMPL.service_get_all_by_binary(context, binary)
|
||||
|
||||
|
||||
def service_get_all_by_host(context, host):
|
||||
"""Get all services for a given host."""
|
||||
return IMPL.service_get_all_by_host(context, host)
|
||||
|
||||
@@ -406,6 +406,23 @@ def service_get_by_host_and_topic(context, host, topic):
|
||||
first()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def service_get_all_by_binary(context, binary):
|
||||
return model_query(context, models.Service, read_deleted="no").\
|
||||
filter_by(disabled=False).\
|
||||
filter_by(binary=binary).\
|
||||
all()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def service_get_by_host_and_binary(context, host, binary):
|
||||
return model_query(context, models.Service, read_deleted="no").\
|
||||
filter_by(disabled=False).\
|
||||
filter_by(host=host).\
|
||||
filter_by(binary=binary).\
|
||||
first()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def service_get_all_by_host(context, host):
|
||||
return model_query(context, models.Service, read_deleted="no").\
|
||||
@@ -418,7 +435,7 @@ def service_get_by_compute_host(context, host, use_slave=False):
|
||||
result = model_query(context, models.Service, read_deleted="no",
|
||||
use_slave=use_slave).\
|
||||
filter_by(host=host).\
|
||||
filter_by(topic=CONF.compute_topic).\
|
||||
filter_by(binary='nova-compute').\
|
||||
first()
|
||||
|
||||
if not result:
|
||||
|
||||
@@ -40,7 +40,8 @@ class Service(base.NovaPersistentObject, base.NovaObject,
|
||||
# Version 1.8: ComputeNode version 1.9
|
||||
# Version 1.9: ComputeNode version 1.10
|
||||
# Version 1.10: Changes behaviour of loading compute_node
|
||||
VERSION = '1.10'
|
||||
# Version 1.11: Added get_by_host_and_binary
|
||||
VERSION = '1.11'
|
||||
|
||||
fields = {
|
||||
'id': fields.IntegerField(read_only=True),
|
||||
@@ -98,6 +99,7 @@ class Service(base.NovaPersistentObject, base.NovaObject,
|
||||
if not self._context:
|
||||
raise exception.OrphanedObjectError(method='obj_load_attr',
|
||||
objtype=self.obj_name())
|
||||
|
||||
LOG.debug("Lazy-loading `%(attr)s' on %(name)s id %(id)s",
|
||||
{'attr': attrname,
|
||||
'name': self.obj_name(),
|
||||
@@ -130,6 +132,11 @@ class Service(base.NovaPersistentObject, base.NovaObject,
|
||||
db_service = db.service_get_by_host_and_topic(context, host, topic)
|
||||
return cls._from_db_object(context, cls(), db_service)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_host_and_binary(cls, context, host, binary):
|
||||
db_service = db.service_get_by_host_and_binary(context, host, binary)
|
||||
return cls._from_db_object(context, cls(), db_service)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_compute_host(cls, context, host, use_slave=False):
|
||||
db_service = db.service_get_by_compute_host(context, host)
|
||||
@@ -172,7 +179,8 @@ class ServiceList(base.ObjectListBase, base.NovaObject):
|
||||
# Version 1.6: Service version 1.8
|
||||
# Version 1.7: Service version 1.9
|
||||
# Version 1.8: Service version 1.10
|
||||
VERSION = '1.8'
|
||||
# Version 1.9: Added get_by_binary() and Service version 1.11
|
||||
VERSION = '1.9'
|
||||
|
||||
fields = {
|
||||
'objects': fields.ListOfObjectsField('Service'),
|
||||
@@ -188,6 +196,7 @@ class ServiceList(base.ObjectListBase, base.NovaObject):
|
||||
'1.6': '1.8',
|
||||
'1.7': '1.9',
|
||||
'1.8': '1.10',
|
||||
'1.9': '1.11',
|
||||
}
|
||||
|
||||
@base.remotable_classmethod
|
||||
@@ -196,6 +205,12 @@ class ServiceList(base.ObjectListBase, base.NovaObject):
|
||||
return base.obj_make_list(context, cls(context), objects.Service,
|
||||
db_services)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_binary(cls, context, binary):
|
||||
db_services = db.service_get_all_by_binary(context, binary)
|
||||
return base.obj_make_list(context, cls(context), objects.Service,
|
||||
db_services)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_host(cls, context, host):
|
||||
db_services = db.service_get_all_by_host(context, host)
|
||||
|
||||
@@ -2788,6 +2788,15 @@ class ServiceTestCase(test.TestCase, ModelsObjectComparatorMixin):
|
||||
topic='topic1')
|
||||
self._assertEqualObjects(service1, real_service1)
|
||||
|
||||
def test_service_get_by_host_and_binary(self):
|
||||
service1 = self._create_service({'host': 'host1', 'binary': 'foo'})
|
||||
self._create_service({'host': 'host2', 'binary': 'bar'})
|
||||
|
||||
real_service1 = db.service_get_by_host_and_binary(self.ctxt,
|
||||
host='host1',
|
||||
binary='foo')
|
||||
self._assertEqualObjects(service1, real_service1)
|
||||
|
||||
def test_service_get_all(self):
|
||||
values = [
|
||||
{'host': 'host1', 'topic': 'topic1'},
|
||||
@@ -2818,6 +2827,18 @@ class ServiceTestCase(test.TestCase, ModelsObjectComparatorMixin):
|
||||
real = db.service_get_all_by_topic(self.ctxt, 't1')
|
||||
self._assertEqualListsOfObjects(expected, real)
|
||||
|
||||
def test_service_get_all_by_binary(self):
|
||||
values = [
|
||||
{'host': 'host1', 'binary': 'b1'},
|
||||
{'host': 'host2', 'binary': 'b1'},
|
||||
{'disabled': True, 'binary': 'b1'},
|
||||
{'host': 'host3', 'binary': 'b2'}
|
||||
]
|
||||
services = [self._create_service(vals) for vals in values]
|
||||
expected = services[:2]
|
||||
real = db.service_get_all_by_binary(self.ctxt, 'b1')
|
||||
self._assertEqualListsOfObjects(expected, real)
|
||||
|
||||
def test_service_get_all_by_host(self):
|
||||
values = [
|
||||
{'host': 'host1', 'topic': 't11', 'binary': 'b11'},
|
||||
@@ -2833,9 +2854,9 @@ class ServiceTestCase(test.TestCase, ModelsObjectComparatorMixin):
|
||||
|
||||
def test_service_get_by_compute_host(self):
|
||||
values = [
|
||||
{'host': 'host1', 'topic': CONF.compute_topic},
|
||||
{'host': 'host2', 'topic': 't1'},
|
||||
{'host': 'host3', 'topic': CONF.compute_topic}
|
||||
{'host': 'host1', 'binary': 'nova-compute'},
|
||||
{'host': 'host2', 'binary': 'nova-scheduler'},
|
||||
{'host': 'host3', 'binary': 'nova-compute'}
|
||||
]
|
||||
services = [self._create_service(vals) for vals in values]
|
||||
|
||||
|
||||
@@ -1207,8 +1207,8 @@ object_data = {
|
||||
'SecurityGroupList': '1.0-528e6448adfeeb78921ebeda499ab72f',
|
||||
'SecurityGroupRule': '1.1-a9175baf7664439af1a16c2010b55576',
|
||||
'SecurityGroupRuleList': '1.1-667fca3a9928f23d2d10e61962c55f3c',
|
||||
'Service': '1.10-82bbfd46a744a9c89bc44b47a1b81683',
|
||||
'ServiceList': '1.8-41d0a9f83d49950ffb6efa4978201d57',
|
||||
'Service': '1.11-2b157261ffa37b3d2675b39b6c29ce07',
|
||||
'ServiceList': '1.9-54656820acc49b3cc0eb57b2a684b84a',
|
||||
'Tag': '1.0-a11531f4e4e3166eef6243d6d58a18bd',
|
||||
'TagList': '1.0-e89bf8c8055f1f1d654fb44f0abf1f53',
|
||||
'TestSubclassedObject': '1.6-87177ccbefd7a740a9e261f958e15b00',
|
||||
|
||||
@@ -83,6 +83,10 @@ class _TestServiceObject(object):
|
||||
self._test_query('service_get_by_host_and_topic',
|
||||
'get_by_host_and_topic', 'fake-host', 'fake-topic')
|
||||
|
||||
def test_get_by_host_and_binary(self):
|
||||
self._test_query('service_get_by_host_and_binary',
|
||||
'get_by_host_and_binary', 'fake-host', 'fake-binary')
|
||||
|
||||
def test_get_by_compute_host(self):
|
||||
self._test_query('service_get_by_compute_host', 'get_by_compute_host',
|
||||
'fake-host')
|
||||
@@ -156,6 +160,14 @@ class _TestServiceObject(object):
|
||||
self.assertEqual(1, len(services))
|
||||
self.compare_obj(services[0], fake_service, allow_missing=OPTIONAL)
|
||||
|
||||
@mock.patch('nova.db.service_get_all_by_binary')
|
||||
def test_get_by_binary(self, mock_get):
|
||||
mock_get.return_value = [fake_service]
|
||||
services = service.ServiceList.get_by_binary(self.context,
|
||||
'fake-binary')
|
||||
self.assertEqual(1, len(services))
|
||||
mock_get.assert_called_once_with(self.context, 'fake-binary')
|
||||
|
||||
def test_get_by_host(self):
|
||||
self.mox.StubOutWithMock(db, 'service_get_all_by_host')
|
||||
db.service_get_all_by_host(self.context, 'fake-host').AndReturn(
|
||||
|
||||
Reference in New Issue
Block a user