Skip to content

Commit e3642cd

Browse files
authored
Merge pull request #6448 from opsmill/pog-alternate-attribute-value-selection-IFC-1251
Add support for alternate fields in schema path
2 parents 4afed39 + 771cc01 commit e3642cd

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

backend/infrahub/core/attribute.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ def label(self) -> str:
782782

783783
return ""
784784

785+
@staticmethod
786+
def get_allowed_property_in_path() -> list[str]:
787+
return ["color", "description", "label", "value"]
788+
785789
@classmethod
786790
def validate_content(cls, value: Any, name: str, schema: AttributeSchema) -> None:
787791
"""Validate the content of the dropdown."""
@@ -817,7 +821,18 @@ class IPNetwork(BaseAttribute):
817821

818822
@staticmethod
819823
def get_allowed_property_in_path() -> list[str]:
820-
return ["value", "version", "binary_address", "prefixlen"]
824+
return [
825+
"binary_address",
826+
"broadcast_address",
827+
"hostmask",
828+
"netmask",
829+
"num_addresses",
830+
"prefixlen",
831+
"value",
832+
"version",
833+
"with_hostmask",
834+
"with_netmask",
835+
]
821836

822837
@property
823838
def obj(self) -> ipaddress.IPv4Network | ipaddress.IPv6Network:
@@ -950,7 +965,17 @@ class IPHost(BaseAttribute):
950965

951966
@staticmethod
952967
def get_allowed_property_in_path() -> list[str]:
953-
return ["value", "version", "binary_address"]
968+
return [
969+
"binary_address",
970+
"hostmask",
971+
"ip",
972+
"netmask",
973+
"prefixlen",
974+
"value",
975+
"version",
976+
"with_hostmask",
977+
"with_netmask",
978+
]
954979

955980
@property
956981
def obj(self) -> ipaddress.IPv4Interface | ipaddress.IPv6Interface:
@@ -1170,6 +1195,22 @@ def serialize_value(self) -> str:
11701195
"""Serialize the value as standard EUI-48 or EUI-64 before storing it in the database."""
11711196
return str(netaddr.EUI(addr=self.value))
11721197

1198+
@staticmethod
1199+
def get_allowed_property_in_path() -> list[str]:
1200+
return [
1201+
"bare",
1202+
"binary",
1203+
"dot_notation",
1204+
"ei",
1205+
"eui48",
1206+
"eui64",
1207+
"oui",
1208+
"semicolon_notation",
1209+
"split_notation",
1210+
"value",
1211+
"version",
1212+
]
1213+
11731214

11741215
class MacAddressOptional(MacAddress):
11751216
value: str | None
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
from graphene.types.field import Field
3+
4+
from infrahub.core import attribute
5+
from infrahub.graphql import types
6+
from infrahub.types import ATTRIBUTE_TYPES
7+
8+
9+
@pytest.mark.parametrize(
10+
"test_case",
11+
[pytest.param(attribute_name, id=attribute_name) for attribute_name in ATTRIBUTE_TYPES.keys()],
12+
)
13+
def test_attribute_types_allowed_property_path(test_case: str) -> None:
14+
"""Validates that the get_allowed_property_in_path() method returns the correct fields for all types
15+
16+
This ensures that we can use the entries properly when evaluating the schema path for instance with the
17+
computed attributes
18+
"""
19+
attribute_type = ATTRIBUTE_TYPES[test_case]
20+
21+
graphql_query_type = getattr(types, attribute_type.graphql_query)
22+
include_binary_address = test_case in ["IPHost", "IPNetwork"]
23+
path_list = _get_path_field_list(
24+
include_binary_address=include_binary_address, fields=graphql_query_type._meta.fields
25+
)
26+
infrahub_type: attribute.BaseAttribute = getattr(attribute, attribute_type.infrahub)
27+
assert path_list == infrahub_type.get_allowed_property_in_path()
28+
29+
30+
def _get_path_field_list(include_binary_address: bool, fields: dict[str, Field]) -> list[str]:
31+
"""Return list of valid property paths for the specified type"""
32+
excluded_fields = [
33+
"id",
34+
"is_default",
35+
"is_from_profile",
36+
"is_inherited",
37+
"is_protected",
38+
"is_visible",
39+
"owner",
40+
"source",
41+
"permissions",
42+
"updated_at",
43+
]
44+
included = ["binary_address"] if include_binary_address else []
45+
for name in fields.keys():
46+
if name not in excluded_fields:
47+
included.append(name)
48+
49+
return sorted(included)

changelog/5769.added.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!-- vale off -->
2+
Added the ability to use alternative value types for all attribute types with computed attributes. For attributes of type IPHost or Dropdown you can now access the `ip` or `label` fields and not only the `value` field.
3+
<!-- vale on -->

0 commit comments

Comments
 (0)