diff --git a/cid/crud.py b/cid/crud.py index fce488c..1863a29 100644 --- a/cid/crud.py +++ b/cid/crud.py @@ -445,6 +445,43 @@ def find_azure_images( return paginate(query, page, page_size) +def find_google_images( + db: Session, + arch: Optional[str] = None, + version: Optional[str] = None, + name: Optional[str] = None, + family: Optional[str] = None, + page: int = 1, + page_size: int = 100, +) -> dict: + """Return paginated Google images that match the given criteria. + + Args: + db (Session): database session + arch (Optional[str]): architecture to search + version (Optional[str]): RHEL version to search + name (Optional[str]): image name to search + family (Optional[str]): Google image family to search + page (int): page number + page_size (int): number of images per page + + Returns: + dict: paginated results + """ + query = db.query(GoogleImage).order_by(GoogleImage.creationTimestamp.desc()) + + if arch: + query = query.filter(GoogleImage.arch == arch) + if version: + query = query.filter(GoogleImage.version == version) + if name: + query = query.filter(GoogleImage.name.contains(name)) + if family: + query = query.filter(GoogleImage.family == family) + + return paginate(query, page, page_size) + + def paginate( query: Query, page: int = 1, diff --git a/cid/main.py b/cid/main.py index eac5aeb..6c52c1b 100644 --- a/cid/main.py +++ b/cid/main.py @@ -98,6 +98,20 @@ def azure_versions(db: Session = Depends(get_db)) -> list: # noqa: B008 return crud.find_available_azure_versions(db) +@app.get("/google") +def all_google_images( + db: Session = Depends(get_db), # noqa: B008 + arch: Optional[str] = None, + version: Optional[str] = None, + name: Optional[str] = None, + family: Optional[str] = None, + page: int = 1, + page_size: int = 100, +) -> dict: + result = crud.find_google_images(db, arch, version, name, family, page, page_size) + return dict(jsonable_encoder(result)) + + @app.get("/google/versions") def google_versions(db: Session = Depends(get_db)) -> list: # noqa: B008 return crud.find_available_google_versions(db) diff --git a/tests/test_crud.py b/tests/test_crud.py index 3e2e537..3fd9405 100644 --- a/tests/test_crud.py +++ b/tests/test_crud.py @@ -797,3 +797,137 @@ def test_find_azure_images_paginated(db): assert result["page_size"] == 1 assert result["total_count"] == 5 assert result["total_pages"] == 5 + + +def find_google_images(db): + images = [ + GoogleImage( + id="id-a", + arch="X86_64", + name="test_image_1a", + version="1.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-b", + arch="X86_64", + name="test_image_2a", + version="2.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-c", + arch="ARM64", + name="test_image_1b", + version="1.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-d", + arch="ARM64", + name="test_image_2b", + version="2.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + ] + db.add_all(images) + db.commit() + + result = crud.find_google_images(db, None, None, None, None, None) + assert len(result["results"]) == 4 + + result = crud.find_google_images(db, "X86_64", None, None, None, None) + assert len(result["results"]) == 2 + + result = crud.find_google_images(db, None, "2.0", None, None, None) + assert len(result["results"]) == 2 + + result = crud.find_google_images(db, None, None, "test_image_1b", None, None) + assert len(result["results"]) == 1 + + result = crud.find_google_images(db, None, None, None, "test_image_1b", None) + assert len(result["results"]) == 1 + + +def test_find_google_images_paginated(db): + images = [ + GoogleImage( + id="id-a", + arch="X86_64", + name="test_image_1a", + version="1.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-b", + arch="X86_64", + name="test_image_2a", + version="2.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-c", + arch="ARM64", + name="test_image_1b", + version="1.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + GoogleImage( + id="id-d", + arch="ARM64", + name="test_image_2b", + version="2.0", + creationTimestamp=datetime.strptime("2022-01-01", "%Y-%m-%d").date(), + ), + ] + db.add_all(images) + db.commit() + + result = crud.find_google_images(db, None, None, None, None) + assert len(result["results"]) == 4 + assert result["page"] == 1 + assert result["page_size"] == 100 + assert result["total_count"] == 4 + assert result["total_pages"] == 1 + + result = crud.find_google_images(db, None, None, None, None, 1, 1) + assert len(result["results"]) == 1 + assert result["page"] == 1 + assert result["page_size"] == 1 + assert result["total_count"] == 4 + assert result["total_pages"] == 4 + + result = crud.find_google_images(db, None, None, None, None, 2, 1) + assert len(result["results"]) == 1 + assert result["page"] == 2 + assert result["page_size"] == 1 + assert result["total_count"] == 4 + assert result["total_pages"] == 4 + + result = crud.find_google_images(db, None, None, None, None, 6, 1) + assert len(result["results"]) == 0 + assert result["page"] == 6 + assert result["page_size"] == 1 + assert result["total_count"] == 4 + assert result["total_pages"] == 4 + + result = crud.find_google_images(db, None, None, None, None, 1, 1000) + assert len(result["results"]) == 4 + assert result["page"] == 1 + assert result["page_size"] == 1000 + assert result["total_count"] == 4 + assert result["total_pages"] == 1 + + result = crud.find_google_images(db, None, None, None, None, -1, 10) + assert len(result["results"]) == 4 + assert result["page"] == 1 + assert result["page_size"] == 10 + assert result["total_count"] == 4 + assert result["total_pages"] == 1 + + result = crud.find_google_images(db, None, None, None, None, 1, -10) + assert len(result["results"]) == 1 + assert result["page"] == 1 + assert result["page_size"] == 1 + assert result["total_count"] == 4 + assert result["total_pages"] == 4 diff --git a/tests/test_main.py b/tests/test_main.py index 951ccfe..32e9d6f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -293,6 +293,53 @@ def test_latest_azure_image(mock_azure): assert result["urn"] == "urn-b" +def test_all_google_images(): + response = client.get("/google") + assert response.status_code == 200 + assert len(response.json()["results"]) == 4 + assert response.json()["page"] == 1 + assert response.json()["page_size"] == 100 + assert response.json()["total_count"] == 4 + assert response.json()["total_pages"] == 1 + + +def test_all_google_images_with_query(): + response = client.get("/google?version=7") + assert response.status_code == 200 + assert response.json()["results"][0]["version"] == "7" + + +def test_all_google_images_with_query_arch(): + response = client.get("/google?arch=X86_64") + assert response.status_code == 200 + assert response.json()["results"][0]["arch"] == "X86_64" + + +def test_all_google_images_with_query_name(): + response = client.get("/google?name=rhel-7-v20240611") + assert response.status_code == 200 + assert response.json()["results"][0]["name"] == "rhel-7-v20240611" + + +def test_all_google_images_with_query_combination(): + response = client.get( + "/google" + "?name=rhel-7-v20240611" + "&version=7" + "&arch=X86_64" + ) + assert response.status_code == 200 + assert len(response.json()["results"]) == 1 + assert response.json()["results"][0]["name"] == "rhel-7-v20240611" + assert response.json()["results"][0]["version"] == "7" + assert response.json()["results"][0]["arch"] == "X86_64" + + +def test_all_google_images_with_query_combination_no_match(): + response = client.get( + "/google" + "?name=does-not-exist" + "&version=7" + "&arch=X86_64" + ) + assert response.status_code == 200 + assert len(response.json()["results"]) == 0 + + @patch("cid.crud.latest_google_image") def test_latest_google_image(mock_google): mock_google.return_value = {