Skip to content

Commit dc8596f

Browse files
committed
Prepare for osc-lib changes
Change-Id: I665cd61272f881dce2d387da6035a2f35c866add Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent c9e4e54 commit dc8596f

File tree

6 files changed

+85
-63
lines changed

6 files changed

+85
-63
lines changed

openstackclient/common/envvars.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import os
14+
15+
from openstackclient.i18n import _
16+
17+
18+
def bool_from_str(value, strict=False):
19+
true_strings = ('1', 't', 'true', 'on', 'y', 'yes')
20+
false_strings = ('0', 'f', 'false', 'off', 'n', 'no')
21+
22+
if isinstance(value, bool):
23+
return value
24+
25+
lowered = value.strip().lower()
26+
if lowered in true_strings:
27+
return True
28+
elif lowered in false_strings or not strict:
29+
return False
30+
31+
msg = _(
32+
"Unrecognized value '%(value)s'; acceptable values are: %(valid)s"
33+
) % {
34+
'value': value,
35+
'valid': ', '.join(
36+
f"'{s}'" for s in sorted(true_strings + false_strings)
37+
),
38+
}
39+
raise ValueError(msg)
40+
41+
42+
def boolenv(*vars, default=False):
43+
"""Search for the first defined of possibly many bool-like env vars.
44+
45+
Returns the first environment variable defined in vars, or returns the
46+
default.
47+
48+
:param vars: Arbitrary strings to search for. Case sensitive.
49+
:param default: The default to return if no value found.
50+
:returns: A boolean corresponding to the value found, else the default if
51+
no value found.
52+
"""
53+
for v in vars:
54+
value = os.environ.get(v, None)
55+
if value:
56+
return bool_from_str(value)
57+
return default

openstackclient/compute/v2/server.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from osc_lib import utils
3535

3636
from openstackclient.api import compute_v2
37+
from openstackclient.common import envvars
3738
from openstackclient.common import pagination
3839
from openstackclient.i18n import _
3940
from openstackclient.identity import common as identity_common
@@ -324,48 +325,6 @@ def _prep_server_detail(compute_client, image_client, server, *, refresh=True):
324325
return info
325326

326327

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-
369328
class AddFixedIP(command.ShowOne):
370329
_description = _("Add fixed IP address to server")
371330

@@ -1876,7 +1835,7 @@ def _match_image(image_api, wanted_properties):
18761835

