Clean up db network db calls for fixed and float

Use a join instead of a subquery for fixed_ip_get_by_floating_address
and floating_ip_get_by_fixed_address. Adds test to verify behavior
is the same.

Change-Id: Iab00142d477308c354777a2c64b6d03a21184585
This commit is contained in:
Vishvananda Ishaya 2013-01-17 11:29:34 -08:00
parent 9c7cd5996d
commit 5639e27d6f
2 changed files with 37 additions and 17 deletions

View File

@ -890,16 +890,13 @@ def _floating_ip_get_by_address(context, address, session=None):
@require_context
def floating_ip_get_by_fixed_address(context, fixed_address):
subq = model_query(context, models.FixedIp.id).\
filter_by(address=fixed_address).\
limit(1).\
subquery()
return model_query(context, models.FloatingIp).\
filter_by(fixed_ip_id=subq.as_scalar()).\
outerjoin(models.FixedIp,
models.FixedIp.id ==
models.FloatingIp.fixed_ip_id).\
filter(models.FixedIp.address == fixed_address).\
all()
# NOTE(tr3buchet) please don't invent an exception here, empty list is fine
@require_context
def floating_ip_get_by_fixed_ip_id(context, fixed_ip_id, session=None):
@ -1196,15 +1193,12 @@ def fixed_ip_get_by_address_detailed(context, address, session=None):
@require_context
def fixed_ip_get_by_floating_address(context, floating_address):
subq = model_query(context, models.FloatingIp.fixed_ip_id,
read_deleted="no").\
filter_by(address=floating_address).\
limit(1).\
subquery()
return model_query(context, models.FixedIp, read_deleted="no").\
filter_by(id=subq.as_scalar()).\
return model_query(context, models.FixedIp).\
outerjoin(models.FloatingIp,
models.FloatingIp.fixed_ip_id ==
models.FixedIp.id).\
filter(models.FloatingIp.address == floating_address).\
first()
# NOTE(tr3buchet) please don't invent an exception here, empty list is fine

View File

@ -261,6 +261,32 @@ class DbApiTestCase(test.TestCase):
res = db.floating_ip_disassociate(ctxt, floating)
self.assertEqual(res, None)
def test_fixed_ip_get_by_floating_address(self):
ctxt = context.get_admin_context()
values = {'address': 'fixed'}
fixed = db.fixed_ip_create(ctxt, values)
fixed_ip_ref = db.fixed_ip_get_by_address(ctxt, fixed)
values = {'address': 'floating',
'fixed_ip_id': fixed_ip_ref['id']}
floating = db.floating_ip_create(ctxt, values)
fixed_ip_ref = db.fixed_ip_get_by_floating_address(ctxt, floating)
self.assertEqual(fixed, fixed_ip_ref['address'])
def test_floating_ip_get_by_fixed_address(self):
ctxt = context.get_admin_context()
values = {'address': 'fixed'}
fixed = db.fixed_ip_create(ctxt, values)
fixed_ip_ref = db.fixed_ip_get_by_address(ctxt, fixed)
values = {'address': 'floating1',
'fixed_ip_id': fixed_ip_ref['id']}
floating1 = db.floating_ip_create(ctxt, values)
values = {'address': 'floating2',
'fixed_ip_id': fixed_ip_ref['id']}
floating2 = db.floating_ip_create(ctxt, values)
floating_ip_refs = db.floating_ip_get_by_fixed_address(ctxt, fixed)
self.assertEqual(floating1, floating_ip_refs[0]['address'])
self.assertEqual(floating2, floating_ip_refs[1]['address'])
def test_network_create_safe(self):
ctxt = context.get_admin_context()
values = {'host': 'localhost', 'project_id': 'project1'}