|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import importlib |
2 | 4 | import logging |
3 | 5 | import os |
|
14 | 16 |
|
15 | 17 | from infrahub import config |
16 | 18 | from infrahub.core import registry |
| 19 | +from infrahub.core.constants import InfrahubKind |
17 | 20 | from infrahub.core.graph import GRAPH_VERSION |
18 | 21 | from infrahub.core.graph.constraints import ConstraintManagerBase, ConstraintManagerMemgraph, ConstraintManagerNeo4j |
19 | 22 | from infrahub.core.graph.index import node_indexes, rel_indexes |
20 | 23 | from infrahub.core.graph.schema import GRAPH_SCHEMA |
21 | | -from infrahub.core.initialization import first_time_initialization, get_root_node, initialization, initialize_registry |
| 24 | +from infrahub.core.initialization import ( |
| 25 | + create_default_menu, |
| 26 | + create_default_roles, |
| 27 | + create_super_administrator_role, |
| 28 | + create_super_administrators_group, |
| 29 | + first_time_initialization, |
| 30 | + get_root_node, |
| 31 | + initialization, |
| 32 | + initialize_registry, |
| 33 | +) |
| 34 | +from infrahub.core.manager import NodeManager |
22 | 35 | from infrahub.core.migrations.graph import get_graph_migrations |
23 | 36 | from infrahub.core.migrations.schema.models import SchemaApplyMigrationData |
24 | 37 | from infrahub.core.schema import SchemaRoot, core_models, internal_schema |
|
35 | 48 |
|
36 | 49 | if TYPE_CHECKING: |
37 | 50 | from infrahub.cli.context import CliContext |
| 51 | + from infrahub.database import InfrahubDatabase |
38 | 52 |
|
39 | 53 | app = AsyncTyper() |
40 | 54 |
|
@@ -228,6 +242,7 @@ async def update_core_schema( # pylint: disable=too-many-statements |
228 | 242 | raise typer.Exit(1) |
229 | 243 |
|
230 | 244 | if not result.diff.all: |
| 245 | + await create_defaults(db=db) |
231 | 246 | rprint("Core Schema Up to date, nothing to update") |
232 | 247 | raise typer.Exit(0) |
233 | 248 |
|
@@ -298,6 +313,8 @@ async def update_core_schema( # pylint: disable=too-many-statements |
298 | 313 | rprint(message) |
299 | 314 | raise typer.Exit(1) |
300 | 315 |
|
| 316 | + await create_defaults(db=db) |
| 317 | + |
301 | 318 |
|
302 | 319 | @app.command() |
303 | 320 | async def constraint( |
@@ -381,3 +398,34 @@ async def index( |
381 | 398 | console.print(table) |
382 | 399 |
|
383 | 400 | await dbdriver.close() |
| 401 | + |
| 402 | + |
| 403 | +async def create_defaults(db: InfrahubDatabase) -> None: |
| 404 | + """Create and assign default objects.""" |
| 405 | + existing_permissions = await NodeManager.query( |
| 406 | + schema=InfrahubKind.OBJECTPERMISSION, |
| 407 | + db=db, |
| 408 | + limit=1, |
| 409 | + ) |
| 410 | + if not existing_permissions: |
| 411 | + await setup_permissions(db=db) |
| 412 | + |
| 413 | + existing_menu_items = await NodeManager.query( |
| 414 | + schema=InfrahubKind.MENUITEM, |
| 415 | + db=db, |
| 416 | + limit=1, |
| 417 | + ) |
| 418 | + if not existing_menu_items: |
| 419 | + await create_default_menu(db=db) |
| 420 | + |
| 421 | + |
| 422 | +async def setup_permissions(db: InfrahubDatabase) -> None: |
| 423 | + existing_accounts = await NodeManager.query( |
| 424 | + schema=InfrahubKind.ACCOUNT, |
| 425 | + db=db, |
| 426 | + limit=1, |
| 427 | + ) |
| 428 | + administrator_role = await create_super_administrator_role(db=db) |
| 429 | + await create_super_administrators_group(db=db, role=administrator_role, admin_accounts=existing_accounts) |
| 430 | + |
| 431 | + await create_default_roles(db=db) |
0 commit comments