From: Salvatore Orlando Date: Tue, 9 Jun 2015 10:41:07 +0000 (-0700) Subject: NSX QoS ext: RXTX factor can be decimal X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=538a7bf3c73d968185590d81a9b3a6523190aeb4;p=openstack-build%2Fneutron-build.git NSX QoS ext: RXTX factor can be decimal In Nova flavors it is ok to specify a decimal RXTX factor. For this reason when applying QoS to a port Neutron should not convert this factor to an integer value, but simply ensure it's a valid float number and positive. Partial-Bug: #1463363 Change-Id: I983123ef7fd8f1b52b358aff3b579459fce63033 --- diff --git a/neutron/api/v2/attributes.py b/neutron/api/v2/attributes.py index bfc056010..7bd61fa73 100644 --- a/neutron/api/v2/attributes.py +++ b/neutron/api/v2/attributes.py @@ -518,6 +518,24 @@ def convert_to_int_if_not_none(data): return data +def convert_to_positive_float_or_none(val): + # NOTE(salv-orlando): This conversion function is currently used by + # a vendor specific extension only at the moment It is used for + # port's RXTX factor in neutron.plugins.vmware.extensions.qos. + # It is deemed however generic enough to be in this module as it + # might be used in future for other API attributes. + if val is None: + return + try: + val = float(val) + if val < 0: + raise ValueError() + except (ValueError, TypeError): + msg = _("'%s' must be a non negative decimal.") % val + raise n_exc.InvalidInput(error_message=msg) + return val + + def convert_kvp_str_to_list(data): """Convert a value of the form 'key=value' to ['key', 'value']. diff --git a/neutron/plugins/vmware/extensions/qos.py b/neutron/plugins/vmware/extensions/qos.py index 30e6f8c7d..94272466d 100644 --- a/neutron/plugins/vmware/extensions/qos.py +++ b/neutron/plugins/vmware/extensions/qos.py @@ -145,7 +145,7 @@ EXTENDED_ATTRIBUTES_2_0 = { 'is_visible': False, 'default': 1, 'enforce_policy': True, - 'convert_to': convert_to_unsigned_int_or_none}, + 'convert_to': attr.convert_to_positive_float_or_none}, QUEUE: {'allow_post': False, 'allow_put': False, diff --git a/neutron/tests/unit/api/v2/test_attributes.py b/neutron/tests/unit/api/v2/test_attributes.py index 28b8d6621..512fc3022 100644 --- a/neutron/tests/unit/api/v2/test_attributes.py +++ b/neutron/tests/unit/api/v2/test_attributes.py @@ -799,6 +799,35 @@ class TestConvertToInt(base.BaseTestCase): value, attributes.convert_none_to_empty_list(value)) +class TestConvertToFloat(base.BaseTestCase): + # NOTE: the routine being tested here is a plugin-specific extension + # module. As the plugin split proceed towards its second phase this + # test should either be remove, or the validation routine moved into + # neutron.api.v2.attributes + + def test_convert_to_float_positve_value(self): + self.assertEqual( + 1.111, attributes.convert_to_positive_float_or_none(1.111)) + self.assertEqual(1, attributes.convert_to_positive_float_or_none(1)) + self.assertEqual(0, attributes.convert_to_positive_float_or_none(0)) + + def test_convert_to_float_negative_value(self): + self.assertRaises(n_exc.InvalidInput, + attributes.convert_to_positive_float_or_none, + -1.11) + + def test_convert_to_float_string(self): + self.assertEqual(4, attributes.convert_to_positive_float_or_none('4')) + self.assertEqual( + 4.44, attributes.convert_to_positive_float_or_none('4.44')) + self.assertRaises(n_exc.InvalidInput, + attributes.convert_to_positive_float_or_none, + 'garbage') + + def test_convert_to_float_none_value(self): + self.assertIsNone(attributes.convert_to_positive_float_or_none(None)) + + class TestConvertKvp(base.BaseTestCase): def test_convert_kvp_list_to_dict_succeeds_for_missing_values(self):