infrahub-server 1.2.11__py3-none-any.whl → 1.3.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. infrahub/actions/constants.py +86 -0
  2. infrahub/actions/gather.py +114 -0
  3. infrahub/actions/models.py +241 -0
  4. infrahub/actions/parsers.py +104 -0
  5. infrahub/actions/schema.py +382 -0
  6. infrahub/actions/tasks.py +126 -0
  7. infrahub/actions/triggers.py +21 -0
  8. infrahub/cli/db.py +1 -2
  9. infrahub/core/account.py +24 -47
  10. infrahub/core/attribute.py +13 -15
  11. infrahub/core/constants/__init__.py +5 -0
  12. infrahub/core/constants/infrahubkind.py +9 -0
  13. infrahub/core/convert_object_type/__init__.py +0 -0
  14. infrahub/core/convert_object_type/conversion.py +122 -0
  15. infrahub/core/convert_object_type/schema_mapping.py +56 -0
  16. infrahub/core/diff/query/all_conflicts.py +1 -5
  17. infrahub/core/diff/query/artifact.py +10 -20
  18. infrahub/core/diff/query/diff_get.py +3 -6
  19. infrahub/core/diff/query/field_summary.py +2 -4
  20. infrahub/core/diff/query/merge.py +70 -123
  21. infrahub/core/diff/query/save.py +20 -32
  22. infrahub/core/diff/query/summary_counts_enricher.py +34 -54
  23. infrahub/core/manager.py +14 -11
  24. infrahub/core/migrations/graph/m003_relationship_parent_optional.py +1 -2
  25. infrahub/core/migrations/graph/m013_convert_git_password_credential.py +2 -4
  26. infrahub/core/migrations/graph/m019_restore_rels_to_time.py +11 -22
  27. infrahub/core/migrations/graph/m020_duplicate_edges.py +3 -6
  28. infrahub/core/migrations/graph/m021_missing_hierarchy_merge.py +1 -2
  29. infrahub/core/migrations/graph/m024_missing_hierarchy_backfill.py +1 -2
  30. infrahub/core/migrations/query/attribute_add.py +1 -2
  31. infrahub/core/migrations/query/attribute_rename.py +5 -10
  32. infrahub/core/migrations/query/delete_element_in_schema.py +19 -17
  33. infrahub/core/migrations/query/node_duplicate.py +19 -21
  34. infrahub/core/migrations/query/relationship_duplicate.py +19 -17
  35. infrahub/core/migrations/schema/node_attribute_remove.py +4 -8
  36. infrahub/core/migrations/schema/node_remove.py +19 -19
  37. infrahub/core/models.py +29 -2
  38. infrahub/core/node/__init__.py +90 -18
  39. infrahub/core/node/create.py +211 -0
  40. infrahub/core/node/resource_manager/number_pool.py +31 -5
  41. infrahub/core/node/standard.py +6 -1
  42. infrahub/core/protocols.py +56 -0
  43. infrahub/core/protocols_base.py +3 -0
  44. infrahub/core/query/__init__.py +2 -2
  45. infrahub/core/query/diff.py +19 -32
  46. infrahub/core/query/ipam.py +10 -20
  47. infrahub/core/query/node.py +28 -46
  48. infrahub/core/query/relationship.py +53 -32
  49. infrahub/core/query/resource_manager.py +1 -2
  50. infrahub/core/query/subquery.py +2 -4
  51. infrahub/core/relationship/model.py +3 -0
  52. infrahub/core/schema/__init__.py +2 -1
  53. infrahub/core/schema/attribute_parameters.py +160 -0
  54. infrahub/core/schema/attribute_schema.py +111 -8
  55. infrahub/core/schema/basenode_schema.py +25 -1
  56. infrahub/core/schema/definitions/core/__init__.py +29 -1
  57. infrahub/core/schema/definitions/core/group.py +45 -0
  58. infrahub/core/schema/definitions/internal.py +27 -4
  59. infrahub/core/schema/generated/attribute_schema.py +16 -3
  60. infrahub/core/schema/manager.py +3 -0
  61. infrahub/core/schema/schema_branch.py +67 -7
  62. infrahub/core/validators/__init__.py +13 -1
  63. infrahub/core/validators/attribute/choices.py +1 -3
  64. infrahub/core/validators/attribute/enum.py +1 -3
  65. infrahub/core/validators/attribute/kind.py +1 -3
  66. infrahub/core/validators/attribute/length.py +13 -7
  67. infrahub/core/validators/attribute/min_max.py +118 -0
  68. infrahub/core/validators/attribute/number_pool.py +106 -0
  69. infrahub/core/validators/attribute/optional.py +1 -4
  70. infrahub/core/validators/attribute/regex.py +5 -6
  71. infrahub/core/validators/attribute/unique.py +1 -3
  72. infrahub/core/validators/determiner.py +18 -2
  73. infrahub/core/validators/enum.py +12 -0
  74. infrahub/core/validators/node/hierarchy.py +3 -6
  75. infrahub/core/validators/query.py +1 -3
  76. infrahub/core/validators/relationship/count.py +6 -12
  77. infrahub/core/validators/relationship/optional.py +2 -4
  78. infrahub/core/validators/relationship/peer.py +3 -8
  79. infrahub/core/validators/uniqueness/query.py +5 -9
  80. infrahub/database/__init__.py +11 -2
  81. infrahub/events/group_action.py +1 -0
  82. infrahub/git/base.py +5 -3
  83. infrahub/git/integrator.py +102 -3
  84. infrahub/graphql/analyzer.py +139 -18
  85. infrahub/graphql/manager.py +4 -0
  86. infrahub/graphql/mutations/action.py +164 -0
  87. infrahub/graphql/mutations/convert_object_type.py +62 -0
  88. infrahub/graphql/mutations/main.py +24 -175
  89. infrahub/graphql/mutations/proposed_change.py +20 -17
  90. infrahub/graphql/mutations/resource_manager.py +62 -6
  91. infrahub/graphql/queries/convert_object_type_mapping.py +36 -0
  92. infrahub/graphql/queries/resource_manager.py +7 -1
  93. infrahub/graphql/schema.py +6 -0
  94. infrahub/menu/menu.py +31 -0
  95. infrahub/message_bus/messages/__init__.py +0 -10
  96. infrahub/message_bus/operations/__init__.py +0 -8
  97. infrahub/patch/queries/consolidate_duplicated_nodes.py +3 -6
  98. infrahub/patch/queries/delete_duplicated_edges.py +5 -10
  99. infrahub/pools/number.py +5 -3
  100. infrahub/prefect_server/models.py +1 -19
  101. infrahub/proposed_change/models.py +68 -3
  102. infrahub/proposed_change/tasks.py +907 -30
  103. infrahub/task_manager/models.py +10 -6
  104. infrahub/trigger/catalogue.py +2 -0
  105. infrahub/trigger/models.py +18 -2
  106. infrahub/trigger/tasks.py +3 -1
  107. infrahub/types.py +6 -0
  108. infrahub/workflows/catalogue.py +76 -0
  109. infrahub_sdk/client.py +43 -10
  110. infrahub_sdk/node/__init__.py +39 -0
  111. infrahub_sdk/node/attribute.py +122 -0
  112. infrahub_sdk/node/constants.py +21 -0
  113. infrahub_sdk/{node.py → node/node.py} +50 -749
  114. infrahub_sdk/node/parsers.py +15 -0
  115. infrahub_sdk/node/property.py +24 -0
  116. infrahub_sdk/node/related_node.py +266 -0
  117. infrahub_sdk/node/relationship.py +302 -0
  118. infrahub_sdk/protocols.py +112 -0
  119. infrahub_sdk/protocols_base.py +34 -2
  120. infrahub_sdk/query_groups.py +13 -2
  121. infrahub_sdk/schema/main.py +1 -0
  122. infrahub_sdk/schema/repository.py +16 -0
  123. infrahub_sdk/spec/object.py +1 -1
  124. infrahub_sdk/store.py +1 -1
  125. infrahub_sdk/testing/schemas/car_person.py +1 -0
  126. {infrahub_server-1.2.11.dist-info → infrahub_server-1.3.0b1.dist-info}/METADATA +4 -4
  127. {infrahub_server-1.2.11.dist-info → infrahub_server-1.3.0b1.dist-info}/RECORD +134 -122
  128. {infrahub_server-1.2.11.dist-info → infrahub_server-1.3.0b1.dist-info}/WHEEL +1 -1
  129. infrahub_testcontainers/container.py +0 -1
  130. infrahub_testcontainers/docker-compose.test.yml +1 -1
  131. infrahub_testcontainers/helpers.py +8 -2
  132. infrahub/message_bus/messages/check_generator_run.py +0 -26
  133. infrahub/message_bus/messages/finalize_validator_execution.py +0 -15
  134. infrahub/message_bus/messages/proposed_change/base_with_diff.py +0 -16
  135. infrahub/message_bus/messages/proposed_change/request_proposedchange_refreshartifacts.py +0 -11
  136. infrahub/message_bus/messages/request_generatordefinition_check.py +0 -20
  137. infrahub/message_bus/messages/request_proposedchange_pipeline.py +0 -23
  138. infrahub/message_bus/operations/check/__init__.py +0 -3
  139. infrahub/message_bus/operations/check/generator.py +0 -156
  140. infrahub/message_bus/operations/finalize/__init__.py +0 -3
  141. infrahub/message_bus/operations/finalize/validator.py +0 -133
  142. infrahub/message_bus/operations/requests/__init__.py +0 -9
  143. infrahub/message_bus/operations/requests/generator_definition.py +0 -140
  144. infrahub/message_bus/operations/requests/proposed_change.py +0 -629
  145. /infrahub/{message_bus/messages/proposed_change → actions}/__init__.py +0 -0
  146. {infrahub_server-1.2.11.dist-info → infrahub_server-1.3.0b1.dist-info}/LICENSE.txt +0 -0
  147. {infrahub_server-1.2.11.dist-info → infrahub_server-1.3.0b1.dist-info}/entry_points.txt +0 -0
