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 )
@@ -181,6 +182,9 @@ async def import_objects_from_files(
181182
182183 config_file = await self .get_repository_config (branch_name = infrahub_branch_name , commit = commit ) # type: ignore[misc]
183184 sync_status = RepositorySyncStatus .IN_SYNC if config_file else RepositorySyncStatus .ERROR_IMPORT
185+ if sync_status == RepositorySyncStatus .ERROR_IMPORT :
186+ raise ValueError ("Unable to import the repository here" )
187+
184188 error : Exception | None = None
185189
186190 try :
@@ -191,6 +195,19 @@ async def import_objects_from_files(
191195 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
192196 ) # type: ignore[misc]
193197
198+ await self .import_objects (
199+ branch_name = infrahub_branch_name ,
200+ commit = commit ,
201+ files_pathes = config_file .objects ,
202+ object_type = RepositoryObjects .OBJECT ,
203+ ) # type: ignore[misc]
204+ await self .import_objects (
205+ branch_name = infrahub_branch_name ,
206+ commit = commit ,
207+ files_pathes = config_file .menus ,
208+ object_type = RepositoryObjects .MENU ,
209+ ) # type: ignore[misc]
210+
194211 await self .import_all_python_files ( # type: ignore[call-overload]
195212 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
196213 ) # type: ignore[misc]
@@ -200,11 +217,15 @@ async def import_objects_from_files(
200217 await self .import_artifact_definitions (
201218 branch_name = infrahub_branch_name , commit = commit , config_file = config_file
202219 ) # type: ignore[misc]
203- await self .import_objects (branch_name = infrahub_branch_name , commit = commit , config_file = config_file ) # type: ignore[misc]
220+
221+ # TODO: import menu, check def load() in menu.^y + add a deidcated group for menu, work as for objects
222+ # "CoreRepositoryMenuGroup"
204223
205224 except Exception as exc :
206225 sync_status = RepositorySyncStatus .ERROR_IMPORT
207226 error = exc
227+ log = get_run_logger ()
228+ log .error (f"Error importing the repository { self .name } : { exc } " )
208229
209230 await self ._update_sync_status (branch_name = infrahub_branch_name , status = sync_status )
210231
@@ -831,23 +852,65 @@ async def _load_objects(
831852 self ,
832853 paths : list [Path ],
833854 branch : str ,
855+ file_type : type [InfrahubFile ],
834856 ) -> None :
835857 """Load one or multiple objects files into Infrahub."""
836858
837- files = await self ._load_yamlfile_from_disk (paths = paths , file_type = ObjectFile )
859+ log = get_run_logger ()
860+ files = await self ._load_yamlfile_from_disk (paths = paths , file_type = file_type )
861+ log .info (f"Loaded files { files = } " )
862+
838863 for file in files :
839- await file .validate_format (client = self .client , branch = branch )
864+ await file .validate_format (client = self .get_client (), branch = branch )
865+ schema = await self .get_client ().schema .get (kind = file .spec .kind , branch = branch )
866+ if not schema .human_friendly_id and not schema .default_filter :
867+ raise ValueError (
868+ f"Schemas of objects or menus defined within { file .file_path } "
869+ "should have a `human_friendly_id` defined to avoid creating duplicated objects."
870+ )
871+
840872 for file in files :
841- await file .process (client = self .client , branch = branch )
873+ # TODO: client.log that is then used should log into infrahub.tasks
874+ # TODO: do not create if no hfid/default_filter
875+ await file .process (client = self .get_client (), branch = branch )
842876
843877 @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 :
878+ async def import_objects (
879+ self , branch_name : str , commit : str , files_pathes : list [Path ], object_type : RepositoryObjects
880+ ) -> None :
881+ log = get_run_logger ()
882+ log .info (f"Importing { object_type .value } from { files_pathes = } " )
883+
845884 branch_wt = self .get_worktree (identifier = commit or branch_name )
846885
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 )
886+ file_pathes = [branch_wt .directory / file_path for file_path in files_pathes ]
887+
888+ log .info ("Before getting the repository object" )
889+ # do not clone for now, assume only one import at a time
890+ if self .is_read_only :
891+ sdk_repo_obj = await self .get_client ().get (
892+ kind = InfrahubKind .READONLYREPOSITORY , id = str (self .id ), raise_when_missing = True
893+ )
894+ else :
895+ sdk_repo_obj = await self .get_client ().get (
896+ kind = InfrahubKind .REPOSITORY , id = str (self .id ), raise_when_missing = True
897+ )
898+
899+ log .info (f"After getting the repository object { sdk_repo_obj .id } " )
849900
850- # TODO attach to a group?
901+ async with self .get_client ().start_tracking (
902+ identifier = f"group-repo-{ object_type .value } -{ self .id } " ,
903+ delete_unused_nodes = True ,
904+ group_type = "CoreRepositoryGroup" ,
905+ group_params = {"content" : object_type .value , "repository" : sdk_repo_obj },
906+ ):
907+ log .info ("Started to track" )
908+ file_type = ObjectFile if object_type == RepositoryObjects .OBJECT else MenuFile
909+ await self ._load_objects (
910+ paths = file_pathes ,
911+ branch = branch_name ,
912+ file_type = file_type ,
913+ )
851914
852915 @task (name = "check-definition-get" , task_run_name = "Get Check Definition" , cache_policy = NONE ) # type: ignore[arg-type]
853916 async def get_check_definition (
0 commit comments