diff --git a/CHANGELOG.md b/CHANGELOG.md index c242a18c..eb3f435c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ This project uses [*towncrier*](https://towncrier.readthedocs.io/) and the chang +## [0.14.1](https://github.com/opsmill/infrahub-sdk-python/tree/v0.14.1) - 2024-10-22 + +### Fixed + +- Make `infrahubctl transform` command set up the InfrahubTransform class with an InfrahubClient instance ([#8](https://github.com/opsmill/infrahub-sdk-python/issues/8)) +- Command `infrahubctl protocols` now supports every kind of schema attribute. ([#57](https://github.com/opsmill/infrahub-sdk-python/issues/57)) + ## [0.14.0](https://github.com/opsmill/infrahub-sdk-python/tree/v0.14.0) - 2024-10-04 ### Removed diff --git a/changelog/8.fixed.md b/changelog/8.fixed.md deleted file mode 100644 index a25e1b44..00000000 --- a/changelog/8.fixed.md +++ /dev/null @@ -1 +0,0 @@ -Make `infrahubctl transform` command set up the InfrahubTransform class with an InfrahubClient instance \ No newline at end of file diff --git a/infrahub_sdk/code_generator.py b/infrahub_sdk/code_generator.py index f9e100d8..e0cc8326 100644 --- a/infrahub_sdk/code_generator.py +++ b/infrahub_sdk/code_generator.py @@ -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]): @@ -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: diff --git a/infrahub_sdk/ctl/constants.py b/infrahub_sdk/ctl/constants.py index 5c82c8a4..8d797150 100644 --- a/infrahub_sdk/ctl/constants.py +++ b/infrahub_sdk/ctl/constants.py @@ -15,6 +15,8 @@ from infrahub_sdk.node import RelatedNode, RelationshipManager {% endif %} from infrahub_sdk.protocols_base import ( + AnyAttribute, + AnyAttributeOptional, String, StringOptional, Integer, @@ -27,6 +29,8 @@ DropdownOptional, HashedPassword, HashedPasswordOptional, + MacAddress, + MacAddressOptional, IPHost, IPHostOptional, IPNetwork, @@ -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 @@ -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 @@ -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 @@ -107,6 +110,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 19d5f643..df6a9000 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] @@ -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): @@ -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): @@ -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