@@ -1,7 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from dataclasses import dataclass, field
4
- from typing import TYPE_CHECKING, Any, Mapping
4
+ from typing import TYPE_CHECKING, Any
5
5
 
6
6
  from graphene import InputObjectType, Mutation
7
7
  from graphene.types.mutation import MutationOptions
@@ -9,12 +9,15 @@ from infrahub_sdk.utils import extract_fields
9
9
  from typing_extensions import Self
10
10
 
11
11
  from infrahub import config, lock
12
- from infrahub.core import registry
13
- from infrahub.core.constants import InfrahubKind, MutationAction, RelationshipCardinality, RelationshipKind
12
+ from infrahub.core.constants import InfrahubKind, MutationAction
14
13
  from infrahub.core.constraint.node.runner import NodeConstraintRunner
15
14
  from infrahub.core.manager import NodeManager
16
- from infrahub.core.node import Node
17
- from infrahub.core.schema import MainSchemaTypes, NodeSchema, RelationshipSchema
15
+ from infrahub.core.node.create import (
16
+ create_node,
17
+ get_profile_ids,
18
+ refresh_for_profile_update,
19
+ )
20
+ from infrahub.core.schema import MainSchemaTypes, NodeSchema
18
21
  from infrahub.core.schema.generic_schema import GenericSchema
