|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +from prefect import flow |
| 4 | + |
| 5 | +from infrahub.artifacts.models import CheckArtifactCreate |
| 6 | +from infrahub.core.constants import InfrahubKind, ValidatorConclusion |
| 7 | +from infrahub.core.timestamp import Timestamp |
| 8 | +from infrahub.git import InfrahubReadOnlyRepository, InfrahubRepository |
| 9 | +from infrahub.services import InfrahubServices |
| 10 | +from infrahub.tasks.artifact import define_artifact |
| 11 | +from infrahub.workflows.utils import add_tags |
| 12 | + |
| 13 | + |
| 14 | +@flow(name="git-repository-check-artifact-create", flow_run_name="Check artifact creation") |
| 15 | +async def create(model: CheckArtifactCreate, service: InfrahubServices) -> ValidatorConclusion: |
| 16 | + await add_tags(branches=[model.branch_name], nodes=[model.target_id]) |
| 17 | + validator = await service.client.get(kind=InfrahubKind.ARTIFACTVALIDATOR, id=model.validator_id, include=["checks"]) |
| 18 | + |
| 19 | + repo: InfrahubReadOnlyRepository | InfrahubRepository |
| 20 | + if InfrahubKind.READONLYREPOSITORY: |
| 21 | + repo = await InfrahubReadOnlyRepository.init( |
| 22 | + id=model.repository_id, |
| 23 | + name=model.repository_name, |
| 24 | + client=service.client, |
| 25 | + service=service, |
| 26 | + ) |
| 27 | + else: |
| 28 | + repo = await InfrahubRepository.init( |
| 29 | + id=model.repository_id, |
| 30 | + name=model.repository_name, |
| 31 | + client=service.client, |
| 32 | + service=service, |
| 33 | + ) |
| 34 | + |
| 35 | + artifact = await define_artifact(model=model, service=service) |
| 36 | + |
| 37 | + severity = "info" |
| 38 | + artifact_result: dict[str, Union[str, bool, None]] = { |
| 39 | + "changed": None, |
| 40 | + "checksum": None, |
| 41 | + "artifact_id": None, |
| 42 | + "storage_id": None, |
| 43 | + } |
| 44 | + check_message = "Failed to render artifact" |
| 45 | + |
| 46 | + try: |
| 47 | + result = await repo.render_artifact(artifact=artifact, message=model) |
| 48 | + artifact_result["changed"] = result.changed |
| 49 | + artifact_result["checksum"] = result.checksum |
| 50 | + artifact_result["artifact_id"] = result.artifact_id |
| 51 | + artifact_result["storage_id"] = result.storage_id |
| 52 | + check_message = "Artifact rendered successfully" |
| 53 | + conclusion = ValidatorConclusion.SUCCESS |
| 54 | + |
| 55 | + except Exception as exc: |
| 56 | + artifact.status.value = "Error" |
| 57 | + await artifact.save() |
| 58 | + severity = "critical" |
| 59 | + conclusion = ValidatorConclusion.FAILURE |
| 60 | + check_message += f": {str(exc)}" |
| 61 | + |
| 62 | + check = None |
| 63 | + check_name = f"{model.artifact_name}: {model.target_name}" |
| 64 | + existing_check = await service.client.filters( |
| 65 | + kind=InfrahubKind.ARTIFACTCHECK, validator__ids=validator.id, name__value=check_name |
| 66 | + ) |
| 67 | + if existing_check: |
| 68 | + check = existing_check[0] |
| 69 | + |
| 70 | + if check: |
| 71 | + check.created_at.value = Timestamp().to_string() |
| 72 | + check.conclusion.value = conclusion.value |
| 73 | + check.severity.value = severity |
| 74 | + check.changed.value = artifact_result["changed"] |
| 75 | + check.checksum.value = artifact_result["checksum"] |
| 76 | + check.artifact_id.value = artifact_result["artifact_id"] |
| 77 | + check.storage_id.value = artifact_result["storage_id"] |
| 78 | + await check.save() |
| 79 | + else: |
| 80 | + check = await service.client.create( |
| 81 | + kind=InfrahubKind.ARTIFACTCHECK, |
| 82 | + data={ |
| 83 | + "name": check_name, |
| 84 | + "origin": model.repository_id, |
| 85 | + "kind": "ArtifactDefinition", |
| 86 | + "validator": model.validator_id, |
| 87 | + "created_at": Timestamp().to_string(), |
| 88 | + "message": check_message, |
| 89 | + "conclusion": conclusion.value, |
| 90 | + "severity": severity, |
| 91 | + "changed": artifact_result["changed"], |
| 92 | + "checksum": artifact_result["checksum"], |
| 93 | + "artifact_id": artifact_result["artifact_id"], |
| 94 | + "storage_id": artifact_result["storage_id"], |
| 95 | + }, |
| 96 | + ) |
| 97 | + await check.save() |
| 98 | + |
| 99 | + return conclusion |
0 commit comments