From 912937a961f9caed38a046abc76c4b2a36219bdb Mon Sep 17 00:00:00 2001 From: MoritzWeber Date: Thu, 14 Nov 2024 12:12:43 +0100 Subject: [PATCH] chore: Add TeamForCapella cluster instance by default In addition, add GitLab as additional Git instance. --- backend/Makefile | 1 + .../capellacollab/core/database/migration.py | 95 +++++++++++++------ .../teamforcapella/test_t4c_instances.py | 5 +- .../test_t4c_license_servers.py | 16 ++-- backend/tests/settings/test_git_instances.py | 2 +- 5 files changed, 76 insertions(+), 43 deletions(-) diff --git a/backend/Makefile b/backend/Makefile index 7e090e14b..b9271f6e0 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -99,6 +99,7 @@ psql: clear: echo "DROP DATABASE $(DB_NAME); CREATE DATABASE $(DB_NAME);" | docker run -e PGPASSWORD=$(DB_PASSWORD) -i --network host --entrypoint="psql" postgres:latest -h 'localhost' -p $(DB_PORT) -U '$(DB_USER)' postgres + touch trigger_reload.yml && rm trigger_reload.yml load: clear cat $(DATABASE_LOAD_FILE) | docker run -i -e PGPASSWORD=$(DB_PASSWORD) --network host --entrypoint="psql" postgres:latest -h 'localhost' -p $(DB_PORT) -U '$(DB_USER)' $(DB_NAME) diff --git a/backend/capellacollab/core/database/migration.py b/backend/capellacollab/core/database/migration.py index 1c48916d9..197559145 100644 --- a/backend/capellacollab/core/database/migration.py +++ b/backend/capellacollab/core/database/migration.py @@ -23,10 +23,9 @@ from capellacollab.projects.toolmodels.modelsources.git import ( models as git_models, ) -from capellacollab.projects.toolmodels.modelsources.t4c import crud as t4c_crud -from capellacollab.settings.modelsources.git import crud as modelssources_crud +from capellacollab.settings.modelsources.git import crud as modelsources_crud from capellacollab.settings.modelsources.git import ( - models as modelssources_models, + models as modelsources_models, ) from capellacollab.settings.modelsources.t4c.instance import ( crud as t4c_instance_crud, @@ -34,9 +33,6 @@ from capellacollab.settings.modelsources.t4c.instance import ( models as t4c_instance_models, ) -from capellacollab.settings.modelsources.t4c.instance.repositories import ( - crud as repositories_crud, -) from capellacollab.settings.modelsources.t4c.license_server import ( crud as t4c_license_server_crud, ) @@ -86,7 +82,7 @@ def migrate_db(engine, database_url: str): create_tools(session) create_t4c_instance_and_repositories(session) - create_github_instance(session) + create_git_instances(session) create_default_models(session) create_coffee_machine_model(session) @@ -363,49 +359,86 @@ def create_t4c_instance_and_repositories(db): assert tool version = tools_crud.get_version_by_tool_id_version_name( - db, tool.id, "6.0.0" + db, tool.id, "7.0.0" ) assert version - default_license_server = ( - t4c_license_server_models.DatabaseT4CLicenseServer( - name="default", - usage_api="http://localhost:8086", - license_key="placeholder", - ) - ) - t4c_license_server_crud.create_t4c_license_server( - db, default_license_server + _create_local_t4c_environment(db, version) + _create_cluster_t4c_environment(db, version) + LOGGER.info("Initialized T4C instance and repositories") + + +def _create_local_t4c_environment( + db: orm.Session, version: tools_models.DatabaseVersion +): + local_license_server = t4c_license_server_models.DatabaseT4CLicenseServer( + name="Local license server", + usage_api="http://localhost:8086", + license_key="Placeholder", ) + t4c_license_server_crud.create_t4c_license_server(db, local_license_server) - default_instance = t4c_instance_models.DatabaseT4CInstance( - name="default", + local_instance = t4c_instance_models.DatabaseT4CInstance( + name="Local server", protocol=t4c_instance_models.Protocol.tcp, host="localhost", port=2036, cdo_port=12036, http_port=8080, - license_server=default_license_server, + license_server=local_license_server, rest_api="http://localhost:8081/api/v1.0", username="admin", password="password", version=version, ) - t4c_instance_crud.create_t4c_instance(db, default_instance) - for t4c_model in t4c_crud.get_t4c_models(db): - t4c_model.repository = repositories_crud.create_t4c_repository( - db=db, repo_name=t4c_model.name, instance=default_instance + t4c_instance_crud.create_t4c_instance(db, local_instance) + + +def _create_cluster_t4c_environment( + db: orm.Session, version: tools_models.DatabaseVersion +): + cluster_license_server = ( + t4c_license_server_models.DatabaseT4CLicenseServer( + name="Cluster-internal license server", + usage_api="http://dev-license-server.t4c.svc.cluster.local:8086", + license_key="Placeholder", ) - db.commit() - LOGGER.info("Initialized T4C instance and repositories") + ) + t4c_license_server_crud.create_t4c_license_server( + db, cluster_license_server + ) + + cluster_instance = t4c_instance_models.DatabaseT4CInstance( + name="Cluster-internal server", + protocol=t4c_instance_models.Protocol.ws, + host="dev-server-internal.t4c.svc.cluster.local", + port=2036, + cdo_port=12036, + http_port=8080, + license_server=cluster_license_server, + rest_api="http://dev-server-internal.t4c.svc.cluster.local:8080/api/v1.0", + username="admin", + password="password", + version=version, + ) + t4c_instance_crud.create_t4c_instance(db, cluster_instance) -def create_github_instance(db: orm.Session): - modelssources_crud.create_git_instance( +def create_git_instances(db: orm.Session): + modelsources_crud.create_git_instance( + db=db, + body=modelsources_models.PostGitInstance( + type=modelsources_models.GitType.GITLAB, + name="GitLab", + url="https://gitlab.com", + api_url="https://gitlab.com/api/v4", + ), + ) + modelsources_crud.create_git_instance( db=db, - body=modelssources_models.PostGitInstance( - type=modelssources_models.GitType.GITHUB, - name="Github", + body=modelsources_models.PostGitInstance( + type=modelsources_models.GitType.GITHUB, + name="GitHub", url="https://github.com", api_url="https://api.github.com", ), diff --git a/backend/tests/settings/teamforcapella/test_t4c_instances.py b/backend/tests/settings/teamforcapella/test_t4c_instances.py index cb0d7fb07..980151cbd 100644 --- a/backend/tests/settings/teamforcapella/test_t4c_instances.py +++ b/backend/tests/settings/teamforcapella/test_t4c_instances.py @@ -113,9 +113,8 @@ def test_get_t4c_instances( "/api/v1/settings/modelsources/t4c/instances", ) - assert len(response.json()) == 2 - - assert response.json()[1]["name"] == "test server" + assert len(response.json()) == 3 + assert response.json()[-1]["name"] == "test server" # Password should not be exposed via API assert "password" not in response.json()[1] diff --git a/backend/tests/settings/teamforcapella/test_t4c_license_servers.py b/backend/tests/settings/teamforcapella/test_t4c_license_servers.py index de3a8c7e6..b4907da71 100644 --- a/backend/tests/settings/teamforcapella/test_t4c_license_servers.py +++ b/backend/tests/settings/teamforcapella/test_t4c_license_servers.py @@ -76,11 +76,11 @@ def test_get_t4c_license_servers_admin( "/api/v1/settings/modelsources/t4c/license-servers", ) - assert len(response.json()) == 2 + assert len(response.json()) == 3 - assert response.json()[1]["name"] == "test license server" - assert response.json()[1]["license_key"] == "test key" - assert response.json()[1]["usage_api"] == "http://localhost:8086" + assert response.json()[-1]["name"] == "test license server" + assert response.json()[-1]["license_key"] == "test key" + assert response.json()[-1]["usage_api"] == "http://localhost:8086" @pytest.mark.usefixtures("t4c_license_server") @@ -97,11 +97,11 @@ def test_get_t4c_license_servers( "/api/v1/settings/modelsources/t4c/license-servers", ) - assert len(response.json()) == 2 + assert len(response.json()) == 3 - assert response.json()[1]["name"] == "test license server" - assert response.json()[1]["license_key"] == "" - assert response.json()[1]["usage_api"] == "" + assert response.json()[-1]["name"] == "test license server" + assert response.json()[-1]["license_key"] == "" + assert response.json()[-1]["usage_api"] == "" def test_get_t4c_license_server( diff --git a/backend/tests/settings/test_git_instances.py b/backend/tests/settings/test_git_instances.py index f28fd0451..ac0d1317b 100644 --- a/backend/tests/settings/test_git_instances.py +++ b/backend/tests/settings/test_git_instances.py @@ -28,7 +28,7 @@ def fixture_git_instance(db: orm.Session) -> git_models.DatabaseGitInstance: def test_get_git_instances(client: testclient.TestClient): response = client.get("/api/v1/settings/modelsources/git") assert response.status_code == 200 - assert len(response.json()) == 2 + assert len(response.json()) == 3 assert response.json()[-1]["name"] == "test"