19
22
  from infrahub.core.schema.profile_schema import ProfileSchema
20
23
  from infrahub.core.schema.template_schema import TemplateSchema
@@ -33,8 +36,7 @@ if TYPE_CHECKING:
33
36
  from graphql import GraphQLResolveInfo
34
37
 
35
38
  from infrahub.core.branch import Branch
36
- from infrahub.core.protocols import CoreObjectTemplate
37
- from infrahub.core.relationship.model import RelationshipManager
39
+ from infrahub.core.node import Node
38
40
  from infrahub.core.schema.schema_branch import SchemaBranch
39
41
  from infrahub.database import InfrahubDatabase
40
42
  from infrahub.graphql.types.context import ContextInput
@@ -143,33 +145,6 @@ class InfrahubMutationMixin:
143
145
 
144
146
  return mutation
145
147
 
146
- @classmethod
147
- async def _get_profile_ids(cls, db: InfrahubDatabase, obj: Node) -> set[str]:
148
- if not hasattr(obj, "profiles"):
149
- return set()
150
- profile_rels = await obj.profiles.get_relationships(db=db)
151
- return {pr.peer_id for pr in profile_rels}
152
-
153
- @classmethod
154
- async def _refresh_for_profile_update(
155
- cls, db: InfrahubDatabase, branch: Branch, obj: Node, previous_profile_ids: set[str] | None = None
156
- ) -> Node:
157
- if not hasattr(obj, "profiles"):
158
- return obj
159
- current_profile_ids = await cls._get_profile_ids(db=db, obj=obj)
160
- if previous_profile_ids is None or previous_profile_ids != current_profile_ids:
161
- refreshed_node = await NodeManager.get_one_by_id_or_default_filter(
162
- db=db,
163
- kind=cls._meta.active_schema.kind,
164
- id=obj.get_id(),
165
- branch=branch,
166
- include_owner=True,
167
- include_source=True,
168
- )
169
- refreshed_node._node_changelog = obj.node_changelog
170
- return refreshed_node
171
- return obj
172
-
173
148
  @classmethod
174
149
  async def _call_mutate_create_object(cls, data: InputObjectType, db: InfrahubDatabase, branch: Branch) -> Node:
