Skip to content

Commit

Permalink
Add pulp label commands
Browse files Browse the repository at this point in the history
fixes pulp#100
  • Loading branch information
David Davis committed Jan 28, 2021
1 parent e092af4 commit 6d6aeed
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/100.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added label subcommands (set and unset).
10 changes: 10 additions & 0 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ def update(
def delete(self, href: str) -> Any:
return self.pulp_ctx.call(self.DELETE_ID, parameters={self.HREF: href})

def set_label(self, href: str, key: str, value: str) -> Any:
entity = self.show(href)
entity["pulp_labels"][key] = value
return self.update(href, body=entity)

def unset_label(self, href: str, key: str) -> Any:
entity = self.show(href)
entity["pulp_labels"].pop(key)
return self.update(href, body=entity)

def find_repository(self, definition: RepositoryDefinition) -> Any:
name, repo_type = definition
if repo_type in self.REPOSITORY_FIND_IDS:
Expand Down
35 changes: 35 additions & 0 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,38 @@ def version_group(
version_group.add_command(show_version)
version_group.add_command(destroy_version)
version_group.add_command(repair_version)


# Generic label command group
@click.group(name="label")
@pass_entity_context
@pass_pulp_context
@click.pass_context
def label_group(ctx: click.Context, entity_ctx: PulpEntityContext, pulp_ctx: PulpContext) -> None:
"""Manage labels for this resource"""
pass


@label_group.command(name="set")
@click.option("--name", required=True, help="Name of the entry")
@click.option("--key", required=True, help="Key of the label")
@click.option("--value", required=True, help="Value of the label")
@pass_entity_context
@pass_pulp_context
def label_set(
pulp_ctx: PulpContext, entity_ctx: PulpEntityContext, name: str, key: str, value: str
) -> None:
"""Add or update a label"""
href = entity_ctx.find(name=name)["pulp_href"]
entity_ctx.set_label(href, key, value)


@label_group.command(name="unset")
@click.option("--name", required=True, help="Name of the entry")
@click.option("--key", required=True, help="Key of the label")
@pass_entity_context
@pass_pulp_context
def label_unset(pulp_ctx: PulpContext, entity_ctx: PulpEntityContext, name: str, key: str) -> None:
"""Remove a label"""
href = entity_ctx.find(name=name)["pulp_href"]
entity_ctx.unset_label(href, key)
2 changes: 2 additions & 0 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pulpcore.cli.common.generic import (
destroy_command,
href_option,
label_group,
list_command,
name_option,
show_command,
Expand Down Expand Up @@ -47,6 +48,7 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
repository.add_command(show_command(decorators=lookup_options))
repository.add_command(destroy_command(decorators=lookup_options))
repository.add_command(version_command())
repository.add_command(label_group)


@repository.command()
Expand Down
23 changes: 19 additions & 4 deletions tests/scripts/config.source
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ settings="$(dirname "$(realpath "$0")")/config/pulp/settings.toml"
PULP_BASE_URL="$(sed -n -e 's/^base_url\s*=\s*"\(\S*\)"\s*$/\1/p' "$settings")"
VERIFY_SSL="$(sed -n -e 's/^verify_ssl\s*=\s*\(\S*\)\s*$/\1/p' "$settings")"

# Configure path to config
export XDG_CONFIG_HOME="$(dirname "$(realpath "$0")")/config"

# Constants used in tests
PULPCORE_VERSION=$(http "$PULP_BASE_URL/pulp/api/v3/status/" | jq -r '.versions | .[] | select(.component=="pulpcore").version')
PULPCORE_VERSION=$(pulp status | jq -r '.versions | .[] | select(.component=="pulpcore").version')
PULP_FIXTURES_URL="${PULP_FIXTURES_URL:-https://fixtures.pulpproject.org}"
FILE_REMOTE_URL="${PULP_FIXTURES_URL}/file/PULP_MANIFEST"
CONTAINER_REMOTE_URL="https://registry-1.docker.io"
Expand All @@ -13,9 +16,6 @@ RPM_REMOTE_URL="${PULP_FIXTURES_URL}/rpm-unsigned"
ANSIBLE_COLLECTION_REMOTE_URL="https://galaxy.ansible.com/"
ANSIBLE_ROLE_REMOTE_URL="https://galaxy.ansible.com/api/v1/roles/?namespace__name=elastic"

# Configure path to config
export XDG_CONFIG_HOME="$(dirname "$(realpath "$0")")/config"

# Library for test helper functions

TMP="$(mktemp -d)"
Expand Down Expand Up @@ -67,4 +67,19 @@ expect_fail () {
fi
}

# Returns true if $PULPCORE_VERSION >= $1, false otherwise
require_min_pulp () {
if [ -z "${PULPCORE_VERSION+x}" ]
then
echo "Error: require_min_pulp called without \$PULPCORE_VERSION defined."
exit 1
fi

if [ "$1" != "`printf "$PULPCORE_VERSION\n$1" | sort -V | head -n1`" ]
then
echo "Skipping $0"
exit 3 # skip exit code
fi
}

set -eu
29 changes: 29 additions & 0 deletions tests/scripts/test_label.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(realpath "$0")")/config.source"

require_min_pulp "3.10.0"

cleanup() {
pulp file repository destroy --name "cli_test_file_repo" || true
}
trap cleanup EXIT

name="cli_test_file_repo"
expect_succ pulp file repository create --name "$name"
expect_succ pulp file repository label set --name "$name" --key "atani" --value "hurin"
expect_succ pulp file repository label set --name "$name" --key "ainu" --value "ulmo"

expect_succ pulp file repository show --name "$name"
test "$(echo "$OUTPUT" | jq -r .pulp_labels)" = '{ "atani": "hurin", "ainu": "ulmo" }'

# update a label
expect_succ pulp file repository label set --name "$name" --key "atani" --value "beor"
expect_succ pulp file repository show --name "$name"
test "$(echo "$OUTPUT" | jq -r .pulp_labels)" = '{ "atani": "hurin", "ainu": "ulmo" }'

# remove a label
expect_succ pulp file repository label unset --name "$name" --key "atani"
expect_succ pulp file repository show --name "$name"
test "$(echo "$OUTPUT" | jq -r .pulp_labels)" = '{ "ainu": "ulmo" }'
2 changes: 2 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
@pytest.mark.parametrize("test_name", TEST_NAMES)
def test_script(test_name):
run = subprocess.run([os.path.join("tests", "scripts", "test_" + test_name + ".sh")])
if run.returncode == 3:
pytest.skip("Skipped as requested by the script.")
assert run.returncode == 0

0 comments on commit 6d6aeed

Please sign in to comment.