Merge nova and neutron normalize methods

In keeping with the other normalize methods, combine these two into a
single normalize method.

Removed a unittest that did literally nothing but was mocking out things
at a crazy level. Remind me that we should REALLY stop mocking out
anything other than the requests calls.

Depends-On: I59e9834d249ccda0beeb715a8d6271140fb144f4
Change-Id: I077f9d8c4d250d180dec9ce4aabc8f64b507b05f
This commit is contained in:
Monty Taylor 2016-10-17 09:47:20 -05:00
parent ea04528cad
commit 78e999776a
5 changed files with 34 additions and 88 deletions

View File

@ -200,8 +200,8 @@ class Normalizer(object):
return server
def _normalize_neutron_floating_ips(self, ips):
"""Normalize the structure of Neutron floating IPs
def _normalize_floating_ips(self, ips):
"""Normalize the structure of floating IPs
Unfortunately, not all the Neutron floating_ip attributes are available
with Nova and not all Nova floating_ip attributes are available with
@ -230,16 +230,28 @@ class Normalizer(object):
"""
return [
self._normalize_neutron_floating_ip(ip) for ip in ips
self._normalize_floating_ip(ip) for ip in ips
]
def _normalize_neutron_floating_ip(self, ip):
network_id = ip.get('floating_network_id', ip.get('network'))
def _normalize_floating_ip(self, ip):
fixed_ip_address = ip.get('fixed_ip_address', ip.get('fixed_ip'))
floating_ip_address = ip.get('floating_ip_address', ip.get('ip'))
network_id = ip.get(
'floating_network_id', ip.get('network', ip.get('pool')))
project_id = ip.get('project_id', ip.get('tenant_id', ''))
if self._use_neutron_floating():
attached = (ip.get('port_id') is not None and ip['port_id'] != '')
status = ip.get('status', 'UNKNOWN')
else:
instance_id = ip.get('instance_id')
attached = instance_id is not None and instance_id != ''
# In neutron's terms, Nova floating IPs are always ACTIVE
status = 'ACTIVE'
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'],
attached=attached,
fixed_ip_address=fixed_ip_address,
floating_ip_address=floating_ip_address,
floating_network_id=network_id,
id=ip['id'],
location=self._get_current_location(project_id=project_id),
@ -247,6 +259,6 @@ class Normalizer(object):
port_id=ip.get('port_id'),
project_id=project_id,
router_id=ip.get('router_id'),
status=ip.get('status', 'UNKNOWN'),
status=status,
tenant_id=project_id
)

View File

@ -182,47 +182,6 @@ def normalize_keystone_services(services):
return meta.obj_list_to_dict(ret)
def normalize_nova_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. _nova_allocate_floating_ip)
:param list ips: A list of Nova 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 = [dict(
id=ip['id'],
fixed_ip_address=ip.get('fixed_ip'),
floating_ip_address=ip['ip'],
network=ip['pool'],
attached=(ip.get('instance_id') is not None and
ip.get('instance_id') != ''),
status='ACTIVE' # In neutrons terms, Nova floating IPs are always
# ACTIVE
) for ip in ips]
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 self._normalize_neutron_floating_ips(
return self._normalize_floating_ips(
self._neutron_list_floating_ips())
except OpenStackCloudURINotFound as e:
self.log.debug(
@ -1690,7 +1690,7 @@ class OpenStackCloud(_normalize.Normalizer):
# Fall-through, trying with Nova
floating_ips = self._nova_list_floating_ips()
return _utils.normalize_nova_floating_ips(floating_ips)
return self._normalize_floating_ips(floating_ips)
def list_floating_ips(self):
"""List all available floating IPs.
@ -3731,7 +3731,7 @@ class OpenStackCloud(_normalize.Normalizer):
"""
if self._use_neutron_floating():
try:
f_ips = self._normalize_neutron_floating_ips(
f_ips = self._normalize_floating_ips(
self._neutron_available_floating_ips(
network=network, server=server))
return f_ips[0]
@ -3741,7 +3741,7 @@ class OpenStackCloud(_normalize.Normalizer):
"'{msg}'. Trying with Nova.".format(msg=str(e)))
# Fall-through, trying with Nova
f_ips = _utils.normalize_nova_floating_ips(
f_ips = self._normalize_floating_ips(
self._nova_available_floating_ips(pool=network)
)
return f_ips[0]
@ -3800,7 +3800,7 @@ class OpenStackCloud(_normalize.Normalizer):
'tenant_id': project_id
}
floating_ips = self._neutron_list_floating_ips()
floating_ips = self._list_floating_ips()
available_ips = _utils._filter_list(
floating_ips, name_or_id=None, filters=filters)
if available_ips:
@ -3905,13 +3905,13 @@ class OpenStackCloud(_normalize.Normalizer):
" to neutron, or alternately provide the server,"
" fixed_address and nat_destination arguments as appropriate")
# Else, we are using Nova network
f_ips = _utils.normalize_nova_floating_ips(
f_ips = self._normalize_floating_ips(
[self._nova_create_floating_ip(pool=network)])
return f_ips[0]
def _submit_create_fip(self, kwargs):
# Split into a method to aid in test mocking
return self._normalize_neutron_floating_ips(
return self._normalize_floating_ips(
[self.manager.submit_task(_tasks.NeutronFloatingIPCreate(
body={'floatingip': kwargs}))['floatingip']])[0]

View File

@ -24,7 +24,6 @@ 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
@ -148,7 +147,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 = self.cloud._normalize_neutron_floating_ips(
self.floating_ip = self.cloud._normalize_floating_ips(
self.mock_floating_ip_list_rep['floatingips'])[0]
def test_float_no_status(self):
@ -163,7 +162,7 @@ class TestFloatingIP(base.TestCase):
'tenant_id': '4969c491a3c74ee4af974e6d800c62df'
}
]
normalized = self.cloud._normalize_neutron_floating_ips(floating_ips)
normalized = self.cloud._normalize_floating_ips(floating_ips)
self.assertEqual('UNKNOWN', normalized[0]['status'])
@patch.object(OpenStackCloud, 'neutron_client')
@ -287,29 +286,6 @@ class TestFloatingIP(base.TestCase):
self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'],
ip['floating_ip_address'])
@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')
def test_available_floating_ip_neutron(self,
mock_keystone,
mock_has_service,
mock__neutron_call,
mock_normalize):
"""
Test the correct path is taken when using neutron.
"""
# force neutron path
mock_has_service.return_value = True
mock__neutron_call.return_value = []
self.cloud.available_floating_ip(network='netname')
mock_has_service.assert_called_once_with('network')
mock__neutron_call.assert_called_once_with(network='netname',
server=None)
mock_normalize.assert_called_once_with([])
@patch.object(_utils, '_filter_list')
@patch.object(OpenStackCloud, '_neutron_create_floating_ip')
@patch.object(OpenStackCloud, '_neutron_list_floating_ips')
@ -412,7 +388,7 @@ class TestFloatingIP(base.TestCase):
mock_keystone_session,
mock_nova_client):
mock_has_service.return_value = True
fip = self.cloud._normalize_neutron_floating_ips(
fip = self.cloud._normalize_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 = \
@ -571,7 +547,7 @@ class TestFloatingIP(base.TestCase):
mock_get_floating_ip):
mock_has_service.return_value = True
mock_get_floating_ip.return_value = \
self.cloud._normalize_neutron_floating_ips(
self.cloud._normalize_floating_ips(
self.mock_floating_ip_list_rep['floatingips'])[0]
self.cloud.detach_ip_from_server(
@ -595,7 +571,7 @@ class TestFloatingIP(base.TestCase):
mock_attach_ip_to_server):
mock_has_service.return_value = True
mock_available_floating_ip.return_value = \
self.cloud._normalize_neutron_floating_ips([
self.cloud._normalize_floating_ips([
self.mock_floating_ip_new_rep['floatingip']])[0]
mock_attach_ip_to_server.return_value = self.fake_server

View File

@ -22,7 +22,6 @@ Tests Floating IP resource methods for nova-network
from mock import patch
from novaclient import exceptions as n_exc
from shade import _utils
from shade import meta
from shade import OpenStackCloud
from shade.tests import fakes
@ -84,7 +83,7 @@ class TestFloatingIP(base.TestCase):
u'OS-EXT-IPS-MAC:mac_addr':
u'fa:16:3e:ae:7d:42'}]}))
self.floating_ip = _utils.normalize_nova_floating_ips(
self.floating_ip = self.cloud._normalize_floating_ips(
meta.obj_list_to_dict(self.floating_ips))[0]
@patch.object(OpenStackCloud, 'nova_client')