Skip to content

Commit cc169f3

Browse files
authored
Support all kind of attributes in protocols (#90)
* Support all kind of attributes in protocols * Adding back the initial attributes classes for builtin schema * Remove duplicated * Add changelog shard * Map to generics kind * Add back Enum class
1 parent e91bd2c commit cc169f3

File tree

4 files changed

+81
-56
lines changed

4 files changed

+81
-56
lines changed

changelog/57.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Command `infrahubctl protocols` now supports every kind of schema attribute.

infrahub_sdk/code_generator.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313
RelationshipSchema,
1414
)
1515

16+
ATTRIBUTE_KIND_MAP = {
17+
"ID": "String",
18+
"Text": "String",
19+
"TextArea": "String",
20+
"DateTime": "DateTime",
21+
"Email": "String",
22+
"Password": "String",
23+
"HashedPassword": "HashedPassword",
24+
"URL": "URL",
25+
"File": "String",
26+
"MacAddress": "MacAddress",
27+
"Color": "String",
28+
"Dropdown": "Dropdown",
29+
"Number": "Integer",
30+
"Bandwidth": "Integer",
31+
"IPHost": "IPHost",
32+
"IPNetwork": "IPNetwork",
33+
"Boolean": "Boolean",
34+
"Checkbox": "Boolean",
35+
"List": "ListAttribute",
36+
"JSON": "JSONAttribute",
37+
"Any": "AnyAttribute",
38+
}
39+
1640

1741
class CodeGenerator:
1842
def __init__(self, schema: dict[str, MainSchemaTypes]):
@@ -68,30 +92,12 @@ def _jinja2_filter_inheritance(value: dict[str, Any]) -> str:
6892

6993
@staticmethod
7094
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-
}
86-
87-
name = value.name
88-
kind = value.kind
95+
attribute_kind: str = ATTRIBUTE_KIND_MAP[value.kind]
8996

90-
attribute_kind = attribute_kind_map[kind.lower()]
9197
if value.optional:
92-
attribute_kind = f"{attribute_kind}Optional"
98+
attribute_kind += "Optional"
9399

94-
return f"{name}: {attribute_kind}"
100+
return f"{value.name}: {attribute_kind}"
95101

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

infrahub_sdk/ctl/constants.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from infrahub_sdk.node import RelatedNode, RelationshipManager
1616
{% endif %}
1717
from infrahub_sdk.protocols_base import (
18+
AnyAttribute,
19+
AnyAttributeOptional,
1820
String,
1921
StringOptional,
2022
Integer,
@@ -27,6 +29,8 @@
2729
DropdownOptional,
2830
HashedPassword,
2931
HashedPasswordOptional,
32+
MacAddress,
33+
MacAddressOptional,
3034
IPHost,
3135
IPHostOptional,
3236
IPNetwork,
@@ -38,9 +42,9 @@
3842
URL,
3943
URLOptional,
4044
)
45+
{% for generic in generics %}
4146
4247
43-
{% for generic in generics %}
4448
class {{ generic.namespace + generic.name }}(CoreNode):
4549
{% if not generic.attributes|default([]) and not generic.relationships|default([]) %}
4650
pass
@@ -60,11 +64,10 @@ class {{ generic.namespace + generic.name }}(CoreNode):
6064
children: RelationshipManager
6165
{% endif %}
6266
{% endif %}
63-
6467
{% endfor %}
68+
{% for node in nodes %}
6569
6670
67-
{% for node in nodes %}
6871
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
6972
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
7073
pass
@@ -84,10 +87,10 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
8487
children: RelationshipManager
8588
{% endif %}
8689
{% endif %}
87-
8890
{% endfor %}
89-
9091
{% for node in profiles %}
92+
93+
9194
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
9295
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
9396
pass
@@ -107,6 +110,6 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
107110
children: RelationshipManager
108111
{% endif %}
109112
{% endif %}
110-
111113
{% endfor %}
114+
112115
"""

infrahub_sdk/protocols_base.py

Lines changed: 44 additions & 29 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]
@@ -37,60 +36,60 @@ class StringOptional(Attribute):
3736
value: Optional[str]
3837

3938

40-
class Integer(Attribute):
41-
value: int
39+
class DateTime(Attribute):
40+
value: str
4241

4342

44-
class IntegerOptional(Attribute):
45-
value: Optional[int]
43+
class DateTimeOptional(Attribute):
44+
value: Optional[str]
4645

4746

48-
class Boolean(Attribute):
49-
value: bool
47+
class HashedPassword(Attribute):
48+
value: str
5049

5150

52-
class BooleanOptional(Attribute):
53-
value: Optional[bool]
51+
class HashedPasswordOptional(Attribute):
52+
value: Any
5453

5554

56-
class DateTime(Attribute):
55+
class URL(Attribute):
5756
value: str
5857

5958

60-
class DateTimeOptional(Attribute):
59+
class URLOptional(Attribute):
6160
value: Optional[str]
6261

6362

64-
class Enum(Attribute):
63+
class MacAddress(Attribute):
6564
value: str
6665

6766

68-
class EnumOptional(Attribute):
67+
class MacAddressOptional(Attribute):
6968
value: Optional[str]
7069

7170

72-
class URL(Attribute):
71+
class Dropdown(Attribute):
7372
value: str
7473

7574

76-
class URLOptional(Attribute):
75+
class DropdownOptional(Attribute):
7776
value: Optional[str]
7877

7978

80-
class Dropdown(Attribute):
79+
class Enum(Attribute):
8180
value: str
8281

8382

84-
class DropdownOptional(Attribute):
83+
class EnumOptional(Attribute):
8584
value: Optional[str]
8685

8786

88-
class IPNetwork(Attribute):
89-
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
87+
class Integer(Attribute):
88+
value: int
9089

9190

92-
class IPNetworkOptional(Attribute):
93-
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
91+
class IntegerOptional(Attribute):
92+
value: Optional[int]
9493

9594

9695
class IPHost(Attribute):
@@ -101,20 +100,20 @@ class IPHostOptional(Attribute):
101100
value: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
102101

103102

104-
class HashedPassword(Attribute):
105-
value: str
103+
class IPNetwork(Attribute):
104+
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
106105

107106

108-
class HashedPasswordOptional(Attribute):
109-
value: Any
107+
class IPNetworkOptional(Attribute):
108+
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
110109

111110

112-
class JSONAttribute(Attribute):
113-
value: Any
111+
class Boolean(Attribute):
112+
value: bool
114113

115114

116-
class JSONAttributeOptional(Attribute):
117-
value: Optional[Any]
115+
class BooleanOptional(Attribute):
116+
value: Optional[bool]
118117

119118

120119
class ListAttribute(Attribute):
@@ -125,6 +124,22 @@ class ListAttributeOptional(Attribute):
125124
value: Optional[list[Any]]
126125

127126

127+
class JSONAttribute(Attribute):
128+
value: Any
129+
130+
131+
class JSONAttributeOptional(Attribute):
132+
value: Optional[Any]
133+
134+
135+
class AnyAttribute(Attribute):
136+
value: float
137+
138+
139+
class AnyAttributeOptional(Attribute):
140+
value: Optional[float]
141+
142+
128143
@runtime_checkable
129144
class CoreNodeBase(Protocol):
130145
_schema: MainSchemaTypes

0 commit comments

Comments
 (0)