Merge "Move normalize_neutron_floating_ips to _normalize"

This commit is contained in:
Jenkins 2016-10-17 13:36:48 +00:00 committed by Gerrit Code Review
commit ea04528cad
4 changed files with 70 additions and 56 deletions

View File

@ -199,3 +199,54 @@ class Normalizer(object):
server['volumes'] = []
return server
def _normalize_neutron_floating_ips(self, ips):
"""Normalize the structure of Neutron floating IPs
Unfortunately, not all the Neutron floating_ip attributes are available
with Nova and not all Nova floating_ip attributes are available with
Neutron.
This function extract attributes that are common to Nova and Neutron
floating IP resource.
If the whole structure is needed inside shade, shade provides private
methods that returns "original" objects (e.g.
_neutron_allocate_floating_ip)
:param list ips: A list of Neutron floating IPs.
:returns:
A list of normalized dicts with the following attributes::
[
{
"id": "this-is-a-floating-ip-id",
"fixed_ip_address": "192.0.2.10",
"floating_ip_address": "198.51.100.10",
"network": "this-is-a-net-or-pool-id",
"attached": True,
"status": "ACTIVE"
}, ...
]
"""
return [
self._normalize_neutron_floating_ip(ip) for ip in ips
]
def _normalize_neutron_floating_ip(self, ip):
network_id = ip.get('floating_network_id', ip.get('network'))
project_id = ip.get('project_id', ip.get('tenant_id', ''))
return munch.Munch(
attached=ip.get('port_id') is not None and ip.get('port_id') != '',
fixed_ip_address=ip.get('fixed_ip_address'),
floating_ip_address=ip['floating_ip_address'],
floating_network_id=network_id,
id=ip['id'],
location=self._get_current_location(project_id=project_id),
network=network_id,
port_id=ip.get('port_id'),
project_id=project_id,
router_id=ip.get('router_id'),
status=ip.get('status', 'UNKNOWN'),
tenant_id=project_id
)

View File

