Improve aggregate version check error messages with min_version

The aggregate set/list commands require at least version 1.1 but
if you don't specify any version we default to 1.0 and you get this
unhelpful message:

  Operation or argument is not supported with version 1.0

This change adds a simple min_version kwarg to the version compare
method such that if we fail the version check and the kwarg is passed
we use it to improve the message so the user doesn't have to read
the help of the command to figure out what they did wrong, e.g.:

  Operation or argument is not supported with version 1.0; \
  requires at least version 1.1

This only handles the aggregate set/list commands and could be used
elsewhere but this is just a start.

Change-Id: I28a859918ab78c8aa47e54928cb14339c7c75498
Story: #2005448
Task: #30498
This commit is contained in:
Matt Riedemann 2019-04-12 11:47:50 -04:00 committed by Tetsuro Nakamura
parent f2d41ee1df
commit cf0d72191f
3 changed files with 18 additions and 4 deletions

View File

@ -62,7 +62,7 @@ class SetAggregate(command.Lister, version.CheckerMixin):
return parser
@version.check(version.ge('1.1'))
@version.check(version.ge('1.1'), min_version='1.1')
def take_action(self, parsed_args):
http = self.app.client_manager.placement
@ -106,7 +106,7 @@ class ListAggregate(command.Lister):
return parser
@version.check(version.ge('1.1'))
@version.check(version.ge('1.1'), min_version='1.1')
def take_action(self, parsed_args):
http = self.app.client_manager.placement

View File

@ -13,6 +13,7 @@
import mock
import oslotest.base as base
import six
from osc_placement import version
@ -42,6 +43,12 @@ class TestVersion(base.BaseTestCase):
ValueError, version._compare, '1.0', version.le('.0'))
self.assertRaises(
ValueError, version._compare, '1', version.le('2'))
ex = self.assertRaises(
ValueError, version.compare, '1.0', version.ge('1.1'),
min_version='1.1')
self.assertEqual(
'Operation or argument is not supported with version 1.0; '
'requires at least version 1.1', six.text_type(ex))
def test_compare_with_exc(self):
self.assertTrue(version.compare('1.05', version.gt('1.4')))

View File

@ -78,17 +78,24 @@ def compare(ver, *predicates, **kwargs):
kwargs['exc'] - boolean whether exception should be raised
kwargs['op'] - (all, any) how predicates should be checked
kwargs['min_version'] - optional; used to aid in the error message that is
given, for example, to specify a minimum version to run a command.
Examples:
compare('1.1', version.gt('1.2'), exc=False) - False
compare('1.1', version.eq('1.0'), version.eq('1.1'), op=any) - True
compare('1.0', version.ge('1.1'), min_version='1.1') - raise ValueError
"""
exc = kwargs.get('exc', True)
if not _compare(ver, *predicates, **kwargs):
if exc:
raise ValueError(
'Operation or argument is not supported with version %s' % ver)
msg = ('Operation or argument is not supported with version %s' %
ver)
if 'min_version' in kwargs:
msg = ('%s; requires at least version %s' %
(msg, kwargs['min_version']))
raise ValueError(msg)
return False
return True