Skip to content

Commit

Permalink
Allow bios field to be optional (#182)
Browse files Browse the repository at this point in the history
* Allow bios field to be optional

* If bios is specified in the request but is not found, return Not Certified response
  • Loading branch information
nadzyah authored Jul 19, 2024
1 parent 1bb3444 commit 4a27002
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 230 deletions.
23 changes: 11 additions & 12 deletions server/hwapi/data_models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,27 @@ def get_bios(


def get_machine_with_same_hardware_params(
db: Session, arch: str, board: models.Device, bios: models.Bios
db: Session, arch: str, board: models.Device, bios: models.Bios | None
) -> models.Machine | None:
"""
Get a machines that have the given architecture, motherboard, bios
Get a machines that have the given architecture, motherboard, and optionally bios
"""
stmt = (
select(models.Machine)
.select_from(models.Machine)
.join(models.Certificate)
.join(models.Report, models.Certificate.reports)
.join(models.Device, models.Report.devices)
.join(models.Bios, models.Report.bios)
.filter(
and_(
models.Device.id == board.id,
models.Report.architecture == arch,
models.Bios.id == bios.id,
)
)
.distinct()
.filter(and_(models.Device.id == board.id, models.Report.architecture == arch))
)
return db.execute(stmt).scalars().first()

if bios:
stmt = stmt.join(models.Bios, models.Report.bios_id == models.Bios.id).filter(
models.Bios.id == bios.id
)

machine = db.execute(stmt.distinct()).scalars().first()
return machine


def get_machine_by_canonical_id(
Expand Down
20 changes: 12 additions & 8 deletions server/hwapi/endpoints/certification/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@


def find_main_hardware_components(
db: Session, board_data: BoardValidator, bios_data: BiosValidator
) -> tuple[models.Device, models.Bios]:
db: Session, board_data: BoardValidator, bios_data: BiosValidator | None
) -> tuple[models.Device, models.Bios | None]:
"""
A function to get "main hardware components" like board and bios. Can be extended
in future
"""
board = repository.get_board(
db, board_data.manufacturer, board_data.product_name, board_data.version
)
bios = repository.get_bios(
db, bios_data.vendor, bios_data.version, bios_data.firmware_revision
)
if not board or not bios:
if not board:
raise ValueError("Hardware not certified")
return board, bios
if bios_data:
bios = repository.get_bios(
db, bios_data.vendor, bios_data.version, bios_data.firmware_revision
)
if not bios:
raise ValueError("Hardware not certified")
return board, bios
return board, None


def find_certified_machine(
db: Session, arch: str, board: models.Device, bios: models.Bios
db: Session, arch: str, board: models.Device, bios: models.Bios | None
) -> models.Machine:
machine = repository.get_machine_with_same_hardware_params(db, arch, board, bios)
if not machine:
Expand Down
2 changes: 1 addition & 1 deletion server/hwapi/endpoints/certification/request_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CertificationStatusRequest(BaseModel):
architecture: str
board: BoardValidator
os: OSValidator
bios: BiosValidator
bios: BiosValidator | None = None
chassis: ChassisValidator | None = None
processor: ProcessorValidator
pci_peripherals: list[PCIPeripheralValidator] = []
Expand Down
63 changes: 39 additions & 24 deletions server/hwapi/endpoints/certification/response_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,30 @@ def build_related_certified_response(
db: Session,
machine: models.Machine,
board: models.Device,
bios: models.Bios,
bios: models.Bios | None,
releases: list[models.Release],
kernels: list[models.Kernel],
) -> RelatedCertifiedSystemExistsResponse:
architecture = repository.get_machine_architecture(db, machine.id)
bios_validator = (
data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
)
if bios
else None
)
return RelatedCertifiedSystemExistsResponse(
architecture=architecture,
board=data_validators.BoardValidator(
manufacturer=board.vendor.name,
product_name=board.name,
version=board.version,
),
bios=data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
),
bios=bios_validator,
available_releases=[
data_validators.OSValidator(
distributor="Ubuntu",
Expand All @@ -70,26 +75,31 @@ def build_certified_response(
db: Session,
machine: models.Machine,
board: models.Device,
bios: models.Bios,
bios: models.Bios | None,
releases: list[models.Release],
kernels: list[models.Kernel],
) -> CertifiedResponse:
architecture = repository.get_machine_architecture(db, machine.id)
releases, kernels = repository.get_releases_and_kernels_for_machine(db, machine.id)
bios_validator = (
data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
)
if bios
else None
)
return CertifiedResponse(
architecture=architecture,
board=data_validators.BoardValidator(
manufacturer=board.vendor.name,
product_name=board.name,
version=board.version,
),
bios=data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
),
bios=bios_validator,
available_releases=[
data_validators.OSValidator(
distributor="Ubuntu",
Expand All @@ -110,25 +120,30 @@ def build_certified_image_exists_response(
db: Session,
machine: models.Machine,
board: models.Device,
bios: models.Bios,
bios: models.Bios | None,
releases: list[models.Release],
kernels: list[models.Kernel],
) -> CertifiedImageExistsResponse:
architecture = repository.get_machine_architecture(db, machine.id)
bios_validator = (
data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
)
if bios
else None
)
return CertifiedImageExistsResponse(
architecture=architecture,
board=data_validators.BoardValidator(
manufacturer=board.vendor.name,
product_name=board.name,
version=board.version,
),
bios=data_validators.BiosValidator(
vendor=bios.vendor.name,
version=bios.version,
revision=bios.revision,
firmware_revision=bios.firmware_revision,
release_date=bios.release_date,
),
bios=bios_validator,
available_releases=[
data_validators.OSValidator(
distributor="Ubuntu",
Expand Down
6 changes: 3 additions & 3 deletions server/hwapi/endpoints/certification/response_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class CertifiedResponse(BaseModel):
status: Literal[CertificationStatus.CERTIFIED] = CertificationStatus.CERTIFIED
architecture: str
bios: BiosValidator
bios: BiosValidator | None
board: BoardValidator
chassis: ChassisValidator | None = None
available_releases: list[OSValidator]
Expand All @@ -57,7 +57,7 @@ class RelatedCertifiedSystemExistsResponse(BaseModel):
)
architecture: str
board: BoardValidator
bios: BiosValidator
bios: BiosValidator | None
chassis: ChassisValidator | None = None
gpu: list[GPUValidator] | None = None
audio: list[AudioValidator] | None = None
Expand All @@ -74,7 +74,7 @@ class CertifiedImageExistsResponse(BaseModel):
CertificationStatus.CERTIFIED_IMAGE_EXISTS
)
architecture: str
bios: BiosValidator
bios: BiosValidator | None
board: BoardValidator
chassis: ChassisValidator | None = None
available_releases: list[OSValidator]
Loading

0 comments on commit 4a27002

Please sign in to comment.