Skip to content

Commit e0fa5d7

Browse files
committed
Support all kind of attributes in protocols
1 parent e91bd2c commit e0fa5d7

File tree

3 files changed

+151
-77
lines changed

3 files changed

+151
-77
lines changed

infrahub_sdk/code_generator.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,14 @@ def _jinja2_filter_inheritance(value: dict[str, Any]) -> str:
6868

6969
@staticmethod
7070
def _jinja2_filter_render_attribute(value: AttributeSchema) -> str:
71-
attribute_kind_map = {
72-
"boolean": "Boolean",
73-
"datetime": "DateTime",
74-
"dropdown": "Dropdown",
75-
"hashedpassword": "HashedPassword",
76-
"iphost": "IPHost",
77-
"ipnetwork": "IPNetwork",
78-
"json": "JSONAttribute",
79-
"list": "ListAttribute",
80-
"number": "Integer",
81-
"password": "String",
82-
"text": "String",
83-
"textarea": "String",
84-
"url": "URL",
85-
}
71+
attribute_kind: str = value.kind
8672

87-
name = value.name
88-
kind = value.kind
73+
attribute_kind += "Attribute"
8974

90-
attribute_kind = attribute_kind_map[kind.lower()]
9175
if value.optional:
92-
attribute_kind = f"{attribute_kind}Optional"
76+
attribute_kind += "Optional"
9377

94-
return f"{name}: {attribute_kind}"
78+
return f"{value.name}: {attribute_kind}"
9579

9680
@staticmethod
9781
def _jinja2_filter_render_relationship(value: RelationshipSchema, sync: bool = False) -> str:

