Add 'convert_to_string' and apply for firewall-rule
I7b4108772e8370e8f51971caf40ecd23e9f977e9 has been merged but missing
converter for firewall_rule. This commit adds a new converter to modify
string and applies this one for following attributes both fwaas and
fwaas_v2.
* source_port as in firewall_rule
* destination_port as in firewall_rule
Change-Id: Iced5a588b2618735cbe6fc081e42e49896d7a91a
This commit is contained in:
@@ -252,3 +252,14 @@ def convert_to_protocol(data):
|
|||||||
raise n_exc.InvalidInput(error_message=error_message)
|
raise n_exc.InvalidInput(error_message=error_message)
|
||||||
except n_exc.InvalidInput:
|
except n_exc.InvalidInput:
|
||||||
raise n_exc.InvalidInput(error_message=error_message)
|
raise n_exc.InvalidInput(error_message=error_message)
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_string(data):
|
||||||
|
"""Convert a data value into a string.
|
||||||
|
|
||||||
|
:param data: The data value to convert to a string.
|
||||||
|
:returns: The string value of 'data' if data is not None
|
||||||
|
"""
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
return str(data)
|
||||||
|
|||||||
@@ -83,9 +83,11 @@ RESOURCE_ATTRIBUTE_MAP = {
|
|||||||
'is_visible': True, 'default': None},
|
'is_visible': True, 'default': None},
|
||||||
'source_port': {'allow_post': True, 'allow_put': True,
|
'source_port': {'allow_post': True, 'allow_put': True,
|
||||||
'validate': {'type:port_range': None},
|
'validate': {'type:port_range': None},
|
||||||
|
'convert_to': converters.convert_to_string,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
'destination_port': {'allow_post': True, 'allow_put': True,
|
'destination_port': {'allow_post': True, 'allow_put': True,
|
||||||
'validate': {'type:port_range': None},
|
'validate': {'type:port_range': None},
|
||||||
|
'convert_to': converters.convert_to_string,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
'position': {'allow_post': False, 'allow_put': False,
|
'position': {'allow_post': False, 'allow_put': False,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
|
|||||||
@@ -84,9 +84,11 @@ RESOURCE_ATTRIBUTE_MAP = {
|
|||||||
'is_visible': True, 'default': None},
|
'is_visible': True, 'default': None},
|
||||||
'source_port': {'allow_post': True, 'allow_put': True,
|
'source_port': {'allow_post': True, 'allow_put': True,
|
||||||
'validate': {'type:port_range': None},
|
'validate': {'type:port_range': None},
|
||||||
|
'convert_to': converters.convert_to_string,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
'destination_port': {'allow_post': True, 'allow_put': True,
|
'destination_port': {'allow_post': True, 'allow_put': True,
|
||||||
'validate': {'type:port_range': None},
|
'validate': {'type:port_range': None},
|
||||||
|
'convert_to': converters.convert_to_string,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
'position': {'allow_post': False, 'allow_put': False,
|
'position': {'allow_post': False, 'allow_put': False,
|
||||||
'default': None, 'is_visible': True},
|
'default': None, 'is_visible': True},
|
||||||
|
|||||||
@@ -270,3 +270,31 @@ class TestConvertProtocol(base.BaseTestCase):
|
|||||||
def test_unknown_string(self):
|
def test_unknown_string(self):
|
||||||
with testtools.ExpectedException(n_exc.InvalidInput):
|
with testtools.ExpectedException(n_exc.InvalidInput):
|
||||||
converters.convert_to_protocol("Invalid")
|
converters.convert_to_protocol("Invalid")
|
||||||
|
|
||||||
|
|
||||||
|
class TestConvertToString(base.BaseTestCase):
|
||||||
|
|
||||||
|
def test_data_is_string(self):
|
||||||
|
self.assertEqual('10000', converters.convert_to_string('10000'))
|
||||||
|
|
||||||
|
def test_data_is_integer(self):
|
||||||
|
self.assertEqual('10000', converters.convert_to_string(10000))
|
||||||
|
|
||||||
|
def test_data_is_integer_zero(self):
|
||||||
|
self.assertEqual('0', converters.convert_to_string(0))
|
||||||
|
|
||||||
|
def test_data_is_none(self):
|
||||||
|
self.assertIsNone(converters.convert_to_string(None))
|
||||||
|
|
||||||
|
def test_data_is_empty_list(self):
|
||||||
|
self.assertEqual('[]', converters.convert_to_string([]))
|
||||||
|
|
||||||
|
def test_data_is_list(self):
|
||||||
|
self.assertEqual("[1, 2, 3]", converters.convert_to_string([1, 2, 3]))
|
||||||
|
|
||||||
|
def test_data_is_empty_dict(self):
|
||||||
|
self.assertEqual('{}', converters.convert_to_string({}))
|
||||||
|
|
||||||
|
def test_data_is_dict(self):
|
||||||
|
self.assertEqual("{'foo': 'bar'}",
|
||||||
|
converters.convert_to_string({'foo': 'bar'}))
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- |
|
||||||
|
A new converter ``convert_to_string`` into ``neutron_lib.api.converters``.
|
||||||
|
This method can convert an argument which is not None into string value.
|
||||||
Reference in New Issue
Block a user