Skip to content

Commit 17d4a45

Browse files
committed
Refactor internal store
1 parent 8174e97 commit 17d4a45

File tree

10 files changed

+657
-125
lines changed

10 files changed

+657
-125
lines changed

infrahub_sdk/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,11 @@ async def process_non_batch() -> tuple[list[InfrahubNode], list[InfrahubNode]]:
840840
if populate_store:
841841
for node in nodes:
842842
if node.id:
843-
self.store.set(key=node.id, node=node)
843+
self.store.set(node=node)
844844
related_nodes = list(set(related_nodes))
845845
for node in related_nodes:
846846
if node.id:
847-
self.store.set(key=node.id, node=node)
847+
self.store.set(node=node)
848848
return nodes
849849

850850
def clone(self) -> InfrahubClient:
@@ -1997,11 +1997,11 @@ def process_non_batch() -> tuple[list[InfrahubNodeSync], list[InfrahubNodeSync]]
19971997
if populate_store:
19981998
for node in nodes:
19991999
if node.id:
2000-
self.store.set(key=node.id, node=node)
2000+
self.store.set(node=node)
20012001
related_nodes = list(set(related_nodes))
20022002
for node in related_nodes:
20032003
if node.id:
2004-
self.store.set(key=node.id, node=node)
2004+
self.store.set(node=node)
20052005
return nodes
20062006

20072007
@overload

infrahub_sdk/node.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from copy import copy
77
from typing import TYPE_CHECKING, Any, Callable, Union, get_args
88

9+
import shortuuid
10+
911
from .constants import InfrahubClientMode
1012
from .exceptions import (
1113
Error,
@@ -43,6 +45,16 @@
4345
"calling generate is only supported for CoreArtifactDefinition nodes"
4446
)
4547

48+
HFID_STR_SEPARATOR = "__"
49+
50+
51+
def parse_human_friendly_id(hfid: str) -> tuple[str | None, list[str]]:
52+
"""Parse a human friendly ID into a kind and an identifier."""
53+
hfid_parts = hfid.split(HFID_STR_SEPARATOR)
54+
if len(hfid_parts) == 1:
55+
return None, hfid_parts
56+
return hfid_parts[0], hfid_parts[1:]
57+
4658

4759
class Attribute:
4860
"""Represents an attribute of a Node, including its schema, value, and properties."""
@@ -678,6 +690,11 @@ def __init__(self, schema: MainSchemaTypesAPI, branch: str, data: dict | None =
678690
self._branch = branch
679691
self._existing: bool = True
680692

693+
# Generate a unique ID only to be used inside the SDK
694+
# The format if this ID is purposely different from the ID used by the API
695+
# This is done to avoid confusion and potential conflicts between the IDs
696+
self._internal_id = shortuuid.uuid()
697+
681698
self.id = data.get("id", None) if isinstance(data, dict) else None
682699
self.display_label: str | None = data.get("display_label", None) if isinstance(data, dict) else None
683700
self.typename: str | None = data.get("__typename", schema.kind) if isinstance(data, dict) else schema.kind
@@ -694,6 +711,9 @@ def __init__(self, schema: MainSchemaTypesAPI, branch: str, data: dict | None =
694711
self._init_attributes(data)
695712
self._init_relationships(data)
696713

714+
def get_branch(self) -> str:
715+
return self._branch
716+
697717
def get_path_value(self, path: str) -> Any:
698718
path_parts = path.split("__")
699719
return_value = None

infrahub_sdk/protocols_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class AnyAttributeOptional(Attribute):
144144
@runtime_checkable
145145
class CoreNodeBase(Protocol):
146146
_schema: MainSchemaTypes
147-
id: str
147+
_internal_id: str
148+
id: str # NOTE this is incorrect, should be str | None
148149
display_label: str | None
149150

150151
@property
@@ -153,10 +154,14 @@ def hfid(self) -> list[str] | None: ...
153154
@property
154155
def hfid_str(self) -> str | None: ...
155156

157+
def get_human_friendly_id(self) -> list[str] | None: ...
158+
156159
def get_human_friendly_id_as_string(self, include_kind: bool = False) -> str | None: ...
157160

158161
def get_kind(self) -> str: ...
159162

163+
def get_branch(self) -> str: ...
164+
160165
def is_ip_prefix(self) -> bool: ...
161166

162167
def is_ip_address(self) -> bool: ...

0 commit comments

Comments
 (0)