Skip to content

Commit c66020b

Browse files
committed
Improve support for order weight in menu
1 parent f11ccdc commit c66020b

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

infrahub_sdk/ctl/cli_commands.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
from infrahub_sdk.ctl.repository import app as repository_app
2929
from infrahub_sdk.ctl.repository import get_repository_config
3030
from infrahub_sdk.ctl.schema import app as schema_app
31-
from infrahub_sdk.ctl.schema import load_schemas_from_disk_and_exit
3231
from infrahub_sdk.ctl.transform import list_transforms
33-
from infrahub_sdk.ctl.utils import catch_exception, execute_graphql_query, parse_cli_vars
32+
from infrahub_sdk.ctl.utils import (
33+
catch_exception,
34+
execute_graphql_query,
35+
load_yamlfile_from_disk_and_exit,
36+
parse_cli_vars,
37+
)
3438
from infrahub_sdk.ctl.validate import app as validate_app
3539
from infrahub_sdk.exceptions import GraphQLError, InfrahubTransformNotFoundError
3640
from infrahub_sdk.jinja2 import identify_faulty_jinja_code
@@ -41,6 +45,7 @@
4145
)
4246
from infrahub_sdk.transforms import get_transform_class_instance
4347
from infrahub_sdk.utils import get_branch, write_to_file
48+
from infrahub_sdk.yaml import SchemaFile
4449

4550
from .exporter import dump
4651
from .importer import load
@@ -343,7 +348,7 @@ def protocols( # noqa: PLR0915
343348
schema: dict[str, MainSchemaTypes] = {}
344349

345350
if schemas:
346-
schemas_data = load_schemas_from_disk_and_exit(schemas=schemas)
351+
schemas_data = load_yamlfile_from_disk_and_exit(paths=schemas, file_type=SchemaFile, console=console)
347352

348353
for data in schemas_data:
349354
data.load_content()

infrahub_sdk/ctl/menu.py

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

34
import typer
@@ -34,6 +35,8 @@ async def load(
3435

3536
init_logging(debug=debug)
3637

38+
logging.getLogger("infrahub_sdk").setLevel(logging.INFO)
39+
3740
files = load_yamlfile_from_disk_and_exit(paths=menus, file_type=MenuFile, console=console)
3841
client = await initialize_client()
3942

@@ -46,7 +49,12 @@ async def load(
4649

4750
schema = await client.schema.get(kind=file.spec.kind, branch=branch)
4851

49-
for item in file.spec.data:
52+
for idx, item in enumerate(file.spec.data):
5053
await file.spec.create_node(
51-
client=client, schema=schema, data=item, branch=branch, default_schema_kind=default_kind
54+
client=client,
55+
schema=schema,
56+
data=item,
57+
branch=branch,
58+
default_schema_kind=default_kind,
59+
context={"list_index": idx},
5260
)

infrahub_sdk/spec/menu.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
class InfrahubMenuFileData(InfrahubObjectFileData):
99
@classmethod
10-
def enrich_node(cls, data: dict) -> dict:
10+
def enrich_node(cls, data: dict, context: dict) -> dict:
1111
if "kind" in data and "path" not in data:
1212
data["path"] = "/objects/" + data["kind"]
13+
14+
if "list_index" in context and "order_weight" not in data:
15+
data["order_weight"] = (context["list_index"] + 1) * 1000
16+
1317
return data
1418

1519

infrahub_sdk/spec/object.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class InfrahubObjectFileData(BaseModel):
1212
data: list[dict[str, Any]] = Field(default_factory=list)
1313

1414
@classmethod
15-
def enrich_node(cls, data: dict) -> dict:
15+
def enrich_node(cls, data: dict, context: dict) -> dict:
1616
return data
1717

1818
@classmethod
@@ -52,13 +52,19 @@ async def create_node(
5252
remaining_rels.append(key)
5353

5454
if context:
55-
clean_data.update(context)
55+
clean_context = {
56+
ckey: cvalue
57+
for ckey, cvalue in context.items()
58+
if ckey in schema.relationship_names + schema.attribute_names
59+
}
60+
clean_data.update(clean_context)
5661

57-
clean_data = cls.enrich_node(data=clean_data)
62+
clean_data = cls.enrich_node(data=clean_data, context=context or {})
5863

5964
node = await client.create(kind=schema.kind, branch=branch, data=clean_data)
6065
await node.save(allow_upsert=True)
61-
print(f"Created Node: {node.get_human_friendly_id_as_string()}")
66+
display_label = node.get_human_friendly_id_as_string() or f"{node.get_kind()} : {node.id}"
67+
client.log.info(f"Node: {display_label}")
6268

6369
for rel in remaining_rels:
6470
# identify what is the name of the relationship on the other side
@@ -90,7 +96,8 @@ async def create_node(
9096
)
9197

9298
elif rel_schema.cardinality == "many" and isinstance(rel_data, list):
93-
for peer_data in rel_data:
99+
for idx, peer_data in enumerate(rel_data):
100+
context["list_index"] = idx
94101
await cls.create_node(
95102
client=client,
96103
schema=peer_schema,

0 commit comments

Comments
 (0)