|
29 | 29 | InfrahubPythonTransformConfig, |
30 | 30 | InfrahubRepositoryConfig, |
31 | 31 | ) |
| 32 | +from infrahub_sdk.spec.object import ObjectFile |
32 | 33 | from infrahub_sdk.template import Jinja2Template |
33 | 34 | from infrahub_sdk.template.exceptions import JinjaTemplateError |
34 | 35 | from infrahub_sdk.utils import compare_lists |
|
54 | 55 | import types |
55 | 56 |
|
56 | 57 | from infrahub_sdk.checks import InfrahubCheck |
| 58 | + from infrahub_sdk.ctl.utils import YamlFileVar |
57 | 59 | from infrahub_sdk.schema.repository import InfrahubRepositoryArtifactDefinitionConfig |
58 | 60 | from infrahub_sdk.transforms import InfrahubTransform |
59 | 61 |
|
@@ -198,6 +200,7 @@ async def import_objects_from_files( |
198 | 200 | await self.import_artifact_definitions( |
199 | 201 | branch_name=infrahub_branch_name, commit=commit, config_file=config_file |
200 | 202 | ) # type: ignore[misc] |
| 203 | + await self.import_objects(branch_name=infrahub_branch_name, commit=commit, config_file=config_file) # type: ignore[misc] |
201 | 204 |
|
202 | 205 | except Exception as exc: |
203 | 206 | sync_status = RepositorySyncStatus.ERROR_IMPORT |
@@ -815,6 +818,37 @@ async def import_python_transforms( |
815 | 818 | log.info(f"TransformPython {transform_name!r} not found locally, deleting") |
816 | 819 | await transform_definition_in_graph[transform_name].delete() |
817 | 820 |
|
| 821 | + async def _load_yamlfile_from_disk(self, paths: list[Path], file_type: type[YamlFileVar]) -> list[YamlFileVar]: |
| 822 | + data_files = file_type.load_from_disk(paths=paths) |
| 823 | + |
| 824 | + for data_file in data_files: |
| 825 | + if not data_file.valid or not data_file.content: |
| 826 | + raise ValueError(f"{data_file.error_message} ({data_file.location})") |
| 827 | + |
| 828 | + return data_files |
| 829 | + |
| 830 | + async def _load_objects( |
| 831 | + self, |
| 832 | + paths: list[Path], |
| 833 | + branch: str, |
| 834 | + ) -> None: |
| 835 | + """Load one or multiple objects files into Infrahub.""" |
| 836 | + |
| 837 | + files = await self._load_yamlfile_from_disk(paths=paths, file_type=ObjectFile) |
| 838 | + for file in files: |
| 839 | + await file.validate_format(client=self.client, branch=branch) |
| 840 | + for file in files: |
| 841 | + await file.process(client=self.client, branch=branch) |
| 842 | + |
| 843 | + @task(name="import-objects", task_run_name="Import Objects", cache_policy=NONE) # type: ignore[arg-type] |
| 844 | + async def import_objects(self, branch_name: str, commit: str, config_file: InfrahubRepositoryConfig) -> None: |
| 845 | + branch_wt = self.get_worktree(identifier=commit or branch_name) |
| 846 | + |
| 847 | + file_pathes = [branch_wt.directory / obj.file_path for obj in config_file.objects] |
| 848 | + await self._load_objects(paths=file_pathes, branch=branch_name) |
| 849 | + |
| 850 | + # TODO attach to a group? |
| 851 | + |
818 | 852 | @task(name="check-definition-get", task_run_name="Get Check Definition", cache_policy=NONE) # type: ignore[arg-type] |
819 | 853 | async def get_check_definition( |
820 | 854 | self, |
|
0 commit comments