Add NetworkRequestList.from_tuples helper

We have duplicated logic in several places to convert a
legacy list of requested networks tuples into a
NetworkRequestList object. Let's just hide that in a helper
method in the object itself.

Change-Id: I301d34ada0af362ec0f8e809a99187ee13f9eb1b
This commit is contained in:
Matt Riedemann 2016-03-25 17:12:15 -04:00
parent 0d8042df82
commit fe649a2543
6 changed files with 37 additions and 28 deletions

View File

@ -368,9 +368,8 @@ class ComputeTaskManager(base.Base):
if (requested_networks and
not isinstance(requested_networks,
objects.NetworkRequestList)):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest.from_tuple(t)
for t in requested_networks])
requested_networks = objects.NetworkRequestList.from_tuples(
requested_networks)
# TODO(melwitt): Remove this in version 2.0 of the RPC API
flavor = filter_properties.get('instance_type')
if flavor and not isinstance(flavor, objects.Flavor):

View File

@ -398,9 +398,8 @@ class NetworkManager(manager.Manager):
if (requested_networks and
not isinstance(requested_networks,
objects.NetworkRequestList)):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest.from_tuple(t)
for t in requested_networks])
requested_networks = objects.NetworkRequestList.from_tuples(
requested_networks)
vpn = kwargs['vpn']
macs = kwargs['macs']
admin_context = context.elevated()

View File

@ -1098,9 +1098,8 @@ class API(base_api.NetworkAPI):
# TODO(danms): Remove me when all callers pass an object
if isinstance(requested_networks[0], tuple):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest.from_tuple(t)
for t in requested_networks])
requested_networks = objects.NetworkRequestList.from_tuples(
requested_networks)
for request in requested_networks:
if request.port_id:

View File

@ -63,6 +63,18 @@ class NetworkRequestList(obj_base.ObjectListBase, obj_base.NovaObject):
def as_tuples(self):
return [x.to_tuple() for x in self.objects]
@classmethod
def from_tuples(cls, net_tuples):
"""Convenience method for converting a list of network request tuples
into a NetworkRequestList object.
:param net_tuples: list of network request tuples
:returns: NetworkRequestList object
"""
requested_networks = cls(objects=[NetworkRequest.from_tuple(t)
for t in net_tuples])
return requested_networks
@property
def is_single_unspecified(self):
return ((len(self.objects) == 1) and

View File

@ -2022,10 +2022,8 @@ class CommonNetworkTestCase(test.TestCase):
db = manager.db
db.virtual_interface_delete_by_instance = mock.Mock()
ctx = context.RequestContext('igonre', 'igonre')
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest.from_tuple(t)
for t in [('123', '1.2.3.4'), ('123', '4.3.2.1'),
('123', None)]])
requested_networks = objects.NetworkRequestList.from_tuples(
[('123', '1.2.3.4'), ('123', '4.3.2.1'), ('123', None)])
manager.deallocate_for_instance(
ctx,
instance=fake_instance.fake_instance_obj(ctx),
@ -2041,9 +2039,8 @@ class CommonNetworkTestCase(test.TestCase):
db = manager.db
db.virtual_interface_delete_by_instance = mock.Mock()
ctx = context.RequestContext('igonre', 'igonre')
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest.from_tuple(t)
for t in [('123', '1.2.3.4'), ('123', '4.3.2.1')]])
requested_networks = objects.NetworkRequestList.from_tuples(
[('123', '1.2.3.4'), ('123', '4.3.2.1')])
with mock.patch.object(manager.network_rpcapi,
'update_dns') as mock_update_dns:
manager.deallocate_for_instance(

View File

@ -49,19 +49,22 @@ class _TestNetworkRequestObject(object):
self.assertEqual(('123', '1.2.3.4'),
request.to_tuple())
def test_from_tuple_neutron(self):
request = objects.NetworkRequest.from_tuple(
('123', '1.2.3.4', FAKE_UUID, None))
self.assertEqual('123', request.network_id)
self.assertEqual('1.2.3.4', str(request.address))
self.assertEqual(FAKE_UUID, request.port_id)
def test_from_tuples_neutron(self):
requests = objects.NetworkRequestList.from_tuples(
[('123', '1.2.3.4', FAKE_UUID, None)])
self.assertEqual(1, len(requests))
self.assertEqual('123', requests[0].network_id)
self.assertEqual('1.2.3.4', str(requests[0].address))
self.assertEqual(FAKE_UUID, requests[0].port_id)
self.assertIsNone(requests[0].pci_request_id)
def test_from_tuple_nova(self):
request = objects.NetworkRequest.from_tuple(
('123', '1.2.3.4'))
self.assertEqual('123', request.network_id)
self.assertEqual('1.2.3.4', str(request.address))
self.assertIsNone(request.port_id)
def test_from_tuples_nova(self):
requests = objects.NetworkRequestList.from_tuples(
[('123', '1.2.3.4')])
self.assertEqual(1, len(requests))
self.assertEqual('123', requests[0].network_id)
self.assertEqual('1.2.3.4', str(requests[0].address))
self.assertIsNone(requests[0].port_id)
@mock.patch('nova.utils.is_neutron', return_value=True)
def test_list_as_tuples(self, is_neutron):