Skip to content
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

Commit 0a2babe

Browse files
committed
Add the capability to download a pull secret associated with a topic
Change-Id: I7bffac7277737cc85fc30bec08f7c08e8d609515
1 parent b14ee33 commit 0a2babe

7 files changed

Lines changed: 122 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ AUTHORS
1313
ChangeLog
1414
password
1515
current
16+
venv
17+
.venv

dciclient/v1/shell_commands/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,15 @@ def parse_arguments(args, environment={}):
778778
p.add_argument("--offset", default=0)
779779
p.set_defaults(command="pipeline-show-jobs")
780780

781+
p = subparsers.add_parser(
782+
"download-pull-secret",
783+
help="Download pull secret associated with a topic.",
784+
parents=[base_parser],
785+
)
786+
p.add_argument("--topic", required=True)
787+
p.add_argument("--destination", required=True)
788+
p.set_defaults(command="download-pull-secret")
789+
781790
args = parser.parse_args(args)
782791

783792
if "command" not in args:

dciclient/v1/shell_commands/runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"topic-show": topic.show,
6060
"topic-update": topic.update,
6161
"topic-delete": topic.delete,
62+
"download-pull-secret": topic.download_pull_secret,
6263
"component-list": component.list,
6364
"component-create": component.create,
6465
"component-show": component.show,

dciclient/v1/shell_commands/topic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17+
import json
18+
import sys
1719
from dciclient.v1.api import topic
1820
from dciclient.v1.utils import active_string, get_search_params
1921

@@ -73,3 +75,19 @@ def delete(context, args):
7375

7476
def show(context, args):
7577
return topic.get(context, args.id)
78+
79+
80+
def download_pull_secret(context, args):
81+
topic_name = args.topic
82+
get_topics = topic.list(context, where=f"name:{topic_name}")
83+
topics = get_topics.json()["topics"]
84+
if len(topics) == 0:
85+
print(f"Topic {topic_name} does not exist.")
86+
sys.exit(1)
87+
topic_data = topics[0]["data"]
88+
if "pull_secret" not in topic_data:
89+
print(f"Topic {topic_name} doesn't have a pull secret.")
90+
sys.exit(1)
91+
with open(args.destination, "w") as f:
92+
pull_secret = topic_data["pull_secret"]
93+
json.dump(pull_secret, f)

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,19 @@ def feeder(dci_context, team_id):
480480
kwargs = {"name": "feeder", "team_id": team_id, "state": "active"}
481481
feeder = api_base.create(dci_context, "feeders", **kwargs).json()
482482
return feeder["feeder"]
483+
484+
485+
@pytest.fixture
486+
def rhel10_topic(dci_context, product_id):
487+
kwargs = {
488+
"name": "RHEL-10.0",
489+
"component_types": ["container"],
490+
"product_id": product_id,
491+
"export_control": True,
492+
"data": {
493+
"pull_secret": {
494+
"auths": {"registry.distributed-ci.io": {"auth": "ZGNpOmRjaQ=="}}
495+
}
496+
},
497+
}
498+
return api_topic.create(dci_context, **kwargs).json()["topic"]

tests/shell_commands/test_cli.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,18 @@ def test_csv():
285285
]
286286
)
287287
assert args.tags == []
288+
289+
290+
def test_parse_download_authfile():
291+
args = parse_arguments(
292+
[
293+
"download-pull-secret",
294+
"--topic",
295+
"RHEL-10.0",
296+
"--destination",
297+
"/tmp/auth.json",
298+
]
299+
)
300+
assert args.command == "download-pull-secret"
301+
assert args.topic == "RHEL-10.0"
302+
assert args.destination == "/tmp/auth.json"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- encoding: utf-8 -*-
2+
#
3+
# Copyright 2024 Red Hat, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import json
18+
import pytest
19+
20+
21+
def test_download_pull_secret(tmp_path, runner, rhel10_topic):
22+
pull_secret = rhel10_topic["data"]["pull_secret"]
23+
destination_file = tmp_path / "auth.json"
24+
destination_file_path = str(destination_file)
25+
runner.invoke_raw(
26+
[
27+
"download-pull-secret",
28+
"--topic",
29+
rhel10_topic["name"],
30+
"--destination",
31+
destination_file_path,
32+
]
33+
)
34+
with open(destination_file_path) as f:
35+
assert f.read() == json.dumps(pull_secret)
36+
37+
38+
def test_download_pull_secret_without_a_pull_secret_in_topic_data(runner, topic):
39+
with pytest.raises(SystemExit):
40+
runner.invoke_raw(
41+
[
42+
"download-pull-secret",
43+
"--topic",
44+
topic,
45+
"--destination",
46+
"/tmp/auth.json",
47+
]
48+
)
49+
50+
51+
def test_download_pull_secret_without_specified_topic(runner):
52+
with pytest.raises(SystemExit):
53+
runner.invoke_raw(
54+
[
55+
"download-pull-secret",
56+
"--topic",
57+
"unknown-topic",
58+
"--destination",
59+
"/tmp/auth.json",
60+
]
61+
)

0 commit comments

Comments
 (0)