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

Commit

Permalink
Add AWS image name parser
Browse files Browse the repository at this point in the history
Parse additional information about AWS images using their name.

Fixes #34

Signed-off-by: Major Hayden <major@redhat.com>
  • Loading branch information
major committed Oct 10, 2022
1 parent 0e0b13a commit da2f223
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
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

0 comments on commit da2f223

Please sign in to comment.