3232from osc_lib .command import command
3333from osc_lib import exceptions
3434from osc_lib import utils
35- from oslo_utils import strutils
3635
3736from openstackclient .i18n import _
3837from openstackclient .identity import common as identity_common
3938from openstackclient .network import common as network_common
4039
41-
4240LOG = logging .getLogger (__name__ )
4341
4442IMAGE_STRING_FOR_BFV = 'N/A (booted from volume)'
@@ -273,6 +271,30 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
273271 return info
274272
275273
274+ def bool_from_str (value , strict = False ):
275+ true_strings = ('1' , 't' , 'true' , 'on' , 'y' , 'yes' )
276+ false_strings = ('0' , 'f' , 'false' , 'off' , 'n' , 'no' )
277+
278+ if isinstance (value , bool ):
279+ return value
280+
281+ lowered = value .strip ().lower ()
282+ if lowered in true_strings :
283+ return True
284+ elif lowered in false_strings or not strict :
285+ return False
286+
287+ msg = _ (
288+ "Unrecognized value '%(value)s'; acceptable values are: %(valid)s"
289+ ) % {
290+ 'value' : value ,
291+ 'valid' : ', ' .join (
292+ f"'{ s } '" for s in sorted (true_strings + false_strings )
293+ ),
294+ }
295+ raise ValueError (msg )
296+
297+
276298def boolenv (* vars , default = False ):
277299 """Search for the first defined of possibly many bool-like env vars.
278300
@@ -287,7 +309,7 @@ def boolenv(*vars, default=False):
287309 for v in vars :
288310 value = os .environ .get (v , None )
289311 if value :
290- return strutils . bool_from_string (value )
312+ return bool_from_str (value )
291313 return default
292314
293315
@@ -1713,8 +1735,9 @@ def _match_image(image_api, wanted_properties):
17131735
17141736 if 'delete_on_termination' in mapping :
17151737 try :
1716- value = strutils .bool_from_string (
1717- mapping ['delete_on_termination' ], strict = True
1738+ value = bool_from_str (
1739+ mapping ['delete_on_termination' ],
1740+ strict = True ,
17181741 )
17191742 except ValueError :
17201743 msg = _ (
0 commit comments