Skip to content

Commit f414027

Browse files
authored
Add objects field within InfrahubConfig (#422)
1 parent 49116c8 commit f414027

File tree

6 files changed

+160
-5
lines changed

6 files changed

+160
-5
lines changed

infrahub_sdk/client.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,18 @@ def start_tracking(
172172
params: dict[str, Any] | None = None,
173173
delete_unused_nodes: bool = False,
174174
group_type: str | None = None,
175+
group_params: dict[str, Any] | None = None,
176+
branch: str | None = None,
175177
) -> Self:
176178
self.mode = InfrahubClientMode.TRACKING
177179
identifier = identifier or self.identifier or "python-sdk"
178180
self.set_context_properties(
179-
identifier=identifier, params=params, delete_unused_nodes=delete_unused_nodes, group_type=group_type
181+
identifier=identifier,
182+
params=params,
183+
delete_unused_nodes=delete_unused_nodes,
184+
group_type=group_type,
185+
group_params=group_params,
186+
branch=branch,
180187
)
181188
return self
182189

@@ -187,14 +194,22 @@ def set_context_properties(
187194
delete_unused_nodes: bool = True,
188195
reset: bool = True,
189196
group_type: str | None = None,
197+
group_params: dict[str, Any] | None = None,
198+
branch: str | None = None,
190199
) -> None:
191200
if reset:
192201
if isinstance(self, InfrahubClient):
193202
self.group_context = InfrahubGroupContext(self)
194203
elif isinstance(self, InfrahubClientSync):
195204
self.group_context = InfrahubGroupContextSync(self)
205+
196206
self.group_context.set_properties(
197-
identifier=identifier, params=params, delete_unused_nodes=delete_unused_nodes, group_type=group_type
207+
identifier=identifier,
208+
params=params,
209+
delete_unused_nodes=delete_unused_nodes,
210+
group_type=group_type,
211+
group_params=group_params,
212+
branch=branch,
198213
)
199214

200215
def _graphql_url(

infrahub_sdk/protocols.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class BuiltinIPPrefix(CoreNode):
6868
children: RelationshipManager
6969

7070

71+
class CoreAction(CoreNode):
72+
name: String
73+
description: StringOptional
74+
triggers: RelationshipManager
75+
76+
7177
class CoreArtifactTarget(CoreNode):
7278
artifacts: RelationshipManager
7379

@@ -154,6 +160,10 @@ class CoreMenu(CoreNode):
154160
children: RelationshipManager
155161

156162

163+
class CoreNodeTriggerMatch(CoreNode):
164+
trigger: RelatedNode
165+
166+
157167
class CoreObjectComponentTemplate(CoreNode):
158168
template_name: String
159169

@@ -195,6 +205,14 @@ class CoreTransformation(CoreNode):
195205
tags: RelationshipManager
196206

197207

208+
class CoreTriggerRule(CoreNode):
209+
name: String
210+
description: StringOptional
211+
active: Boolean
212+
branch_scope: Dropdown
213+
action: RelatedNode
214+
215+
198216
class CoreValidator(CoreNode):
199217
label: StringOptional
200218
state: Enum
@@ -328,6 +346,10 @@ class CoreFileThread(CoreThread):
328346
repository: RelatedNode
329347

330348

349+
class CoreGeneratorAction(CoreAction):
350+
generator: RelatedNode
351+
352+
331353
class CoreGeneratorCheck(CoreCheck):
332354
instance: String
333355

@@ -382,6 +404,16 @@ class CoreGraphQLQueryGroup(CoreGroup):
382404
query: RelatedNode
383405

384406

407+
class CoreGroupAction(CoreAction):
408+
add_members: Boolean
409+
group: RelatedNode
410+
411+
412+
class CoreGroupTriggerRule(CoreTriggerRule):
413+
members_added: Boolean
414+
group: RelatedNode
415+
416+
385417
class CoreIPAddressPool(CoreResourcePool, LineageSource):
386418
default_address_type: String
387419
default_prefix_length: IntegerOptional
@@ -401,6 +433,25 @@ class CoreMenuItem(CoreMenu):
401433
pass
402434

403435

436+
class CoreNodeTriggerAttributeMatch(CoreNodeTriggerMatch):
437+
attribute_name: String
438+
value: StringOptional
439+
value_previous: StringOptional
440+
value_match: Dropdown
441+
442+
443+
class CoreNodeTriggerRelationshipMatch(CoreNodeTriggerMatch):
444+
relationship_name: String
445+
added: Boolean
446+
peer: StringOptional
447+
448+
449+
class CoreNodeTriggerRule(CoreTriggerRule):
450+
node_kind: String
451+
mutation_action: Enum
452+
matches: RelationshipManager
453+
454+
404455
class CoreNumberPool(CoreResourcePool, LineageSource):
405456
node: String
406457
node_attribute: String
@@ -448,6 +499,11 @@ class CoreRepository(LineageOwner, LineageSource, CoreGenericRepository, CoreTas
448499
commit: StringOptional
449500

450501

502+
class CoreRepositoryGroup(CoreGroup):
503+
content: Dropdown
504+
repository: RelatedNode
505+
506+
451507
class CoreRepositoryValidator(CoreValidator):
452508
repository: RelatedNode
453509

@@ -545,6 +601,12 @@ class BuiltinIPPrefixSync(CoreNodeSync):
545601
children: RelationshipManagerSync
546602

547603

604+
class CoreActionSync(CoreNodeSync):
605+
name: String
606+
description: StringOptional
607+
triggers: RelationshipManagerSync
608+
609+
548610
class CoreArtifactTargetSync(CoreNodeSync):
549611
artifacts: RelationshipManagerSync
550612

@@ -631,6 +693,10 @@ class CoreMenuSync(CoreNodeSync):
631693
children: RelationshipManagerSync
632694

633695

696+
class CoreNodeTriggerMatchSync(CoreNodeSync):
697+
trigger: RelatedNodeSync
698+
699+
634700
class CoreObjectComponentTemplateSync(CoreNodeSync):
635701
template_name: String
636702

@@ -672,6 +738,14 @@ class CoreTransformationSync(CoreNodeSync):
672738
tags: RelationshipManagerSync
673739

674740

741+
class CoreTriggerRuleSync(CoreNodeSync):
742+
name: String
743+
description: StringOptional
744+
active: Boolean
745+
branch_scope: Dropdown
746+
action: RelatedNodeSync
747+
748+
675749
class CoreValidatorSync(CoreNodeSync):
676750
label: StringOptional
677751
state: Enum
@@ -805,6 +879,10 @@ class CoreFileThreadSync(CoreThreadSync):
805879
repository: RelatedNodeSync
806880

807881

882+
class CoreGeneratorActionSync(CoreActionSync):
883+
generator: RelatedNodeSync
884+
885+
808886
class CoreGeneratorCheckSync(CoreCheckSync):
809887
instance: String
810888

@@ -859,6 +937,16 @@ class CoreGraphQLQueryGroupSync(CoreGroupSync):
859937
query: RelatedNodeSync
860938

861939

940+
class CoreGroupActionSync(CoreActionSync):
941+
add_members: Boolean
942+
group: RelatedNodeSync
943+
944+
945+
class CoreGroupTriggerRuleSync(CoreTriggerRuleSync):
946+
members_added: Boolean
947+
group: RelatedNodeSync
948+
949+
862950
class CoreIPAddressPoolSync(CoreResourcePoolSync, LineageSourceSync):
863951
default_address_type: String
864952
default_prefix_length: IntegerOptional
@@ -878,6 +966,25 @@ class CoreMenuItemSync(CoreMenuSync):
878966
pass
879967

880968

969+
class CoreNodeTriggerAttributeMatchSync(CoreNodeTriggerMatchSync):
970+
attribute_name: String
971+
value: StringOptional
972+
value_previous: StringOptional
973+
value_match: Dropdown
974+
975+
976+
class CoreNodeTriggerRelationshipMatchSync(CoreNodeTriggerMatchSync):
977+
relationship_name: String
978+
added: Boolean
979+
peer: StringOptional
980+
981+
982+
class CoreNodeTriggerRuleSync(CoreTriggerRuleSync):
983+
node_kind: String
984+
mutation_action: Enum
985+
matches: RelationshipManagerSync
986+
987+
881988
class CoreNumberPoolSync(CoreResourcePoolSync, LineageSourceSync):
882989
node: String
883990
node_attribute: String
@@ -925,6 +1032,11 @@ class CoreRepositorySync(LineageOwnerSync, LineageSourceSync, CoreGenericReposit
9251032
commit: StringOptional
9261033

9271034

1035+
class CoreRepositoryGroupSync(CoreGroupSync):
1036+
content: Dropdown
1037+
repository: RelatedNodeSync
1038+
1039+
9281040
class CoreRepositoryValidatorSync(CoreValidatorSync):
9291041
repository: RelatedNodeSync
9301042

infrahub_sdk/query_groups.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Any
44

55
from .constants import InfrahubClientMode
66
from .exceptions import NodeNotFoundError
@@ -25,13 +25,16 @@ def __init__(self) -> None:
2525
self.params: dict[str, str] = {}
2626
self.delete_unused_nodes: bool = False
2727
self.group_type: str = "CoreStandardGroup"
28+
self.group_params: dict[str, Any] = {}
2829

2930
def set_properties(
3031
self,
3132
identifier: str,
3233
params: dict[str, str] | None = None,
3334
delete_unused_nodes: bool = False,
3435
group_type: str | None = None,
36+
group_params: dict[str, Any] | None = None,
37+
branch: str | None = None,
3538
) -> None:
3639
"""Setter method to set the values of identifier and params.
3740
@@ -43,6 +46,8 @@ def set_properties(
4346
self.params = params or {}
4447
self.delete_unused_nodes = delete_unused_nodes
4548
self.group_type = group_type or self.group_type
49+
self.group_params = group_params or {}
50+
self.branch = branch
4651

4752
def _get_params_as_str(self) -> str:
4853
"""Convert the params in dict format, into a string"""
@@ -87,7 +92,9 @@ def __init__(self, client: InfrahubClient) -> None:
8792
async def get_group(self, store_peers: bool = False) -> InfrahubNode | None:
8893
group_name = self._generate_group_name()
8994
try:
90-
group = await self.client.get(kind=self.group_type, name__value=group_name, include=["members"])
95+
group = await self.client.get(
96+
kind=self.group_type, name__value=group_name, include=["members"], branch=self.branch
97+
)
9198
except NodeNotFoundError:
9299
return None
93100

@@ -151,6 +158,8 @@ async def update_group(self) -> None:
151158
name=group_name,
152159
description=description,
153160
members=members,
161+
branch=self.branch,
162+
**self.group_params,
154163
)
155164
await group.save(allow_upsert=True, update_group_context=False)
156165

@@ -243,6 +252,8 @@ def update_group(self) -> None:
243252
name=group_name,
244253
description=description,
245254
members=members,
255+
branch=self.branch,
256+
**self.group_params,
246257
)
247258
group.save(allow_upsert=True, update_group_context=False)
248259

infrahub_sdk/schema/repository.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,27 @@ def load_query(self, relative_path: str = ".") -> str:
147147
return file.read()
148148

149149

150+
class InfrahubObjectConfig(InfrahubRepositoryConfigElement):
151+
model_config = ConfigDict(extra="forbid")
152+
name: str = Field(..., description="The name associated to the object file")
153+
file_path: Path = Field(..., description="The file within the repository containing object data.")
154+
155+
156+
class InfrahubMenuConfig(InfrahubRepositoryConfigElement):
157+
model_config = ConfigDict(extra="forbid")
158+
name: str = Field(..., description="The name of the menu")
159+
file_path: Path = Field(..., description="The file within the repository containing menu data.")
160+
161+
150162
RESOURCE_MAP: dict[Any, str] = {
151163
InfrahubJinja2TransformConfig: "jinja2_transforms",
152164
InfrahubCheckDefinitionConfig: "check_definitions",
153165
InfrahubRepositoryArtifactDefinitionConfig: "artifact_definitions",
154166
InfrahubPythonTransformConfig: "python_transforms",
155167
InfrahubGeneratorDefinitionConfig: "generator_definitions",
156168
InfrahubRepositoryGraphQLConfig: "queries",
169+
InfrahubObjectConfig: "objects",
170+
InfrahubMenuConfig: "menus",
157171
}
158172

159173

@@ -176,6 +190,8 @@ class InfrahubRepositoryConfig(BaseModel):
176190
default_factory=list, description="Generator definitions"
177191
)
178192
queries: list[InfrahubRepositoryGraphQLConfig] = Field(default_factory=list, description="GraphQL Queries")
193+
objects: list[Path] = Field(default_factory=list, description="Objects")
194+
menus: list[Path] = Field(default_factory=list, description="Menus")
179195

180196
@field_validator(
181197
"check_definitions",

infrahub_sdk/spec/object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ async def create_node(
459459
await node.save(allow_upsert=True)
460460

461461
display_label = node.get_human_friendly_id_as_string() or f"{node.get_kind()} : {node.id}"
462-
client.log.info(f"Node: {display_label}")
462+
client.log.info(f"Created node: {display_label}")
463463

464464
for rel in remaining_rels:
465465
context = {}

infrahub_sdk/testing/schemas/car_person.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def schema_person_base(self) -> NodeSchema:
4848
namespace=NAMESPACE,
4949
include_in_menu=True,
5050
label="Person",
51+
default_filter="name__value",
5152
human_friendly_id=["name__value"],
5253
attributes=[
5354
Attr(name="name", kind=AttributeKind.TEXT, unique=True),

0 commit comments

Comments
 (0)