From c606a1da7237621edeebc76eb42fe6acbce24b43 Mon Sep 17 00:00:00 2001 From: Bar RH Date: Wed, 6 Dec 2017 01:58:51 +0200 Subject: [PATCH] Allow case-insensitive entries when selecting from choices Change-Id: Iececc977d9e23f9b5175e69892f02d9f4fd938ec --- octaviaclient/osc/v2/health_monitor.py | 18 +++++++++++------- octaviaclient/osc/v2/l7policy.py | 6 ++++-- octaviaclient/osc/v2/l7rule.py | 12 ++++++++---- octaviaclient/osc/v2/listener.py | 7 +++++-- octaviaclient/osc/v2/pool.py | 18 ++++++++++++------ .../tests/unit/osc/v2/test_health_monitor.py | 6 ++++-- .../tests/unit/osc/v2/test_l7policy.py | 2 +- octaviaclient/tests/unit/osc/v2/test_l7rule.py | 2 +- .../tests/unit/osc/v2/test_listener.py | 2 +- 9 files changed, 47 insertions(+), 26 deletions(-) diff --git a/octaviaclient/osc/v2/health_monitor.py b/octaviaclient/osc/v2/health_monitor.py index b697c8e..a928dcd 100644 --- a/octaviaclient/osc/v2/health_monitor.py +++ b/octaviaclient/osc/v2/health_monitor.py @@ -24,6 +24,7 @@ from octaviaclient.osc.v2 import utils as v2_utils HTTP_METHODS = ['GET', 'POST', 'DELETE', 'PUT', 'HEAD', 'OPTIONS', 'PATCH', 'CONNECT', 'TRACE'] +TYPE_CHOICES = ['PING', 'HTTP', 'TCP', 'HTTPS', 'TLS-HELLO'] class CreateHealthMonitor(command.ShowOne): @@ -55,9 +56,10 @@ class CreateHealthMonitor(command.ShowOne): "the member to declare it healthy." ) parser.add_argument( - '--http_method', - metavar="{GET,POST,DELETE,PUT,HEAD,OPTIONS,PATCH,CONNECT,TRACE}", + '--http-method', + metavar='{' + ','.join(HTTP_METHODS) + '}', choices=HTTP_METHODS, + type=lambda s: s.upper(), # case insensitive help="Set the HTTP method that the health monitor uses for " "requests." ) @@ -86,10 +88,11 @@ class CreateHealthMonitor(command.ShowOne): ) parser.add_argument( '--type', - metavar="{PING,HTTP,TCP,HTTPS,TLS-HELLO}", + metavar='{' + ','.join(TYPE_CHOICES) + '}', required=True, - choices=['PING', 'HTTP', 'TCP', 'HTTPS', 'TLS-HELLO'], - help="Set the type of health monitor." + choices=TYPE_CHOICES, + type=lambda s: s.upper(), # case insensitive + help="Set the health monitor type." ) parser.add_argument( '--max-retries-down', @@ -234,9 +237,10 @@ class SetHealthMonitor(command.Command): "the member to declare it healthy." ) parser.add_argument( - '--http_method', - metavar="{GET,POST,DELETE,PUT,HEAD,OPTIONS,PATCH,CONNECT,TRACE}", + '--http-method', + metavar='{' + ','.join(HTTP_METHODS) + '}', choices=HTTP_METHODS, + type=lambda s: s.upper(), # case insensitive help="Set the HTTP method that the health monitor uses for " "requests." ) diff --git a/octaviaclient/osc/v2/l7policy.py b/octaviaclient/osc/v2/l7policy.py index 85dc721..ab9ba35 100644 --- a/octaviaclient/osc/v2/l7policy.py +++ b/octaviaclient/osc/v2/l7policy.py @@ -49,9 +49,10 @@ class CreateL7Policy(command.ShowOne): parser.add_argument( '--action', - metavar="{REDIRECT_TO_URL,REDIRECT_TO_POOL,REJECT}", + metavar='{' + ','.join(ACTION_CHOICES) + '}', required=True, choices=ACTION_CHOICES, + type=lambda s: s.upper(), # case insensitive help="Set the action of the policy." ) @@ -202,8 +203,9 @@ class SetL7Policy(command.Command): ) parser.add_argument( '--action', - metavar="{REDIRECT_TO_URL,REDIRECT_TO_POOL,REJECT}", + metavar='{' + ','.join(ACTION_CHOICES) + '}', choices=ACTION_CHOICES, + type=lambda s: s.upper(), # case insensitive help="Set the action of the policy." ) diff --git a/octaviaclient/osc/v2/l7rule.py b/octaviaclient/osc/v2/l7rule.py index 7811b6a..b4d1bd2 100644 --- a/octaviaclient/osc/v2/l7rule.py +++ b/octaviaclient/osc/v2/l7rule.py @@ -38,9 +38,10 @@ class CreateL7Rule(command.ShowOne): ) parser.add_argument( '--compare-type', - metavar="{REGEX,EQUAL_TO,CONTAINS,ENDS_WITH,STARTS_WITH}", + metavar='{' + ','.join(COMPARE_TYPES) + '}', required=True, choices=COMPARE_TYPES, + type=lambda s: s.upper(), # case insensitive help="Set the compare type for the l7rule." ) parser.add_argument( @@ -62,9 +63,10 @@ class CreateL7Rule(command.ShowOne): ) parser.add_argument( '--type', - metavar="{FILE_TYPE,PATH,COOKIE,HOST_NAME,HEADER}", + metavar='{' + ','.join(TYPES) + '}', required=True, choices=TYPES, + type=lambda s: s.upper(), # case insensitive help="Set the type for the l7rule." ) admin_group = parser.add_mutually_exclusive_group() @@ -205,8 +207,9 @@ class SetL7Rule(command.Command): ) parser.add_argument( '--compare-type', - metavar="{REGEX,EQUAL_TO,CONTAINS,ENDS_WITH,STARTS_WITH}", + metavar='{' + ','.join(COMPARE_TYPES) + '}', choices=COMPARE_TYPES, + type=lambda s: s.upper(), # case insensitive help="Set the compare type for the l7rule." ) parser.add_argument( @@ -227,8 +230,9 @@ class SetL7Rule(command.Command): ) parser.add_argument( '--type', - metavar="{FILE_TYPE,PATH,COOKIE,HOST_NAME,HEADER}", + metavar='{' + ','.join(TYPES) + '}', choices=TYPES, + type=lambda s: s.upper(), # case insensitive help="Set the type for the l7rule." ) admin_group = parser.add_mutually_exclusive_group() diff --git a/octaviaclient/osc/v2/listener.py b/octaviaclient/osc/v2/listener.py index 296899c..382477c 100644 --- a/octaviaclient/osc/v2/listener.py +++ b/octaviaclient/osc/v2/listener.py @@ -22,6 +22,8 @@ from osc_lib import utils from octaviaclient.osc.v2 import constants as const from octaviaclient.osc.v2 import utils as v2_utils +PROTOCOL_CHOICES = ['TCP', 'HTTP', 'HTTPS', 'TERMINATED_HTTPS'] + class CreateListener(command.ShowOne): """Create a listener""" @@ -46,8 +48,9 @@ class CreateListener(command.ShowOne): ) parser.add_argument( '--protocol', - metavar='{TCP,HTTP,HTTPS,TERMINATED_HTTPS}', - choices=['TCP', 'HTTP', 'HTTPS', 'TERMINATED_HTTPS'], + metavar='{' + ','.join(PROTOCOL_CHOICES) + '}', + choices=PROTOCOL_CHOICES, + type=lambda s: s.upper(), # case insensitive required=True, help="The protocol for the listener." ) diff --git a/octaviaclient/osc/v2/pool.py b/octaviaclient/osc/v2/pool.py index b96cddd..b547a89 100644 --- a/octaviaclient/osc/v2/pool.py +++ b/octaviaclient/osc/v2/pool.py @@ -21,6 +21,9 @@ from osc_lib import utils from octaviaclient.osc.v2 import constants as const from octaviaclient.osc.v2 import utils as v2_utils +PROTOCOL_CHOICES = ['TCP', 'HTTP', 'HTTPS', 'TERMINATED_HTTPS', 'PROXY'] +ALGORITHM_CHOICES = ['SOURCE_IP', 'ROUND_ROBIN', 'LEAST_CONNECTIONS'] + class CreatePool(command.ShowOne): """Create a pool""" @@ -40,9 +43,10 @@ class CreatePool(command.ShowOne): ) parser.add_argument( '--protocol', - metavar="{TCP,HTTP,HTTPS,TERMINATED_HTTPS,PROXY}", + metavar='{' + ','.join(PROTOCOL_CHOICES) + '}', required=True, - choices=['TCP', 'HTTP', 'HTTPS', 'TERMINATED_HTTPS', 'PROXY'], + choices=PROTOCOL_CHOICES, + type=lambda s: s.upper(), # case insensitive help="Set the pool protocol." ) parent_group = parser.add_mutually_exclusive_group(required=True) @@ -63,9 +67,10 @@ class CreatePool(command.ShowOne): ) parser.add_argument( '--lb-algorithm', - metavar="{SOURCE_IP,ROUND_ROBIN,LEAST_CONNECTIONS}", + metavar='{' + ','.join(ALGORITHM_CHOICES) + '}', required=True, - choices=['SOURCE_IP', 'ROUND_ROBIN', 'LEAST_CONNECTIONS'], + choices=ALGORITHM_CHOICES, + type=lambda s: s.upper(), # case insensitive help="Load balancing algorithm to use." ) admin_group = parser.add_mutually_exclusive_group() @@ -208,8 +213,9 @@ class SetPool(command.Command): ) parser.add_argument( '--lb-algorithm', - metavar="{SOURCE_IP,ROUND_ROBIN,LEAST_CONNECTIONS}", - choices=['SOURCE_IP', 'ROUND_ROBIN', 'LEAST_CONNECTIONS'], + metavar='{' + ','.join(ALGORITHM_CHOICES) + '}', + choices=ALGORITHM_CHOICES, + type=lambda s: s.upper(), # case insensitive help="Set the load balancing algorithm to use." ) admin_group = parser.add_mutually_exclusive_group() diff --git a/octaviaclient/tests/unit/osc/v2/test_health_monitor.py b/octaviaclient/tests/unit/osc/v2/test_health_monitor.py index b885027..74a46d2 100644 --- a/octaviaclient/tests/unit/osc/v2/test_health_monitor.py +++ b/octaviaclient/tests/unit/osc/v2/test_health_monitor.py @@ -137,14 +137,16 @@ class TestHealthMonitorCreate(TestHealthMonitor): '--delay', str(self._hm.delay), '--timeout', str(self._hm.timeout), '--max-retries', str(self._hm.max_retries), - '--type', self._hm.type] + '--type', self._hm.type.lower(), + '--http-method', self._hm.http_method.lower()] verifylist = [ ('pool', 'mock_pool_id'), ('name', self._hm.name), ('delay', str(self._hm.delay)), ('timeout', str(self._hm.timeout)), ('max_retries', self._hm.max_retries), - ('type', self._hm.type) + ('type', self._hm.type), + ('http_method', self._hm.http_method), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) diff --git a/octaviaclient/tests/unit/osc/v2/test_l7policy.py b/octaviaclient/tests/unit/osc/v2/test_l7policy.py index 955299b..80ff348 100644 --- a/octaviaclient/tests/unit/osc/v2/test_l7policy.py +++ b/octaviaclient/tests/unit/osc/v2/test_l7policy.py @@ -143,7 +143,7 @@ class TestL7PolicyCreate(TestL7Policy): } arglist = ['mock_li_id', '--name', self._l7po.name, - '--action', 'REDIRECT_TO_POOL', + '--action', 'REDIRECT_TO_POOL'.lower(), '--redirect-pool', self._l7po.redirect_pool_id] verifylist = [ diff --git a/octaviaclient/tests/unit/osc/v2/test_l7rule.py b/octaviaclient/tests/unit/osc/v2/test_l7rule.py index 34a59a9..2e59320 100644 --- a/octaviaclient/tests/unit/osc/v2/test_l7rule.py +++ b/octaviaclient/tests/unit/osc/v2/test_l7rule.py @@ -158,7 +158,7 @@ class TestL7RuleCreate(TestL7Policy): arglist = [self._l7po.id, '--compare-type', 'ENDS_WITH', '--value', '.example.com', - '--type', 'HOST_NAME'] + '--type', 'HOST_NAME'.lower()] verifylist = [ ('l7policy', self._l7po.id), diff --git a/octaviaclient/tests/unit/osc/v2/test_listener.py b/octaviaclient/tests/unit/osc/v2/test_listener.py index 337d65f..517fe7d 100644 --- a/octaviaclient/tests/unit/osc/v2/test_listener.py +++ b/octaviaclient/tests/unit/osc/v2/test_listener.py @@ -172,7 +172,7 @@ class TestListenerCreate(TestListener): mock_client.return_value = self.li_info['listeners'][0] arglist = ['mock_lb_id', '--name', self._li.name, - '--protocol', 'TERMINATED_HTTPS', + '--protocol', 'TERMINATED_HTTPS'.lower(), '--protocol-port', '443', '--sni-container-refs', self._li.sni_container_refs[0],