From a388f7e159ad9baccde385ee22f1fd201c1510c6 Mon Sep 17 00:00:00 2001 From: Felix Kolwa Date: Fri, 28 Jun 2024 12:16:19 +0200 Subject: [PATCH] Add optional params to /aws Add optional arch filter Add optional name filter Add optional version filter Add optional region filter Add find_aws_images crud function Add necessary testing --- cid/crud.py | 33 ++++++++++++++++++++++ cid/main.py | 10 +++++-- tests/test_crud.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_main.py | 64 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 170 insertions(+), 6 deletions(-) diff --git a/cid/crud.py b/cid/crud.py index 33ebe9d..d322954 100644 --- a/cid/crud.py +++ b/cid/crud.py @@ -364,3 +364,36 @@ def find_images_for_version(db: Session, version: str) -> list: print(image.id) return [{"ami": x.id, "name": x.name} for x in images] + + +def find_aws_images( + db: Session, + arch: Optional[str] = None, + version: Optional[str] = None, + name: Optional[str] = None, + region: Optional[str] = None, +) -> list: + """Return all AWS 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 + region (Optional[str]): AWS region to search + + Returns: + list: list of images that match the given criteria + """ + query = db.query(AwsImage).order_by(AwsImage.creationDate.desc()) + + if arch: + query = query.filter(AwsImage.arch == arch) + if version: + query = query.filter(AwsImage.version == version) + if name: + query = query.filter(AwsImage.name.contains(name)) + if region: + query = query.filter(AwsImage.region == region) + + return query.all() diff --git a/cid/main.py b/cid/main.py index 5be8520..8aa3aa9 100644 --- a/cid/main.py +++ b/cid/main.py @@ -33,8 +33,14 @@ def read_root() -> dict: @app.get("/aws") -def all_aws_images(db: Session = Depends(get_db)) -> list: # noqa: B008 - result = db.query(AwsImage).order_by(AwsImage.creationDate.desc()).all() +def all_aws_images( + db: Session = Depends(get_db), # noqa: B008 + arch: Optional[str] = None, + version: Optional[str] = None, + name: Optional[str] = None, + region: Optional[str] = None, +) -> list: + result = crud.find_aws_images(db, arch, version, name, region) return list(jsonable_encoder(result)) diff --git a/tests/test_crud.py b/tests/test_crud.py index 9d33965..73ae5fe 100644 --- a/tests/test_crud.py +++ b/tests/test_crud.py @@ -461,3 +461,72 @@ def test_find_images_for_version(db): {"ami": "ami-c", "name": "RHEL-9.5.0v1"}, {"ami": "ami-e", "name": "RHEL-9.5.0v2"}, ] + + +def test_find_aws_images(db): + images = [ + AwsImage( + id="ami-a", + name="RHEL-8.2.0", + version="8.2.0", + arch="x86_64", + region="us-west-1", + ), + AwsImage( + id="ami-b", + name="RHEL-7.9.0", + version="7.9.0", + arch="x86_64", + region="us-west-1", + ), + AwsImage( + id="ami-c", + name="RHEL-9.5.0", + version="9.5.0", + arch="x86_64", + region="us-west-1", + ), + AwsImage( + id="ami-d", + name="RHEL-10.0.0", + version="10.0.0", + arch="x86_64", + region="us-west-2", + ), + AwsImage( + id="ami-e", + name="RHEL-9.5.0", + version="9.5.0", + arch="arm64", + region="us-west-2", + ), + ] + db.add_all(images) + db.commit() + + result = crud.find_aws_images(db, None, None, None, None) + assert len(result) == 5 + + result = crud.find_aws_images(db, "arm64", None, None, None) + assert len(result) == 1 + assert result[0].name == "RHEL-9.5.0" + assert result[0].arch == "arm64" + assert result[0].region == "us-west-2" + + result = crud.find_aws_images(db, None, "9.5.0", None, None) + assert len(result) == 2 + assert result[0].name == "RHEL-9.5.0" + assert result[0].arch == "x86_64" + assert result[0].region == "us-west-1" + assert result[1].name == "RHEL-9.5.0" + assert result[1].arch == "arm64" + assert result[1].region == "us-west-2" + + result = crud.find_aws_images(db, None, None, "10.0.0", None) + assert len(result) == 1 + assert result[0].name == "RHEL-10.0.0" + assert result[0].arch == "x86_64" + assert result[0].region == "us-west-2" + + result = crud.find_aws_images(db, None, None, None, "us-west-1") + assert len(result) == 3 diff --git a/tests/test_main.py b/tests/test_main.py index 39ed618..781981f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -61,7 +61,7 @@ def test_latest_aws_image(mock_aws): "name": "test_image", "version": "1.0", "date": "2022-01-01", - "amis": {"us-west-1": "ami-12345678"}, + "amis": {"af-south-1": "ami-12345678"}, } response = client.get("/aws/latest") @@ -71,7 +71,7 @@ def test_latest_aws_image(mock_aws): assert result["name"] == "test_image" assert result["version"] == "1.0" assert result["date"] == "2022-01-01" - assert result["amis"] == {"us-west-1": "ami-12345678"} + assert result["amis"] == {"af-south-1": "ami-12345678"} @patch("cid.crud.latest_aws_image") @@ -80,7 +80,7 @@ def test_latest_aws_image_query_for_arch(mock_aws): "name": "test_image", "version": "1.0", "date": "2022-01-01", - "amis": {"us-west-1": "ami-12345678"}, + "amis": {"af-south-1": "ami-12345678"}, } response = client.get("/aws/latest?arch=x86_64") @@ -90,7 +90,7 @@ def test_latest_aws_image_query_for_arch(mock_aws): assert result["name"] == "test_image" assert result["version"] == "1.0" assert result["date"] == "2022-01-01" - assert result["amis"] == {"us-west-1": "ami-12345678"} + assert result["amis"] == {"af-south-1": "ami-12345678"} @patch("cid.crud.find_available_aws_versions") @@ -126,6 +126,62 @@ def test_all_aws_images(): assert len(response.json()) == 500 +def test_all_aws_images_with_query(): + response = client.get("/aws?version=9.4.0") + assert response.status_code == 200 + assert response.json()[0]["version"] == "9.4.0" + + +def test_all_aws_images_with_query_region(): + response = client.get("/aws?region=af-south-1") + assert response.status_code == 200 + assert response.json()[0]["region"] == "af-south-1" + + +def test_all_aws_images_with_query_arch(): + response = client.get("/aws?arch=x86_64") + assert response.status_code == 200 + assert response.json()[0]["arch"] == "x86_64" + + +def test_all_aws_images_with_query_name(): + response = client.get("/aws?name=RHEL_HA-9.4.0_HVM-20240605-x86_64-82-Hourly2-GP3") + assert response.status_code == 200 + assert ( + response.json()[0]["name"] == "RHEL_HA-9.4.0_HVM-20240605-x86_64-82-Hourly2-GP3" + ) + + +def test_all_aws_images_with_query_combination(): + response = client.get( + "/aws" + + "?name=RHEL_HA-9.4.0_HVM-20240605-x86_64-82-Hourly2-GP3" + + "®ion=af-south-1" + + "&version=9.4.0" + + "&arch=x86_64" + ) + assert response.status_code == 200 + assert len(response.json()) == 1 + assert ( + response.json()[0]["name"] == "RHEL_HA-9.4.0_HVM-20240605-x86_64-82-Hourly2-GP3" + ) + assert response.json()[0]["region"] == "af-south-1" + assert response.json()[0]["version"] == "9.4.0" + assert response.json()[0]["arch"] == "x86_64" + + +def test_all_aws_images_with_query_combination_no_match(): + response = client.get( + "/aws" + + "?name=THIS_DOES_NOT_EXIST" + + "®ion=af-south-1" + + "&version=9.4.0" + + "&arch=arm64" + ) + assert response.status_code == 200 + assert len(response.json()) == 0 + + def test_single_aws_image(): response = client.get("/aws/image/ami-08a20c15f394e5531") assert response.status_code == 200