From aa50def4d76ec745fb91d7de45a3714f25bc06a2 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Fri, 11 Oct 2024 14:48:53 +0200 Subject: [PATCH 1/2] Support checkbox kind --- infrahub_sdk/code_generator.py | 1 + infrahub_sdk/ctl/constants.py | 2 ++ infrahub_sdk/protocols_base.py | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/infrahub_sdk/code_generator.py b/infrahub_sdk/code_generator.py index 0bfb4e34..c3395023 100644 --- a/infrahub_sdk/code_generator.py +++ b/infrahub_sdk/code_generator.py @@ -70,6 +70,7 @@ def _jinja2_filter_inheritance(value: dict[str, Any]) -> str: def _jinja2_filter_render_attribute(value: AttributeSchema) -> str: attribute_kind_map = { "boolean": "Boolean", + "checkbox": "Checkbox", "datetime": "DateTime", "dropdown": "Dropdown", "hashedpassword": "HashedPassword", diff --git a/infrahub_sdk/ctl/constants.py b/infrahub_sdk/ctl/constants.py index 5c82c8a4..35f9bbbb 100644 --- a/infrahub_sdk/ctl/constants.py +++ b/infrahub_sdk/ctl/constants.py @@ -21,6 +21,8 @@ IntegerOptional, Boolean, BooleanOptional, + Checkbox, + CheckboxOptional, DateTime, DateTimeOptional, Dropdown, diff --git a/infrahub_sdk/protocols_base.py b/infrahub_sdk/protocols_base.py index e2520d01..5ca35f94 100644 --- a/infrahub_sdk/protocols_base.py +++ b/infrahub_sdk/protocols_base.py @@ -53,6 +53,14 @@ class BooleanOptional(Attribute): value: Optional[bool] +class Checkbox(Attribute): + value: bool + + +class CheckboxOptional(Attribute): + value: Optional[bool] + + class DateTime(Attribute): value: str From 56ce558ac5b897919d596cd4a9a5e06dfe780860 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Mon, 14 Oct 2024 18:19:31 +0200 Subject: [PATCH 2/2] Support all kind of attributes as protocols --- infrahub_sdk/code_generator.py | 25 +----- infrahub_sdk/ctl/constants.py | 69 ++++++++++------- infrahub_sdk/protocols_base.py | 137 ++++++++++++++++++++++++--------- 3 files changed, 147 insertions(+), 84 deletions(-) diff --git a/infrahub_sdk/code_generator.py b/infrahub_sdk/code_generator.py index c3395023..b56511ed 100644 --- a/infrahub_sdk/code_generator.py +++ b/infrahub_sdk/code_generator.py @@ -68,31 +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", - "checkbox": "Checkbox", - "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: diff --git a/infrahub_sdk/ctl/constants.py b/infrahub_sdk/ctl/constants.py index 35f9bbbb..00f9c19a 100644 --- a/infrahub_sdk/ctl/constants.py +++ b/infrahub_sdk/ctl/constants.py @@ -15,34 +15,52 @@ from infrahub_sdk.node import RelatedNode, RelationshipManager {% endif %} from infrahub_sdk.protocols_base import ( - String, - StringOptional, - Integer, - IntegerOptional, - Boolean, - BooleanOptional, - Checkbox, - CheckboxOptional, - 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 @@ -62,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 @@ -86,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 @@ -109,6 +126,6 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor children: RelationshipManager {% endif %} {% endif %} - {% endfor %} + """ diff --git a/infrahub_sdk/protocols_base.py b/infrahub_sdk/protocols_base.py index 5ca35f94..8e96fa1b 100644 --- a/infrahub_sdk/protocols_base.py +++ b/infrahub_sdk/protocols_base.py @@ -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] @@ -29,100 +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 Checkbox(Attribute): - value: bool +class DateTimeAttribute(Attribute): + value: str -class CheckboxOptional(Attribute): - value: Optional[bool] +class DateTimeAttributeOptional(Attribute): + value: Optional[str] -class DateTime(Attribute): +class EmailAttribute(Attribute): value: str -class DateTimeOptional(Attribute): +class EmailAttributeOptional(Attribute): value: Optional[str] -class Enum(Attribute): +class PasswordAttribute(Attribute): value: str -class EnumOptional(Attribute): +class PasswordAttributeOptional(Attribute): value: Optional[str] -class URL(Attribute): +class HashedPasswordAttribute(Attribute): value: str -class URLOptional(Attribute): +class HashedPasswordAttributeOptional(Attribute): value: Optional[str] -class Dropdown(Attribute): +class URLAttribute(Attribute): value: str -class DropdownOptional(Attribute): +class URLAttributeOptional(Attribute): value: Optional[str] -class IPNetwork(Attribute): - value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network] +class FileAttribute(Attribute): + value: str -class IPNetworkOptional(Attribute): - value: Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]] +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 NumberAttribute(Attribute): + value: float -class IPHost(Attribute): + +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): @@ -133,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