@ -223,53 +223,6 @@ def normalize_nova_floating_ips(ips):
return meta.obj_list_to_dict(ret)
def normalize_neutron_floating_ips(ips):
"""Normalize the structure of Neutron floating IPs
Unfortunately, not all the Neutron floating_ip attributes are available
with Nova and not all Nova floating_ip attributes are available with
Neutron.
This function extract attributes that are common to Nova and Neutron
floating IP resource.
If the whole structure is needed inside shade, shade provides private
methods that returns "original" objects (e.g.
_neutron_allocate_floating_ip)
:param list ips: A list of Neutron floating IPs.
:returns:
A list of normalized dicts with the following attributes::
[
{
"id": "this-is-a-floating-ip-id",
"fixed_ip_address": "192.0.2.10",
"floating_ip_address": "198.51.100.10",
"network": "this-is-a-net-or-pool-id",
"attached": True,
"status": "ACTIVE"
}, ...
]
"""
ret = []
for ip in ips:
network_id = ip.get('floating_network_id', ip.get('network'))
ret.append(dict(
id=ip['id'],
fixed_ip_address=ip.get('fixed_ip_address'),
floating_ip_address=ip['floating_ip_address'],
network=network_id,
floating_network_id=network_id,
port_id=ip.get('port_id'),
router_id=ip.get('router_id'),
attached=(ip.get('port_id') is not None and
ip.get('port_id') != ''),
status=ip.get('status', 'UNKNOWN')
))
return meta.obj_list_to_dict(ret)
def localhost_supports_ipv6():
"""Determine whether the local host supports IPv6

View File

@ -1681,7 +1681,7 @@ class OpenStackCloud(_normalize.Normalizer):
def _list_floating_ips(self):
if self._use_neutron_floating():
try:
return _utils.normalize_neutron_floating_ips(
return self._normalize_neutron_floating_ips(
self._neutron_list_floating_ips())
except OpenStackCloudURINotFound as e:
self.log.debug(
@ -3731,7 +3731,7 @@ class OpenStackCloud(_normalize.Normalizer):
"""
if self._use_neutron_floating():
try:
f_ips = _utils.normalize_neutron_floating_ips(
f_ips = self._normalize_neutron_floating_ips(
self._neutron_available_floating_ips(
network=network, server=server))
return f_ips[0]
@ -3911,7 +3911,7 @@ class OpenStackCloud(_normalize.Normalizer):
def _submit_create_fip(self, kwargs):
# Split into a method to aid in test mocking
return _utils.normalize_neutron_floating_ips(
return self._normalize_neutron_floating_ips(
[self.manager.submit_task(_tasks.NeutronFloatingIPCreate(
body={'floatingip': kwargs}))['floatingip']])[0]

View File

@ -24,6 +24,7 @@ from mock import patch
from neutronclient.common import exceptions as n_exc
from shade import _normalize
from shade import _utils
from shade import exc
from shade import meta
@ -147,7 +148,7 @@ class TestFloatingIP(base.TestCase):
u'version': 4,
u'OS-EXT-IPS-MAC:mac_addr':
u'fa:16:3e:ae:7d:42'}]}))
self.floating_ip = _utils.normalize_neutron_floating_ips(
self.floating_ip = self.cloud._normalize_neutron_floating_ips(
self.mock_floating_ip_list_rep['floatingips'])[0]
def test_float_no_status(self):
@ -162,7 +163,7 @@ class TestFloatingIP(base.TestCase):
'tenant_id': '4969c491a3c74ee4af974e6d800c62df'
}
]
normalized = _utils.normalize_neutron_floating_ips(floating_ips)
normalized = self.cloud._normalize_neutron_floating_ips(floating_ips)
self.assertEqual('UNKNOWN', normalized[0]['status'])
@patch.object(OpenStackCloud, 'neutron_client')
@ -207,6 +208,15 @@ class TestFloatingIP(base.TestCase):
mock_neutron_client.list_floatingips.assert_called_with()
self.assertIsInstance(floating_ip, dict)
self.assertEqual('203.0.113.29', floating_ip['floating_ip_address'])
self.assertEqual(
self.mock_floating_ip_list_rep['floatingips'][0]['tenant_id'],
floating_ip['project_id']
)
self.assertEqual(
self.mock_floating_ip_list_rep['floatingips'][0]['tenant_id'],
floating_ip['tenant_id']
)
self.assertIn('location', floating_ip)
@patch.object(OpenStackCloud, 'neutron_client')
@patch.object(OpenStackCloud, 'has_service')
@ -277,7 +287,7 @@ class TestFloatingIP(base.TestCase):
self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'],
ip['floating_ip_address'])
@patch.object(_utils, 'normalize_neutron_floating_ips')
@patch.object(_normalize.Normalizer, '_normalize_neutron_floating_ips')
@patch.object(OpenStackCloud, '_neutron_available_floating_ips')
@patch.object(OpenStackCloud, 'has_service')
@patch.object(OpenStackCloud, 'keystone_session')
@ -402,7 +412,7 @@ class TestFloatingIP(base.TestCase):
mock_keystone_session,
mock_nova_client):
mock_has_service.return_value = True
fip = _utils.normalize_neutron_floating_ips(
fip = self.cloud._normalize_neutron_floating_ips(
self.mock_floating_ip_list_rep['floatingips'])[0]
mock__neutron_create_floating_ip.return_value = fip
mock_keystone_session.get_project_id.return_value = \
@ -561,7 +571,7 @@ class TestFloatingIP(base.TestCase):
mock_get_floating_ip):
mock_has_service.return_value = True
mock_get_floating_ip.return_value = \
_utils.normalize_neutron_floating_ips(
self.cloud._normalize_neutron_floating_ips(
self.mock_floating_ip_list_rep['floatingips'])[0]
self.cloud.detach_ip_from_server(
@ -585,7 +595,7 @@ class TestFloatingIP(base.TestCase):
mock_attach_ip_to_server):
mock_has_service.return_value = True
mock_available_floating_ip.return_value = \
_utils.normalize_neutron_floating_ips([
self.cloud._normalize_neutron_floating_ips([
self.mock_floating_ip_new_rep['floatingip']])[0]
mock_attach_ip_to_server.return_value = self.fake_server