diff options
| author | Jenkins <jenkins@review.openstack.org> | 2013-06-13 05:39:25 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2013-06-13 05:39:25 +0000 |
| commit | bdf362b81097e35b325151db981c32ed3099f2dc (patch) | |
| tree | 8f7df81def43fcff7ef52ae7001eea9de7d34e85 | |
| parent | c3cbae0978ff37af06a67fb187160241dcab6d63 (diff) | |
| parent | 7fadc012939faf063bbe9ad3a2f35067ce16a4b2 (diff) | |
| download | nova-bdf362b81097e35b325151db981c32ed3099f2dc.tar.gz nova-bdf362b81097e35b325151db981c32ed3099f2dc.tar.xz nova-bdf362b81097e35b325151db981c32ed3099f2dc.zip | |
Merge "Use an inner join on aggregate_hosts in aggregate_get_by_host"
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 13 | ||||
| -rw-r--r-- | nova/tests/db/test_db_api.py | 5 |
2 files changed, 15 insertions, 3 deletions
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 01f7ad3de..8fd946963 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -4605,12 +4605,19 @@ def aggregate_get(context, aggregate_id): @require_admin_context def aggregate_get_by_host(context, host, key=None): - query = _aggregate_get_query(context, models.Aggregate, - models.AggregateHost.host, host) + """Return rows that match host (mandatory) and metadata key (optional). + + :param host matches host, and is required. + :param key Matches metadata key, if not None. + """ + query = model_query(context, models.Aggregate) + query = query.options(joinedload('_hosts', innerjoin=True)) + query = query.options(joinedload('_metadata')) + query = query.filter(models.AggregateHost.host == host) if key: query = query.join("_metadata").filter( - models.AggregateMetadata.key == key) + models.AggregateMetadata.key == key) return query.all() diff --git a/nova/tests/db/test_db_api.py b/nova/tests/db/test_db_api.py index d40d05bdb..b952b4b00 100644 --- a/nova/tests/db/test_db_api.py +++ b/nova/tests/db/test_db_api.py @@ -1240,17 +1240,22 @@ class AggregateDBApiTestCase(test.TestCase): def test_aggregate_get_by_host(self): ctxt = context.get_admin_context() values = {'name': 'fake_aggregate2'} + values2 = {'name': 'fake_aggregate4'} a1 = _create_aggregate_with_hosts(context=ctxt) a2 = _create_aggregate_with_hosts(context=ctxt, values=values) + # a3 has no hosts and should not be in the results. + a3 = _create_aggregate(context=ctxt, values=values2) r1 = db.aggregate_get_by_host(ctxt, 'foo.openstack.org') self.assertEqual([a1['id'], a2['id']], [x['id'] for x in r1]) def test_aggregate_get_by_host_with_key(self): ctxt = context.get_admin_context() values = {'name': 'fake_aggregate2'} + values2 = {'name': 'fake_aggregate4'} a1 = _create_aggregate_with_hosts(context=ctxt, metadata={'goodkey': 'good'}) a2 = _create_aggregate_with_hosts(context=ctxt, values=values) + a3 = _create_aggregate(context=ctxt, values=values2) # filter result by key r1 = db.aggregate_get_by_host(ctxt, 'foo.openstack.org', key='goodkey') self.assertEqual([a1['id']], [x['id'] for x in r1]) |
