Skip to content

Commit 5947294

Browse files
committed
Create permissions and menu items if none previously existed
The `infrahub db update-core-schema` command will now also create menu items and permission objects assuming that no previous menu item exists for the new menu, and the same for permissions if one permission has been defined this step will be ignored otherwise full permissions will be assigned to the existing users and the Infrahub Users group will be created. This is a bit of a poor mans migration as these type of objects don't fit into the typical current workflow. ```bash infrahub db migrate infrahub db update-core-schema ```
1 parent bd9bae2 commit 5947294

File tree

1 file changed

+49
-1
lines changed
  • backend/infrahub/cli

1 file changed

+49
-1
lines changed

backend/infrahub/cli/db.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import importlib
24
import logging
35
import os
@@ -14,11 +16,22 @@
1416

1517
from infrahub import config
1618
from infrahub.core import registry
19+
from infrahub.core.constants import InfrahubKind
1720
from infrahub.core.graph import GRAPH_VERSION
1821
from infrahub.core.graph.constraints import ConstraintManagerBase, ConstraintManagerMemgraph, ConstraintManagerNeo4j
1922
from infrahub.core.graph.index import node_indexes, rel_indexes
2023
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
2235
from infrahub.core.migrations.graph import get_graph_migrations
2336
from infrahub.core.migrations.schema.models import SchemaApplyMigrationData
2437
from infrahub.core.schema import SchemaRoot, core_models, internal_schema
@@ -35,6 +48,7 @@
3548

3649
if TYPE_CHECKING:
3750
from infrahub.cli.context import CliContext
51+
from infrahub.database import InfrahubDatabase
3852

3953
app = AsyncTyper()
4054

@@ -228,6 +242,7 @@ async def update_core_schema( # pylint: disable=too-many-statements
228242
raise typer.Exit(1)
229243

230244
if not result.diff.all:
245+
await create_defaults(db=db)
231246
rprint("Core Schema Up to date, nothing to update")
232247
raise typer.Exit(0)
233248

@@ -298,6 +313,8 @@ async def update_core_schema( # pylint: disable=too-many-statements
298313
rprint(message)
299314
raise typer.Exit(1)
300315

316+
await create_defaults(db=db)
317+
301318

302319
@app.command()
303320
async def constraint(
@@ -381,3 +398,34 @@ async def index(
381398
console.print(table)
382399

383400
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

Comments
 (0)