11from __future__ import annotations
22
3- from asyncio import run as aiorun
4-
5- from infrahub_sdk .async_typer import AsyncTyper
6-
73from typing import Annotated
84
9- from pydantic import BaseModel , Field , ConfigDict
10- from infrahub_sdk import InfrahubClient
5+ from pydantic import ConfigDict , Field
116from 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
137
8+ from infrahub_sdk import InfrahubClient
9+ from infrahub_sdk .async_typer import AsyncTyper
10+ from infrahub_sdk .schema import (
11+ GenericModel ,
12+ GenericSchema ,
13+ NodeModel ,
14+ NodeSchema ,
15+ RelationshipKind ,
16+ from_pydantic ,
17+ )
18+ from infrahub_sdk .schema import (
19+ InfrahubAttributeParam as AttrParam ,
20+ )
21+ from infrahub_sdk .schema import (
22+ InfrahubRelationshipParam as RelParam ,
23+ )
1424
1525app = AsyncTyper ()
1626
1727
1828class Site (NodeModel ):
1929 model_config = ConfigDict (
20- node_schema = NodeSchema (name = "Site" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ])
30+ node_schema = NodeSchema (
31+ name = "Site" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ]
32+ )
2133 )
2234
2335 name : Annotated [str , AttrParam (unique = True )] = Field (description = "The name of the site" )
2436
2537
2638class Vlan (NodeModel ):
2739 model_config = ConfigDict (
28- node_schema = NodeSchema (name = "Vlan" , namespace = "Infra" , human_friendly_id = ["vlan_id__value" ], display_labels = ["vlan_id__value" ])
40+ node_schema = NodeSchema (
41+ name = "Vlan" , namespace = "Infra" , human_friendly_id = ["vlan_id__value" ], display_labels = ["vlan_id__value" ]
42+ )
2943 )
3044
3145 name : str
@@ -35,39 +49,45 @@ class Vlan(NodeModel):
3549
3650class Device (NodeModel ):
3751 model_config = ConfigDict (
38- node_schema = NodeSchema (name = "Device" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ])
52+ node_schema = NodeSchema (
53+ name = "Device" , namespace = "Infra" , human_friendly_id = ["name__value" ], display_labels = ["name__value" ]
54+ )
3955 )
4056
4157 name : Annotated [str , AttrParam (unique = True )] = Field (description = "The name of the car" )
4258 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 )
59+ interfaces : Annotated [
60+ list [Interface ], RelParam (kind = RelationshipKind .COMPONENT , identifier = "device__interfaces" )
61+ ] = Field (default_factory = list )
4462
4563
4664class Interface (GenericModel ):
4765 model_config = ConfigDict (
48- generic_schema = GenericSchema (name = "Interface" , namespace = "Infra" , human_friendly_id = ["device__name__value" , "name__value" ], display_labels = ["name__value" ])
66+ generic_schema = GenericSchema (
67+ name = "Interface" ,
68+ namespace = "Infra" ,
69+ human_friendly_id = ["device__name__value" , "name__value" ],
70+ display_labels = ["name__value" ],
71+ )
4972 )
5073
5174 device : Annotated [Device , RelParam (kind = RelationshipKind .PARENT , identifier = "device__interfaces" )]
5275 name : str
5376 description : str | None = None
5477
78+
5579class L2Interface (Interface ):
56- model_config = ConfigDict (
57- node_schema = NodeSchema (name = "L2Interface" , namespace = "Infra" )
58- )
59-
80+ model_config = ConfigDict (node_schema = NodeSchema (name = "L2Interface" , namespace = "Infra" ))
81+
6082 vlans : list [Vlan ] = Field (default_factory = list )
6183
84+
6285class LoopbackInterface (Interface ):
63- model_config = ConfigDict (
64- node_schema = NodeSchema (name = "LoopbackInterface" , namespace = "Infra" )
65- )
66-
86+ model_config = ConfigDict (node_schema = NodeSchema (name = "LoopbackInterface" , namespace = "Infra" ))
6787
6888
6989@app .command ()
70- async def load_schema ():
90+ async def load_schema () -> None :
7191 client = InfrahubClient ()
7292 schema = from_pydantic (models = [Site , Device , Interface , L2Interface , LoopbackInterface , Vlan ])
7393 rprint (schema .to_schema_dict ())
@@ -76,7 +96,7 @@ async def load_schema():
7696
7797
7898@app .command ()
79- async def load_data ():
99+ async def load_data () -> None :
80100 client = InfrahubClient ()
81101
82102 atl = await client .create ("InfraSite" , name = "ATL" )
@@ -100,14 +120,15 @@ async def load_data():
100120
101121
102122@app .command ()
103- async def query_data ():
123+ async def query_data () -> None :
104124 client = InfrahubClient ()
105125 sites = await client .all (kind = Site )
126+ rprint (sites )
106127
107- breakpoint ()
108128 devices = await client .all (kind = Device )
109129 for device in devices :
110130 rprint (device )
111131
132+
112133if __name__ == "__main__" :
113- app ()
134+ app ()
0 commit comments