From aa50c49b6f39cac6748386088a82848dee7f55ee Mon Sep 17 00:00:00 2001 From: ycx Date: Tue, 1 Aug 2017 19:54:49 +0800 Subject: [PATCH] Add "delay" value determination for health monitor Creating health monitor with excessive delay value can produce an error, because delay exceeds database restrictions. This fix adds delay value determination in the health monitor creation, error message can get displayed over prompt. Change-Id: I216c3fd947136ce7c26d3094edfa72084632c5a3 Closes-bug: 1706078 --- neutron_lbaas/extensions/loadbalancerv2.py | 16 +++++++++- .../loadbalancer/test_loadbalancer_plugin.py | 30 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/neutron_lbaas/extensions/loadbalancerv2.py b/neutron_lbaas/extensions/loadbalancerv2.py index 46e196a7f..b92e85b29 100644 --- a/neutron_lbaas/extensions/loadbalancerv2.py +++ b/neutron_lbaas/extensions/loadbalancerv2.py @@ -146,6 +146,17 @@ def _validate_connection_limit(data, min_value=lb_const.MIN_CONNECT_VALUE): validators.validators['type:connection_limit'] = _validate_connection_limit + +def _validate_db_limit(data, max_value=db_const.DB_INTEGER_MAX_VALUE): + if int(data) > max_value: + msg = (_("'%(data)s' is not a valid value, " + "because it is more than %(max_value)s") % + {'data': data, 'max_value': max_value}) + LOG.debug(msg) + return msg + +validators.validators['type:db_out_of_bounds'] = _validate_db_limit + RESOURCE_ATTRIBUTE_MAP = { 'loadbalancers': { 'id': {'allow_post': False, 'allow_put': False, @@ -318,7 +329,10 @@ RESOURCE_ATTRIBUTE_MAP = { 'type:values': lb_const.SUPPORTED_HEALTH_MONITOR_TYPES}, 'is_visible': True}, 'delay': {'allow_post': True, 'allow_put': True, - 'validate': {'type:non_negative': None}, + 'validate': { + 'type:db_out_of_bounds': + db_const.DB_INTEGER_MAX_VALUE, + 'type:non_negative': None}, 'convert_to': converters.convert_to_int, 'is_visible': True}, 'timeout': {'allow_post': True, 'allow_put': True, diff --git a/neutron_lbaas/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py b/neutron_lbaas/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py index 90a8bbe32..e3cc1c96d 100644 --- a/neutron_lbaas/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py +++ b/neutron_lbaas/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py @@ -629,6 +629,26 @@ class TestLoadBalancerExtensionV2TestCase(base.ExtensionTestCase): self.assertIn('healthmonitor', res) self.assertEqual(return_value, res['healthmonitor']) + def test_health_monitor_create_with_db_limit_more_than_max_value(self): + project_id = _uuid() + data = {'healthmonitor': {'type': 'HTTP', + 'delay': 3000000000000, + 'timeout': 1, + 'max_retries': 3, + 'http_method': 'GET', + 'url_path': '/path', + 'expected_codes': '200-300', + 'admin_state_up': True, + 'tenant_id': project_id, + 'project_id': project_id, + 'pool_id': _uuid(), + 'name': 'monitor1'}} + res = self.api.post(_get_path('lbaas/healthmonitors', fmt=self.fmt), + self.serialize(data), + content_type='application/%s' % self.fmt, + expect_errors=True) + self.assertEqual(exc.HTTPBadRequest.code, res.status_int) + def test_health_monitor_create_with_timeout_negative(self): project_id = _uuid() data = {'healthmonitor': {'type': 'HTTP', @@ -691,6 +711,16 @@ class TestLoadBalancerExtensionV2TestCase(base.ExtensionTestCase): self.assertIn('healthmonitor', res) self.assertEqual(return_value, res['healthmonitor']) + def test_health_monitor_update_with_db_limit_more_than_max_value(self): + health_monitor_id = _uuid() + update_data = {'healthmonitor': {'delay': 3000000000000}} + res = self.api.put(_get_path('lbaas/healthmonitors', + id=health_monitor_id, + fmt=self.fmt), + self.serialize(update_data), + expect_errors=True) + self.assertEqual(exc.HTTPBadRequest.code, res.status_int) + def test_health_monitor_get(self): health_monitor_id = _uuid() return_value = {'type': 'HTTP',