Skip to content

Commit c9cc008

Browse files
committed
Allow useof HFID to create a related node on generic relationship
1 parent 48ffeba commit c9cc008

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

infrahub_sdk/node.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def __init__(self, branch: str, schema: RelationshipSchemaAPI, data: Any | dict,
187187
if node_data:
188188
self._id = node_data.get("id", None)
189189
self._hfid = node_data.get("hfid", None)
190+
self._kind = node_data.get("kind", None)
190191
self._display_label = node_data.get("display_label", None)
191192
self._typename = node_data.get("__typename", None)
192193

@@ -255,6 +256,9 @@ def _generate_input_data(self, allocate_from_pool: bool = False) -> dict[str, An
255256
data["id"] = self.id
256257
elif self.hfid is not None:
257258
data["hfid"] = self.hfid
259+
if self._kind is not None:
260+
# We might need `kind` for https://github.com/opsmill/infrahub/issues/4649
261+
data["kind"] = self._kind
258262

259263
for prop_name in self._properties:
260264
if getattr(self, prop_name) is not None:

infrahub_sdk/testing/schemas/animal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def schema_cat(self) -> NodeSchema:
8080
namespace=NAMESPACE,
8181
include_in_menu=True,
8282
inherit_from=[TESTING_ANIMAL],
83+
# Different hfid than Animal one, for testing https://github.com/opsmill/infrahub-sdk-python/issues/277
84+
human_friendly_id=["owner__name__value", "name__value", "color__value"],
8385
display_labels=["name__value", "breed__value", "color__value"],
8486
order_by=["name__value"],
8587
attributes=[
@@ -108,6 +110,14 @@ def schema_person(self) -> NodeSchema:
108110
identifier="person__animal",
109111
cardinality="many",
110112
direction=RelationshipDirection.INBOUND,
113+
max_count=10,
114+
),
115+
Rel(
116+
name="favorite_animal",
117+
peer=TESTING_ANIMAL,
118+
identifier="favorite_animal",
119+
cardinality="one",
120+
direction=RelationshipDirection.INBOUND,
111121
),
112122
Rel(
113123
name="best_friends",

tests/integration/test_infrahub_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ async def test_count_with_filter(self, client: InfrahubClient, base_dataset):
146146
count = await client.count(kind=TESTING_PERSON, name__values=["Liam Walker", "Ethan Carter"])
147147
assert count == 2
148148

149+
async def test_create_generic_rel_with_hfid(self, client: InfrahubClient, base_dataset, cat_luna, person_sophia):
150+
person_sophia.favorite_animal = {"hfid": cat_luna.hfid, "kind": TESTING_CAT}
151+
await person_sophia.save()
152+
person_sophia = await client.get(kind=TESTING_PERSON, id=person_sophia.id, prefetch_relationships=True)
153+
assert person_sophia.favorite_animal.id == cat_luna.id
154+
149155
# async def test_get_generic_filter_source(self, client: InfrahubClient, base_dataset):
150156
# admin = await client.get(kind="CoreAccount", name__value="admin")
151157

0 commit comments

Comments
 (0)