Skip to content

Commit cd83cde

Browse files
committed
Improve support for relationship & cleanup
1 parent 258b3ff commit cd83cde

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

infrahub_sdk/ctl/menu.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ async def load(
4040
files = load_yamlfile_from_disk_and_exit(paths=menus, file_type=MenuFile, console=console)
4141
client = await initialize_client()
4242

43-
default_kind = "CoreMenuItem"
44-
4543
for file in files:
4644
file.validate_content()
47-
if not file.spec.kind:
48-
file.spec.kind = default_kind
49-
5045
schema = await client.schema.get(kind=file.spec.kind, branch=branch)
5146

5247
for idx, item in enumerate(file.spec.data):
@@ -55,6 +50,6 @@ async def load(
5550
schema=schema,
5651
data=item,
5752
branch=branch,
58-
default_schema_kind=default_kind,
53+
default_schema_kind=file.spec.kind,
5954
context={"list_index": idx},
6055
)

infrahub_sdk/ctl/object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import logging
12
from pathlib import Path
23

34
import typer
45
from rich.console import Console
56

67
from infrahub_sdk.async_typer import AsyncTyper
78
from infrahub_sdk.ctl.client import initialize_client
8-
from infrahub_sdk.ctl.exceptions import FileNotValidError
99
from infrahub_sdk.ctl.utils import catch_exception, init_logging
1010
from infrahub_sdk.spec.object import ObjectFile
1111

@@ -35,13 +35,13 @@ async def load(
3535

3636
init_logging(debug=debug)
3737

38+
logging.getLogger("infrahub_sdk").setLevel(logging.INFO)
39+
3840
files = load_yamlfile_from_disk_and_exit(paths=paths, file_type=ObjectFile, console=console)
3941
client = await initialize_client()
4042

4143
for file in files:
4244
file.validate_content()
43-
if not file.spec.kind:
44-
raise FileNotValidError(name=str(file.location), message="kind must be specified.")
4545
schema = await client.schema.get(kind=file.spec.kind, branch=branch)
4646

4747
for item in file.spec.data:

infrahub_sdk/spec/menu.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77

88
class InfrahubMenuFileData(InfrahubObjectFileData):
9+
kind: str = "CoreMenuItem"
10+
911
@classmethod
1012
def enrich_node(cls, data: dict, context: dict) -> dict:
1113
if "kind" in data and "path" not in data:

infrahub_sdk/spec/object.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class InfrahubObjectFileData(BaseModel):
11-
kind: Optional[str] = None
11+
kind: str
1212
data: list[dict[str, Any]] = Field(default_factory=list)
1313

1414
@classmethod
@@ -35,21 +35,21 @@ async def create_node(
3535
remaining_rels = []
3636
for key, value in data.items():
3737
if key in schema.attribute_names:
38-
# NOTE we could validate the format of the data but the API will do it as well
3938
clean_data[key] = value
4039

4140
if key in schema.relationship_names:
4241
rel_schema = schema.get_relationship(name=key)
4342

44-
if not isinstance(value, dict) and "data" in value:
45-
raise ValueError(f"relationship {key} must be a dict with 'data'")
43+
if isinstance(value, dict) and "data" not in value:
44+
raise ValueError(f"Relationship {key} must be a dict with 'data'")
4645

47-
if rel_schema.cardinality == "one" or rel_schema.optional is False:
48-
raise ValueError(
49-
"Not supported yet, we need to have a way to define connect object before they exist"
50-
)
51-
# clean_data[key] = value[data]
52-
remaining_rels.append(key)
46+
# This is a simple implementation for now, need to revisit once we have the integration tests
47+
if isinstance(value, (list)):
48+
clean_data[key] = value
49+
elif rel_schema.cardinality == "one" and isinstance(value, str):
50+
clean_data[key] = [value]
51+
else:
52+
remaining_rels.append(key)
5353

5454
if context:
5555
clean_context = {

0 commit comments

Comments
 (0)