Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/57.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Command `infrahubctl protocols` now supports every kind of schema attribute.
48 changes: 27 additions & 21 deletions infrahub_sdk/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@
RelationshipSchema,
)

ATTRIBUTE_KIND_MAP = {
"ID": "String",
"Text": "String",
"TextArea": "String",
"DateTime": "DateTime",
"Email": "String",
"Password": "String",
"HashedPassword": "HashedPassword",
"URL": "URL",
"File": "String",
"MacAddress": "MacAddress",
"Color": "String",
"Dropdown": "Dropdown",
"Number": "Integer",
"Bandwidth": "Integer",
"IPHost": "IPHost",
"IPNetwork": "IPNetwork",
"Boolean": "Boolean",
"Checkbox": "Boolean",
"List": "ListAttribute",
"JSON": "JSONAttribute",
"Any": "AnyAttribute",
}


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

@staticmethod
def _jinja2_filter_render_attribute(value: AttributeSchema) -> str:
attribute_kind_map = {
"boolean": "Boolean",
"datetime": "DateTime",
"dropdown": "Dropdown",
"hashedpassword": "HashedPassword",
"iphost": "IPHost",
"ipnetwork": "IPNetwork",
"json": "JSONAttribute",
"list": "ListAttribute",
"number": "Integer",
"password": "String",
"text": "String",
"textarea": "String",
"url": "URL",
}

name = value.name
kind = value.kind
attribute_kind: str = ATTRIBUTE_KIND_MAP[value.kind]

attribute_kind = attribute_kind_map[kind.lower()]
if value.optional:
attribute_kind = f"{attribute_kind}Optional"
attribute_kind += "Optional"

return f"{name}: {attribute_kind}"
return f"{value.name}: {attribute_kind}"

@staticmethod
def _jinja2_filter_render_relationship(value: RelationshipSchema, sync: bool = False) -> str:
Expand Down
15 changes: 9 additions & 6 deletions infrahub_sdk/ctl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from infrahub_sdk.node import RelatedNode, RelationshipManager
{% endif %}
from infrahub_sdk.protocols_base import (
AnyAttribute,
AnyAttributeOptional,
String,
StringOptional,
Integer,
Expand All @@ -27,6 +29,8 @@
DropdownOptional,
HashedPassword,
HashedPasswordOptional,
MacAddress,
MacAddressOptional,
IPHost,
IPHostOptional,
IPNetwork,
Expand All @@ -38,9 +42,9 @@
URL,
URLOptional,
)
{% for generic in generics %}


{% for generic in generics %}
class {{ generic.namespace + generic.name }}(CoreNode):
{% if not generic.attributes|default([]) and not generic.relationships|default([]) %}
pass
Expand All @@ -60,11 +64,10 @@ class {{ generic.namespace + generic.name }}(CoreNode):
children: RelationshipManager
{% endif %}
{% endif %}

{% endfor %}
{% for node in nodes %}


{% for node in nodes %}
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
pass
Expand All @@ -84,10 +87,10 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
children: RelationshipManager
{% endif %}
{% endif %}

{% endfor %}

{% for node in profiles %}


class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "CoreNode" }}):
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
pass
Expand All @@ -107,6 +110,6 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
children: RelationshipManager
{% endif %}
{% endif %}

{% endfor %}

"""
73 changes: 44 additions & 29 deletions infrahub_sdk/protocols_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class RelatedNodeSync(Protocol): ...
class Attribute(Protocol):
name: str
id: Optional[str]

is_default: Optional[bool]
is_from_profile: Optional[bool]
is_inherited: Optional[bool]
Expand All @@ -37,60 +36,60 @@ class StringOptional(Attribute):
value: Optional[str]


class Integer(Attribute):
value: int
class DateTime(Attribute):
value: str


class IntegerOptional(Attribute):
value: Optional[int]
class DateTimeOptional(Attribute):
value: Optional[str]


class Boolean(Attribute):
value: bool
class HashedPassword(Attribute):
value: str


class BooleanOptional(Attribute):
value: Optional[bool]
class HashedPasswordOptional(Attribute):
value: Any


class DateTime(Attribute):
class URL(Attribute):
value: str


class DateTimeOptional(Attribute):
class URLOptional(Attribute):
value: Optional[str]


class Enum(Attribute):
class MacAddress(Attribute):
value: str


class EnumOptional(Attribute):
class MacAddressOptional(Attribute):
value: Optional[str]


class URL(Attribute):
class Dropdown(Attribute):
value: str


class URLOptional(Attribute):
class DropdownOptional(Attribute):
value: Optional[str]


class Dropdown(Attribute):
class Enum(Attribute):
value: str


class DropdownOptional(Attribute):
class EnumOptional(Attribute):
value: Optional[str]


class IPNetwork(Attribute):
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
class Integer(Attribute):
value: int


class IPNetworkOptional(Attribute):
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
class IntegerOptional(Attribute):
value: Optional[int]


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


class HashedPassword(Attribute):
value: str
class IPNetwork(Attribute):
value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]


class HashedPasswordOptional(Attribute):
value: Any
class IPNetworkOptional(Attribute):
value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]


class JSONAttribute(Attribute):
value: Any
class Boolean(Attribute):
value: bool


class JSONAttributeOptional(Attribute):
value: Optional[Any]
class BooleanOptional(Attribute):
value: Optional[bool]


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


class JSONAttribute(Attribute):
value: Any


class JSONAttributeOptional(Attribute):
value: Optional[Any]


class AnyAttribute(Attribute):
value: float


class AnyAttributeOptional(Attribute):
value: Optional[float]


@runtime_checkable
class CoreNodeBase(Protocol):
_schema: MainSchemaTypes
Expand Down