Skip to content

Commit b5276af

Browse files
authored
Merge pull request #6637 from opsmill/pog-forbid-overwriting-generic-numberpool
Forbid overriding a generic numberpool at a node level
2 parents f48fe47 + c30d376 commit b5276af

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

backend/infrahub/core/schema/schema_branch.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,7 @@ def validate_attribute_parameters(self) -> None:
10441044
for name in self.nodes.keys():
10451045
node_schema = self.get_node(name=name, duplicate=False)
10461046
for attribute in node_schema.attributes:
1047-
if (
1048-
attribute.kind == "NumberPool"
1049-
and isinstance(attribute.parameters, NumberPoolParameters)
1050-
and not attribute.parameters.number_pool_id
1051-
):
1047+
if attribute.kind == "NumberPool" and isinstance(attribute.parameters, NumberPoolParameters):
10521048
self._validate_number_pool_parameters(
10531049
node_schema=node_schema, attribute=attribute, number_pool_parameters=attribute.parameters
10541050
)
@@ -1064,7 +1060,7 @@ def _validate_number_pool_parameters(
10641060
f"{node_schema.kind}.{attribute.name} is a NumberPool it has to be a read_only attribute"
10651061
)
10661062

1067-
if attribute.inherited:
1063+
if attribute.inherited and not number_pool_parameters.number_pool_id:
10681064
generics_with_attribute = []
10691065
for generic_name in node_schema.inherit_from:
10701066
generic_schema = self.get_generic(name=generic_name, duplicate=False)
@@ -1078,9 +1074,16 @@ def _validate_number_pool_parameters(
10781074
raise ValidationError(
10791075
f"{node_schema.kind}.{attribute.name} is a NumberPool inherited from more than one generic"
10801076
)
1077+
elif not attribute.inherited:
1078+
for generic_name in node_schema.inherit_from:
1079+
generic_schema = self.get_generic(name=generic_name, duplicate=False)
1080+
if attribute.name in generic_schema.attribute_names:
1081+
raise ValidationError(
1082+
f"Overriding '{node_schema.kind}.{attribute.name}' NumberPool attribute from generic '{generic_name}' is not supported"
1083+
)
10811084

1082-
else:
1083-
number_pool_parameters.number_pool_id = str(uuid4())
1085+
if not number_pool_parameters.number_pool_id:
1086+
number_pool_parameters.number_pool_id = str(uuid4())
10841087

10851088
def validate_computed_attributes(self) -> None:
10861089
self.computed_attributes = ComputedAttributes()

backend/tests/unit/core/schema/test_attribute_parameters.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from infrahub.core.node import Node
1010
from infrahub.core.node.resource_manager.number_pool import CoreNumberPool
1111
from infrahub.core.registry import registry
12-
from infrahub.core.schema import NodeSchema, SchemaRoot
12+
from infrahub.core.schema import GenericSchema, NodeSchema, SchemaRoot
1313
from infrahub.core.schema.attribute_parameters import (
1414
NumberAttributeParameters,
1515
NumberPoolParameters,
@@ -117,6 +117,51 @@ def test_number_pool_read_only() -> None:
117117
schema_branch.process()
118118

119119

120+
def test_number_pool_override_generic() -> None:
121+
node_schema_definition: dict[str, Any] = {
122+
"name": "NumberAttribute",
123+
"namespace": "Test",
124+
"inherit_from": ["BaseNumberAttribute"],
125+
"attributes": [
126+
{"name": "name", "kind": "Text", "unique": True},
127+
{
128+
"name": "number",
129+
"kind": "NumberPool",
130+
"optional": False,
131+
"unique": True,
132+
"read_only": True,
133+
"parameters": {"start_range": 16, "end_range": 25},
134+
},
135+
],
136+
}
137+
node_schema = NodeSchema(**node_schema_definition)
138+
generic_node_schema_definition: dict[str, Any] = {
139+
"name": "NumberAttribute",
140+
"namespace": "Base",
141+
"attributes": [
142+
{"name": "name", "kind": "Text", "unique": True},
143+
{
144+
"name": "number",
145+
"kind": "NumberPool",
146+
"optional": False,
147+
"unique": True,
148+
"read_only": True,
149+
"parameters": {"start_range": 10, "end_range": 25},
150+
},
151+
],
152+
}
153+
generic_schema = GenericSchema(**generic_node_schema_definition)
154+
155+
schema = SchemaRoot(nodes=[node_schema], generics=[generic_schema])
156+
schema_branch = SchemaBranch(cache={})
157+
schema_branch.load_schema(schema=schema)
158+
with pytest.raises(
159+
ValidationError,
160+
match="Overriding 'TestNumberAttribute.number' NumberPool attribute from generic 'BaseNumberAttribute' is not supported",
161+
):
162+
schema_branch.process()
163+
164+
120165
def test_number_pool_assign_from_generics() -> None:
121166
schema = SchemaRoot(generics=[SNOW_TASK], nodes=[SNOW_INCIDENT, SNOW_REQUEST])
122167
schema_branch = SchemaBranch(cache={})

0 commit comments

Comments
 (0)