18771836
if 'delete_on_termination' in mapping:
18781837
try:
1879-
value = bool_from_str(
1838+
value = envvars.bool_from_str(
18801839
mapping['delete_on_termination'],
18811840
strict=True,
18821841
)
@@ -2223,7 +2182,7 @@ def get_parser(self, prog_name):
22232182
parser.add_argument(
22242183
'--all-projects',
22252184
action='store_true',
2226-
default=boolenv('ALL_PROJECTS'),
2185+
default=envvars.boolenv('ALL_PROJECTS'),
22272186
help=_(
22282187
'Delete server(s) in another project by name (admin only)'
22292188
'(can be specified using the ALL_PROJECTS envvar)'
@@ -2389,7 +2348,7 @@ def get_parser(self, prog_name):
23892348
parser.add_argument(
23902349
'--all-projects',
23912350
action='store_true',
2392-
default=boolenv('ALL_PROJECTS'),
2351+
default=envvars.boolenv('ALL_PROJECTS'),
23932352
help=_(
23942353
'Include all projects (admin only) '
23952354
'(can be specified using the ALL_PROJECTS envvar)'
@@ -4967,7 +4926,7 @@ def get_parser(self, prog_name):
49674926
parser.add_argument(
49684927
'--all-projects',
49694928
action='store_true',
4970-
default=boolenv('ALL_PROJECTS'),
4929+
default=envvars.boolenv('ALL_PROJECTS'),
49714930
help=_(
49724931
'Start server(s) in another project by name (admin only) '
49734932
'(can be specified using the ALL_PROJECTS envvar)'
@@ -5002,7 +4961,7 @@ def get_parser(self, prog_name):
50024961
parser.add_argument(
50034962
'--all-projects',
50044963
action='store_true',
5005-
default=boolenv('ALL_PROJECTS'),
4964+
default=envvars.boolenv('ALL_PROJECTS'),
50064965
help=_(
50074966
'Stop server(s) in another project by name (admin only) '
50084967
'(can be specified using the ALL_PROJECTS envvar)'

openstackclient/tests/unit/test_shell.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ def _assert_token_auth(self, cmd_options, default_args):
183183
osc_lib_test_utils.fake_execute(_shell, _cmd)
184184

185185
self.app.assert_called_with(["list", "role"])
186-
self.assertEqual(
187-
default_args.get("token", ''), _shell.options.token, "token"
188-
)
189-
self.assertEqual(
190-
default_args.get("auth_url", ''),
191-
_shell.options.auth_url,
192-
"auth_url",
193-
)
186+
187+
if default_args.get('token'):
188+
self.assertEqual(default_args['token'], _shell.options.token)
189+
190+
if default_args.get('auth_url'):
191+
self.assertEqual(
192+
default_args['auth_url'], _shell.options.auth_url
193+
)
194194

195195
def _assert_cli(self, cmd_options, default_args):
196196
with mock.patch(
@@ -204,25 +204,28 @@ def _assert_cli(self, cmd_options, default_args):
204204
osc_lib_test_utils.fake_execute(_shell, _cmd)
205205

206206
self.app.assert_called_with(["list", "server"])
207+
208+
# TODO(stephenfin): Remove "or ''" when we bump osc-lib minimum to
209+
# a version that includes I1d26133c9d9ed299d1035f207059aa8fe463a001
207210
self.assertEqual(
208211
default_args["compute_api_version"],
209-
_shell.options.os_compute_api_version,
212+
_shell.options.os_compute_api_version or '',
210213
)
211214
self.assertEqual(
212215
default_args["identity_api_version"],
213-
_shell.options.os_identity_api_version,
216+
_shell.options.os_identity_api_version or '',
214217
)
215218
self.assertEqual(
216219
default_args["image_api_version"],
217-
_shell.options.os_image_api_version,
220+
_shell.options.os_image_api_version or '',
218221
)
219222
self.assertEqual(
220223
default_args["volume_api_version"],
221-
_shell.options.os_volume_api_version,
224+
_shell.options.os_volume_api_version or '',
222225
)
223226
self.assertEqual(
224227
default_args["network_api_version"],
225-
_shell.options.os_network_api_version,
228+
_shell.options.os_network_api_version or '',
226229
)
227230

228231

openstackclient/volume/v3/volume_attachment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from osc_lib import exceptions
1919
from osc_lib import utils
2020

21+
from openstackclient.common import envvars
2122
from openstackclient.common import pagination
2223
from openstackclient.i18n import _
2324
from openstackclient.identity import common as identity_common
@@ -399,7 +400,7 @@ def get_parser(self, prog_name):
399400
'--all-projects',
400401
dest='all_projects',
401402
action='store_true',
402-
default=utils.env('ALL_PROJECTS', default=False),
403+
default=envvars.boolenv('ALL_PROJECTS'),
403404
help=_('Shows details for all projects (admin only).'),
404405
)
405406
parser.add_argument(

openstackclient/volume/v3/volume_group.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from osc_lib import exceptions
1818
from osc_lib import utils
1919

20+
from openstackclient.common import envvars
2021
from openstackclient.i18n import _
2122

2223

@@ -410,7 +411,7 @@ def get_parser(self, prog_name):
410411
'--all-projects',
411412
dest='all_projects',
412413
action='store_true',
413-
default=utils.env('ALL_PROJECTS', default=False),
414+
default=envvars.boolenv('ALL_PROJECTS'),
414415
help=_('Shows details for all projects (admin only).'),
415416
)
416417
# TODO(stephenfin): Add once we have an equivalent command for

openstackclient/volume/v3/volume_group_snapshot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from osc_lib import exceptions
1818
from osc_lib import utils
1919

20+
from openstackclient.common import envvars
2021
from openstackclient.i18n import _
2122

2223
LOG = logging.getLogger(__name__)
@@ -145,7 +146,7 @@ def get_parser(self, prog_name):
145146
'--all-projects',
146147
dest='all_projects',
147148
action='store_true',
148-
default=utils.env('ALL_PROJECTS', default=False),
149+
default=envvars.boolenv('ALL_PROJECTS'),
149150
help=_('Shows details for all projects (admin only).'),
150151
)
151152
# TODO(stephenfin): Add once we have an equivalent command for

0 commit comments

Comments
 (0)