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

Commit

Permalink
use regex to determine rhel version
Browse files Browse the repository at this point in the history
Signed-off-by: Janine Olear <pninak@web.de>
  • Loading branch information
miyunari committed Nov 16, 2022
1 parent 83da1df commit 5e5460d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/rhelocator/update_images/gcp.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

from typing import Any

from google.cloud import compute_v1
Expand Down Expand Up @@ -62,6 +64,8 @@ def normalize_google_images(image_list: list[Any]) -> list[dict[str, str]]:
def parse_image_version_from_name(image_name: str) -> str:
"""Parse an google image name and return version string.
Regex101: https://regex101.com/r/CiABs5/1
Args:
image_name: String containing the image name, such as:
rhel-7-9-sap-v20220719
Expand All @@ -70,8 +74,16 @@ def parse_image_version_from_name(image_name: str) -> str:
Returns the google image version as string
rhel-7-9
"""
res = image_name.split("-", 3)
return res[0] + "-" + res[1] + "-" + res[2]
google_image_name_regex = (
r"(?P<product>\w*)-(?P<version>[\d]+(?:\-[\d]+)?)-?"
r"(?P<intprod>\w*)?-v(?P<date>\d{4}\d{2}\d{2})"
)

matches = re.match(google_image_name_regex, image_name, re.IGNORECASE)
if matches:
image_data = matches.groupdict()
return "rhel-" + image_data["version"]
return "unknown"


def format_all_images() -> object:
Expand Down
20 changes: 19 additions & 1 deletion tests/update_images/test_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,25 @@ def test_parse_image_version_from_name():
image_name = "rhel-7-9-sap-v20220719"
version = gcp.parse_image_version_from_name(image_name)

assert version == "rhel-7-9"
assert version == "7-9"

"""Test parsing a google image name with a basic image."""
image_name = "rhel-7-sap-v20220719"
version = gcp.parse_image_version_from_name(image_name)

assert version == "7"

"""Test parsing a google image name with a basic image."""
image_name = "rhel-7-1-2-sap-v20220719"
version = gcp.parse_image_version_from_name(image_name)

assert version == "7-1-2"

"""Test parsing a google image name with a basic image."""
image_name = "rhel-sap-v20220719"
version = gcp.parse_image_version_from_name(image_name)

assert version == "unknown"


def test_format_image():
Expand Down

0 comments on commit 5e5460d

Please sign in to comment.