Skip to content

Commit

Permalink
Merge pull request #40 from redhatcloudx/extend-aws-endpoint
Browse files Browse the repository at this point in the history
Add optional parameters to /aws for enhanced queries
  • Loading branch information
major authored Jun 28, 2024
2 parents be828f8 + a388f7e commit 7011f8c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 6 deletions.
33 changes: 33 additions & 0 deletions cid/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,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()
10 changes: 8 additions & 2 deletions cid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
69 changes: 69 additions & 0 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,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
64 changes: 60 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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"
+ "&region=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"
+ "&region=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
Expand Down

0 comments on commit 7011f8c

Please sign in to comment.