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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ This project uses [*towncrier*](https://towncrier.readthedocs.io/) and the chang

<!-- towncrier release notes start -->

## [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
Expand Down
1 change: 0 additions & 1 deletion changelog/8.fixed.md

This file was deleted.

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 @@

@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]

Check warning on line 95 in infrahub_sdk/code_generator.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/code_generator.py#L95

Added line #L95 was not covered by tests

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

Check warning on line 98 in infrahub_sdk/code_generator.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/code_generator.py#L98

Added line #L98 was not covered by tests

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

Check warning on line 100 in infrahub_sdk/code_generator.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/code_generator.py#L100

Added line #L100 was not covered by tests

@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