Skip to content

Commit 5fb0758

Browse files
authored
IHS-128: Replace Sync in protocol sync classes schema name (#586)
* IHS-128: Replace `Sync` in protocol sync classes schema name Fixes #380 This PR replaces the `Sync` word in the protocol schema name so that the correct kind can be gotten from the cache. * add towncrier * refactor get_schema_name, add test * update test
1 parent b61c7bd commit 5fb0758

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

changelog/380.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced the `Sync` word in the protocol schema name so that the correct kind can be gotten from the cache

infrahub_sdk/schema/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import inspect
45
import json
56
import warnings
67
from collections.abc import MutableMapping
@@ -185,12 +186,16 @@ def _validate_load_schema_response(response: httpx.Response) -> SchemaLoadRespon
185186

186187
@staticmethod
187188
def _get_schema_name(schema: type[SchemaType | SchemaTypeSync] | str) -> str:
188-
if hasattr(schema, "_is_runtime_protocol") and schema._is_runtime_protocol: # type: ignore[union-attr]
189-
return schema.__name__ # type: ignore[union-attr]
190-
191189
if isinstance(schema, str):
192190
return schema
193191

192+
if hasattr(schema, "_is_runtime_protocol") and getattr(schema, "_is_runtime_protocol", None):
193+
if inspect.iscoroutinefunction(schema.save):
194+
return schema.__name__
195+
if schema.__name__[-4:] == "Sync":
196+
return schema.__name__[:-4]
197+
return schema.__name__
198+
194199
raise ValueError("schema must be a protocol or a string")
195200

196201
@staticmethod

tests/unit/sdk/test_schema.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from infrahub_sdk import Config, InfrahubClient, InfrahubClientSync
1010
from infrahub_sdk.ctl.schema import display_schema_load_errors
1111
from infrahub_sdk.exceptions import SchemaNotFoundError, ValidationError
12-
from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaSync, NodeSchemaAPI
12+
from infrahub_sdk.protocols import BuiltinIPAddress, BuiltinIPAddressSync, BuiltinTag, BuiltinTagSync
13+
from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaBase, InfrahubSchemaSync, NodeSchemaAPI
1314
from infrahub_sdk.schema.repository import (
1415
InfrahubCheckDefinitionConfig,
1516
InfrahubJinja2TransformConfig,
@@ -452,3 +453,12 @@ async def test_display_schema_load_errors_details_when_error_is_in_attribute_or_
452453
Node: SecurityTailscaleSSHRule | Attribute: check_period (10080) | Extra inputs are not permitted (extra_forbidden)
453454
"""
454455
assert output == expected_console
456+
457+
458+
def test_schema_base__get_schema_name__returns_correct_schema_name_for_protocols():
459+
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTagSync) == "BuiltinTag"
460+
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTag) == "BuiltinTag"
461+
assert InfrahubSchemaBase._get_schema_name(schema="BuiltinTag") == "BuiltinTag"
462+
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddressSync) == "BuiltinIPAddress"
463+
assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddress) == "BuiltinIPAddress"
464+
assert InfrahubSchemaBase._get_schema_name(schema="BuiltinIPAddress") == "BuiltinIPAddress"

0 commit comments

Comments
 (0)