Skip to content

Commit

Permalink
Merge pull request #292 from opsmill/lgu-set-generic-rel-with-hfid
Browse files Browse the repository at this point in the history
Allow useof HFID to create a related node on generic relationship
  • Loading branch information
LucasG0 authored Mar 4, 2025
2 parents 48ffeba + 6901418 commit 0988b2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions infrahub_sdk/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def __init__(self, branch: str, schema: RelationshipSchemaAPI, data: Any | dict,
if node_data:
self._id = node_data.get("id", None)
self._hfid = node_data.get("hfid", None)
self._kind = node_data.get("kind", None)
self._display_label = node_data.get("display_label", None)
self._typename = node_data.get("__typename", None)

Expand Down Expand Up @@ -255,6 +256,8 @@ def _generate_input_data(self, allocate_from_pool: bool = False) -> dict[str, An
data["id"] = self.id
elif self.hfid is not None:
data["hfid"] = self.hfid
if self._kind is not None:
data["kind"] = self._kind

for prop_name in self._properties:
if getattr(self, prop_name) is not None:
Expand Down
9 changes: 9 additions & 0 deletions infrahub_sdk/testing/schemas/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def schema_cat(self) -> NodeSchema:
namespace=NAMESPACE,
include_in_menu=True,
inherit_from=[TESTING_ANIMAL],
human_friendly_id=["owner__name__value", "name__value", "color__value"],
display_labels=["name__value", "breed__value", "color__value"],
order_by=["name__value"],
attributes=[
Expand Down Expand Up @@ -108,6 +109,14 @@ def schema_person(self) -> NodeSchema:
identifier="person__animal",
cardinality="many",
direction=RelationshipDirection.INBOUND,
max_count=10,
),
Rel(
name="favorite_animal",
peer=TESTING_ANIMAL,
identifier="favorite_animal",
cardinality="one",
direction=RelationshipDirection.INBOUND,
),
Rel(
name="best_friends",
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_infrahub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ async def test_count_with_filter(self, client: InfrahubClient, base_dataset):
count = await client.count(kind=TESTING_PERSON, name__values=["Liam Walker", "Ethan Carter"])
assert count == 2

async def test_create_generic_rel_with_hfid(
self, client: InfrahubClient, base_dataset, cat_luna, person_sophia, schema_animal, schema_cat
):
# See https://github.com/opsmill/infrahub-sdk-python/issues/277
assert (
schema_animal.human_friendly_id != schema_cat.human_friendly_id
), "Inherited node schema should have a different hfid than generic one for this test to be relevant"
person_sophia.favorite_animal = {"hfid": cat_luna.hfid, "kind": TESTING_CAT}
await person_sophia.save()
person_sophia = await client.get(kind=TESTING_PERSON, id=person_sophia.id, prefetch_relationships=True)
assert person_sophia.favorite_animal.id == cat_luna.id

# async def test_get_generic_filter_source(self, client: InfrahubClient, base_dataset):
# admin = await client.get(kind="CoreAccount", name__value="admin")

Expand Down

0 comments on commit 0988b2b

Please sign in to comment.