1
+ from __future__ import annotations
2
+
3
+ from asyncio import run as aiorun
4
+
5
+ from infrahub_sdk .async_typer import AsyncTyper
6
+
7
+ from typing import Annotated
8
+
9
+ from pydantic import BaseModel , Field , ConfigDict
10
+ from infrahub_sdk import InfrahubClient
11
+ from rich import print as rprint
12
+ from infrahub_sdk .schema import InfrahubAttributeParam as AttrParam , InfrahubRelationshipParam as RelParam , AttributeKind , from_pydantic , NodeSchema , NodeModel , GenericSchema , GenericModel , RelationshipKind
13
+
14
+
15
+ app = AsyncTyper ()
16
+
17
+
18
+ class Site (NodeModel ):
19
+ model_config = ConfigDict (
20
+ node_schema = NodeSchema (name = "Site" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ])
21
+ )
22
+
23
+ name : Annotated [str , AttrParam (unique = True )] = Field (description = "The name of the site" )
24
+
25
+
26
+ class Vlan (NodeModel ):
27
+ model_config = ConfigDict (
28
+ node_schema = NodeSchema (name = "Vlan" , namespace = "Infra" , human_friendly_id = ["vlan_id__value" ], display_labels = ["vlan_id__value" ])
29
+ )
30
+
31
+ name : str
32
+ vlan_id : int
33
+ description : str | None = None
34
+
35
+
36
+ class Device (NodeModel ):
37
+ model_config = ConfigDict (
38
+ node_schema = NodeSchema (name = "Device" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ])
39
+ )
40
+
41
+ name : Annotated [str , AttrParam (unique = True )] = Field (description = "The name of the car" )
42
+ site : Annotated [Site , RelParam (kind = RelationshipKind .ATTRIBUTE , identifier = "device__site" )]
43
+ interfaces : Annotated [list [Interface ], RelParam (kind = RelationshipKind .COMPONENT , identifier = "device__interfaces" )] = Field (default_factory = list )
44
+
45
+
46
+ class Interface (GenericModel ):
47
+ model_config = ConfigDict (
48
+ generic_schema = GenericSchema (name = "Interface" , namespace = "Infra" , human_friendly_id = ["device__name__value" , "name__value" ], display_labels = ["name__value" ])
49
+ )
50
+
51
+ device : Annotated [Device , RelParam (kind = RelationshipKind .PARENT , identifier = "device__interfaces" )]
52
+ name : str
53
+ description : str | None = None
54
+
55
+ class L2Interface (Interface ):
56
+ model_config = ConfigDict (
57
+ node_schema = NodeSchema (name = "L2Interface" , namespace = "Infra" )
58
+ )
59
+
60
+ vlans : list [Vlan ] = Field (default_factory = list )
61
+
62
+ class LoopbackInterface (Interface ):
63
+ model_config = ConfigDict (
64
+ node_schema = NodeSchema (name = "LoopbackInterface" , namespace = "Infra" )
65
+ )
66
+
67
+
68
+
69
+ @app .command ()
70
+ async def load_schema ():
71
+ client = InfrahubClient ()
72
+ schema = from_pydantic (models = [Site , Device , Interface , L2Interface , LoopbackInterface , Vlan ])
73
+ rprint (schema .to_schema_dict ())
74
+ response = await client .schema .load (schemas = [schema .to_schema_dict ()], wait_until_converged = True )
75
+ rprint (response )
76
+
77
+
78
+ @app .command ()
79
+ async def load_data ():
80
+ client = InfrahubClient ()
81
+
82
+ atl = await client .create ("InfraSite" , name = "ATL" )
83
+ await atl .save (allow_upsert = True )
84
+ cdg = await client .create ("InfraSite" , name = "CDG" )
85
+ await cdg .save (allow_upsert = True )
86
+
87
+ device1 = await client .create ("InfraDevice" , name = "atl1-dev1" , site = atl )
88
+ await device1 .save (allow_upsert = True )
89
+ device2 = await client .create ("InfraDevice" , name = "atl1-dev2" , site = atl )
90
+ await device2 .save (allow_upsert = True )
91
+
92
+ lo0dev1 = await client .create ("InfraLoopbackInterface" , name = "lo0" , device = device1 )
93
+ await lo0dev1 .save (allow_upsert = True )
94
+ lo0dev2 = await client .create ("InfraLoopbackInterface" , name = "lo0" , device = device2 )
95
+ await lo0dev2 .save (allow_upsert = True )
96
+
97
+ for idx in range (1 , 3 ):
98
+ interface = await client .create ("InfraL2Interface" , name = f"Ethernet{ idx } " , device = device1 )
99
+ await interface .save (allow_upsert = True )
100
+
101
+
102
+ @app .command ()
103
+ async def query_data ():
104
+ client = InfrahubClient ()
105
+ sites = await client .all (kind = Site )
106
+
107
+ breakpoint ()
108
+ devices = await client .all (kind = Device )
109
+ for device in devices :
110
+ rprint (device )
111
+
112
+ if __name__ == "__main__" :
113
+ app ()
0 commit comments