-
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 the ability to override resources on a per-user basis
Closes #1951
- Loading branch information
Showing
19 changed files
with
354 additions
and
54 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
backend/capellacollab/alembic/versions/4d42177579a2_add_resource_override.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,51 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Add resource override | ||
Revision ID: 4d42177579a2 | ||
Revises: 320c5b39c509 | ||
Create Date: 2024-11-12 17:43:23.486104 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "4d42177579a2" | ||
down_revision = "320c5b39c509" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
t_tools = sa.Table( | ||
"tools", | ||
sa.MetaData(), | ||
sa.Column("id", sa.Integer()), | ||
sa.Column("integrations", postgresql.JSONB(astext_type=sa.Text())), | ||
sa.Column("config", postgresql.JSONB(astext_type=sa.Text())), | ||
) | ||
|
||
|
||
def upgrade(): | ||
connection = op.get_bind() | ||
results = connection.execute(sa.select(t_tools)).mappings().all() | ||
|
||
for row in results: | ||
config = row["config"] | ||
config["resources"] = { | ||
"profiles": { | ||
"default": { | ||
"cpu": config["resources"]["cpu"], | ||
"memory": config["resources"]["memory"], | ||
"users": None, | ||
} | ||
} | ||
} | ||
|
||
connection.execute( | ||
sa.update(t_tools) | ||
.where(t_tools.c.id == row["id"]) | ||
.values(config=config) | ||
) |
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
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
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,68 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pydantic | ||
import pytest | ||
|
||
from capellacollab.tools import models as tools_models | ||
|
||
|
||
def test_validate_tools(): | ||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
profiles={ | ||
"test1": tools_models.ResourceProfile(usernames=["test"]) | ||
} | ||
) | ||
|
||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
profiles={ | ||
"default": tools_models.ResourceProfile(), | ||
"test1": tools_models.ResourceProfile(), | ||
} | ||
) | ||
|
||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
profiles={ | ||
"default": tools_models.ResourceProfile(usernames=["test"]) | ||
} | ||
) | ||
|
||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
profiles={ | ||
"default": tools_models.ResourceProfile(), | ||
"test1": tools_models.ResourceProfile(usernames=["test"]), | ||
"test2": tools_models.ResourceProfile(usernames=["test"]), | ||
} | ||
) | ||
|
||
|
||
def test_get_profile(): | ||
default_profile = tools_models.ResourceProfile( | ||
memory=tools_models.MemoryResources(requests="1Gi", limits="2Gi"), | ||
cpu=tools_models.CPUResources(requests=0.4, limits=2), | ||
) | ||
different_profile = tools_models.ResourceProfile( | ||
usernames=["testuser"], | ||
memory=tools_models.MemoryResources(requests="1Gi", limits="2Gi"), | ||
cpu=tools_models.CPUResources(requests=0.4, limits=2), | ||
) | ||
|
||
resources = tools_models.Resources( | ||
profiles={ | ||
"default": default_profile, | ||
"test": different_profile, | ||
} | ||
) | ||
|
||
resource_profile = resources.get_profile(None) | ||
assert resource_profile == default_profile | ||
|
||
resource_profile = resources.get_profile("fakeuser") | ||
assert resource_profile == default_profile | ||
|
||
resource_profile = resources.get_profile("testuser") | ||
assert resource_profile == different_profile |
Oops, something went wrong.