Skip to content

Commit f838b4b

Browse files
committed
DRAFT versioned-api support
1 parent 212d6d2 commit f838b4b

5 files changed

Lines changed: 27 additions & 5 deletions

File tree

pulp-glue/src/pulp_glue/common/context.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class PulpContext:
287287
It is an abstraction layer for api access and output handling.
288288
289289
Parameters:
290-
api_root: The base url (excluding "api/v3/") to the servers api.
290+
api_root: The base url (excluding "api/{version}/") to the servers api.
291291
api_kwargs: Extra arguments to pass to the wrapped `OpenAPI` object.
292292
background_tasks: Whether to wait for tasks. If `True`, all tasks triggered will
293293
immediately raise `PulpNoWait`.
@@ -297,6 +297,7 @@ class PulpContext:
297297
Where possible, instead of failing, the requested result will be faked.
298298
This implies `dry_run=True` on the `api_kwargs`.
299299
verify_ssl: A boolean or a path to the CA bundle.
300+
api-version: Version of the Pulp API to talk to (e.g., "v3")
300301
"""
301302

302303
def echo(self, message: str, nl: bool = True, err: bool = False) -> None:
@@ -328,8 +329,10 @@ def __init__(
328329
verify_ssl: bool | str | None = None,
329330
verify: bool | str | None = None, # Deprecated
330331
chunk_size: int | None = None,
332+
api_version: str | None = "v3",
331333
) -> None:
332334
self._api: OpenAPI | None = None
335+
self._api_version = api_version
333336
self._api_root: str = api_root
334337
self._api_kwargs = api_kwargs
335338
self.verify_ssl = verify_ssl
@@ -353,6 +356,7 @@ def __init__(
353356
self.fake_mode: bool = fake_mode
354357
if self.fake_mode:
355358
self._api_kwargs["dry_run"] = True
359+
self._api_kwargs["api_version"] = self._api_version
356360
self.chunk_size = chunk_size
357361

358362
@classmethod
@@ -435,6 +439,7 @@ def from_config(cls, config: dict[str, t.Any]) -> "t.Self":
435439
api_root=config.get("api_root", "/pulp/"),
436440
domain=config.get("domain", "default"),
437441
verify_ssl=config.get("verify_ssl", True),
442+
api_version=config.get("api_version", "v3"),
438443
api_kwargs=api_kwargs,
439444
)
440445

@@ -453,8 +458,8 @@ def domain_enabled(self) -> bool:
453458
@property
454459
def api_path(self) -> str:
455460
if self.domain_enabled:
456-
return self._api_root + self.pulp_domain + "/api/v3/"
457-
return self._api_root + "api/v3/"
461+
return f"{self._api_root}{self.pulp_domain}/api/{self._api_version}/"
462+
return f"{self._api_root}api/{self._api_version}/"
458463

459464
@property
460465
def api(self) -> OpenAPI:
@@ -481,7 +486,7 @@ def api(self) -> OpenAPI:
481486
)
482487
try:
483488
self._api = OpenAPI(
484-
doc_path=f"{self._api_root}api/v3/docs/api.json",
489+
doc_path=f"{self._api_root}api/{self._api_version}/docs/api.json",
485490
verify_ssl=self.verify_ssl,
486491
patch_api_hook=_patch_api_hook,
487492
**self._api_kwargs,
@@ -535,6 +540,7 @@ def call(
535540
if "pulp_domain" in self.api.param_spec(operation_id, "path", required=True):
536541
parameters["pulp_domain"] = self.pulp_domain
537542
parameters = preprocess_payload(parameters)
543+
538544
if body is not None:
539545
body = preprocess_payload(body)
540546
try:

pulp-glue/src/pulp_glue/common/openapi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class OpenAPI:
9999
cid: Correlation ID to send with all requests.
100100
validate_certs: DEPRECATED use verify_ssl instead.
101101
safe_calls_only: DEPRECATED use dry_run instead.
102+
api_version: Version of the Pulp APi to contact (e.g., "v3")
102103
"""
103104

