Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/+89d1d0b7.deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Marked InfrahubCheck.init() as deprecated and scheduled to be removed in Infrahub SDK 2.0.0
21 changes: 15 additions & 6 deletions infrahub_sdk/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
import asyncio
import importlib
import os
import warnings
from abc import abstractmethod
from typing import TYPE_CHECKING, Any, Optional

import ujson
from git.repo import Repo
from pydantic import BaseModel, Field

from . import InfrahubClient
from .exceptions import InfrahubCheckNotFoundError, UninitializedError

if TYPE_CHECKING:
from pathlib import Path

from . import InfrahubClient
from .schema import InfrahubCheckDefinitionConfig

INFRAHUB_CHECK_VARIABLE_TO_IMPORT = "INFRAHUB_CHECKS"

_client_class = "InfrahubClient"


class InfrahubCheckInitializer(BaseModel):
"""Information about the originator of the check."""
Expand Down Expand Up @@ -81,11 +84,17 @@ def client(self, value: InfrahubClient) -> None:
@classmethod
async def init(cls, client: Optional[InfrahubClient] = None, *args: Any, **kwargs: Any) -> InfrahubCheck:
"""Async init method, If an existing InfrahubClient client hasn't been provided, one will be created automatically."""

instance = cls(*args, **kwargs)
instance.client = client or InfrahubClient()

return instance
warnings.warn(
"InfrahubCheck.init has been deprecated and will be removed in the version in Infrahub SDK 2.0.0",
DeprecationWarning,
stacklevel=1,
)
if not client:
client_module = importlib.import_module("infrahub_sdk.client")
client_class = getattr(client_module, _client_class)
client = client_class()
kwargs["client"] = client
return cls(*args, **kwargs)

@property
def errors(self) -> list[dict[str, Any]]:
Expand Down
6 changes: 3 additions & 3 deletions infrahub_sdk/ctl/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Optional
from typing import Optional, Type

import typer
from rich.console import Console
Expand All @@ -30,7 +30,7 @@ class CheckModule:
module: ModuleType
definition: InfrahubCheckDefinitionConfig

def get_check(self) -> InfrahubCheck:
def get_check(self) -> Type[InfrahubCheck]:
return getattr(self.module, self.definition.class_name)


Expand Down Expand Up @@ -100,7 +100,7 @@ async def run_check(
log = logging.getLogger("infrahub")
passed = True
check_class = check_module.get_check()
check = await check_class.init(client=client, params=params, output=output, root_directory=path, branch=branch)
check = check_class(client=client, params=params, output=output, root_directory=path, branch=branch)
param_log = f" - {params}" if params else ""
try:
data = execute_graphql_query(
Expand Down