Skip to content

Commit 90edae0

Browse files
jriddle-linodelgarber-akamaizliang-akamai
authored
fix: obj plugin error with invalid cluster (#552)
## 📝 Description **What does this PR do and why is this change necessary?** When using the command with a invalid region it would print out a large stack trace error, to fix this I added a list of valid clusters generated from the API. ## ✔️ How to Test **What are the steps to reproduce the issue or verify the changes?** Use the command with a invalid cluster ID ```bash lin obj --cluster us-west ``` Co-authored-by: Lena Garber <114949949+lgarber-akamai@users.noreply.github.com> Co-authored-by: Zhiwei Liang <zliang@akamai.com>
1 parent 4010b88 commit 90edae0

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

linodecli/plugins/obj/__init__.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@
7878
INVALID_PAGE_MSG = "No result to show in this page."
7979

8080

81+
def get_available_cluster(cli: CLI):
82+
"""Get list of possible clusters for the account"""
83+
return [
84+
c["id"]
85+
for c in _do_get_request( # pylint: disable=protected-access
86+
cli.config.base_url,
87+
"/object-storage/clusters",
88+
token=cli.config.get_token(),
89+
)["data"]
90+
]
91+
92+
8193
def flip_to_page(iterable: Iterable, page: int = 1):
8294
"""Given a iterable object and return a specific iteration (page)"""
8395
iterable = iter(iterable)
@@ -451,7 +463,7 @@ def print_help(parser: ArgumentParser):
451463
print("See --help for individual commands for more information")
452464

453465

454-
def get_obj_args_parser():
466+
def get_obj_args_parser(clusters: List[str]):
455467
"""
456468
Initialize and return the argument parser for the obj plug-in.
457469
"""
@@ -468,6 +480,7 @@ def get_obj_args_parser():
468480
"--cluster",
469481
metavar="CLUSTER",
470482
type=str,
483+
choices=clusters,
471484
help="The cluster to use for the operation",
472485
)
473486

@@ -527,7 +540,8 @@ def call(
527540

528541
sys.exit(2) # requirements not met - we can't go on
529542

530-
parser = get_obj_args_parser()
543+
clusters = get_available_cluster(context.client)
544+
parser = get_obj_args_parser(clusters)
531545
parsed, args = parser.parse_known_args(args)
532546

533547
# don't mind --no-defaults if it's there; the top-level parser already took care of it
@@ -710,14 +724,7 @@ def _configure_plugin(client: CLI):
710724
"""
711725
Configures a default cluster value.
712726
"""
713-
clusters = [
714-
c["id"]
715-
for c in _do_get_request( # pylint: disable=protected-access
716-
client.config.base_url,
717-
"/object-storage/clusters",
718-
token=client.config.get_value("token"),
719-
)["data"]
720-
]
727+
clusters = get_available_cluster(client)
721728

722729
cluster = _default_thing_input( # pylint: disable=protected-access
723730
"Configure a default Cluster for operations.",

tests/unit/test_plugin_obj.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from pytest import CaptureFixture
22

3+
from linodecli import CLI
34
from linodecli.plugins.obj import get_obj_args_parser, helpers, print_help
45

56

6-
def test_print_help(capsys: CaptureFixture):
7-
parser = get_obj_args_parser()
7+
def test_print_help(mock_cli: CLI, capsys: CaptureFixture):
8+
parser = get_obj_args_parser(["us-mia-1"])
89
print_help(parser)
910
captured_text = capsys.readouterr()
1011
assert parser.format_help() in captured_text.out

0 commit comments

Comments
 (0)