175
150
  """
@@ -185,100 +160,6 @@ class InfrahubMutationMixin:
185
160
 
186
161
  return await cls.mutate_create_object(data=data, db=db, branch=branch)
187
162
 
188
- @classmethod
189
- async def _get_template_relationship_peers(
190
- cls, db: InfrahubDatabase, template: CoreObjectTemplate, relationship: RelationshipSchema
191
- ) -> Mapping[str, Node]:
192
- """For a given relationship on the template, fetch the related peers."""
193
- template_relationship_manager: RelationshipManager = getattr(template, relationship.name)
194
- if relationship.cardinality == RelationshipCardinality.MANY:
195
- return await template_relationship_manager.get_peers(db=db)
196
-
197
- peers: dict[str, Node] = {}
198
- template_relationship_peer = await template_relationship_manager.get_peer(db=db)
199
- if template_relationship_peer:
200
- peers[template_relationship_peer.id] = template_relationship_peer
201
- return peers
202
-
203
- @classmethod
204
- async def _extract_peer_data(
205
- cls,
206
- db: InfrahubDatabase,
207
- template_peer: Node,
208
- obj_peer_schema: MainSchemaTypes,
209
- parent_obj: Node,
210
- current_template: CoreObjectTemplate,
211
- ) -> Mapping[str, Any]:
212
- obj_peer_data: dict[str, Any] = {}
213
-
214
- for attr in template_peer.get_schema().attribute_names:
215
- if attr not in obj_peer_schema.attribute_names:
216
- continue
217
- obj_peer_data[attr] = {"value": getattr(template_peer, attr).value, "source": template_peer.id}
218
-
219
- for rel in template_peer.get_schema().relationship_names:
220
- rel_manager: RelationshipManager = getattr(template_peer, rel)
221
- if (
222
- rel_manager.schema.kind not in [RelationshipKind.COMPONENT, RelationshipKind.PARENT]
223
- or rel_manager.schema.name not in obj_peer_schema.relationship_names
224
- ):
225
- continue
226
-
227
- if list(await rel_manager.get_peers(db=db)) == [current_template.id]:
228
- obj_peer_data[rel] = {"id": parent_obj.id}
229
-
230
- return obj_peer_data
231
-
232
- @classmethod
233
- async def _handle_template_relationships(
234
- cls,
235
- db: InfrahubDatabase,
236
- branch: Branch,
237
- obj: Node,
238
- template: CoreObjectTemplate,
239
- data: InputObjectType,
240
- constraint_runner: NodeConstraintRunner | None = None,
241
- ) -> None:
242
- if constraint_runner is None:
243
- component_registry = get_component_registry()
244
- constraint_runner = await component_registry.get_component(NodeConstraintRunner, db=db, branch=branch)
245
-
246
- for relationship in obj.get_relationships(kind=RelationshipKind.COMPONENT, exclude=list(data)):
247
- template_relationship_peers = await cls._get_template_relationship_peers(
248
- db=db, template=template, relationship=relationship
249
- )
250
- if not template_relationship_peers:
251
- continue
252
-
253
- for template_relationship_peer in template_relationship_peers.values():
254
- # We retrieve peer schema for each peer in case we are processing a relationship which is based on a generic
255
- obj_peer_schema = registry.schema.get_node_schema(
256
- name=template_relationship_peer.get_schema().kind.removeprefix("Template"),
257
- branch=branch,
258
- duplicate=False,
259
- )
260
- obj_peer_data = await cls._extract_peer_data(
261
- db=db,
262
- template_peer=template_relationship_peer,
263
- obj_peer_schema=obj_peer_schema,
264
- parent_obj=obj,
265
- current_template=template,
266
- )
267
-
268
- obj_peer = await Node.init(schema=obj_peer_schema, db=db, branch=branch)
269
- await obj_peer.new(db=db, **obj_peer_data)
270
- await constraint_runner.check(node=obj_peer, field_filters=list(obj_peer_data))
271
- await obj_peer.save(db=db)
272
-
273
- await cls._handle_template_relationships(
274
- db=db,
275
- branch=branch,
276
- constraint_runner=constraint_runner,
277
- obj=obj_peer,
278
- template=template_relationship_peer,
279
- data=data,
280
- )
281
-
282
163
  @classmethod
283
164
  async def mutate_create(
284
165
  cls,
@@ -301,51 +182,15 @@ class InfrahubMutationMixin:
301
182
  db: InfrahubDatabase,
302
183
  branch: Branch,
303
184
  ) -> Node:
304
- component_registry = get_component_registry()
305
- node_constraint_runner = await component_registry.get_component(
306
- NodeConstraintRunner, db=db.start_session(), branch=branch
185
+ schema = cls._meta.active_schema
186
+ if isinstance(schema, GenericSchema):
187
+ raise ValueError(f"Node of generic schema `{schema.name=}` can not be instantiated.")
188
+ return await create_node(
189
+ data=dict(data),
190
+ db=db,
191
+ branch=branch,
192
+ schema=schema,
307
193
  )
308
- node_class = Node
309
- if cls._meta.active_schema.kind in registry.node:
310
- node_class = registry.node[cls._meta.active_schema.kind]
311
-
312
- fields_to_validate = list(data)
313
- if db.is_transaction:
314
- obj = await node_class.init(db=db, schema=cls._meta.schema, branch=branch)
315
- await obj.new(db=db, **data)
316
- await node_constraint_runner.check(node=obj, field_filters=fields_to_validate)
317
- await obj.save(db=db)
318
-
319
- object_template = await obj.get_object_template(db=db)
320
- if object_template:
321
- await cls._handle_template_relationships(
322
- db=db,
323
- branch=branch,
324
- template=object_template,
325
- obj=obj,
326
- data=data,
327
- )
328
- else:
329
- async with db.start_transaction() as dbt:
330
- obj = await node_class.init(db=dbt, schema=cls._meta.schema, branch=branch)
331
- await obj.new(db=dbt, **data)
332
- await node_constraint_runner.check(node=obj, field_filters=fields_to_validate)
333
- await obj.save(db=dbt)
334
-
335
- object_template = await obj.get_object_template(db=dbt)
336
- if object_template:
337
- await cls._handle_template_relationships(
338
- db=dbt,
339
- branch=branch,
340
- template=object_template,
341
- obj=obj,
342
- data=data,
343
- )
344
-
345
- if await cls._get_profile_ids(db=db, obj=obj):
346
- obj = await cls._refresh_for_profile_update(db=db, branch=branch, obj=obj)
347
-
348
- return obj
349
194
 
350
195
  @classmethod
351
196
  async def mutate_create_to_graphql(cls, info: GraphQLResolveInfo, db: InfrahubDatabase, obj: Node) -> Self:
@@ -439,7 +284,7 @@ class InfrahubMutationMixin:
439
284
  component_registry = get_component_registry()
440
285
  node_constraint_runner = await component_registry.get_component(NodeConstraintRunner, db=db, branch=branch)
441
286
 
442
- before_mutate_profile_ids = await cls._get_profile_ids(db=db, obj=obj)
287
+ before_mutate_profile_ids = await get_profile_ids(db=db, obj=obj)
443
288
  await obj.from_graphql(db=db, data=data)
444
289
  fields_to_validate = list(data)
445
290
  await node_constraint_runner.check(
@@ -453,8 +298,12 @@ class InfrahubMutationMixin:
453
298
 
454
299
  await obj.save(db=db, fields=fields)
455
300
 
456
- obj = await cls._refresh_for_profile_update(
457
- db=db, branch=branch, obj=obj, previous_profile_ids=before_mutate_profile_ids
301
+ obj = await refresh_for_profile_update(
302
+ db=db,
303
+ branch=branch,
304
+ obj=obj,
305
+ previous_profile_ids=before_mutate_profile_ids,
306
+ schema=cls._meta.active_schema,
458
307
  )
459
308
  return obj
460
309
 
@@ -18,10 +18,10 @@ from infrahub.database import InfrahubDatabase, retry_db_transaction
18
18
  from infrahub.exceptions import BranchNotFoundError, PermissionDeniedError, ValidationError
19
19
  from infrahub.graphql.mutations.main import InfrahubMutationMixin
20
20
  from infrahub.graphql.types.enums import CheckType as GraphQLCheckType
21
- from infrahub.message_bus import messages
22
21
  from infrahub.proposed_change.constants import ProposedChangeState
23
- from infrahub.workflows.catalogue import PROPOSED_CHANGE_MERGE
22
+ from infrahub.workflows.catalogue import PROPOSED_CHANGE_MERGE, REQUEST_PROPOSED_CHANGE_PIPELINE
24
23
 
24
+ from ...proposed_change.models import RequestProposedChangePipeline
25
25
  from ..types.task import TaskInfo
26
26
  from .main import InfrahubMutationOptions
27
27
 
@@ -65,18 +65,18 @@ class InfrahubProposedChangeMutation(InfrahubMutationMixin, Mutation):
65
65
  )
66
66
 
67
67
  if graphql_context.service:
68
- message_list = [
69
- messages.RequestProposedChangePipeline(
70
- proposed_change=proposed_change.id,
71
- source_branch=source_branch.name,
72
- source_branch_sync_with_git=source_branch.sync_with_git,
73
- destination_branch=destination_branch,
74
- context=graphql_context.get_context(),
75
- ),
76
- ]
77
-
78
- for message in message_list:
79
- await graphql_context.service.message_bus.send(message=message)
68
+ request_proposed_change_model = RequestProposedChangePipeline(
69
+ proposed_change=proposed_change.id,
70
+ source_branch=source_branch.name,
71
+ source_branch_sync_with_git=source_branch.sync_with_git,
72
+ destination_branch=destination_branch,
73
+ )
74
+
75
+ await graphql_context.service.workflow.submit_workflow(
76
+ workflow=REQUEST_PROPOSED_CHANGE_PIPELINE,
77
+ parameters={"model": request_proposed_change_model},
78
+ context=graphql_context.get_context(),
79
+ )
80
80
 
81
81
  return proposed_change, result
82
82
 
@@ -175,16 +175,19 @@ class ProposedChangeRequestRunCheck(Mutation):
175
175
  destination_branch = proposed_change.destination_branch.value
176
176
  source_branch = await _get_source_branch(db=graphql_context.db, name=proposed_change.source_branch.value)
177
177
 
178
- message = messages.RequestProposedChangePipeline(
178
+ request_proposed_change_model = RequestProposedChangePipeline(
179
179
  proposed_change=proposed_change.id,
180
180
  source_branch=source_branch.name,
181
181
  source_branch_sync_with_git=source_branch.sync_with_git,
182
182
  destination_branch=destination_branch,
183
183
  check_type=check_type,
184
- context=graphql_context.get_context(),
185
184
  )
186
185
  if graphql_context.service:
187
- await graphql_context.service.message_bus.send(message=message)
186
+ await graphql_context.service.workflow.submit_workflow(
187
+ workflow=REQUEST_PROPOSED_CHANGE_PIPELINE,
188
+ parameters={"model": request_proposed_change_model},
189
+ context=graphql_context.get_context(),
190
+ )
188
191
 
189
192
  return {"ok": True}
190
193
 
@@ -6,15 +6,17 @@ from graphene import Boolean, Field, InputField, InputObjectType, Int, List, Mut
6
6
  from graphene.types.generic import GenericScalar
7
7
  from typing_extensions import Self
8
8
 
9
- from infrahub.core import registry
9
+ from infrahub.core import protocols, registry
10
10
  from infrahub.core.constants import InfrahubKind
11
11
  from infrahub.core.ipam.constants import PrefixMemberType
12
+ from infrahub.core.manager import NodeManager
12
13
  from infrahub.core.schema import NodeSchema
14
+ from infrahub.core.schema.attribute_parameters import NumberAttributeParameters
13
15
  from infrahub.database import retry_db_transaction
14
16
  from infrahub.exceptions import QueryValidationError, SchemaNotFoundError, ValidationError
15
17
 
16
18
  from ..queries.resource_manager import PoolAllocatedNode
17
- from .main import InfrahubMutationMixin, InfrahubMutationOptions
19
+ from .main import DeleteResult, InfrahubMutationMixin, InfrahubMutationOptions
18
20
 
19
21
  if TYPE_CHECKING:
20
22
  from graphql import GraphQLResolveInfo
@@ -177,14 +179,16 @@ class InfrahubNumberPoolMutation(InfrahubMutationMixin, Mutation):
177
179
  database: InfrahubDatabase | None = None, # noqa: ARG003
178
180
  ) -> Any:
179
181
  try:
180
- pool_node = registry.schema.get(name=data["node"].value)
181
- if not pool_node.is_generic_schema and not pool_node.is_node_schema:
182
+ schema_node = registry.schema.get(name=data["node"].value)
183
+ if not schema_node.is_generic_schema and not schema_node.is_node_schema:
182
184
  raise ValidationError(input_value="The selected model is not a Node or a Generic")
183
185
  except SchemaNotFoundError as exc:
184
186
  exc.message = "The selected model does not exist"
185
187
  raise exc
186
188
 
187
- attributes = [attribute for attribute in pool_node.attributes if attribute.name == data["node_attribute"].value]
189
+ attributes = [
190
+ attribute for attribute in schema_node.attributes if attribute.name == data["node_attribute"].value
191
+ ]
188
192
  if not attributes:
189
193
  raise ValidationError(input_value="The selected attribute doesn't exist in the selected model")
190
194
 
@@ -192,9 +196,22 @@ class InfrahubNumberPoolMutation(InfrahubMutationMixin, Mutation):
192
196
  if attribute.kind != "Number":
193
197
  raise ValidationError(input_value="The selected attribute is not of the kind Number")
194
198
 
195
- if data["start_range"].value > data["end_range"].value:
199
+ start_range = data["start_range"].value
200
+ end_range = data["end_range"].value
201
+ if start_range > end_range:
196
202
  raise ValidationError(input_value="start_range can't be larger than end_range")
197
203
 
204
+ if not isinstance(attribute.parameters, NumberAttributeParameters):
205
+ raise ValidationError(
206
+ input_value="The selected attribute parameters are not of the kind NumberAttributeParameters"
207
+ )
208
+
209
+ if attribute.parameters.min_value is not None and start_range < attribute.parameters.min_value:
210
+ raise ValidationError(input_value="start_range can't be less than min_value")
211
+
212
+ if attribute.parameters.max_value is not None and end_range > attribute.parameters.max_value:
213
+ raise ValidationError(input_value="end_range can't be larger than max_value")
214
+
198
215
  return await super().mutate_create(info=info, data=data, branch=branch)
199
216
 
200
217
  @classmethod
@@ -221,3 +238,42 @@ class InfrahubNumberPoolMutation(InfrahubMutationMixin, Mutation):
221
238
  raise ValidationError(input_value="start_range can't be larger than end_range")
222
239
 
223
240
  return number_pool, result
241
+
242
+ @classmethod
243
+ @retry_db_transaction(name="resource_manager_update")
244
+ async def mutate_delete(
245
+ cls,
246
+ info: GraphQLResolveInfo,
247
+ data: InputObjectType,
248
+ branch: Branch,
249
+ ) -> DeleteResult:
250
+ graphql_context: GraphqlContext = info.context
251
+
252
+ number_pool = await NodeManager.find_object(
253
+ db=graphql_context.db,
254
+ kind=protocols.CoreNumberPool,
255
+ id=data.get("id"),
256
+ hfid=data.get("hfid"),
257
+ branch=branch,
258
+ )
259
+
260
+ active_branches = registry.schema.get_branches()
261
+ violating_branches = []
262
+ for active_branch in active_branches:
263
+ try:
264
+ schema = registry.schema.get(name=number_pool.node.value, branch=active_branch)
265
+ except SchemaNotFoundError:
266
+ continue
267
+
268
+ if number_pool.node_attribute.value in schema.attribute_names:
269
+ attribute = schema.get_attribute(name=number_pool.node_attribute.value)
270
+ if attribute.kind == "NumberPool":
271
+ violating_branches.append(active_branch)
272
+
273
+ if violating_branches:
274
+ raise ValidationError(
275
+ input_value=f"Unable to delete number pool {number_pool.node.value}.{number_pool.node_attribute.value}"
276
+ f" is in use (branches: {','.join(violating_branches)})"
277
+ )
278
+
279
+ return await super().mutate_delete(info=info, data=data, branch=branch)
@@ -0,0 +1,36 @@
1
+ from graphene import Field, ObjectType, String
2
+ from graphene.types.generic import GenericScalar
3
+ from graphql import GraphQLResolveInfo
4
+
5
+ from infrahub.core import registry
6
+ from infrahub.core.convert_object_type.schema_mapping import get_schema_mapping
7
+
8
+
9
+ class FieldsMapping(ObjectType):
10
+ mapping = GenericScalar(required=True)
11
+
12
+
13
+ async def fields_mapping_type_conversion_resolver(
14
+ root: dict, # noqa: ARG001
15
+ info: GraphQLResolveInfo, # noqa: ARG001
16
+ source_kind: str,
17
+ target_kind: str,
18
+ branch: str,
19
+ ) -> dict:
20
+ source_schema = registry.get_node_schema(name=source_kind, branch=branch)
21
+ target_schema = registry.get_node_schema(name=target_kind, branch=branch)
22
+
23
+ mapping = get_schema_mapping(source_schema=source_schema, target_schema=target_schema)
24
+ mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in mapping.items()}
25
+ return {"mapping": mapping_dict}
26
+
27
+
28
+ FieldsMappingTypeConversion = Field(
29
+ FieldsMapping,
30
+ source_kind=String(),
31
+ target_kind=String(),
32
+ branch=String(),
33
+ description="Retrieve fields mapping for converting object type",
34
+ resolver=fields_mapping_type_conversion_resolver,
35
+ required=True,
36
+ )
@@ -306,7 +306,13 @@ async def resolve_number_pool_allocation(
306
306
  async def resolve_number_pool_utilization(
307
307
  db: InfrahubDatabase, pool: CoreNode, at: Timestamp | str | None, branch: Branch
308
308
  ) -> dict:
309
- number_pool = NumberUtilizationGetter(db=db, pool=pool, at=at, branch=branch)
309
+ """
310
+ Returns a mapping containg utilization info of a number pool.
311
+ The utilization is calculated as the percentage of the total number of values in the pool that are not excluded for the corresponding attribute.
312
+ """
313
+
314
+ core_number_pool = await registry.manager.get_one_by_id_or_default_filter(db=db, id=pool.id, kind="CoreNumberPool")
315
+ number_pool = NumberUtilizationGetter(db=db, pool=core_number_pool, at=at, branch=branch)
310
316
  await number_pool.load_data()
311
317
 
312
318
  return {
@@ -16,6 +16,7 @@ from .mutations.branch import (
16
16
  BranchValidate,
17
17
  )
18
18
  from .mutations.computed_attribute import UpdateComputedAttribute
19
+ from .mutations.convert_object_type import ConvertObjectType
19
20
  from .mutations.diff import DiffUpdateMutation
20
21
  from .mutations.diff_conflict import ResolveDiffConflict
21
22
  from .mutations.generator import GeneratorDefinitionRequestRun
@@ -48,6 +49,7 @@ from .queries import (
48
49
  InfrahubStatus,
49
50
  Relationship,
50
51
  )
52
+ from .queries.convert_object_type_mapping import FieldsMappingTypeConversion
51
53
  from .queries.diff.tree import DiffTreeQuery, DiffTreeSummaryQuery
52
54
  from .queries.event import Event
53
55
  from .queries.task import Task, TaskBranchStatus
@@ -77,6 +79,8 @@ class InfrahubBaseQuery(ObjectType):
77
79
  InfrahubResourcePoolAllocated = InfrahubResourcePoolAllocated
78
80
  InfrahubResourcePoolUtilization = InfrahubResourcePoolUtilization
79
81
 
82
+ FieldsMappingTypeConversion = FieldsMappingTypeConversion
83
+
80
84
 
81
85
  class InfrahubBaseMutation(ObjectType):
82
86
  InfrahubAccountTokenCreate = InfrahubAccountTokenCreate.Field()
@@ -109,3 +113,5 @@ class InfrahubBaseMutation(ObjectType):
109
113
  SchemaEnumAdd = SchemaEnumAdd.Field()
110
114
  SchemaEnumRemove = SchemaEnumRemove.Field()
111
115
  ResolveDiffConflict = ResolveDiffConflict.Field()
116
+
117
+ ConvertObjectType = ConvertObjectType.Field()
infrahub/menu/menu.py CHANGED
@@ -246,6 +246,37 @@ default_menu = [
246
246
  section=MenuSection.INTERNAL,
247
247
  order_weight=2000,
248
248
  ),
249
+ MenuItemDefinition(
250
+ namespace="Builtin",
251
+ name="TriggerDefinition",
252
+ label="Events",
253
+ icon=_extract_node_icon(infrahub_schema.get(InfrahubKind.TRIGGERRULE)),
254
+ protected=True,
255
+ section=MenuSection.INTERNAL,
256
+ order_weight=6000,
257
+ children=[
258
+ MenuItemDefinition(
259
+ namespace="Core",
260
+ name="TriggerRule",
261
+ label="Rules",
262
+ kind=InfrahubKind.TRIGGERRULE,
263
+ icon=_extract_node_icon(infrahub_schema.get(InfrahubKind.TRIGGERRULE)),
264
+ protected=True,
265
+ section=MenuSection.INTERNAL,
266
+ order_weight=1000,
267
+ ),
268
+ MenuItemDefinition(
269
+ namespace="Core",
270
+ name="Action",
271
+ label="Actions",
272
+ kind=InfrahubKind.ACTION,
273
+ icon=_extract_node_icon(infrahub_schema.get(InfrahubKind.ACTION)),
274
+ protected=True,
275
+ section=MenuSection.INTERNAL,
276
+ order_weight=2000,
277
+ ),
278
+ ],
279
+ ),
249
280
  ],
250
281
  ),
251
282
  MenuItemDefinition(
@@ -1,28 +1,18 @@
1
1
  from infrahub.message_bus import InfrahubMessage, InfrahubResponse
2
2
 
3
- from .check_generator_run import CheckGeneratorRun
4
- from .finalize_validator_execution import FinalizeValidatorExecution
5
3
  from .git_file_get import GitFileGet, GitFileGetResponse
6
4
  from .git_repository_connectivity import GitRepositoryConnectivity
7
- from .proposed_change.request_proposedchange_refreshartifacts import RequestProposedChangeRefreshArtifacts
8
5
  from .refresh_git_fetch import RefreshGitFetch
9
6
  from .refresh_registry_branches import RefreshRegistryBranches
10
7
  from .refresh_registry_rebasedbranch import RefreshRegistryRebasedBranch
11
- from .request_generatordefinition_check import RequestGeneratorDefinitionCheck
12
- from .request_proposedchange_pipeline import RequestProposedChangePipeline
13
8
  from .send_echo_request import SendEchoRequest, SendEchoRequestResponse
14
9
 
15
10
  MESSAGE_MAP: dict[str, type[InfrahubMessage]] = {
16
- "check.generator.run": CheckGeneratorRun,
17
- "finalize.validator.execution": FinalizeValidatorExecution,
18
11
  "git.file.get": GitFileGet,
19
12
  "git.repository.connectivity": GitRepositoryConnectivity,
20
13
  "refresh.git.fetch": RefreshGitFetch,
21
14
  "refresh.registry.branches": RefreshRegistryBranches,
22
15
  "refresh.registry.rebased_branch": RefreshRegistryRebasedBranch,
23
- "request.generator_definition.check": RequestGeneratorDefinitionCheck,
24
- "request.proposed_change.pipeline": RequestProposedChangePipeline,
25
- "request.proposed_change.refresh_artifacts": RequestProposedChangeRefreshArtifacts,
26
16
  "send.echo.request": SendEchoRequest,
27
17
  }
28
18
 
@@ -3,11 +3,8 @@ from prefect import Flow
3
3
 
4
4
  from infrahub.message_bus import RPCErrorResponse, messages
5
5
  from infrahub.message_bus.operations import (
6
- check,
7
- finalize,
8
6
  git,
9
7
  refresh,
10
- requests,
11
8
  send,
12
9
  )
13
10
  from infrahub.message_bus.types import MessageTTL
@@ -15,16 +12,11 @@ from infrahub.services import InfrahubServices
15
12
  from infrahub.tasks.check import set_check_status
16
13
 
17
14
  COMMAND_MAP = {
18
- "check.generator.run": check.generator.run,
19
- "finalize.validator.execution": finalize.validator.execution,
20
15
  "git.file.get": git.file.get,
21
16
  "git.repository.connectivity": git.repository.connectivity,
22
17
  "refresh.git.fetch": git.repository.fetch,
23
18
  "refresh.registry.branches": refresh.registry.branches,
24
19
  "refresh.registry.rebased_branch": refresh.registry.rebased_branch,
25
- "request.generator_definition.check": requests.generator_definition.check,
26
- "request.proposed_change.pipeline": requests.proposed_change.pipeline,
27
- "request.proposed_change.refresh_artifacts": requests.proposed_change.refresh_artifacts,
28
20
  "send.echo.request": send.echo.request,
29
21
  }
30
22
 
@@ -22,8 +22,7 @@ WITH n.uuid AS node_uuid, count(*) as num_nodes_with_uuid
22
22
  WHERE num_nodes_with_uuid > 1
23
23
  WITH DISTINCT node_uuid
24
24
  MATCH (n:Node {uuid: node_uuid})
25
- CALL {
26
- WITH n
25
+ CALL (n) {
27
26
  WITH labels(n) AS n_labels
28
27
  UNWIND n_labels AS n_label
29
28
  WITH n_label
@@ -37,8 +36,7 @@ UNWIND nodes_to_delete AS node_to_delete
37
36
  //------------
38
37
  // Find the edges that we need to move to the selected node_to_keep
39
38
  //------------
40
- CALL {
41
- WITH node_to_keep, node_to_delete
39
+ CALL (node_to_keep, node_to_delete) {
42
40
  MATCH (node_to_delete)-[edge_to_delete]->(peer)
43
41
  RETURN {
44
42
  from_id: %(id_func_name)s(node_to_keep),
@@ -60,8 +58,7 @@ WITH node_to_delete, collect(edge_to_create) AS edges_to_create
60
58
  //------------
61
59
  // Find the edges that we need to remove from the duplicated nodes
62
60
  //------------
63
- CALL {
64
- WITH node_to_delete
61
+ CALL (node_to_delete) {
65
62
  MATCH (node_to_delete)-[e]->(peer)
66
63
  RETURN {
67
64
  db_id: %(id_func_name)s(e),