Convert network/api.py to use FloatingIP object
This converts floating IP manipulation code in the network/api.py module to use the FloatingIP object. Basically no testing of these interfaces was being done, so this adds that as well. Related to blueprint compute-manager-objects-juno Change-Id: I6120dd4a32473ca9029832503444fbeae1ab0576
This commit is contained in:
@@ -21,16 +21,16 @@ from nova import network
|
||||
authorize = extensions.extension_authorizer('compute', 'floating_ip_pools')
|
||||
|
||||
|
||||
def _translate_floating_ip_view(pool):
|
||||
def _translate_floating_ip_view(pool_name):
|
||||
return {
|
||||
'name': pool['name'],
|
||||
'name': pool_name,
|
||||
}
|
||||
|
||||
|
||||
def _translate_floating_ip_pools_view(pools):
|
||||
return {
|
||||
'floating_ip_pools': [_translate_floating_ip_view(pool)
|
||||
for pool in pools]
|
||||
'floating_ip_pools': [_translate_floating_ip_view(pool_name)
|
||||
for pool_name in pools]
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -112,26 +112,26 @@ class API(base_api.NetworkAPI):
|
||||
def get_floating_ip(self, context, id):
|
||||
if not utils.is_int_like(id):
|
||||
raise exception.InvalidID(id=id)
|
||||
return self.db.floating_ip_get(context, id)
|
||||
return objects.FloatingIP.get_by_id(context, id)
|
||||
|
||||
@wrap_check_policy
|
||||
def get_floating_ip_pools(self, context):
|
||||
return self.db.floating_ip_get_pools(context)
|
||||
return objects.FloatingIP.get_pool_names(context)
|
||||
|
||||
@wrap_check_policy
|
||||
def get_floating_ip_by_address(self, context, address):
|
||||
return self.db.floating_ip_get_by_address(context, address)
|
||||
return objects.FloatingIP.get_by_address(context, address)
|
||||
|
||||
@wrap_check_policy
|
||||
def get_floating_ips_by_project(self, context):
|
||||
return self.db.floating_ip_get_all_by_project(context,
|
||||
return objects.FloatingIPList.get_by_project(context,
|
||||
context.project_id)
|
||||
|
||||
@wrap_check_policy
|
||||
def get_floating_ips_by_fixed_address(self, context, fixed_address):
|
||||
floating_ips = self.db.floating_ip_get_by_fixed_address(context,
|
||||
fixed_address)
|
||||
return [floating_ip['address'] for floating_ip in floating_ips]
|
||||
floating_ips = objects.FloatingIPList.get_by_fixed_address(
|
||||
context, fixed_address)
|
||||
return [str(floating_ip.address) for floating_ip in floating_ips]
|
||||
|
||||
@wrap_check_policy
|
||||
def get_instance_id_by_floating_address(self, context, address):
|
||||
|
||||
@@ -23,8 +23,7 @@ from nova.tests.api.openstack import fakes
|
||||
|
||||
|
||||
def fake_get_floating_ip_pools(self, context):
|
||||
return [{'name': 'nova'},
|
||||
{'name': 'other'}]
|
||||
return ['nova', 'other']
|
||||
|
||||
|
||||
class FloatingIpPoolTest(test.NoDBTestCase):
|
||||
@@ -41,16 +40,16 @@ class FloatingIpPoolTest(test.NoDBTestCase):
|
||||
view = floating_ip_pools._translate_floating_ip_pools_view(pools)
|
||||
self.assertIn('floating_ip_pools', view)
|
||||
self.assertEqual(view['floating_ip_pools'][0]['name'],
|
||||
pools[0]['name'])
|
||||
pools[0])
|
||||
self.assertEqual(view['floating_ip_pools'][1]['name'],
|
||||
pools[1]['name'])
|
||||
pools[1])
|
||||
|
||||
def test_floating_ips_pools_list(self):
|
||||
req = fakes.HTTPRequest.blank('/v2/fake/os-floating-ip-pools')
|
||||
res_dict = self.controller.index(req)
|
||||
|
||||
pools = fake_get_floating_ip_pools(None, self.context)
|
||||
response = {'floating_ip_pools': pools}
|
||||
response = {'floating_ip_pools': [{'name': name} for name in pools]}
|
||||
self.assertEqual(res_dict, response)
|
||||
|
||||
|
||||
|
||||
@@ -3022,8 +3022,7 @@ class FloatingIPPoolsSampleJsonTests(ApiSampleTestBaseV2):
|
||||
pool_list = ["pool1", "pool2"]
|
||||
|
||||
def fake_get_floating_ip_pools(self, context):
|
||||
return [{'name': pool_list[0]},
|
||||
{'name': pool_list[1]}]
|
||||
return pool_list
|
||||
|
||||
self.stubs.Set(network_api.API, "get_floating_ip_pools",
|
||||
fake_get_floating_ip_pools)
|
||||
|
||||
@@ -224,6 +224,53 @@ class ApiTestCase(test.TestCase):
|
||||
self.network_api.get_floating_ip,
|
||||
self.context, '123zzz')
|
||||
|
||||
@mock.patch('nova.objects.FloatingIP.get_by_id')
|
||||
def test_get_floating_ip(self, mock_get):
|
||||
floating = mock.sentinel.floating
|
||||
mock_get.return_value = floating
|
||||
self.assertEqual(floating,
|
||||
self.network_api.get_floating_ip(self.context, 123))
|
||||
mock_get.assert_called_once_with(self.context, 123)
|
||||
|
||||
@mock.patch('nova.objects.FloatingIP.get_pool_names')
|
||||
def test_get_floating_ip_pools(self, mock_get):
|
||||
pools = ['foo', 'bar']
|
||||
mock_get.return_value = pools
|
||||
self.assertEqual(pools,
|
||||
self.network_api.get_floating_ip_pools(
|
||||
self.context))
|
||||
|
||||
@mock.patch('nova.objects.FloatingIP.get_by_address')
|
||||
def test_get_floating_ip_by_address(self, mock_get):
|
||||
floating = mock.sentinel.floating
|
||||
mock_get.return_value = floating
|
||||
self.assertEqual(floating,
|
||||
self.network_api.get_floating_ip_by_address(
|
||||
self.context, mock.sentinel.address))
|
||||
mock_get.assert_called_once_with(self.context,
|
||||
mock.sentinel.address)
|
||||
|
||||
@mock.patch('nova.objects.FloatingIPList.get_by_project')
|
||||
def test_get_floating_ips_by_project(self, mock_get):
|
||||
floatings = mock.sentinel.floating_ips
|
||||
mock_get.return_value = floatings
|
||||
self.assertEqual(floatings,
|
||||
self.network_api.get_floating_ips_by_project(
|
||||
self.context))
|
||||
mock_get.assert_called_once_with(self.context,
|
||||
self.context.project_id)
|
||||
|
||||
@mock.patch('nova.objects.FloatingIPList.get_by_fixed_address')
|
||||
def test_get_floating_ips_by_fixed_address(self, mock_get):
|
||||
floatings = [objects.FloatingIP(id=1, address='1.2.3.4'),
|
||||
objects.FloatingIP(id=2, address='5.6.7.8')]
|
||||
mock_get.return_value = floatings
|
||||
self.assertEqual(['1.2.3.4', '5.6.7.8'],
|
||||
self.network_api.get_floating_ips_by_fixed_address(
|
||||
self.context, mock.sentinel.fixed_address))
|
||||
mock_get.assert_called_once_with(self.context,
|
||||
mock.sentinel.fixed_address)
|
||||
|
||||
def _stub_migrate_instance_calls(self, method, multi_host, info):
|
||||
fake_flavor = flavors.get_default_flavor()
|
||||
fake_flavor['rxtx_factor'] = 1.21
|
||||
|
||||
Reference in New Issue
Block a user