2929 InfrahubPythonTransformConfig ,
3030 InfrahubRepositoryConfig ,
3131)
32+ from infrahub_sdk .spec .menu import MenuFile
3233from infrahub_sdk .spec .object import ObjectFile
3334from infrahub_sdk .template import Jinja2Template
3435from infrahub_sdk .template .exceptions import JinjaTemplateError
3536from infrahub_sdk .utils import compare_lists
36- from infrahub_sdk .yaml import SchemaFile
37+ from infrahub_sdk .yaml import InfrahubFile , SchemaFile
3738from prefect import flow , task
3839from prefect .cache_policies import NONE
3940from prefect .logging import get_run_logger
4041from pydantic import BaseModel , Field
4142from pydantic import ValidationError as PydanticValidationError
4243from typing_extensions import Self
4344
44- from infrahub .core .constants import ArtifactStatus , ContentType , InfrahubKind , RepositorySyncStatus
45+ from infrahub .core .constants import ArtifactStatus , ContentType , InfrahubKind , RepositoryObjects , RepositorySyncStatus
4546from infrahub .core .registry import registry
4647from infrahub .events .artifact_action import ArtifactCreatedEvent , ArtifactUpdatedEvent
4748from infrahub .events .models import EventMeta
@@ -161,7 +162,7 @@ async def init(cls, service: InfrahubServices, commit: str | None = None, **kwar
161162 async def ensure_location_is_defined (self ) -> None :
162163 if self .location :
163164 return
164- client = self .get_client ()
165+ client = self .sdk
165166 repo = await client .get (
166167 kind = CoreGenericRepository , name__value = self .name , exclude = ["tags" , "credential" ], raise_when_missing = True
167168 )
@@ -179,8 +180,15 @@ async def import_objects_from_files(
179180 self .create_commit_worktree (commit )
180181 await self ._update_sync_status (branch_name = infrahub_branch_name , status = RepositorySyncStatus .SYNCING )
181182
183+
184+ print (f"Before reading config { InfrahubRepositoryConfig .model_fields ["objects" ]= } " )
185+ print (f"Before reading config { InfrahubRepositoryConfig .model_fields ["menus" ]= } " )
186+
182187 config_file = await self .get_repository_config (branch_name = infrahub_branch_name , commit = commit ) # type: ignore[misc]
183188 sync_status = RepositorySyncStatus .IN_SYNC if config_file else RepositorySyncStatus .ERROR_IMPORT
189+ if sync_status == RepositorySyncStatus .ERROR_IMPORT :
190+ raise ValueError ("Unable to import the repository here" )
191+
184192 error : Exception | None = None
185193
186194 try :
@@ -191,6 +199,19 @@ async def import_objects_from_files(
191199 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
192200 ) # type: ignore[misc]
193201
202+ await self .import_objects (
203+ branch_name = infrahub_branch_name ,
204+ commit = commit ,
205+ files_pathes = [obj_config .file_path for obj_config in config_file .objects ],
206+ object_type = RepositoryObjects .OBJECTS ,
207+ ) # type: ignore[misc]
208+ await self .import_objects (
209+ branch_name = infrahub_branch_name ,
210+ commit = commit ,
211+ files_pathes = [menu_config .file_path for menu_config in config_file .menus ],
212+ object_type = RepositoryObjects .MENUS ,
213+ ) # type: ignore[misc]
214+
194215 await self .import_all_python_files ( # type: ignore[call-overload]
195216 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
196217 ) # type: ignore[misc]
@@ -200,11 +221,15 @@ async def import_objects_from_files(
200221 await self .import_artifact_definitions (
201222 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
202223 ) # type: ignore[misc]
203- await self .import_objects (branch_name = infrahub_branch_name , commit = commit , config_file = config_file ) # type: ignore[misc]
224+
225+ # TODO: import menu, check def load() in menu.^y + add a deidcated group for menu, work as for objects
226+ # "CoreRepositoryMenuGroup"
204227
205228 except Exception as exc :
206229 sync_status = RepositorySyncStatus .ERROR_IMPORT
207230 error = exc
231+ log = get_run_logger ()
232+ log .error (f"Error importing the repository { self .name } : { exc } " )
208233
209234 await self ._update_sync_status (branch_name = infrahub_branch_name , status = sync_status )
210235
@@ -831,23 +856,66 @@ async def _load_objects(
831856 self ,
832857 paths : list [Path ],
833858 branch : str ,
859+ file_type : type [InfrahubFile ],
834860 ) -> None :
835861 """Load one or multiple objects files into Infrahub."""
836862
837- files = await self ._load_yamlfile_from_disk (paths = paths , file_type = ObjectFile )
863+ log = get_run_logger ()
864+ files = await self ._load_yamlfile_from_disk (paths = paths , file_type = file_type )
865+ log .info (f"Loaded files { files = } " )
866+
838867 for file in files :
839- await file .validate_format (client = self .client , branch = branch )
868+ await file .validate_format (client = self .get_client (), branch = branch )
869+ schema = await self .get_client ().schema .get (kind = file .spec .kind , branch = branch )
870+ if not schema .human_friendly_id and not schema .default_filter :
871+ raise ValueError (
872+ f"Schemas of objects or menus defined within { file .file_path } "
873+ "should have a `human_friendly_id` defined to avoid creating duplicated objects."
874+ )
875+
840876 for file in files :
841- await file .process (client = self .client , branch = branch )
877+ # TODO: client.log that is then used should log into infrahub.tasks
878+ # TODO: do not create if no hfid/default_filter
879+ await file .process (client = self .get_client (), branch = branch )
880+
842881
843882 @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 :
883+ async def import_objects (
884+ self , branch_name : str , commit : str , files_pathes : list [Path ], object_type : RepositoryObjects
885+ ) -> None :
886+ log = get_run_logger ()
887+ log .info (f"Importing { object_type .value } from { files_pathes = } " )
888+
845889 branch_wt = self .get_worktree (identifier = commit or branch_name )
846890
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 )
891+ file_pathes = [branch_wt .directory / file_path for file_path in files_pathes ]
849892
850- # TODO attach to a group?
893+ log .info ("Before getting the repository object" )
894+ # do not clone for now, assume only one import at a time
895+ if self .is_read_only :
896+ sdk_repo_obj = await self .get_client ().get (
897+ kind = InfrahubKind .READONLYREPOSITORY , id = str (self .id ), raise_when_missing = True
898+ )
899+ else :
900+ sdk_repo_obj = await self .get_client ().get (
901+ kind = InfrahubKind .REPOSITORY , id = str (self .id ), raise_when_missing = True
902+ )
903+
904+ log .info (f"After getting the repository object { sdk_repo_obj .id } " )
905+
906+ async with self .get_client ().start_tracking (
907+ identifier = f"group-repo-{ object_type .value } -{ self .id } " ,
908+ delete_unused_nodes = True ,
909+ group_type = "CoreRepositoryGroup" ,
910+ group_params = {"content" : object_type .value , "repository" : sdk_repo_obj },
911+ ):
912+ log .info ("Started to track" )
913+ file_type = ObjectFile if object_type == RepositoryObjects .OBJECTS else MenuFile
914+ await self ._load_objects (
915+ paths = file_pathes ,
916+ branch = branch_name ,
917+ file_type = file_type ,
918+ )
851919
852920 @task (name = "check-definition-get" , task_run_name = "Get Check Definition" , cache_policy = NONE ) # type: ignore[arg-type]
853921 async def get_check_definition (
0 commit comments