|
34 | 34 | from osc_lib import utils |
35 | 35 |
|
36 | 36 | from openstackclient.api import compute_v2 |
| 37 | +from openstackclient.common import envvars |
37 | 38 | from openstackclient.common import pagination |
38 | 39 | from openstackclient.i18n import _ |
39 | 40 | from openstackclient.identity import common as identity_common |
@@ -324,48 +325,6 @@ def _prep_server_detail(compute_client, image_client, server, *, refresh=True): |
324 | 325 | return info |
325 | 326 |
|
326 | 327 |
|
327 | | -def bool_from_str(value, strict=False): |
328 | | - true_strings = ('1', 't', 'true', 'on', 'y', 'yes') |
329 | | - false_strings = ('0', 'f', 'false', 'off', 'n', 'no') |
330 | | - |
331 | | - if isinstance(value, bool): |
332 | | - return value |
333 | | - |
334 | | - lowered = value.strip().lower() |
335 | | - if lowered in true_strings: |
336 | | - return True |
337 | | - elif lowered in false_strings or not strict: |
338 | | - return False |
339 | | - |
340 | | - msg = _( |
341 | | - "Unrecognized value '%(value)s'; acceptable values are: %(valid)s" |
342 | | - ) % { |
343 | | - 'value': value, |
344 | | - 'valid': ', '.join( |
345 | | - f"'{s}'" for s in sorted(true_strings + false_strings) |
346 | | - ), |
347 | | - } |
348 | | - raise ValueError(msg) |
349 | | - |
350 | | - |
351 | | -def boolenv(*vars, default=False): |
352 | | - """Search for the first defined of possibly many bool-like env vars. |
353 | | -
|
354 | | - Returns the first environment variable defined in vars, or returns the |
355 | | - default. |
356 | | -
|
357 | | - :param vars: Arbitrary strings to search for. Case sensitive. |
358 | | - :param default: The default to return if no value found. |
359 | | - :returns: A boolean corresponding to the value found, else the default if |
360 | | - no value found. |
361 | | - """ |
362 | | - for v in vars: |
363 | | - value = os.environ.get(v, None) |
364 | | - if value: |
365 | | - return bool_from_str(value) |
366 | | - return default |
367 | | - |
368 | | - |
369 | 328 | class AddFixedIP(command.ShowOne): |
370 | 329 | _description = _("Add fixed IP address to server") |
371 | 330 |
|
@@ -1876,7 +1835,7 @@ def _match_image(image_api, wanted_properties): |
1876 | 1835 |
|
1877 | 1836 | if 'delete_on_termination' in mapping: |
1878 | 1837 | try: |
1879 | | - value = bool_from_str( |
| 1838 | + value = envvars.bool_from_str( |
1880 | 1839 | mapping['delete_on_termination'], |
1881 | 1840 | strict=True, |
1882 | 1841 | ) |
@@ -2223,7 +2182,7 @@ def get_parser(self, prog_name): |
2223 | 2182 | parser.add_argument( |
2224 | 2183 | '--all-projects', |
2225 | 2184 | action='store_true', |
2226 | | - default=boolenv('ALL_PROJECTS'), |
| 2185 | + default=envvars.boolenv('ALL_PROJECTS'), |
2227 | 2186 | help=_( |
2228 | 2187 | 'Delete server(s) in another project by name (admin only)' |
2229 | 2188 | '(can be specified using the ALL_PROJECTS envvar)' |
@@ -2389,7 +2348,7 @@ def get_parser(self, prog_name): |
2389 | 2348 | parser.add_argument( |
2390 | 2349 | '--all-projects', |
2391 | 2350 | action='store_true', |
2392 | | - default=boolenv('ALL_PROJECTS'), |
| 2351 | + default=envvars.boolenv('ALL_PROJECTS'), |
2393 | 2352 | help=_( |
2394 | 2353 | 'Include all projects (admin only) ' |
2395 | 2354 | '(can be specified using the ALL_PROJECTS envvar)' |
@@ -4967,7 +4926,7 @@ def get_parser(self, prog_name): |
4967 | 4926 | parser.add_argument( |
4968 | 4927 | '--all-projects', |
4969 | 4928 | action='store_true', |
4970 | | - default=boolenv('ALL_PROJECTS'), |
| 4929 | + default=envvars.boolenv('ALL_PROJECTS'), |
4971 | 4930 | help=_( |
4972 | 4931 | 'Start server(s) in another project by name (admin only) ' |
4973 | 4932 | '(can be specified using the ALL_PROJECTS envvar)' |
@@ -5002,7 +4961,7 @@ def get_parser(self, prog_name): |
5002 | 4961 | parser.add_argument( |
5003 | 4962 | '--all-projects', |
5004 | 4963 | action='store_true', |
5005 | | - default=boolenv('ALL_PROJECTS'), |
| 4964 | + default=envvars.boolenv('ALL_PROJECTS'), |
5006 | 4965 | help=_( |
5007 | 4966 | 'Stop server(s) in another project by name (admin only) ' |
5008 | 4967 | '(can be specified using the ALL_PROJECTS envvar)' |
|
0 commit comments