Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Add AWS image name parser #35

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/rhelocator/update_images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Update images from public cloud APIs."""
from __future__ import annotations

import re

import boto3
import requests

Expand Down Expand Up @@ -71,6 +73,44 @@ def get_aws_all_images(image_type: str = "hourly") -> dict[str, list[dict[str, s
return images_per_region


def parse_aws_image_name(image_name: str) -> dict[str, str]:
"""Parse an AWS image name and return extra data about the image.

Regex101: https://regex101.com/r/mXCl73/1

Args:
image_name: String containing the image name, such as:
RHEL-9.0.0_HVM_BETA-20211026-x86_64-10-Hourly2-GP2

Returns:
Dictionary with additional information about the image.
"""
# See Regex101 link above to tinker with this regex. Each group is named to make
# it easier to handle parsed data. Explanation of names:
#
# intprod = internal product (such as HA)
# extprod = external product (such as SAP)
# version = RHEL version (such as 9.0.0)
# virt = virtualization type (such as HVM)
# beta = beta vs non-beta release
# date = date image was produced
# arch = architecture (such as x86_64 or arm64)
# release = release number of the image
# billing = Hourly2 or Access2 #noqa: E800
# storage = storage type (almost always GP2)
#
aws_image_name_regex = (
r"RHEL_*(?P<intprod>\w*)?-*(?P<extprod>\w*)?-(?P<version>[\d\-\.]*)_"
r"(?P<virt>[A-Z]*)_*(?P<beta>\w*)?-(?P<date>\d+)-(?P<arch>\w*)-"
r"(?P<release>\d+)-(?P<billing>[\w\d]*)-(?P<storage>\w*)"
)
matches = re.match(aws_image_name_regex, image_name, re.IGNORECASE)
if matches:
return matches.groupdict()

return {}


def get_azure_access_token() -> str:
"""Authenticate with Azure and return the access token to use with API requests.

Expand Down
76 changes: 76 additions & 0 deletions tests/test_update_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,82 @@ def test_get_aws_all_images(mock_aws_regions, mock_aws_images) -> None:
assert billing_codes == {config.AWS_HOURLY_BILLING_CODE}


def test_parse_aws_image_name_basic():
"""Test parsing an AWS image name with a very basic image."""
image_name = "RHEL-7.9_HVM-20220512-x86_64-1-Hourly2-GP2"
data = update_images.parse_aws_image_name(image_name)

assert isinstance(data, dict)

assert not data["intprod"]
assert not data["extprod"]
assert data["version"] == "7.9"
assert data["virt"] == "HVM"
assert not data["beta"]
assert data["date"] == "20220512"
assert data["arch"] == "x86_64"
assert data["release"] == "1"
assert data["billing"] == "Hourly2"
assert data["storage"] == "GP2"


def test_parse_aws_image_name_beta():
"""Test parsing an AWS image name for a beta arm64 image."""
image_name = "RHEL-9.1.0_HVM_BETA-20220829-arm64-0-Hourly2-GP2"
data = update_images.parse_aws_image_name(image_name)

assert isinstance(data, dict)

assert not data["intprod"]
assert not data["extprod"]
assert data["version"] == "9.1.0"
assert data["virt"] == "HVM"
assert data["beta"] == "BETA"
assert data["date"] == "20220829"
assert data["arch"] == "arm64"
assert data["release"] == "0"
assert data["billing"] == "Hourly2"
assert data["storage"] == "GP2"


def test_parse_aws_image_name_internal_product():
"""Test parsing an AWS image name with an internal product."""
image_name = "RHEL_HA-8.5.0_HVM-20211103-x86_64-0-Hourly2-GP2"
data = update_images.parse_aws_image_name(image_name)

assert isinstance(data, dict)

assert data["intprod"] == "HA"
assert not data["extprod"]
assert data["version"] == "8.5.0"
assert data["virt"] == "HVM"
assert not data["beta"]
assert data["date"] == "20211103"
assert data["arch"] == "x86_64"
assert data["release"] == "0"
assert data["billing"] == "Hourly2"
assert data["storage"] == "GP2"


def test_parse_aws_image_name_external_product():
"""Test parsing an AWS image name for a beta arm64 image."""
image_name = "RHEL-SAP-8.2.0_HVM-20211007-x86_64-0-Hourly2-GP2"
data = update_images.parse_aws_image_name(image_name)

assert isinstance(data, dict)

assert not data["intprod"]
assert data["extprod"] == "SAP"
assert data["version"] == "8.2.0"
assert data["virt"] == "HVM"
assert not data["beta"]
assert data["date"] == "20211007"
assert data["arch"] == "x86_64"
assert data["release"] == "0"
assert data["billing"] == "Hourly2"
assert data["storage"] == "GP2"


@patch("rhelocator.update_images.requests.post")
def test_get_azure_access_token(mock_post) -> None:
"""Test retrieving Azure locations."""
Expand Down