Skip to content

Commit

Permalink
Merge pull request #14 from synkd/handle_invalid_sat_versions
Browse files Browse the repository at this point in the history
Sanitize sat_version and handle sat_version errors
  • Loading branch information
synkd authored Sep 12, 2022
2 parents 05ab9b9 + cb12dec commit eb516df
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
26 changes: 26 additions & 0 deletions manifester/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@ def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=240, _cur_time
time.sleep(_cur_timeout)
response = simple_retry(cmd, cmd_args, cmd_kwargs, max_timeout, new_wait)
return response

def process_sat_version(sat_version, valid_sat_versions):
"""Ensure that the sat_version parameter is properly formatted for the RHSM API when creating
a subscription allocation with the 'POST allocations' endpoint"""
if sat_version not in valid_sat_versions:
# The valid values for the sat_version parameter when creating a subscription allocation
# are all 8 characters or less (e.g. 'sat-6.11'). Some data sources may include a Z-stream
# version (e.g. 'sat-6.11.0') when retrieving this value from settings. The conditional
# below assumes that, if the length of sat_version is greated than 8 characters, it includes
# a Z-stream version that should be removed.
if len(sat_version) > 8:
sat_version = sat_version.split('.')
sat_version = sat_version[:-1]
sat_version = ".".join(sat_version)
# The conditional below assumes that an invalid sat_version with the Z-stream version removed
# is a Y-stream version in development. New Y-stream versions are not available as valid
# sat_version values for the 'POST allocations' endpoint until the corresponding Satellite
# versions are generally available. As of Satellite 6.12, decrementing the Y-stream version
# by 1 works around this constraint, but the conditional below may need to be modified to
# accommodate versioning scheme changes or additional use cases.
if sat_version not in valid_sat_versions:
sat_version = sat_version.split('.')
sat_version[1] = str(int(sat_version[1]) - 1)
sat_version = ".".join(sat_version)
assert sat_version in valid_sat_versions
return sat_version
35 changes: 34 additions & 1 deletion manifester/manifester.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import random
import string
from dynaconf.utils.boxing import DynaBox
from functools import cached_property
from pathlib import Path

import requests
from logzero import logger

from manifester.helpers import simple_retry
from manifester.helpers import process_sat_version
from manifester.logger import setup_logzero
from manifester.settings import settings

Expand All @@ -22,7 +24,6 @@ def __init__(self, manifest_category, allocation_name=None, **kwargs):
)
self.offline_token = kwargs.get("offline_token", self.manifest_data.offline_token)
self.subscription_data = self.manifest_data.subscription_data
self.sat_version = kwargs.get("sat_version", self.manifest_data.sat_version)
self.token_request_data = {
"grant_type": "refresh_token",
"client_id": "rhsm-api",
Expand All @@ -35,6 +36,10 @@ def __init__(self, manifest_category, allocation_name=None, **kwargs):
self.allocations_url = self.manifest_data.get("url", {}).get("allocations", settings.url.allocations)
self._access_token = None
self._subscription_pools = None
self.sat_version = process_sat_version(
kwargs.get("sat_version", self.manifest_data.sat_version),
self.valid_sat_versions,
)

@property
def access_token(self):
Expand All @@ -48,6 +53,24 @@ def access_token(self):
).json()
self._access_token = token_data["access_token"]
return self._access_token

@cached_property
def valid_sat_versions(self):
headers = {
"headers": {"Authorization": f"Bearer {self.access_token}"},
"proxies": self.manifest_data.get("proxies", settings.proxies),
}
valid_sat_versions = []
sat_versions_response = simple_retry(
requests.get,
cmd_args=[
f"{self.allocations_url}/versions"
],
cmd_kwargs=headers,
).json()
for ver_dict in sat_versions_response["body"]:
valid_sat_versions.append(ver_dict["value"])
return valid_sat_versions

def create_subscription_allocation(self):
allocation_data = {
Expand All @@ -64,6 +87,16 @@ def create_subscription_allocation(self):
cmd_args=[f"{self.allocations_url}"],
cmd_kwargs=allocation_data,
).json()
logger.debug(
f"Received response {self.allocation} when attempting to create allocation."
)
if ("error" in self.allocation.keys() and
"invalid version" in self.allocation['error'].values()):
raise ValueError(
f"{self.sat_version} is not a valid version number."
"Versions must be in the form of \"sat-X.Y\". Current"
f"valid versions are {self.valid_sat_versions}."
)
self.allocation_uuid = self.allocation["body"]["uuid"]
logger.info(
f"Subscription allocation created with name {self.allocation_name} "
Expand Down

0 comments on commit eb516df

Please sign in to comment.