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

resolve name inconsistency #168

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask import request


def gcp_blueprint(data: list[dict[str, str]]) -> Blueprint:
def google_blueprint(data: list[dict[str, str]]) -> Blueprint:
"""Creates and returns a Flask blueprint based that serves the provided
image data.

Expand All @@ -15,13 +15,13 @@ def gcp_blueprint(data: list[dict[str, str]]) -> Blueprint:
Returns:
Flask blueprint serving the provided data.
"""
gcp_endpoint = Blueprint("gcp_api", __name__)
google_endpoint = Blueprint("google_api", __name__)

@gcp_endpoint.route("/gcp", methods=["GET"])
@swag_from("../schema/gcp.yml")
@google_endpoint.route("/google", methods=["GET"])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FKolwa does it make sense to rename our endpoint too? Wasn't sure about this

Copy link
Member

@F-X64 F-X64 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I'm a bit torn on this one too. I'd like to stay as consistent as possible but gcp would probably make more sense as an endpoint. I'd say we probably should stick to gcp.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to keep it to google, because otherwise we would have a lot of exceptions and inconsistency again.

@swag_from("../schema/google.yml")
def endpoint() -> list[dict[str, str]]:
"""Cloud Image Locator GCP Endpoint Provides RHEL image data for Google
Cloud."""
"""Cloud Image Locator GOOGLE Endpoint Provides RHEL image data for
Google Cloud."""

query: dict[str, str] = {}
name: Optional[str] = request.args.get("name")
Expand Down Expand Up @@ -55,4 +55,4 @@ def endpoint() -> list[dict[str, str]]:
response.append(image)
return response

return gcp_endpoint
return google_endpoint
4 changes: 2 additions & 2 deletions src/rhelocator/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from rhelocator.api.routes.aws import aws_blueprint
from rhelocator.api.routes.azure import azure_blueprint
from rhelocator.api.routes.gcp import gcp_blueprint
from rhelocator.api.routes.google import google_blueprint
from rhelocator.update_images import schema


Expand Down Expand Up @@ -40,7 +40,7 @@ def create_app(file_path: str) -> Flask:
)

app.register_blueprint(
gcp_blueprint(image_data["images"]["google"]), url_prefix="/api"
google_blueprint(image_data["images"]["google"]), url_prefix="/api"
)

