From 5268c1136ed09346d13b28b42626c0b302e3abb2 Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Thu, 8 May 2014 18:27:05 -0400 Subject: [PATCH] Convert address to str in fixed_ip_obj.associate A caller of associate may pass in a netaddr.IPAddress argument which will currently fail when it gets to sqlalchemy. Following the convention of other methods the netaddr.IPAddress will be cast to a string. Change-Id: I67c1876b447217b37104660ba87ed967f477b863 Closes-bug: 1317668 --- nova/objects/fixed_ip.py | 12 +++++++++++- nova/tests/objects/test_fixed_ip.py | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/nova/objects/fixed_ip.py b/nova/objects/fixed_ip.py index d39f57701d30..1b5d50a37f92 100644 --- a/nova/objects/fixed_ip.py +++ b/nova/objects/fixed_ip.py @@ -104,9 +104,19 @@ class FixedIP(obj_base.NovaPersistentObject, obj_base.NovaObject): db_fixedip = db.fixed_ip_get_by_network_host(context, network_id, host) return cls._from_db_object(context, cls(), db_fixedip) - @obj_base.remotable_classmethod + @classmethod def associate(cls, context, address, instance_uuid, network_id=None, reserved=False): + # NOTE(alaski): address may be a netaddr.IPAddress which is not + # serializable for RPC, and fails in SQLAlchemy. + str_address = str(address) + fixedip = cls._associate(context, str_address, instance_uuid, + network_id=network_id, reserved=reserved) + return fixedip + + @obj_base.remotable_classmethod + def _associate(cls, context, address, instance_uuid, network_id=None, + reserved=False): db_fixedip = db.fixed_ip_associate(context, address, instance_uuid, network_id=network_id, reserved=reserved) diff --git a/nova/tests/objects/test_fixed_ip.py b/nova/tests/objects/test_fixed_ip.py index 04dd4d2f49fb..8212ab0a4851 100644 --- a/nova/tests/objects/test_fixed_ip.py +++ b/nova/tests/objects/test_fixed_ip.py @@ -155,6 +155,16 @@ class _TestFixedIPObject(object): network_id=None, reserved=False) self._compare(fixedip, fake_fixed_ip) + @mock.patch('nova.db.fixed_ip_associate') + def test_associate_with_IPAddress(self, associate): + associate.return_value = fake_fixed_ip + address = netaddr.IPAddress('1.2.3.4') + fixedip = fixed_ip.FixedIP.associate(self.context, address, + 'fake-uuid') + associate.assert_called_with(self.context, '1.2.3.4', 'fake-uuid', + network_id=None, reserved=False) + self._compare(fixedip, fake_fixed_ip) + @mock.patch('nova.db.fixed_ip_associate_pool') def test_associate_pool(self, associate): associate.return_value = fake_fixed_ip