Skip to content

Commit

Permalink
Added label command to ansible distribution
Browse files Browse the repository at this point in the history
fixes: pulp#185
  • Loading branch information
gerrod3 committed Mar 26, 2021
1 parent f9a6726 commit 6885ef8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/185.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added label command to ansible distribution group
5 changes: 5 additions & 0 deletions pulpcore/cli/ansible/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 ""
Expand Down Expand Up @@ -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.name, item.min, item.max)

@click.command(name="set", help=_("Add or update a label"))
@click.option("--key", required=True, help=_("Key of the label"))
Expand Down
9 changes: 9 additions & 0 deletions tests/scripts/pulp_ansible/test_distribution.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 6885ef8

Please sign in to comment.