Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/infrahub/core/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,14 @@ async def new_from_fields(
profiles_mandatory_field_getter: ProfilesMandatoryFieldGetter,
process_pools: bool = True,
user_id: str = SYSTEM_USER_ID,
generated_id: str | None = None,
) -> Self:
"""Initialize the node from field values keyed by name, where an `id` key, when set, is the node's uuid.

Keeping the field values in a dict lets a schema name a field like any parameter of this method.
Without an `id` key the node takes `generated_id`, a uuid the caller generated for it, which is not
checked against the database; without either, the node generates its uuid. `generated_id` is a parameter
and never a key of `fields`, so that no payload can set a uuid that is not checked.

Raises:
ValidationError: When the `id` is not a valid UUID or is already in use.
Expand All @@ -1124,7 +1128,7 @@ async def new_from_fields(
if await query.count(db=db):
raise ValidationError({"id": f"{node_id} is already in use"})

self.id = node_id or str(UUIDT())
self.id = node_id or generated_id or str(UUIDT())

await self._process_fields(
db=db,
Expand Down
253 changes: 222 additions & 31 deletions backend/infrahub/core/node/create.py

Large diffs are not rendered by default.

33 changes: 22 additions & 11 deletions backend/infrahub/core/node/lock_utils.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just refactoring to make functions accessible in backend/infrahub/core/node/create.py

Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,38 @@ def _attribute_pool_lock_name(from_pool: dict[str, Any]) -> str:
return picking_lock_name(pool_id=from_pool["id"], division_key=from_pool.get("division"))


def get_pool_lock_names(node: Node) -> set[str]:
"""Return the locks the node takes to draw from the resource pools its attributes and relationships name."""
lock_names: set[str] = set()
for attr_name in node.get_schema().attribute_names:
attribute = getattr(node, attr_name, None)
if attribute is not None and getattr(attribute, "from_pool", None) and "id" in attribute.from_pool:
lock_names.add(_attribute_pool_lock_name(from_pool=attribute.from_pool))

for rel_name in node._relationships:
for rel in node.get_relationship(rel_name)._relationships:
if rel.from_pool and "id" in rel.from_pool:
lock_names.add(ip_pool_lock_name(pool_id=rel.from_pool["id"]))
return lock_names


def ip_pool_lock_name(pool_id: str) -> str:
"""Return the name of the lock that allocations from the same IP address or IP prefix pool take."""
return f"{RESOURCE_POOL_LOCK_NAMESPACE}.{pool_id}"


def get_lock_names_on_object_mutation(node: Node, schema_branch: SchemaBranch) -> list[str]:
"""Return lock names for object on which we want to avoid concurrent mutation (create/update).

Lock names include kind, some generic kinds, resource pool ids, peer ids for cardinality one relationships,
and values of attributes of corresponding uniqueness constraints.
"""
lock_names: set[str] = set()

# Check if node is using resource manager allocation via attributes
for attr_name in node.get_schema().attribute_names:
attribute = getattr(node, attr_name, None)
if attribute is not None and getattr(attribute, "from_pool", None) and "id" in attribute.from_pool:
lock_names.add(_attribute_pool_lock_name(from_pool=attribute.from_pool))
lock_names = get_pool_lock_names(node=node)

# Check if relationships allocate resources or have cardinality one constraint
# Check if relationships have cardinality one constraint
for rel_name in node._relationships:
rel_manager = node.get_relationship(rel_name)
for rel in rel_manager._relationships:
if rel.from_pool and "id" in rel.from_pool:
lock_names.add(f"{RESOURCE_POOL_LOCK_NAMESPACE}.{rel.from_pool['id']}")

peer_id = rel.peer_id
if not peer_id or not rel.schema.identifier:
continue
Expand Down
Loading
Loading