Skip to content

Commit

Permalink
Merge pull request #44 from redhatcloudx/enhance-azure-endoint
Browse files Browse the repository at this point in the history
Add optional params to /azure
  • Loading branch information
major authored Jul 2, 2024
2 parents 4e14836 + a508727 commit fbe5fcc
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cid/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,39 @@ def find_aws_images(
return paginate(query, page, page_size)


def find_azure_images(
db: Session,
arch: Optional[str] = None,
version: Optional[str] = None,
urn: Optional[str] = None,
page: int = 1,
page_size: int = 100,
) -> dict:
"""Return all Azure images that match the given criteria.
Args:
db (Session): database session
arch (Optional[str]): architecture to search
version (Optional[str]): RHEL version to search
urn (Optional[str]): image urn to search
page (int): page number
page_size (int): number of images per page
Returns:
list: list of images that match the given criteria
"""
query = db.query(AzureImage).order_by(desc(AzureImage.version))

if arch:
query = query.filter(AzureImage.architecture == arch)
if version:
query = query.filter(AzureImage.version.contains(version))
if urn:
query = query.filter(AzureImage.urn.contains(urn))

return paginate(query, page, page_size)


def paginate(
query: Query,
page: int = 1,
Expand Down
13 changes: 13 additions & 0 deletions cid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ def match_aws_image(image_id: str, db: Session = Depends(get_db)) -> dict: # no
return result


@app.get("/azure")
def all_azure_images(
db: Session = Depends(get_db), # noqa: B008
arch: Optional[str] = None,
version: Optional[str] = None,
urn: Optional[str] = None,
page: int = 1,
page_size: int = 100,
) -> dict:
result = crud.find_azure_images(db, arch, version, urn, page, page_size)
return dict(jsonable_encoder(result))


@app.get("/azure/latest")
def latest_azure_image(
db: Session = Depends(get_db), # noqa: B008
Expand Down
156 changes: 156 additions & 0 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,159 @@ def test_find_aws_images_paginated(db):
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5


def test_find_azure_images(db):
images = [
AzureImage(
id="urn-a",
architecture="x64",
sku="sku-a",
offer="offer-a",
urn="urn-a",
version="8.2.2023122216",
),
AzureImage(
id="urn-b",
architecture="x64",
sku="sku-b",
offer="offer-b",
urn="urn-b",
version="7.9.2023122216",
),
AzureImage(
id="urn-c",
architecture="x64",
sku="sku-c",
offer="offer-c",
urn="urn-c",
version="9.5.2023122216",
),
AzureImage(
id="urn-d",
architecture="x64",
sku="sku-d",
offer="offer-d",
urn="urn-d",
version="10.0.2023122216",
),
AzureImage(
id="urn-e",
architecture="x64",
sku="sku-e",
urn="urn-e",
version="9.5.2023122216",
),
]
db.add_all(images)
db.commit()

result = crud.find_azure_images(db, None, None, None)
assert len(result["results"]) == 5

result = crud.find_azure_images(db, "x64", None, None)
assert len(result["results"]) == 5

result = crud.find_azure_images(db, None, "9.5", None)
assert len(result["results"]) == 2

result = crud.find_azure_images(db, None, None, "urn-c")
assert len(result["results"]) == 1

result = crud.find_azure_images(db, "x64", "10.0", "urn-d")
assert len(result["results"]) == 1


def test_find_azure_images_paginated(db):
images = [
AzureImage(
id="urn-a",
architecture="x64",
sku="sku-a",
offer="offer-a",
urn="urn-a",
version="8.2.2023122216",
),
AzureImage(
id="urn-b",
architecture="x64",
sku="sku-b",
offer="offer-b",
urn="urn-b",
version="7.9.2023122216",
),
AzureImage(
id="urn-c",
architecture="x64",
sku="sku-c",
offer="offer-c",
urn="urn-c",
version="9.5.2023122216",
),
AzureImage(
id="urn-d",
architecture="x64",
sku="sku-d",
offer="offer-d",
urn="urn-d",
version="10.0.2023122216",
),
AzureImage(
id="urn-e",
architecture="x64",
sku="sku-e",
urn="urn-e",
version="9.5.2023122216",
),
]
db.add_all(images)
db.commit()

result = crud.find_azure_images(db, None, None, None)
assert len(result["results"]) == 5
assert result["page"] == 1
assert result["page_size"] == 100
assert result["total_count"] == 5
assert result["total_pages"] == 1

result = crud.find_azure_images(db, None, None, None, 1, 1)
assert len(result["results"]) == 1
assert result["page"] == 1
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5

result = crud.find_azure_images(db, None, None, None, 2, 1)
assert len(result["results"]) == 1
assert result["page"] == 2
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5

result = crud.find_azure_images(db, None, None, None, 6, 1)
assert len(result["results"]) == 0
assert result["page"] == 6
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5

result = crud.find_azure_images(db, None, None, None, 1, 1000)
assert len(result["results"]) == 5
assert result["page"] == 1
assert result["page_size"] == 1000
assert result["total_count"] == 5
assert result["total_pages"] == 1

result = crud.find_azure_images(db, None, None, None, -1, 10)
assert len(result["results"]) == 5
assert result["page"] == 1
assert result["page_size"] == 10
assert result["total_count"] == 5
assert result["total_pages"] == 1

result = crud.find_azure_images(db, None, None, None, 1, -10)
assert len(result["results"]) == 1
assert result["page"] == 1
assert result["page_size"] == 1
assert result["total_count"] == 5
assert result["total_pages"] == 5
59 changes: 59 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,65 @@ def test_match_aws_image():
assert "region" in response.json()["matching_images"][0]


def test_all_azure_images():
response = client.get("/azure")
assert response.status_code == 200
assert len(response.json()["results"]) == 100
assert response.json()["page"] == 1
assert response.json()["page_size"] == 100
assert response.json()["total_count"] == 152
assert response.json()["total_pages"] == 2


def test_all_azure_images_with_query():
response = client.get("/azure?version=7.9.2023062711")
assert response.status_code == 200
assert response.json()["results"][0]["version"] == "7.9.2023062711"


def test_all_azure_images_with_query_urn():
response = client.get("/azure?urn=redhat-rhel:rh-rhel:rh-rhel7:7.9.2023062711")
assert response.status_code == 200
assert (
response.json()["results"][0]["urn"]
== "redhat-rhel:rh-rhel:rh-rhel7:7.9.2023062711"
)


def test_all_azure_images_with_query_arch():
response = client.get("/azure?arch=x64")
assert response.status_code == 200
assert response.json()["results"][0]["architecture"] == "x64"


def test_all_azure_images_with_query_combination():
response = client.get(
"/azure"
+ "?urn=redhat-rhel:rh-rhel:rh-rhel7:7.9.2023062711"
+ "&version=7.9.2023062711"
+ "&arch=x64"
)
assert response.status_code == 200
assert len(response.json()["results"]) == 1
assert (
response.json()["results"][0]["urn"]
== "redhat-rhel:rh-rhel:rh-rhel7:7.9.2023062711"
)
assert response.json()["results"][0]["version"] == "7.9.2023062711"
assert response.json()["results"][0]["architecture"] == "x64"


def test_all_azure_images_with_query_combination_no_match():
response = client.get(
"/azure"
+ "?urn=rhel-does-not-exist:11.4:11.4.0"
+ "&version=11.4.0"
+ "&architecture=arm64"
)
assert response.status_code == 200
assert len(response.json()["results"]) == 0


@patch("cid.crud.latest_azure_image")
def test_latest_azure_image(mock_azure):
mock_azure.return_value = {
Expand Down

0 comments on commit fbe5fcc

Please sign in to comment.