Skip to content

Commit

Permalink
Update schema for Python checks in .infrahub.yml
Browse files Browse the repository at this point in the history
Add parameters and targets. If targets is empty this will be a global
check, otherwise it will target a specific group.

Also introduces InfrahubCheckInitializer, this is to provide information
to the check that user created code could act upon. For now it contains
information about which proposed change (if any) triggered the check to
run. The reason for this is to support that use case with checking
something within an external system such as ServiceNow to see if a given
proposed change has been approved. We don't yet inject the proposed
change into the check that will come in a follow up PR.

Related to #1049, will move out User checks from the
RepositoryValidators as a next step and also add the ability to target
groups of devices for these checks.
  • Loading branch information
ogenstad committed Nov 20, 2023
1 parent 8378897 commit 0234ee2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 21 additions & 1 deletion python_sdk/infrahub_sdk/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,38 @@

from infrahub_sdk import InfrahubClient

try:
from pydantic import v1 as pydantic # type: ignore[attr-defined]
except ImportError:
import pydantic # type: ignore[no-redef]

INFRAHUB_CHECK_VARIABLE_TO_IMPORT = "INFRAHUB_CHECKS"


class InfrahubCheckInitializer(pydantic.BaseModel):
"""Information about the originator of the check."""

proposed_change_id: str = pydantic.Field(
"", description="If available the ID of the proposed change that requested the check"
)


class InfrahubCheck:
name: Optional[str] = None
query: str = ""
timeout: int = 10
rebase: bool = True

def __init__(self, branch: str = "", root_directory: str = "", output: Optional[str] = None):
def __init__(
self,
branch: str = "",
root_directory: str = "",
output: Optional[str] = None,
initializer: Optional[InfrahubCheckInitializer] = None,
):
self.data: Dict = {}
self.git: Optional[Repo] = None
self.initializer = initializer or InfrahubCheckInitializer()

self.logs: List[Dict[str, Any]] = []
self.passed = False
Expand Down
6 changes: 6 additions & 0 deletions python_sdk/infrahub_sdk/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def payload(self) -> Dict[str, str]:

class InfrahubCheckDefinitionConfig(pydantic.BaseModel):
file_path: Path = pydantic.Field(..., description="The file within the repo with the check code.")
parameters: Dict[str, Any] = pydantic.Field(
default_factory=dict, description="The input parameters required to run this check"
)
targets: Optional[str] = pydantic.Field(
default=None, description="The group to target when running this check, leave blank for global checks"
)


class InfrahubPythonTransformConfig(pydantic.BaseModel):
Expand Down

0 comments on commit 0234ee2

Please sign in to comment.