104105
_api_spec: oas.OpenAPISpec
@@ -121,6 +122,7 @@ def __init__(
121122
validate_certs: bool | None = None,
122123
safe_calls_only: bool | None = None,
123124
patch_api_hook: t.Callable[[t.Any], t.Any] | None = None,
125+
api_version: str | None = "v3"
124126
):
125127
if validate_certs is not None:
126128
warnings.warn(
@@ -167,6 +169,7 @@ def __init__(
167169
self._oauth2_expires: datetime = datetime.now()
168170

169171
self._patch_api_hook: t.Callable[[t.Any], t.Any] = patch_api_hook or (lambda data: data)
172+
self._api_version = api_version
170173
self.load_api(refresh_cache=refresh_cache)
171174

172175
def _setup_session(self) -> None:
@@ -713,6 +716,7 @@ def call(
713716
NotImplementedError: well, the name really says is all.
714717
"""
715718
method, path = self.operations[operation_id]
719+
print(f">>> METHOD {method} PATH {path} WHUT")
716720
path_spec = self._api_spec.paths[path]
717721
operation_spec = getattr(path_spec, method)
718722

@@ -728,7 +732,6 @@ def call(
728732
rel_url = path
729733
for name, value in rendered_parameters["path"].items():
730734
rel_url = path.replace("{" + name + "}", value)
731-
732735
query_params = rendered_parameters["query"]
733736

734737
url = urljoin(self._base_url, rel_url)

src/pulp_cli/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def main(
225225
dry_run: bool,
226226
timeout: int,
227227
cid: str,
228+
api_version: str,
228229
) -> None:
229230
if verbose:
230231
logging.basicConfig(level=logging.DEBUG + 4 - verbose, format="%(message)s")
@@ -252,6 +253,7 @@ def main(
252253
oauth2_client_id=client_id,
253254
oauth2_client_secret=client_secret,
254255
chunk_size=chunk_size,
256+
api_version=api_version,
255257
)
256258

257259

src/pulp_cli/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
str(Path(click.utils.get_app_dir("pulp"), "cli.toml")),
3838
]
3939
FORMAT_CHOICES = list(REGISTERED_OUTPUT_FORMATTERS.keys())
40+
VERSIONS = ["v3", "v4", "v5"]
4041
REQUIRED_SETTINGS = {
4142
"base_url",
4243
"api_root",
@@ -57,6 +58,7 @@
5758
"key",
5859
"chunk_size",
5960
"plugins",
61+
"api_version"
6062
}
6163
SETTINGS = REQUIRED_SETTINGS | OPTIONAL_SETTINGS
6264

@@ -137,6 +139,12 @@ def headers_callback(
137139
count=True,
138140
help=_("Increase verbosity; explain api calls as they are made"),
139141
),
142+
click.option(
143+
"--api-version",
144+
type=click.Choice(VERSIONS, case_sensitive=True),
145+
default="v3",
146+
help=_("API version to talk to (e.g., 'v3')"),
147+
),
140148
]
141149

142150

src/pulp_cli/generic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class PulpCLIContext(PulpContext):
143143
144144
Parameters:
145145
api_root: The base url (excluding "api/v3/") to the server's api.
146+
api_version: The version of Pulp's API to talk to (e.g. "v3").
146147
api_kwargs: Extra arguments to pass to the wrapped `OpenAPI` object.
147148
background_tasks: Whether to wait for tasks. If `True`, all tasks triggered will
148149
immediately raise `PulpNoWait`.
@@ -166,6 +167,7 @@ def __init__(
166167
oauth2_client_id: str | None = None,
167168
oauth2_client_secret: str | None = None,
168169
chunk_size: int | None = None,
170+
api_version: str | None = "v3",
169171
) -> None:
170172
self.username = username
171173
self.password = password
@@ -184,6 +186,7 @@ def __init__(
184186
timeout=timeout,
185187
domain=domain,
186188
chunk_size=chunk_size,
189+
api_version=api_version,
187190
)
188191
self.format = format
189192

0 commit comments

Comments
 (0)