3030 RelationshipSchema ,
3131 TemplateSchema ,
3232)
33+ from infrahub .core .schema .attribute_parameters import NumberPoolParameters
3334from infrahub .core .timestamp import Timestamp
3435from infrahub .exceptions import InitializationError , NodeNotFoundError , PoolExhaustedError , ValidationError
3536from infrahub .types import ATTRIBUTE_TYPES
@@ -254,6 +255,12 @@ async def handle_pool(self, db: InfrahubDatabase, attribute: BaseAttribute, erro
254255 within the create code.
255256 """
256257
258+ number_pool_parameters : NumberPoolParameters | None = None
259+ if attribute .schema .kind == "NumberPool" and isinstance (attribute .schema .parameters , NumberPoolParameters ):
260+ attribute .from_pool = {"id" : attribute .schema .parameters .number_pool_id }
261+ attribute .is_default = False
262+ number_pool_parameters = attribute .schema .parameters
263+
257264 if not attribute .from_pool :
258265 return
259266
@@ -262,6 +269,11 @@ async def handle_pool(self, db: InfrahubDatabase, attribute: BaseAttribute, erro
262269 db = db , id = attribute .from_pool ["id" ], kind = CoreNumberPool
263270 )
264271 except NodeNotFoundError :
272+ if number_pool_parameters :
273+ await self ._create_number_pool (
274+ db = db , attribute = attribute , errors = errors , number_pool_parameters = number_pool_parameters
275+ )
276+ return
265277 errors .append (
266278 ValidationError (
267279 {f"{ attribute .name } .from_pool" : f"The pool requested { attribute .from_pool } was not found." }
@@ -292,6 +304,33 @@ async def handle_pool(self, db: InfrahubDatabase, attribute: BaseAttribute, erro
292304 )
293305 )
294306
307+ async def _create_number_pool (
308+ self , db : InfrahubDatabase , attribute : BaseAttribute , errors : list , number_pool_parameters : NumberPoolParameters
309+ ) -> None :
310+ schema = db .schema .get_node_schema (name = "CoreNumberPool" , duplicate = False )
311+
312+ pool_node = self ._schema .kind
313+ schema_attribute = self ._schema .get_attribute (attribute .schema .name )
314+ if schema_attribute .inherited :
315+ for generic_name in self ._schema .inherit_from :
316+ generic_node = db .schema .get_generic_schema (name = generic_name , duplicate = False )
317+ if attribute .schema .name in generic_node .attribute_names :
318+ pool_node = generic_node .kind
319+ break
320+
321+ number_pool = await Node .init (db = db , schema = schema , branch = self ._branch )
322+ await number_pool .new (
323+ db = db ,
324+ id = number_pool_parameters .number_pool_id ,
325+ name = f"{ pool_node } .{ attribute .schema .name } [{ number_pool_parameters .number_pool_id } ]" ,
326+ node = pool_node ,
327+ node_attribute = attribute .schema .name ,
328+ start_range = number_pool_parameters .start_range ,
329+ end_range = number_pool_parameters .end_range ,
330+ )
331+ await number_pool .save (db = db )
332+ await self .handle_pool (db = db , attribute = attribute , errors = errors )
333+
295334 async def handle_object_template (self , fields : dict , db : InfrahubDatabase , errors : list ) -> None :
296335 """Fill the `fields` parameters with values from an object template if one is in use."""
297336 object_template_field = fields .get (OBJECT_TEMPLATE_RELATIONSHIP_NAME )
@@ -342,7 +381,7 @@ async def handle_object_template(self, fields: dict, db: InfrahubDatabase, error
342381 elif relationship_peers := await relationship .get_peers (db = db ):
343382 fields [relationship_name ] = [{"id" : peer_id } for peer_id in relationship_peers ]
344383
345- async def _process_fields (self , fields : dict , db : InfrahubDatabase ) -> None :
384+ async def _process_fields (self , fields : dict , db : InfrahubDatabase ) -> None : # noqa: PLR0915
346385 errors = []
347386
348387 if "_source" in fields .keys ():
@@ -376,6 +415,9 @@ async def _process_fields(self, fields: dict, db: InfrahubDatabase) -> None:
376415 self ._computed_jinja2_attributes .append (mandatory_attr )
377416 continue
378417
418+ if mandatory_attribute .kind == "NumberPool" :
419+ continue
420+
379421 errors .append (
380422 ValidationError ({mandatory_attr : f"{ mandatory_attr } is mandatory for { self .get_kind ()} " })
381423 )
0 commit comments