Skip to content

Support all attributes kind in protocols #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 4 additions & 20 deletions infrahub_sdk/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,14 @@ 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",
}
attribute_kind: str = value.kind

name = value.name
kind = value.kind
attribute_kind += "Attribute"

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
67 changes: 43 additions & 24 deletions infrahub_sdk/ctl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,52 @@
from infrahub_sdk.node import RelatedNode, RelationshipManager
{% endif %}
from infrahub_sdk.protocols_base import (
String,
StringOptional,
Integer,
IntegerOptional,
Boolean,
BooleanOptional,
DateTime,
DateTimeOptional,
Dropdown,
DropdownOptional,
HashedPassword,
HashedPasswordOptional,
IPHost,
IPHostOptional,
IPNetwork,
IPNetworkOptional,
AnyAttribute,
AnyAttributeOptional,
BandwidthAttribute,
BandwidthAttributeOptional,
BooleanAttribute,
BooleanAttributeOptional,
CheckboxAttribute,
CheckboxAttributeOptional,
ColorAttribute,
ColorAttributeOptional,
DateTimeAttribute,
DateTimeAttributeOptional,
DropdownAttribute,
DropdownAttributeOptional,
EmailAttribute,
EmailAttributeOptional,
FileAttribute,
FileAttributeOptional,
HashedPasswordAttribute,
HashedPasswordAttributeOptional,
IDAttribute,
IDAttributeOptional,
IPHostAttribute,
IPHostAttributeOptional,
IPNetworkAttribute,
IPNetworkAttributeOptional,
JSONAttribute,
JSONAttributeOptional,
ListAttribute,
ListAttributeOptional,
URL,
URLOptional,
MacAddressAttribute,
MacAddressAttributeOptional,
NumberAttribute,
NumberAttributeOptional,
PasswordAttribute,
PasswordAttributeOptional,
TextAreaAttribute,
TextAreaAttributeOptional,
TextAttribute,
TextAttributeOptional,
URLAttribute,
URLAttributeOptional,
)
{% 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 +80,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 +103,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 +126,6 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
children: RelationshipManager
{% endif %}
{% endif %}

{% endfor %}

"""
137 changes: 104 additions & 33 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 @@ -29,92 +28,148 @@ class Attribute(Protocol):
is_protected: Optional[bool]


class String(Attribute):
class IDAttribute(Attribute):
value: int


class IDAttributeOptional(Attribute):
value: Optional[int]


class DropdownAttribute(Attribute):
value: str


class StringOptional(Attribute):
class DropdownAttributeOptional(Attribute):
value: Optional[str]


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


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


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


class BooleanOptional(Attribute):
value: Optional[bool]
class TextAreaAttributeOptional(Attribute):
value: Optional[str]


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


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


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


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


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


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


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


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


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


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


class FileAttribute(Attribute):
value: str


class FileAttributeOptional(Attribute):
value: Optional[str]


class MacAddressAttribute(Attribute):
value: str


class MacAddressAttributeOptional(Attribute):
value: Optional[str]


class ColorAttribute(Attribute):
value: str


class ColorAttributeOptional(Attribute):
value: Optional[str]


class IPHost(Attribute):
class NumberAttribute(Attribute):
value: float


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


class BandwidthAttribute(Attribute):
value: float


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


class IPHostAttribute(Attribute):
value: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]


class IPHostOptional(Attribute):
class IPHostAttributeOptional(Attribute):
value: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]


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


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


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


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


class CheckboxAttribute(Attribute):
value: bool


class CheckboxAttributeOptional(Attribute):
value: Optional[bool]


class ListAttribute(Attribute):
Expand All @@ -125,6 +180,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
Loading