From bdaccfa1f17296098ea15cd60ff768453878236e Mon Sep 17 00:00:00 2001 From: Gerrod Ubben Date: Tue, 23 Mar 2021 19:14:42 -0400 Subject: [PATCH] Added label command to ansible distribution fixes: #185 --- CHANGES/185.feature | 1 + pulpcore/cli/ansible/distribution.py | 5 +++++ pulpcore/cli/common/context.py | 12 ++++++------ pulpcore/cli/common/generic.py | 15 ++++++++++++--- tests/scripts/pulp_ansible/test_distribution.sh | 9 +++++++++ 5 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 CHANGES/185.feature diff --git a/CHANGES/185.feature b/CHANGES/185.feature new file mode 100644 index 000000000..5a94171fc --- /dev/null +++ b/CHANGES/185.feature @@ -0,0 +1 @@ +Added label command to ansible distribution group diff --git a/pulpcore/cli/ansible/distribution.py b/pulpcore/cli/ansible/distribution.py index 73d1c198e..a9d31a68c 100644 --- a/pulpcore/cli/ansible/distribution.py +++ b/pulpcore/cli/ansible/distribution.py @@ -14,12 +14,14 @@ pass_entity_context, pass_pulp_context, ) +from pulpcore.cli.common.generic import PluginRequiredVersion as PRV from pulpcore.cli.common.generic import ( base_path_contains_option, base_path_option, create_command, destroy_command, href_option, + label_command, label_select_option, list_command, name_option, @@ -79,6 +81,9 @@ def distribution(ctx: click.Context, pulp_ctx: PulpContext, distribution_type: s distribution.add_command(show_command(decorators=lookup_options)) distribution.add_command(destroy_command(decorators=lookup_options)) distribution.add_command(create_command(decorators=create_options)) +distribution.add_command( + label_command(need_plugins=[PRV("core", "3.10.0"), PRV("ansible", "0.8.0.dev")]) +) # TODO Add content_guard option diff --git a/pulpcore/cli/common/context.py b/pulpcore/cli/common/context.py index 06484a06a..ef9e63838 100644 --- a/pulpcore/cli/common/context.py +++ b/pulpcore/cli/common/context.py @@ -392,14 +392,14 @@ def delete(self, href: str, non_blocking: bool = False) -> Any: ) def set_label(self, href: str, key: str, value: str, non_blocking: bool = False) -> Any: - entity = self.show(href) - entity["pulp_labels"][key] = value - return self.update(href, body=entity, non_blocking=non_blocking) + labels = self.show(href)["pulp_labels"] + labels[key] = value + return self.update(href, body={"pulp_labels": labels}, non_blocking=non_blocking) def unset_label(self, href: str, key: str, non_blocking: bool = False) -> Any: - entity = self.show(href) - entity["pulp_labels"].pop(key) - return self.update(href, body=entity, non_blocking=non_blocking) + labels = self.show(href)["pulp_labels"] + labels.pop(key) + return self.update(href, body={"pulp_labels": labels}, non_blocking=non_blocking) def show_label(self, href: str, key: str) -> Any: entity = self.show(href) diff --git a/pulpcore/cli/common/generic.py b/pulpcore/cli/common/generic.py index 8b556a044..0b931b4cf 100644 --- a/pulpcore/cli/common/generic.py +++ b/pulpcore/cli/common/generic.py @@ -1,6 +1,6 @@ import gettext import json -from typing import Any, Optional, Tuple, Union +from typing import Any, NamedTuple, Optional, Tuple, Union import click @@ -20,6 +20,12 @@ _ = gettext.gettext +class PluginRequiredVersion(NamedTuple): + name: str + min: Optional[str] = None + max: Optional[str] = None + + class PulpCommand(click.Command): def get_short_help_str(self, limit: int = 45) -> str: return self.short_help or "" @@ -390,10 +396,13 @@ def label_command(**kwargs: Any) -> click.Command: if "name" not in kwargs: kwargs["name"] = "label" decorators = kwargs.pop("decorators", [name_option, href_option]) + need_plugins = kwargs.pop("need_plugins", [PluginRequiredVersion("core", "3.10.0")]) @click.group(**kwargs) - def label_group() -> None: - pass + @pass_pulp_context + def label_group(pulp_ctx: PulpContext) -> None: + for item in need_plugins: + pulp_ctx.needs_plugin(*item) @click.command(name="set", help=_("Add or update a label")) @click.option("--key", required=True, help=_("Key of the label")) diff --git a/tests/scripts/pulp_ansible/test_distribution.sh b/tests/scripts/pulp_ansible/test_distribution.sh index 93eb4cdd2..5b173957e 100755 --- a/tests/scripts/pulp_ansible/test_distribution.sh +++ b/tests/scripts/pulp_ansible/test_distribution.sh @@ -27,4 +27,13 @@ expect_succ pulp ansible distribution list expect_succ pulp ansible distribution show --name "cli_test_ansible_distro" expect_succ pulp ansible distribution update --name "cli_test_ansible_distro" --repository "" +if [ "$(pulp debug has-plugin --name "core" --min-version "3.10.0")" = "true" ] +then + if [ "$(pulp debug has-plugin --name "ansible" --min-version "0.8.0.dev")" = "true" ] + then + expect_succ pulp ansible distribution label set --name "cli_test_ansible_distro" --key "test" --value "success" + else + expect_fail pulp ansible distribution label set --name "cli_test_ansible_distro" --key "test" --value "fail" + fi +fi expect_succ pulp ansible distribution destroy --name "cli_test_ansible_distro"