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
36 changes: 11 additions & 25 deletions infrahub_sdk/ctl/check.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import importlib
import logging
import sys
from asyncio import run as aiorun
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Optional, Type

import typer
Expand All @@ -18,6 +16,7 @@
from ..ctl.exceptions import QueryNotFoundError
from ..ctl.repository import get_repository_config
from ..ctl.utils import catch_exception, execute_graphql_query
from ..exceptions import ModuleImportError
from ..schema import InfrahubCheckDefinitionConfig, InfrahubRepositoryConfig

app = typer.Typer()
Expand All @@ -27,12 +26,9 @@
@dataclass
class CheckModule:
name: str
module: ModuleType
check_class: Type[InfrahubCheck]
definition: InfrahubCheckDefinitionConfig

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


@app.callback()
def callback() -> None:
Expand Down Expand Up @@ -67,11 +63,7 @@

check_definitions = repository_config.check_definitions
if name:
check_definitions = [check for check in repository_config.check_definitions if check.name == name] # pylint: disable=not-an-iterable
if not check_definitions:
console.print(f"[red]Unable to find requested transform: {name}")
list_checks(repository_config=repository_config)
return
check_definitions = [repository_config.get_check_definition(name=name)]

Check warning on line 66 in infrahub_sdk/ctl/check.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/check.py#L66

Added line #L66 was not covered by tests

check_modules = get_modules(check_definitions=check_definitions)
aiorun(
Expand Down Expand Up @@ -99,7 +91,7 @@
output = "stdout" if format_json else None
log = logging.getLogger("infrahub")
passed = True
check_class = check_module.get_check()
check_class = check_module.check_class

Check warning on line 94 in infrahub_sdk/ctl/check.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/check.py#L94

Added line #L94 was not covered by tests
check = check_class(client=client, params=params, output=output, root_directory=path, branch=branch)
param_log = f" - {params}" if params else ""
try:
Expand Down Expand Up @@ -231,25 +223,19 @@


def get_modules(check_definitions: list[InfrahubCheckDefinitionConfig]) -> list[CheckModule]:
log = logging.getLogger("infrahub")
modules = []
for check_definition in check_definitions:
directory_name = str(check_definition.file_path.parent)
module_name = check_definition.file_path.stem

if directory_name not in sys.path:
sys.path.append(directory_name)
relative_path = str(check_definition.file_path.parent) if check_definition.file_path.parent != Path() else None

Check warning on line 230 in infrahub_sdk/ctl/check.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/check.py#L230

Added line #L230 was not covered by tests

try:
module = importlib.import_module(module_name)
except ModuleNotFoundError:
log.error(f"Unable to load {check_definition.file_path}")
continue

if check_definition.class_name not in dir(module):
log.error(f"{check_definition.class_name} class not found in {check_definition.file_path}")
continue
modules.append(CheckModule(name=module_name, module=module, definition=check_definition))
check_class = check_definition.load_class(import_root=str(Path.cwd()), relative_path=relative_path)
except ModuleImportError as exc:
console.print(f"[red]{exc.message}")
raise typer.Exit(1) from exc

Check warning on line 236 in infrahub_sdk/ctl/check.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/check.py#L233-L236

Added lines #L233 - L236 were not covered by tests

modules.append(CheckModule(name=module_name, check_class=check_class, definition=check_definition))

Check warning on line 238 in infrahub_sdk/ctl/check.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/check.py#L238

Added line #L238 was not covered by tests

return modules

Expand Down
14 changes: 14 additions & 0 deletions infrahub_sdk/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing_extensions import TypeAlias

from ._importer import import_module
from .checks import InfrahubCheck
from .exceptions import (
InvalidResponseError,
ModuleImportError,
Expand Down Expand Up @@ -89,6 +90,19 @@
)
class_name: str = Field(default="Check", description="The name of the check class to run.")

def load_class(self, import_root: Optional[str] = None, relative_path: Optional[str] = None) -> type[InfrahubCheck]:
module = import_module(module_path=self.file_path, import_root=import_root, relative_path=relative_path)

Check warning on line 94 in infrahub_sdk/schema.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema.py#L94

Added line #L94 was not covered by tests

if self.class_name not in dir(module):
raise ModuleImportError(message=f"The specified class {self.class_name} was not found within the module")

Check warning on line 97 in infrahub_sdk/schema.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema.py#L97

Added line #L97 was not covered by tests

check_class = getattr(module, self.class_name)

Check warning on line 99 in infrahub_sdk/schema.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema.py#L99

Added line #L99 was not covered by tests

if not issubclass(check_class, InfrahubCheck):
raise ModuleImportError(message=f"The specified class {self.class_name} is not an Infrahub Check")

Check warning on line 102 in infrahub_sdk/schema.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema.py#L102

Added line #L102 was not covered by tests

return check_class

Check warning on line 104 in infrahub_sdk/schema.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema.py#L104

Added line #L104 was not covered by tests


class InfrahubGeneratorDefinitionConfig(InfrahubRepositoryConfigElement):
model_config = ConfigDict(extra="forbid")
Expand Down