infrahub_sdk/ctl/constants.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,52 @@
1515
from infrahub_sdk.node import RelatedNode, RelationshipManager
1616
{% endif %}
1717
from infrahub_sdk.protocols_base import (
18-
String,
19-
StringOptional,
20-
Integer,
21-
IntegerOptional,
22-
Boolean,
23-
BooleanOptional,
24-
DateTime,
25-
DateTimeOptional,
26-
Dropdown,
27-
DropdownOptional,
28-
HashedPassword,
29-
HashedPasswordOptional,
30-
IPHost,
31-
IPHostOptional,
32-
IPNetwork,
33-
IPNetworkOptional,
18+
AnyAttribute,
19+
AnyAttributeOptional,
20+
BandwidthAttribute,
21+
BandwidthAttributeOptional,
22+
BooleanAttribute,
23+
BooleanAttributeOptional,
24+
CheckboxAttribute,
25+
CheckboxAttributeOptional,
26+
ColorAttribute,
27+
ColorAttributeOptional,
28+
DateTimeAttribute,
29+
DateTimeAttributeOptional,
30+
DropdownAttribute,
31+
DropdownAttributeOptional,
32+
EmailAttribute,
33+
EmailAttributeOptional,
34+
FileAttribute,
35+
FileAttributeOptional,
36+
HashedPasswordAttribute,
37+
HashedPasswordAttributeOptional,
38+
IDAttribute,
39+
IDAttributeOptional,
40+
IPHostAttribute,
41+
IPHostAttributeOptional,
42+
IPNetworkAttribute,
43+
IPNetworkAttributeOptional,
3444
JSONAttribute,
3545
JSONAttributeOptional,
3646
ListAttribute,
3747
ListAttributeOptional,
38-
URL,
39-
URLOptional,
48+
MacAddressAttribute,
49+
MacAddressAttributeOptional,
50+
NumberAttribute,
51+
NumberAttributeOptional,
52+
PasswordAttribute,
53+
PasswordAttributeOptional,
54+
TextAreaAttribute,
55+
TextAreaAttributeOptional,
56+
TextAttribute,
57+
TextAttributeOptional,
58+
URLAttribute,
59+
URLAttributeOptional,
4060
)
61+
{% for generic in generics %}
4162
4263
43-
{% for generic in generics %}
4464
class {{ generic.namespace + generic.name }}(CoreNode):
4565
{% if not generic.attributes|default([]) and not generic.relationships|default([]) %}
4666
pass
@@ -60,11 +80,10 @@ class {{ generic.namespace + generic.name }}(CoreNode):
6080
children: RelationshipManager
6181
{% endif %}
6282
{% endif %}
63-
6483
{% endfor %}
84+
{% for node in nodes %}
6585
6686
67-
{% for node in nodes %}
6887
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
6988
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
7089
pass
@@ -84,10 +103,10 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
84103
children: RelationshipManager
85104
{% endif %}
86105
{% endif %}
87-
88106
{% endfor %}
89-
90107
{% for node in profiles %}
108+
109+
91110
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
92111
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
93112
pass
@@ -107,6 +126,6 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
107126
children: RelationshipManager
108127
{% endif %}
109128
{% endif %}
110-
111129
{% endfor %}
130+
112131
"""

infrahub_sdk/protocols_base.py

Lines changed: 104 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class RelatedNodeSync(Protocol): ...
2020
class Attribute(Protocol):
2121
name: str
2222
id: Optional[str]
23-
2423
is_default: Optional[bool]
2524
is_from_profile: Optional[bool]
2625
is_inherited: Optional[bool]
@@ -29,92 +28,148 @@ class Attribute(Protocol):
2928
is_protected: Optional[bool]
3029

3130

32-
class String(Attribute):
31+
class IDAttribute(Attribute):
32+
value: int
33+
34+
35+
class IDAttributeOptional(Attribute):
36+
value: Optional[int]
37+
38+
39+
class DropdownAttribute(Attribute):
3340
value: str
3441

3542

36-
class StringOptional(Attribute):
43+
class DropdownAttributeOptional(Attribute):
3744
value: Optional[str]
3845

3946

40-
class Integer(Attribute):
41-
value: int
47+
class TextAttribute(Attribute):
48+
value: str
4249

4350

44-
class IntegerOptional(Attribute):
45-
value: Optional[int]
51+
class TextAttributeOptional(Attribute):
52+
value: Optional[str]
4653

4754

48-
class Boolean(Attribute):
49-
value: bool
55+
class TextAreaAttribute(Attribute):
56+
value: str
5057

5158

52-
class BooleanOptional(Attribute):
53-
value: Optional[bool]
59+
class TextAreaAttributeOptional(Attribute):
60+
value: Optional[str]
5461

5562

56-
class DateTime(Attribute):
63+
class DateTimeAttribute(Attribute):
5764
value: str
5865

5966

60-
class DateTimeOptional(Attribute):
67+
class DateTimeAttributeOptional(Attribute):
6168
value: Optional[str]
6269

6370

64-
class Enum(Attribute):
71+
class EmailAttribute(Attribute):
6572
value: str
6673

6774

68-
class EnumOptional(Attribute):
75+
class EmailAttributeOptional(Attribute):
6976
value: Optional[str]
7077

7178

72-
class URL(Attribute):
79+
class PasswordAttribute(Attribute):
7380
value: str
7481

7582

76-
class URLOptional(Attribute):
83+
class PasswordAttributeOptional(Attribute):
7784
value: Optional[str]
7885

7986

80-
class Dropdown(Attribute):
87+
class HashedPasswordAttribute(Attribute):
8188
value: str
8289

8390

84-
class DropdownOptional(Attribute):
91+
class HashedPasswordAttributeOptional(Attribute):
8592
value: Optional[str]
8693

8794

88-
class IPNetwork(Attribute):
89-
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
95+
class URLAttribute(Attribute):
96+
value: str
9097

9198

92-
class IPNetworkOptional(Attribute):
93-
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
99+
class URLAttributeOptional(Attribute):
100+
value: Optional[str]
101+
102+
103+
class FileAttribute(Attribute):
104+
value: str
105+
106+
107+
class FileAttributeOptional(Attribute):
108+
value: Optional[str]
109+
110+
111+
class MacAddressAttribute(Attribute):
112+
value: str
113+
114+
115+
class MacAddressAttributeOptional(Attribute):
116+
value: Optional[str]
117+
118+
119+
class ColorAttribute(Attribute):
120+
value: str
121+
122+
123+
class ColorAttributeOptional(Attribute):
124+
value: Optional[str]
94125

95126

96-
class IPHost(Attribute):
127+
class NumberAttribute(Attribute):
128+
value: float
129+
130+
131+
class NumberAttributeOptional(Attribute):
132+
value: Optional[float]
133+
134+
135+
class BandwidthAttribute(Attribute):
136+
value: float
137+
138+
139+
class BandwidthAttributeOptional(Attribute):
140+
value: Optional[float]
141+
142+
143+
class IPHostAttribute(Attribute):
97144
value: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]
98145

99146

100-
class IPHostOptional(Attribute):
147+
class IPHostAttributeOptional(Attribute):
101148
value: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
102149

103150

104-
class HashedPassword(Attribute):
105-
value: str
151+
class IPNetworkAttribute(Attribute):
152+
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
106153

107154

108-
class HashedPasswordOptional(Attribute):
109-
value: Any
155+
class IPNetworkAttributeOptional(Attribute):
156+
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
110157

111158

112-
class JSONAttribute(Attribute):
113-
value: Any
159+
class BooleanAttribute(Attribute):
160+
value: bool
114161

115162

116-
class JSONAttributeOptional(Attribute):
117-
value: Optional[Any]
163+
class BooleanAttributeOptional(Attribute):
164+
value: Optional[bool]
165+
166+
167+
class CheckboxAttribute(Attribute):
168+
value: bool
169+
170+
171+
class CheckboxAttributeOptional(Attribute):
172+
value: Optional[bool]
118173

119174

120175
class ListAttribute(Attribute):
@@ -125,6 +180,22 @@ class ListAttributeOptional(Attribute):
125180
value: Optional[list[Any]]
126181

127182

183+
class JSONAttribute(Attribute):
184+
value: Any
185+
186+
187+
class JSONAttributeOptional(Attribute):
188+
value: Optional[Any]
189+
190+
191+
class AnyAttribute(Attribute):
192+
value: float
193+
194+
195+
class AnyAttributeOptional(Attribute):
196+
value: Optional[float]
197+
198+
128199
@runtime_checkable
129200
class CoreNodeBase(Protocol):
130201
_schema: MainSchemaTypes

0 commit comments

Comments
 (0)