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/380.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replaced the `Sync` word in the protocol schema name so that the correct kind can be gotten from the cache
11 changes: 8 additions & 3 deletions infrahub_sdk/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import inspect
import json
import warnings
from collections.abc import MutableMapping
Expand Down Expand Up @@ -185,12 +186,16 @@ def _validate_load_schema_response(response: httpx.Response) -> SchemaLoadRespon

@staticmethod
def _get_schema_name(schema: type[SchemaType | SchemaTypeSync] | str) -> str:
if hasattr(schema, "_is_runtime_protocol") and schema._is_runtime_protocol: # type: ignore[union-attr]
return schema.__name__ # type: ignore[union-attr]

if isinstance(schema, str):
return schema

if hasattr(schema, "_is_runtime_protocol") and getattr(schema, "_is_runtime_protocol", None):
if inspect.iscoroutinefunction(schema.save):
return schema.__name__
if schema.__name__[-4:] == "Sync":
return schema.__name__[:-4]
return schema.__name__

raise ValueError("schema must be a protocol or a string")

@staticmethod
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/sdk/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from infrahub_sdk import Config, InfrahubClient, InfrahubClientSync
from infrahub_sdk.ctl.schema import display_schema_load_errors
from infrahub_sdk.exceptions import SchemaNotFoundError, ValidationError
from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaSync, NodeSchemaAPI
from infrahub_sdk.protocols import BuiltinIPAddress, BuiltinIPAddressSync, BuiltinTag, BuiltinTagSync
from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaBase, InfrahubSchemaSync, NodeSchemaAPI
from infrahub_sdk.schema.repository import (
InfrahubCheckDefinitionConfig,
InfrahubJinja2TransformConfig,
Expand Down Expand Up @@ -452,3 +453,12 @@ async def test_display_schema_load_errors_details_when_error_is_in_attribute_or_
Node: SecurityTailscaleSSHRule | Attribute: check_period (10080) | Extra inputs are not permitted (extra_forbidden)
"""
assert output == expected_console


def test_schema_base__get_schema_name__returns_correct_schema_name_for_protocols():
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTagSync) == "BuiltinTag"
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTag) == "BuiltinTag"
assert InfrahubSchemaBase._get_schema_name(schema="BuiltinTag") == "BuiltinTag"
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddressSync) == "BuiltinIPAddress"
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddress) == "BuiltinIPAddress"
assert InfrahubSchemaBase._get_schema_name(schema="BuiltinIPAddress") == "BuiltinIPAddress"