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,12 +269,18 @@ async def handle_pool(self, db: InfrahubDatabase, attribute: BaseAttribute, erro
262269 db = db , id = attribute .from_pool ["id" ], kind = CoreNumberPool
263270 )
264271 except NodeNotFoundError :
265- errors . append (
266- ValidationError (
267- { f" { attribute . name } .from_pool" : f"The pool requested { attribute . from_pool } was not found." }
272+ if number_pool_parameters :
273+ number_pool = await self . _create_number_pool (
274+ db = db , attribute = attribute , number_pool_parameters = number_pool_parameters
268275 )
269- )
270- return
276+
277+ else :
278+ errors .append (
279+ ValidationError (
280+ {f"{ attribute .name } .from_pool" : f"The pool requested { attribute .from_pool } was not found." }
281+ )
282+ )
283+ return
271284
272285 if (
273286 number_pool .node .value in [self ._schema .kind ] + self ._schema .inherit_from
@@ -292,6 +305,35 @@ async def handle_pool(self, db: InfrahubDatabase, attribute: BaseAttribute, erro
292305 )
293306 )
294307
308+ async def _create_number_pool (
309+ self , db : InfrahubDatabase , attribute : BaseAttribute , number_pool_parameters : NumberPoolParameters
310+ ) -> CoreNumberPool :
311+ schema = db .schema .get_node_schema (name = "CoreNumberPool" , duplicate = False )
312+
313+ pool_node = self ._schema .kind
314+ schema_attribute = self ._schema .get_attribute (attribute .schema .name )
315+ if schema_attribute .inherited :
316+ for generic_name in self ._schema .inherit_from :
317+ generic_node = db .schema .get_generic_schema (name = generic_name , duplicate = False )
318+ if attribute .schema .name in generic_node .attribute_names :
319+ pool_node = generic_node .kind
320+ break
321+
322+ number_pool = await Node .init (db = db , schema = schema , branch = self ._branch )
323+ await number_pool .new (
324+ db = db ,
325+ id = number_pool_parameters .number_pool_id ,
326+ name = f"{ pool_node } .{ attribute .schema .name } [{ number_pool_parameters .number_pool_id } ]" ,
327+ node = pool_node ,
328+ node_attribute = attribute .schema .name ,
329+ start_range = number_pool_parameters .start_range ,
330+ end_range = number_pool_parameters .end_range ,
331+ )
332+ await number_pool .save (db = db )
333+ # Do a lookup of the number pool to get the correct mapped type from the registry
334+ # without this we don't get access to the .get_resource() method.
335+ return await registry .manager .get_one_by_id_or_default_filter (db = db , id = number_pool .id , kind = CoreNumberPool )
336+
295337 async def handle_object_template (self , fields : dict , db : InfrahubDatabase , errors : list ) -> None :
296338 """Fill the `fields` parameters with values from an object template if one is in use."""
297339 object_template_field = fields .get (OBJECT_TEMPLATE_RELATIONSHIP_NAME )
@@ -342,7 +384,7 @@ async def handle_object_template(self, fields: dict, db: InfrahubDatabase, error
342384 elif relationship_peers := await relationship .get_peers (db = db ):
343385 fields [relationship_name ] = [{"id" : peer_id } for peer_id in relationship_peers ]
344386
345- async def _process_fields (self , fields : dict , db : InfrahubDatabase ) -> None :
387+ async def _process_fields (self , fields : dict , db : InfrahubDatabase ) -> None : # noqa: PLR0915
346388 errors = []
347389
348390 if "_source" in fields .keys ():
@@ -376,6 +418,9 @@ async def _process_fields(self, fields: dict, db: InfrahubDatabase) -> None:
376418 self ._computed_jinja2_attributes .append (mandatory_attr )
377419 continue
378420
421+ if mandatory_attribute .kind == "NumberPool" :
422+ continue
423+
379424 errors .append (
380425 ValidationError ({mandatory_attr : f"{ mandatory_attr } is mandatory for { self .get_kind ()} " })
381426 )
0 commit comments