Move the fixed_ips APIv2 extension to use objects

This converts the fixed_ips extension to use the FixedIP object
instead of direct database access.

Related to blueprint compute-manager-objects-juno

Change-Id: I21afae063a360c1c36a417f0455e213b7236b541
This commit is contained in:
Dan Smith 2014-06-17 09:14:40 -07:00
parent 4e6e35a670
commit 5ace1c27b4
3 changed files with 47 additions and 13 deletions

View File

@ -15,8 +15,8 @@
import webob.exc
from nova.api.openstack import extensions
from nova import db
from nova import exception
from nova import objects
from nova.openstack.common.gettextutils import _
authorize = extensions.extension_authorizer('compute', 'fixed_ips')
@ -28,23 +28,25 @@ class FixedIPController(object):
context = req.environ['nova.context']
authorize(context)
attrs = ['network', 'instance']
try:
fixed_ip = db.fixed_ip_get_by_address_detailed(context, id)
fixed_ip = objects.FixedIP.get_by_address(context, id,
expected_attrs=attrs)
except (exception.FixedIpNotFoundForAddress,
exception.FixedIpInvalid) as ex:
raise webob.exc.HTTPNotFound(explanation=ex.format_message())
fixed_ip_info = {"fixed_ip": {}}
if fixed_ip[1] is None:
if fixed_ip is None:
msg = _("Fixed IP %s has been deleted") % id
raise webob.exc.HTTPNotFound(explanation=msg)
fixed_ip_info['fixed_ip']['cidr'] = fixed_ip[1]['cidr']
fixed_ip_info['fixed_ip']['address'] = fixed_ip[0]['address']
fixed_ip_info['fixed_ip']['cidr'] = str(fixed_ip.network.cidr)
fixed_ip_info['fixed_ip']['address'] = str(fixed_ip.address)
if fixed_ip[2]:
fixed_ip_info['fixed_ip']['hostname'] = fixed_ip[2]['hostname']
fixed_ip_info['fixed_ip']['host'] = fixed_ip[2]['host']
if fixed_ip.instance:
fixed_ip_info['fixed_ip']['hostname'] = fixed_ip.instance.hostname
fixed_ip_info['fixed_ip']['host'] = fixed_ip.instance.host
else:
fixed_ip_info['fixed_ip']['hostname'] = None
fixed_ip_info['fixed_ip']['host'] = None
@ -65,9 +67,9 @@ class FixedIPController(object):
def _set_reserved(self, context, address, reserved):
try:
fixed_ip = db.fixed_ip_get_by_address(context, address)
db.fixed_ip_update(context, fixed_ip['address'],
{'reserved': reserved})
fixed_ip = objects.FixedIP.get_by_address(context, address)
fixed_ip.reserved = reserved
fixed_ip.save()
except (exception.FixedIpNotFoundForAddress, exception.FixedIpInvalid):
msg = _("Fixed IP %s not found") % address
raise webob.exc.HTTPNotFound(explanation=msg)

View File

@ -20,6 +20,7 @@ from nova import db
from nova import exception
from nova import test
from nova.tests.api.openstack import fakes
from nova.tests.objects import test_network
fake_fixed_ips = [{'id': 1,
@ -31,6 +32,11 @@ fake_fixed_ips = [{'id': 1,
'leased': False,
'reserved': False,
'host': None,
'instance': None,
'network': test_network.fake_network,
'created_at': None,
'updated_at': None,
'deleted_at': None,
'deleted': False},
{'id': 2,
'address': '192.168.1.2',
@ -41,6 +47,11 @@ fake_fixed_ips = [{'id': 1,
'leased': False,
'reserved': False,
'host': None,
'instance': None,
'network': test_network.fake_network,
'created_at': None,
'updated_at': None,
'deleted_at': None,
'deleted': False},
{'id': 3,
'address': '10.0.0.2',
@ -51,11 +62,16 @@ fake_fixed_ips = [{'id': 1,
'leased': False,
'reserved': False,
'host': None,
'instance': None,
'network': test_network.fake_network,
'created_at': None,
'updated_at': None,
'deleted_at': None,
'deleted': True},
]
def fake_fixed_ip_get_by_address(context, address):
def fake_fixed_ip_get_by_address(context, address, columns_to_join=None):
for fixed_ip in fake_fixed_ips:
if fixed_ip['address'] == address and not fixed_ip['deleted']:
return fixed_ip

View File

@ -64,6 +64,7 @@ from nova.tests import fake_utils
from nova.tests.image import fake
from nova.tests.integrated import api_samples_test_base
from nova.tests.integrated import integrated_helpers
from nova.tests.objects import test_network
from nova.tests import utils as test_utils
from nova.tests.virt.baremetal.db import base as bm_db_base
from nova import utils
@ -1459,6 +1460,8 @@ class FixedIpJsonTest(ApiSampleTestBaseV2):
def setUp(self):
super(FixedIpJsonTest, self).setUp()
instance = dict(test_utils.get_test_instance(),
hostname='openstack', host='host')
fake_fixed_ips = [{'id': 1,
'address': '192.168.1.1',
'network_id': 1,
@ -1467,6 +1470,12 @@ class FixedIpJsonTest(ApiSampleTestBaseV2):
'allocated': False,
'leased': False,
'reserved': False,
'created_at': None,
'deleted_at': None,
'updated_at': None,
'deleted': None,
'instance': instance,
'network': test_network.fake_network,
'host': None},
{'id': 2,
'address': '192.168.1.2',
@ -1476,10 +1485,17 @@ class FixedIpJsonTest(ApiSampleTestBaseV2):
'allocated': False,
'leased': False,
'reserved': False,
'created_at': None,
'deleted_at': None,
'updated_at': None,
'deleted': None,
'instance': instance,
'network': test_network.fake_network,
'host': None},
]
def fake_fixed_ip_get_by_address(context, address):
def fake_fixed_ip_get_by_address(context, address,
columns_to_join=None):
for fixed_ip in fake_fixed_ips:
if fixed_ip['address'] == address:
return fixed_ip