From ddbf38c6e7797b7a96205fe43df71a41107363b0 Mon Sep 17 00:00:00 2001 From: synkd Date: Tue, 30 Jul 2024 12:53:52 -0400 Subject: [PATCH 1/2] Add offline token argument to inventory command This PR modifies the inventory CLI command to accept an offline_token argument. This should resolve an issue with generating an access token that I'm encountering with a CI pipeline. With this change, the job can pass an environment variable containing the offline token to the Manifester CLI, which mirrors how we access the same token in another CI use case. --- manifester/commands.py | 5 +++-- manifester/manifester.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/manifester/commands.py b/manifester/commands.py index 9e1a0c2..28d87b9 100644 --- a/manifester/commands.py +++ b/manifester/commands.py @@ -73,11 +73,12 @@ def delete(allocations, all_, remove_manifest_file): @cli.command() @click.option("--details", is_flag=True, help="Display full inventory details") @click.option("--sync", is_flag=True, help="Fetch inventory data from RHSM before displaying") -def inventory(details, sync): +@click.option("--offline-token", type=str, default=None) +def inventory(details, sync, offline_token): """Display the local inventory file's contents.""" border = "-" * 38 if sync: - helpers.update_inventory(Manifester(minimal_init=True).subscription_allocations) + helpers.update_inventory(Manifester(minimal_init=True, offline_token=offline_token).subscription_allocations) inv = helpers.load_inventory_file(Path(settings.inventory_path)) if not details: logger.info("Displaying local inventory data") diff --git a/manifester/manifester.py b/manifester/manifester.py index c32594d..6876442 100644 --- a/manifester/manifester.py +++ b/manifester/manifester.py @@ -33,7 +33,10 @@ def __init__( **kwargs, ): if minimal_init: - self.offline_token = settings.get("offline_token") + if kwargs.get("offline_token") is not None: + self.offline_token = kwargs.get("offline_token") + else: + self.offline_token = settings.get("offline_token") self.token_request_url = settings.get("url").get("token_request") self.allocations_url = settings.get("url").get("allocations") self._access_token = None From 94a9f7178e40c9cbba0067393218db0bc9869ed3 Mon Sep 17 00:00:00 2001 From: synkd Date: Wed, 31 Jul 2024 15:43:41 -0400 Subject: [PATCH 2/2] Add exceptions for missing offline_token and access token error response --- manifester/manifester.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/manifester/manifester.py b/manifester/manifester.py index 6876442..010e879 100644 --- a/manifester/manifester.py +++ b/manifester/manifester.py @@ -9,7 +9,7 @@ import string from dynaconf.utils.boxing import DynaBox -from requests.exceptions import Timeout +from requests.exceptions import RequestException, Timeout from manifester.helpers import ( fetch_paginated_data, @@ -35,8 +35,10 @@ def __init__( if minimal_init: if kwargs.get("offline_token") is not None: self.offline_token = kwargs.get("offline_token") - else: + elif settings.get("offline_token") is not None: self.offline_token = settings.get("offline_token") + else: + raise KeyError("Offline token not defined.") self.token_request_url = settings.get("url").get("token_request") self.allocations_url = settings.get("url").get("allocations") self._access_token = None @@ -113,6 +115,8 @@ def access_token(self): cmd_args=[f"{self.token_request_url}"], cmd_kwargs=token_request_data, ).json() + if "error" in token_data: + raise RequestException(f"{token_data['error']}: {token_data['error_description']}") if self.is_mock: self._access_token = token_data.access_token else: