From 96d441a0da82a1f52d7ba5bd6614dd0717a1ba38 Mon Sep 17 00:00:00 2001 From: xianming mao Date: Thu, 13 Oct 2016 10:16:04 +0800 Subject: [PATCH] Use method is_valid_boolstr from oslo_utils Oslo.utils provides same method is_valid_boolstr, in oslo.utils 3.17,and now we are in oslo.utils>=3.17 ,so we should use it directly. Oslo_utils's is_valid_boolstr accepts "t" and "f" , "on" and "off",but Cinder didn't. The rest api in following will be influenced by this: API: cinder type-update Parameter: is-public cinder type-create is-public cinder snapshot-create force get_bool_param() param_string cinder backup-list all-tenants and unimplemented v3 api:group_types update function, and corresponding parameter is is_public. Change-Id: I0326e586fe6c402e3425fa65656821054e560e40 --- cinder/api/contrib/quotas.py | 2 +- cinder/api/contrib/types_manage.py | 6 ++++-- cinder/api/v1/snapshots.py | 2 +- cinder/api/v3/group_types.py | 2 +- cinder/backup/api.py | 3 +-- cinder/tests/unit/test_utils.py | 14 -------------- cinder/utils.py | 8 +------- 7 files changed, 9 insertions(+), 28 deletions(-) diff --git a/cinder/api/contrib/quotas.py b/cinder/api/contrib/quotas.py index f29f93f74ff..7f117696944 100644 --- a/cinder/api/contrib/quotas.py +++ b/cinder/api/contrib/quotas.py @@ -212,7 +212,7 @@ class QuotaSetsController(wsgi.Controller): # Get the optional argument 'skip_validation' from body, # if skip_validation is False, then validate existing resource. skip_flag = body.get('skip_validation', True) - if not utils.is_valid_boolstr(skip_flag): + if not strutils.is_valid_boolstr(skip_flag): msg = _("Invalid value '%s' for skip_validation.") % skip_flag raise exception.InvalidParameterValue(err=msg) skip_flag = strutils.bool_from_string(skip_flag) diff --git a/cinder/api/contrib/types_manage.py b/cinder/api/contrib/types_manage.py index f9d55a7d784..843df33b5a5 100644 --- a/cinder/api/contrib/types_manage.py +++ b/cinder/api/contrib/types_manage.py @@ -18,6 +18,8 @@ import six import webob +from oslo_utils import strutils + from cinder.api import extensions from cinder.api.openstack import wsgi from cinder.api.views import types as views_types @@ -72,7 +74,7 @@ class VolumeTypesManageController(wsgi.Controller): utils.check_string_length(description, 'Type description', min_length=0, max_length=255) - if not utils.is_valid_boolstr(is_public): + if not strutils.is_valid_boolstr(is_public): msg = _("Invalid value '%s' for is_public. Accepted values: " "True or False.") % is_public raise webob.exc.HTTPBadRequest(explanation=msg) @@ -124,7 +126,7 @@ class VolumeTypesManageController(wsgi.Controller): "a combination thereof.") raise webob.exc.HTTPBadRequest(explanation=msg) - if is_public is not None and not utils.is_valid_boolstr(is_public): + if is_public is not None and not strutils.is_valid_boolstr(is_public): msg = _("Invalid value '%s' for is_public. Accepted values: " "True or False.") % is_public raise webob.exc.HTTPBadRequest(explanation=msg) diff --git a/cinder/api/v1/snapshots.py b/cinder/api/v1/snapshots.py index b09dc90576c..c75af2a440c 100644 --- a/cinder/api/v1/snapshots.py +++ b/cinder/api/v1/snapshots.py @@ -142,7 +142,7 @@ class SnapshotsController(wsgi.Controller): msg = _LI("Create snapshot from volume %s") LOG.info(msg, volume_id) - if not utils.is_valid_boolstr(force): + if not strutils.is_valid_boolstr(force): msg = _("Invalid value '%s' for force. ") % force raise exception.InvalidParameterValue(err=msg) diff --git a/cinder/api/v3/group_types.py b/cinder/api/v3/group_types.py index dd6d3be5f4d..e916abe8554 100644 --- a/cinder/api/v3/group_types.py +++ b/cinder/api/v3/group_types.py @@ -124,7 +124,7 @@ class GroupTypesController(wsgi.Controller): "a combination thereof.") raise webob.exc.HTTPBadRequest(explanation=msg) - if is_public is not None and not utils.is_valid_boolstr(is_public): + if is_public is not None and not strutils.is_valid_boolstr(is_public): msg = _("Invalid value '%s' for is_public. Accepted values: " "True or False.") % is_public raise webob.exc.HTTPBadRequest(explanation=msg) diff --git a/cinder/backup/api.py b/cinder/backup/api.py index 69b33cea5a8..b2d5875fa41 100644 --- a/cinder/backup/api.py +++ b/cinder/backup/api.py @@ -39,7 +39,6 @@ from cinder.objects import fields import cinder.policy from cinder import quota from cinder import quota_utils -from cinder import utils import cinder.volume from cinder.volume import utils as volume_utils @@ -123,7 +122,7 @@ class API(base.Base): search_opts = search_opts or {} all_tenants = search_opts.pop('all_tenants', '0') - if not utils.is_valid_boolstr(all_tenants): + if not strutils.is_valid_boolstr(all_tenants): msg = _("all_tenants must be a boolean, got '%s'.") % all_tenants raise exception.InvalidParameterValue(err=msg) diff --git a/cinder/tests/unit/test_utils.py b/cinder/tests/unit/test_utils.py index fedc2f0c749..066840baf67 100644 --- a/cinder/tests/unit/test_utils.py +++ b/cinder/tests/unit/test_utils.py @@ -129,20 +129,6 @@ class GenericUtilsTestCase(test.TestCase): hostname = "<}\x1fh\x10e\x08l\x02l\x05o\x12!{>" self.assertEqual("hello", utils.sanitize_hostname(hostname)) - def test_is_valid_boolstr(self): - self.assertTrue(utils.is_valid_boolstr(True)) - self.assertTrue(utils.is_valid_boolstr('trUe')) - self.assertTrue(utils.is_valid_boolstr(False)) - self.assertTrue(utils.is_valid_boolstr('faLse')) - self.assertTrue(utils.is_valid_boolstr('yeS')) - self.assertTrue(utils.is_valid_boolstr('nO')) - self.assertTrue(utils.is_valid_boolstr('y')) - self.assertTrue(utils.is_valid_boolstr('N')) - self.assertTrue(utils.is_valid_boolstr(1)) - self.assertTrue(utils.is_valid_boolstr('1')) - self.assertTrue(utils.is_valid_boolstr(0)) - self.assertTrue(utils.is_valid_boolstr('0')) - @mock.patch('os.path.join', side_effect=lambda x, y: '/'.join((x, y))) def test_make_dev_path(self, mock_join): self.assertEqual('/dev/xvda', utils.make_dev_path('xvda')) diff --git a/cinder/utils.py b/cinder/utils.py index 4750d7947de..ba621792487 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -268,12 +268,6 @@ def last_completed_audit_period(unit=None): return (begin, end) -def is_valid_boolstr(val): - """Check if the provided string is a valid bool string or not.""" - val = str(val).lower() - return val in ('true', 'false', 'yes', 'no', 'y', 'n', '1', '0') - - def is_none_string(val): """Check if a string represents a None value.""" if not isinstance(val, six.string_types): @@ -609,7 +603,7 @@ def _get_disk_of_partition(devpath, st=None): def get_bool_param(param_string, params): param = params.get(param_string, False) - if not is_valid_boolstr(param): + if not strutils.is_valid_boolstr(param): msg = _('Value %(param)s for %(param_string)s is not a ' 'boolean.') % {'param': param, 'param_string': param_string} raise exception.InvalidParameterValue(err=msg)