Benjamin Shai | 80317fc | 2023-08-22 19:33:41 +0000 | [diff] [blame^] | 1 | # Copyright 2023 The ChromiumOS Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """DLC API Service.""" |
| 6 | |
| 7 | from typing import List, Optional, TYPE_CHECKING |
| 8 | |
| 9 | from chromite.api import controller |
| 10 | from chromite.api import faux |
| 11 | from chromite.api import validate |
| 12 | from chromite.api.controller import controller_util |
| 13 | from chromite.service import image |
| 14 | |
| 15 | |
| 16 | if TYPE_CHECKING: |
| 17 | from chromite.api import api_config |
| 18 | from chromite.api.gen.chromite.api import dlc_pb2 |
| 19 | |
| 20 | |
| 21 | def _GenerateDlcArtifactsResponse(_input_proto, output_proto, _config): |
| 22 | """Set output_proto success field on a successful SignerTest response.""" |
| 23 | artifact = output_proto.dlc_artifacts.add() |
| 24 | artifact.image_hash = ( |
| 25 | "88d54cb6b5bba15a71ffda3ca75446eb453bf7fe393e3595d3bc52beb3b61711" |
| 26 | ) |
| 27 | artifact.image_name = "dlc.img" |
| 28 | artifact.gs_uri_path = "gs://some/uri/prefix/for/dlc-1" |
| 29 | return controller.RETURN_CODE_SUCCESS |
| 30 | |
| 31 | |
| 32 | @faux.success(_GenerateDlcArtifactsResponse) |
| 33 | @faux.empty_error |
| 34 | @validate.require("sysroot") |
| 35 | @validate.validation_complete |
| 36 | def GenerateDlcArtifactsList( |
| 37 | input_proto: "dlc_pb2.GenerateDlcArtifactsListRequest", |
| 38 | output_proto: "dlc_pb2.GenerateDlcArtifactsListResponse", |
| 39 | _config: "api_config.ApiConfig", |
| 40 | ) -> Optional[int]: |
| 41 | """Generate DLC Artifacts List. |
| 42 | |
| 43 | Args: |
| 44 | input_proto: the input message. |
| 45 | output_proto: the output message. |
| 46 | config: the API call config. |
| 47 | |
| 48 | Returns: |
| 49 | Return code (from __init__.py). |
| 50 | """ |
| 51 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
| 52 | |
| 53 | dlc_artifacts = image.generate_dlc_artifacts_metadata_list(sysroot.path) |
| 54 | _parse_dlc_artifacts_to_response(output_proto, dlc_artifacts) |
| 55 | |
| 56 | return controller.RETURN_CODE_SUCCESS |
| 57 | |
| 58 | |
| 59 | def _parse_dlc_artifacts_to_response( |
| 60 | output: "dlc_pb2.GenerateDlcArtifactsListResponse", |
| 61 | dlc_artifacts: List[image.DlcArtifactsMetadata], |
| 62 | ): |
| 63 | """Parse the DLC artifacts into the output proto. |
| 64 | |
| 65 | Args: |
| 66 | output: the output message. |
| 67 | dlc_artifacts: the list of DLC artifacts. |
| 68 | """ |
| 69 | for dlc_artifact in dlc_artifacts: |
| 70 | artifact = output.dlc_artifacts.add() |
| 71 | artifact.image_hash = dlc_artifact.image_hash |
| 72 | artifact.image_name = dlc_artifact.image_name |
| 73 | artifact.gs_uri_path = dlc_artifact.uri_path |