Skip to content

Commit 9d619b7

Browse files
committed
Allow to load data from git repo
1 parent 8b97edb commit 9d619b7

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

backend/infrahub/git/integrator.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
InfrahubPythonTransformConfig,
3030
InfrahubRepositoryConfig,
3131
)
32+
from infrahub_sdk.spec.object import ObjectFile
3233
from infrahub_sdk.template import Jinja2Template
3334
from infrahub_sdk.template.exceptions import JinjaTemplateError
3435
from infrahub_sdk.utils import compare_lists
@@ -54,6 +55,7 @@
5455
import types
5556

5657
from infrahub_sdk.checks import InfrahubCheck
58+
from infrahub_sdk.ctl.utils import YamlFileVar
5759
from infrahub_sdk.schema.repository import InfrahubRepositoryArtifactDefinitionConfig
5860
from infrahub_sdk.transforms import InfrahubTransform
5961

@@ -198,6 +200,7 @@ async def import_objects_from_files(
198200
await self.import_artifact_definitions(
199201
branch_name=infrahub_branch_name, commit=commit, config_file=config_file
200202
) # type: ignore[misc]
203+
await self.import_objects(branch_name=infrahub_branch_name, commit=commit, config_file=config_file) # type: ignore[misc]
201204

202205
except Exception as exc:
203206
sync_status = RepositorySyncStatus.ERROR_IMPORT
@@ -815,6 +818,37 @@ async def import_python_transforms(
815818
log.info(f"TransformPython {transform_name!r} not found locally, deleting")
816819
await transform_definition_in_graph[transform_name].delete()
817820

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+
818852
@task(name="check-definition-get", task_run_name="Get Check Definition", cache_policy=NONE) # type: ignore[arg-type]
819853
async def get_check_definition(
820854
self,

backend/tests/fixtures/repos/car-dealership/initial__main/.infrahub.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ queries:
9898
file_path: "generators/cartags.gql"
9999
- name: person_with_cars
100100
file_path: "templates/person_with_cars.gql"
101+
102+
objects:
103+
- name: "persons"
104+
file_path: "objects/persons.yml"
105+
- name: "manufacturers"
106+
file_path: "objects/manufacturers.yml"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: infrahub.app/v1
3+
kind: Object
4+
spec:
5+
kind: TestingManufacturer
6+
data:
7+
- name: Mercedes
8+
customers:
9+
- "Ethan Carter"
10+
- name: Ford
11+
customers:
12+
- "Olivia Bennett"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
apiVersion: infrahub.app/v1
3+
kind: Object
4+
spec:
5+
kind: TestingPerson
6+
data:
7+
- name: Ethan Carter
8+
height: 180
9+
- name: Olivia Bennett
10+
height: 170

backend/tests/integration/git/test_repository.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ async def test_create_repository(
7171
assert repository.operational_status.value == "online"
7272
assert check_definition.file_path.value == "checks/car_overview.py"
7373

74+
person_ethan = await NodeManager.get_one_by_default_filter(
75+
db=db, id="Ethan Carter", kind="TestingPerson", raise_on_error=True
76+
)
77+
assert person_ethan.name.value == "Ethan Carter"
78+
assert person_ethan.height.value == 180
79+
80+
manufacturer_mercedes = await NodeManager.get_one_by_default_filter(
81+
db=db, id="Mercedes", kind="TestingManufacturer", raise_on_error=True, prefetch_relationships=True
82+
)
83+
assert manufacturer_mercedes.name.value == "Mercedes"
84+
assert list((await manufacturer_mercedes.customers.get_peers(db=db)).values())[0].name.value == "Ethan Carter"
85+
86+
# TODO add a test with invalid yml file OR invalid order of objects in the yml file, and make sure the repository ends
87+
# up in error import state
88+
7489
@pytest.mark.parametrize(
7590
"stderr,expected_operational_status",
7691
[

python_sdk

0 commit comments

Comments
 (0)