app.register_blueprint(
Expand Down
10 changes: 5 additions & 5 deletions src/rhelocator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rhelocator.api import server
from rhelocator.update_images import aws
from rhelocator.update_images import azure
from rhelocator.update_images import gcp
from rhelocator.update_images import google
from rhelocator.update_images import schema


Expand Down Expand Up @@ -54,9 +54,9 @@ def aws_regions() -> None:


@click.command()
def gcp_images() -> None:
"""Dump GCP images for all regions in JSON format."""
images = gcp.format_all_images()
def google_images() -> None:
"""Dump GOOGLE images for all regions in JSON format."""
images = google.format_all_images()
dump_images(images)


Expand Down Expand Up @@ -106,5 +106,5 @@ def serve(file_path: str, port: int, host: str, dev: bool) -> None:
cli.add_command(aws_hourly_images)
cli.add_command(aws_regions)
cli.add_command(azure_images)
cli.add_command(gcp_images)
cli.add_command(google_images)
cli.add_command(serve)
2 changes: 1 addition & 1 deletion src/rhelocator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
# \____/\____/_/

# Defines project name.
GCP_PROJECTNAME = "rhel-cloud"
GOOGLE_PROJECTNAME = "rhel-cloud"
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def get_images() -> list[dict[str, str]]:
# NOTE(mhayden): Google's examples suggest using a filter here for "deprecated.state
# != DEPRECATED" but it returns no images when I tried it.
# https://github.com/googleapis/python-compute/blob/main/samples/recipes/images/pagination.py
images_list_request = compute_v1.ListImagesRequest(project=config.GCP_PROJECTNAME)
images_list_request = compute_v1.ListImagesRequest(
project=config.GOOGLE_PROJECTNAME
)

# Normalize the data first.

Expand All @@ -37,9 +39,9 @@ def get_images() -> list[dict[str, str]]:
def normalize_google_images(image_list: list[Any]) -> list[dict[str, str]]:
"""Normalize the data returned from Google's image listing.

The GCP SDK returns an unusual object with repeated attributes and some attributes
lead to other interesting objects. The goal here is to normalize this data so that
it's dict-like, similar to the Azure and AWS functions.
The GOOGLE SDK returns an unusual object with repeated attributes and some
attributes lead to other interesting objects. The goal here is to normalize
this data so that it's dict-like, similar to the Azure and AWS functions.

Args:
image_list: A Google image listing from the ImagesClient class.
Expand Down
12 changes: 6 additions & 6 deletions tests/api/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def runner(app):
return app.test_cli_runner()


def test_request_gcp(client):
response = client.get("/api/gcp")
def test_request_google(client):
response = client.get("/api/google")
assert len(response.data) > 0
assert response.status_code == 200


def test_request_gcp_with_query(client):
def test_request_google_with_query(client):
response = client.get(
"/api/gcp",
"/api/google",
query_string={
"version": "9",
},
Expand All @@ -48,15 +48,15 @@ def test_request_gcp_with_query(client):
assert response.status_code == 200


def test_request_gcp_with_multi_query(client):
def test_request_google_with_multi_query(client):
test_query = {
"name": "RHEL 9",
"arch": "arm64",
"version": "9",
"imageId": "rhel-9",
"date": "2022-11-02",
}
response = client.get("/api/gcp", query_string=test_query)
response = client.get("/api/google", query_string=test_query)
for image in json.loads(response.data):
assert test_query["name"] in image["name"]
assert test_query["arch"] in image["arch"]
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def mock_azure_images(mocker):


@pytest.fixture
def mock_gcp_images(mocker):
def mock_google_images(mocker):
"""Provide an offline result for calls to get_google_images."""
mock = mocker.patch("rhelocator.update_images.gcp.get_images")
mock = mocker.patch("rhelocator.update_images.google.get_images")

mocked_image = {
"id": "rhel-7-9-sap-v20220719",
Expand All @@ -151,7 +151,7 @@ def mock_gcp_images(mocker):
@pytest.fixture
def mock_normalize_google_images(mocker):
"""Provide an offline result for calls to normalize_google_images."""
mock = mocker.patch("rhelocator.update_images.gcp.normalize_google_images")
mock = mocker.patch("rhelocator.update_images.google.normalize_google_images")

# Fake a Google image listing.
mocked_image = MagicMock()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def test_azure_images_offline(


@pytest.mark.e2e
def test_gcp_images_live(runner):
def test_google_images_live(runner):
"""Run a live test against the Google Cloud API to get images via CLI."""
result = runner.invoke(cli.gcp_images)
result = runner.invoke(cli.google_images)
parsed = json.loads(result.output)

assert isinstance(parsed["images"]["google"], list)
Expand All @@ -146,9 +146,9 @@ def test_gcp_images_live(runner):
assert result.exit_code == 0


def test_gcp_images_offline(mock_gcp_images, runner):
def test_google_images_offline(mock_google_images, runner):
"""Run a live test against the Google Cloud API to get images via CLI."""
result = runner.invoke(cli.gcp_images)
result = runner.invoke(cli.google_images)
parsed = json.loads(result.output)

assert isinstance(parsed["images"]["google"], list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

from jsonschema import ValidationError

from rhelocator.update_images import gcp
from rhelocator.update_images import google
from rhelocator.update_images import schema


@patch("rhelocator.update_images.gcp.compute_v1.ImagesClient")
def test_get_images(mock_gcp: MagicMock) -> None:
@patch("rhelocator.update_images.google.compute_v1.ImagesClient")
def test_get_images(mock_google: MagicMock) -> None:
"""Test getting Google images."""
fam_rhel_images = []
with open("tests/update_images/testdata/gcp_list_images.json") as json_file:
with open("tests/update_images/testdata/google_list_images.json") as json_file:
list = json.load(json_file)
for entry in list:
if entry["family"].__contains__("rhel"):
Expand Down Expand Up @@ -47,9 +47,9 @@ def test_get_images(mock_gcp: MagicMock) -> None:
# Connect the mock to the ImagesClient return value.
mock_response = MagicMock()
mock_response.list.return_value = mocked_list
mock_gcp.return_value = mock_response
mock_google.return_value = mock_response

images = gcp.get_images()
images = google.get_images()
assert len(images) == len(mocked_list) / 2


Expand All @@ -62,7 +62,7 @@ def test_normalize_google_images() -> None:
mocked_image.description = "RHEL"
mocked_image.name = "rhel-9-20221018"

images = gcp.normalize_google_images([mocked_image])
images = google.normalize_google_images([mocked_image])

assert isinstance(images, list)
assert images[0] == {
Expand All @@ -76,25 +76,25 @@ def test_normalize_google_images() -> None:
def test_parse_image_version_from_name():
"""Test parsing a google image name with a basic image."""
image_name = "rhel-7-9-sap-v20220719"
version = gcp.parse_image_version_from_name(image_name)
version = google.parse_image_version_from_name(image_name)

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)
version = google.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)
version = google.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)
version = google.parse_image_version_from_name(image_name)

assert version == "unknown"

Expand All @@ -110,18 +110,18 @@ def test_format_image():
"name": "rhel-7-v20220920",
}

data = {"images": {"google": [gcp.format_image(mocked_image)]}}
data = {"images": {"google": [google.format_image(mocked_image)]}}

try:
schema.validate_json(data)
except ValidationError as exc:
raise AssertionError(f"Formatted data does not expect schema: {exc}")


def test_format_all_images(mock_gcp_images):
def test_format_all_images(mock_google_images):
"""Test transforming a list of google images into a schema approved
format."""
data = gcp.format_all_images()
data = google.format_all_images()

try:
schema.validate_json(data)
Expand Down
2 changes: 1 addition & 1 deletion tests/update_images/testdata/update.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# az vm image list -f RHEL -l eastus --all > azure_list_images.json
gcloud config set account rhel-cloud
gcloud compute images list --format="json" > gcp_list_images.json
gcloud compute images list --format="json" > google_list_images.json
aws --region=us-east-1 ec2 describe-images --owner 309956199498 --filter "Name=is-public,Values=true" > aws_list_images.json