Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add azure/latest endpoint #30

Merged
merged 3 commits into from
Jun 25, 2024
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
37 changes: 27 additions & 10 deletions cid/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,36 @@ def latest_aws_image(db: Session, arch: Optional[str]) -> dict[str, Any]:
return latest_images_dict


def latest_azure_image(db: Session) -> dict[str, Any]:
def latest_azure_image(db: Session, arch: Optional[str]) -> dict[str, Any]:
"""Get the latest RHEL image on Azure."""
latest_image = db.query(AzureImage).order_by(desc(AzureImage.version)).first()
archs = (
[arch]
if arch is not None
else [arch[0] for arch in db.query(AzureImage.architecture).distinct().all()]
)
latest_images_dict = {}
for arch in archs:
latest_image = (
db.query(AzureImage)
.filter(AzureImage.architecture == arch)
.order_by(desc(AzureImage.version))
.first()
)

if latest_image is None:
return {"error": "No images found", "code": 404}
if latest_image is None:
continue

return {
"sku": latest_image.sku,
"offer": latest_image.offer,
"version": latest_image.version,
"urn": latest_image.urn,
}
latest_images_dict[arch] = {
"sku": latest_image.sku,
"offer": latest_image.offer,
"version": latest_image.version,
"urn": latest_image.urn,
}

if not latest_images_dict:
return {"error": "No images found for Azure", "code": 404}

return latest_images_dict


def latest_google_image(db: Session) -> dict:
Expand Down
8 changes: 8 additions & 0 deletions cid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def match_aws_image(image_id: str, db: Session = Depends(get_db)) -> dict: # no
return result


@app.get("/azure/latest")
def latest_azure_image(
db: Session = Depends(get_db), # noqa: B008
arch: Optional[str] = None,
) -> dict:
return crud.latest_azure_image(db, arch)


@app.get("/versions")
def versions(db: Session = Depends(get_db)) -> list: # noqa: B008
return crud.find_available_versions(db)
Expand Down
109 changes: 103 additions & 6 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,28 @@ def test_latest_aws_image_query_for_arch(db):


def test_latest_azure_image_no_images(db):
result = crud.latest_azure_image(db)
assert result == {"error": "No images found", "code": 404}
result = crud.latest_azure_image(db, None)
assert result == {"error": "No images found for Azure", "code": 404}


def test_latest_azure_image_wrong_arch(db):
result = crud.latest_azure_image(db, "arm32")
assert result == {"error": "No images found for Azure", "code": 404}


def test_latest_azure_image(db):
def test_latest_azure_image_single_arch(db):
images = [
AzureImage(
id="urn-a",
architecture="arm64",
sku="sku-a",
offer="offer-a",
version="1.0",
urn="urn-a",
),
AzureImage(
id="urn-b",
architecture="arm64",
sku="sku-a",
offer="offer-a",
version="2.0",
Expand All @@ -155,9 +162,99 @@ def test_latest_azure_image(db):
db.add_all(images)
db.commit()

result = crud.latest_azure_image(db)
assert result["sku"] == "sku-a"
assert result["version"] == "2.0"
result = crud.latest_azure_image(db, None)
print(result)
assert result["arm64"]["sku"] == "sku-a"
assert result["arm64"]["version"] == "2.0"


def test_latest_azure_image_multiple_arch(db):
images = [
AzureImage(
id="urn-1a",
architecture="arm64",
sku="sku-1a",
offer="offer-1a",
version="1.0",
urn="urn-1a",
),
AzureImage(
id="urn-2a",
architecture="arm64",
sku="sku-2a",
offer="offer-2a",
version="1.5",
urn="urn-2a",
),
AzureImage(
id="urn-1b",
architecture="x64",
sku="sku-1b",
offer="offer-1b",
version="2.0",
urn="urn-1b",
),
AzureImage(
id="urn-2b",
architecture="x64",
sku="sku-2b",
offer="offer-2b",
version="1.0",
urn="urn-2b",
),
]
db.add_all(images)
db.commit()

result = crud.latest_azure_image(db, None)
assert result["arm64"]["sku"] == "sku-2a"
assert result["arm64"]["version"] == "1.5"
assert result["x64"]["sku"] == "sku-1b"
assert result["x64"]["version"] == "2.0"


def test_latest_azure_image_query_arch(db):
images = [
AzureImage(
id="urn-1a",
architecture="arm64",
sku="sku-1a",
offer="offer-1a",
version="1.0",
urn="urn-1a",
),
AzureImage(
id="urn-2a",
architecture="arm64",
sku="sku-2a",
offer="offer-2a",
version="1.5",
urn="urn-2a",
),
AzureImage(
id="urn-1b",
architecture="x64",
sku="sku-1b",
offer="offer-1b",
version="2.0",
urn="urn-1b",
),
AzureImage(
id="urn-2b",
architecture="x64",
sku="sku-2b",
offer="offer-2b",
version="1.0",
urn="urn-2b",
),
]
db.add_all(images)
db.commit()

result = crud.latest_azure_image(db, "x64")
assert result["x64"]["sku"] == "sku-1b"
assert result["x64"]["version"] == "2.0"
assert "arm64" not in result


def test_latest_google_image_no_images(db):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def load_test_data():
images = json.load(fileh)
crud.import_aws_images(db, images)

with open("tests/data/azure.json") as fileh:
images = json.load(fileh)
crud.import_azure_images(db, images)


app.dependency_overrides[get_db] = override_get_db

Expand Down Expand Up @@ -112,3 +116,21 @@ def test_match_aws_image():
assert response.json()["name"] == "RHEL_HA-9.4.0_HVM-20240605-x86_64-82-Hourly2-GP3"
assert "ami" in response.json()["matching_images"][0]
assert "region" in response.json()["matching_images"][0]


@patch("cid.crud.latest_azure_image")
def test_latest_azure_image(mock_azure):
mock_azure.return_value = {
"sku": "sku-a",
"offer": "offer-a",
"version": "2.0",
"urn": "urn-b",
}

response = client.get("/azure/latest")
result = response.json()

assert result["sku"] == "sku-a"
assert result["offer"] == "offer-a"
assert result["version"] == "2.0"
assert result["urn"] == "urn-b"