-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for global configuration
The global configuration is defined as yml. We use the `monaco` editor in the frontend.
- Loading branch information
1 parent
ca2d78a
commit 294053d
Showing
26 changed files
with
668 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
backend/capellacollab/alembic/versions/86ab7d4d1684_add_configuration_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Add configuration table | ||
Revision ID: 86ab7d4d1684 | ||
Revises: ac0e6e0f77ee | ||
Create Date: 2023-10-27 14:54:40.452599 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "86ab7d4d1684" | ||
down_revision = "ac0e6e0f77ee" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"configuration", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column( | ||
"configuration", | ||
postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=False, | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
op.f("ix_configuration_id"), "configuration", ["id"], unique=True | ||
) | ||
op.create_index( | ||
op.f("ix_configuration_name"), "configuration", ["name"], unique=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from . import models | ||
|
||
|
||
def get_configuration_by_name( | ||
db: orm.Session, name: str | ||
) -> None | models.DatabaseConfiguration: | ||
return db.execute( | ||
sa.select(models.DatabaseConfiguration).where( | ||
models.DatabaseConfiguration.name == name | ||
) | ||
).scalar_one_or_none() | ||
|
||
|
||
def get_pydantic_configuration_by_name( | ||
db: orm.Session, pydantic_model: models.ConfigurationBase | ||
): | ||
return pydantic_model.model_validate( | ||
get_configuration_by_name(db, pydantic_model._name) | ||
) | ||
|
||
|
||
def create_configuration( | ||
db: orm.Session, | ||
name: str, | ||
configuration: dict[str, str], | ||
) -> models.DatabaseConfiguration: | ||
db_configuration = models.DatabaseConfiguration( | ||
name=name, configuration=configuration | ||
) | ||
db.add(db_configuration) | ||
db.commit() | ||
return db_configuration | ||
|
||
|
||
def update_configuration( | ||
db: orm.Session, | ||
db_configuration: models.DatabaseConfiguration, | ||
configuration: dict[str, str], | ||
) -> models.DatabaseConfiguration: | ||
db_configuration.configuration = configuration | ||
db.commit() | ||
return db_configuration | ||
|
||
|
||
def delete_configuration( | ||
db: orm.Session, db_configuration: models.DatabaseConfiguration | ||
): | ||
db.delete(db_configuration) | ||
db.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing as t | ||
|
||
import pydantic | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.core import database | ||
|
||
|
||
class DatabaseConfiguration(database.Base): | ||
__tablename__ = "configuration" | ||
|
||
id: orm.Mapped[int] = orm.mapped_column( | ||
unique=True, primary_key=True, index=True | ||
) | ||
|
||
name: orm.Mapped[str] = orm.mapped_column(unique=True, index=True) | ||
configuration: orm.Mapped[dict[str, t.Any]] | ||
|
||
|
||
class CPUCostConfiguration(pydantic.BaseModel): | ||
model_config = pydantic.ConfigDict(extra="forbid") | ||
|
||
reserved: float | None = pydantic.Field( | ||
default=None, | ||
description="CPU reserved costs per mCore / hour", | ||
) | ||
burst: float | None = pydantic.Field( | ||
default=None, description="CPU burst costs per mCore / hour" | ||
) | ||
|
||
|
||
class MemoryCostConfiguration(pydantic.BaseModel): | ||
model_config = pydantic.ConfigDict(extra="forbid") | ||
|
||
reserved: float | None = pydantic.Field( | ||
default=None, description="Memory reserved costs per MiB / hour" | ||
) | ||
burst: float | None = pydantic.Field( | ||
default=None, description="Memory burst costs per MiB / hour" | ||
) | ||
|
||
|
||
class SessionsCostConfiguration(pydantic.BaseModel): | ||
model_config = pydantic.ConfigDict(extra="forbid") | ||
|
||
cpu: CPUCostConfiguration = pydantic.Field(default=CPUCostConfiguration()) | ||
memory: MemoryCostConfiguration = pydantic.Field( | ||
default=MemoryCostConfiguration() | ||
) | ||
storage: float | None = pydantic.Field( | ||
default=None, description="Storage costs per GiB / month" | ||
) | ||
currency: str = pydantic.Field(default="€", min_length=1, max_length=5) | ||
|
||
|
||
class SessionsConfiguration(pydantic.BaseModel): | ||
model_config = pydantic.ConfigDict(extra="forbid") | ||
|
||
costs: SessionsCostConfiguration = pydantic.Field( | ||
default=SessionsCostConfiguration() | ||
) | ||
|
||
|
||
class ConfigurationBase(pydantic.BaseModel): | ||
model_config = pydantic.ConfigDict(extra="forbid") | ||
|
||
_name: str | ||
|
||
|
||
class GlobalConfiguration(ConfigurationBase): | ||
"""Global application configuration.""" | ||
|
||
_name = "global" | ||
|
||
sessions: SessionsConfiguration = pydantic.Field( | ||
default=SessionsConfiguration() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import fastapi | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.core import database | ||
from capellacollab.core.authentication import injectables as auth_injectables | ||
from capellacollab.users import models as users_models | ||
|
||
from . import crud, models | ||
|
||
router = fastapi.APIRouter( | ||
dependencies=[ | ||
fastapi.Depends( | ||
auth_injectables.RoleVerification( | ||
required_role=users_models.Role.ADMIN | ||
) | ||
) | ||
] | ||
) | ||
|
||
|
||
@router.get("/global", response_model=models.GlobalConfiguration) | ||
def get_global_configuration( | ||
db: orm.Session = fastapi.Depends(database.get_db), | ||
): | ||
configuration = crud.get_configuration_by_name(db, "global") | ||
if configuration: | ||
return configuration.configuration | ||
return models.GlobalConfiguration().model_validate({}) | ||
|
||
|
||
@router.put("/global", response_model=models.GlobalConfiguration) | ||
def update_global_configuration( | ||
body: models.GlobalConfiguration, | ||
db: orm.Session = fastapi.Depends(database.get_db), | ||
): | ||
configuration = crud.get_configuration_by_name(db, "global") | ||
|
||
if configuration: | ||
return crud.update_configuration(db, configuration, body.model_dump()) | ||
return crud.create_configuration( | ||
db, name="global", configuration=body.model_dump() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.