-
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[draft]: Separate t4c license server and instance
TODO: - Handle Usage and Tests for Usage - Update Grafana - Make sure injection for license server works - Inline todos
- Loading branch information
Showing
75 changed files
with
2,646 additions
and
632 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
111 changes: 111 additions & 0 deletions
111
...nd/capellacollab/alembic/versions/3818a5009130_split_t4c_instances_and_license_servers.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,111 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Split T4C Instances and License Servers | ||
Revision ID: 3818a5009130 | ||
Revises: 7cf3357ddd7b | ||
Create Date: 2024-10-01 15:46:26.054936 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.orm import Session | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "3818a5009130" | ||
down_revision = "7cf3357ddd7b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"t4c_license_servers", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("usage_api", sa.String(), nullable=False), | ||
sa.Column("license_key", sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
) | ||
op.create_index( | ||
op.f("ix_t4c_license_servers_id"), | ||
"t4c_license_servers", | ||
["id"], | ||
unique=True, | ||
) | ||
op.add_column( | ||
"t4c_instances", | ||
sa.Column("license_server_id", sa.Integer(), nullable=True), | ||
) | ||
op.create_foreign_key( | ||
None, | ||
"t4c_instances", | ||
"t4c_license_servers", | ||
["license_server_id"], | ||
["id"], | ||
) | ||
|
||
# Fetch existing t4c_instances and group by usage_api and license | ||
bind = op.get_bind() | ||
session = Session(bind=bind) | ||
t4c_instances = session.execute( | ||
sa.text("SELECT id, usage_api, license FROM t4c_instances") | ||
).fetchall() | ||
|
||
grouped_instances = {} | ||
for instance in t4c_instances: | ||
key = (instance.usage_api, instance.license) | ||
if key not in grouped_instances: | ||
grouped_instances[key] = [] | ||
grouped_instances[key].append(instance.id) | ||
|
||
# Create new license_server for each group and associate with instances | ||
for (usage_api, license_key), instance_ids in grouped_instances.items(): | ||
license_server_id = session.execute( | ||
sa.text( | ||
"INSERT INTO t4c_license_servers (name, usage_api, license_key) VALUES (:name, :usage_api, :license_key) RETURNING id" | ||
), | ||
{ | ||
"name": usage_api, | ||
"usage_api": usage_api, | ||
"license_key": license_key, | ||
}, | ||
).scalar() | ||
|
||
session.execute( | ||
sa.text( | ||
"UPDATE t4c_instances SET license_server_id = :license_server_id WHERE id = ANY(:instance_ids)" | ||
), | ||
{ | ||
"license_server_id": license_server_id, | ||
"instance_ids": instance_ids, | ||
}, | ||
) | ||
|
||
session.commit() | ||
|
||
op.alter_column("t4c_instances", "license_server_id", nullable=False) | ||
op.drop_column("t4c_instances", "license") | ||
op.drop_column("t4c_instances", "usage_api") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# TODO implement this | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"t4c_instances", sa.Column("usage_api", sa.String(), nullable=False) | ||
) | ||
op.add_column( | ||
"t4c_instances", sa.Column("license", sa.String(), nullable=False) | ||
) | ||
op.drop_constraint(None, "t4c_instances", type_="foreignkey") | ||
op.drop_column("t4c_instances", "license_server_id") | ||
op.drop_index( | ||
op.f("ix_t4c_license_servers_id"), table_name="t4c_license_servers" | ||
) | ||
op.drop_table("t4c_license_servers") | ||
# ### end Alembic commands ### |
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
2 changes: 1 addition & 1 deletion
2
backend/capellacollab/projects/toolmodels/modelsources/t4c/util.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
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
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
backend/capellacollab/settings/modelsources/t4c/instance/exceptions.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,34 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from fastapi import status | ||
|
||
from capellacollab.core import exceptions as core_exceptions | ||
|
||
|
||
class T4CInstanceIsArchivedError(core_exceptions.BaseError): | ||
def __init__(self, t4c_instance_id: int): | ||
self.t4c_instance_id = t4c_instance_id | ||
super().__init__( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
title="T4C instance is archived", | ||
reason=f"The T4C instance identified by {t4c_instance_id} is archived, thus prohibiting the execution of the requested operation.", | ||
err_code="T4C_INSTANCE_IS_ARCHIVED", | ||
) | ||
|
||
|
||
class T4CInstanceNotFoundError(core_exceptions.BaseError): | ||
def __init__(self, t4c_instance_id: int): | ||
super().__init__( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
title="T4C server instance not found", | ||
reason=f"The T4C instance with id {t4c_instance_id} was not found.", | ||
err_code="T4C_INSTANCE_NOT_FOUND", | ||
) | ||
|
||
|
||
class T4CInstanceWithNameAlreadyExistsError( | ||
core_exceptions.ResourceAlreadyExistsError | ||
): | ||
def __init__(self): | ||
super().__init__(resource_name="T4C Instance", identifier_name="name") |
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.