33
44import click
55
6- from pulp_glue .common .context import PluginRequirement , PulpContentContext
6+ from pulp_glue .common .context import (
7+ EntityDefinition ,
8+ PluginRequirement ,
9+ PulpContentContext ,
10+ PulpDistributionContext ,
11+ )
712from pulp_glue .container .context import (
813 PulpContainerBlobContext ,
14+ PulpContainerDistributionContext ,
915 PulpContainerManifestContext ,
16+ PulpContainerRepositoryContext ,
1017 PulpContainerTagContext ,
1118)
1219
1320from pulp_cli .generic import (
21+ PulpCLIContext ,
1422 content_filter_options ,
1523 href_option ,
1624 label_command ,
1725 list_command ,
1826 option_group ,
27+ option_processor ,
1928 pulp_group ,
2029 pulp_option ,
30+ resource_option ,
2131 show_command ,
2232 type_option ,
2333)
@@ -34,6 +44,39 @@ def _content_callback(ctx: click.Context, value: dict[str, t.Any]) -> None:
3444 entity_ctx .entity = value
3545
3646
47+ def _repository_version_from_distribution (
48+ pulp_ctx : PulpCLIContext , distribution : EntityDefinition
49+ ) -> str :
50+ if repository_version := distribution .get ("repository_version" ):
51+ return t .cast (str , repository_version )
52+
53+ repository_href = distribution .get ("repository" )
54+ if not repository_href :
55+ raise click .ClickException (
56+ _ (
57+ "Distribution '{name}' is not associated with a repository or repository version."
58+ ).format (name = distribution .get ("name" , "" ))
59+ )
60+ repo_ctx = PulpContainerRepositoryContext (pulp_ctx , pulp_href = repository_href )
61+
62+ return t .cast (str , repo_ctx .entity ["latest_version_href" ])
63+
64+
65+ def _process_distribution_filter (ctx : click .Context ) -> None :
66+ if distribution := ctx .params .pop ("distribution" , None ):
67+ if ctx .params .get ("repository_version" ):
68+ raise click .UsageError (
69+ _ ("Cannot use --distribution together with --repository-version." )
70+ )
71+
72+ assert isinstance (distribution , PulpContainerDistributionContext )
73+ pulp_ctx = ctx .find_object (PulpCLIContext )
74+ assert pulp_ctx is not None
75+ ctx .params ["repository_version" ] = _repository_version_from_distribution (
76+ pulp_ctx , distribution .entity
77+ )
78+
79+
3780@pulp_group ()
3881@type_option (
3982 choices = {
@@ -47,6 +90,20 @@ def content() -> None:
4790 pass
4891
4992
93+ distribution_filter_option = resource_option (
94+ "--distribution" ,
95+ default_plugin = "container" ,
96+ default_type = "container" ,
97+ context_table = {
98+ "container:container" : PulpContainerDistributionContext ,
99+ },
100+ href_pattern = PulpDistributionContext .HREF_PATTERN ,
101+ help = _ (
102+ "Filter {entities} by the repository version served by this distribution (name or href)."
103+ ),
104+ )
105+
106+
50107list_options = [
51108 pulp_option (
52109 "--media-type" ,
@@ -86,6 +143,8 @@ def content() -> None:
86143 allowed_with_contexts = (PulpContainerManifestContext ,),
87144 ),
88145 * content_filter_options ,
146+ distribution_filter_option ,
147+ option_processor (callback = _process_distribution_filter ),
89148]
90149
91150lookup_options = [
0 commit comments