-
Notifications
You must be signed in to change notification settings - Fork 521
feat: experimentation flagsmith warehouse setup api scaffolding #7542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zaimwa9
wants to merge
9
commits into
main
Choose a base branch
from
feat/experimentation-warehouse-setup-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
325f008
feat: add-experimentation-app-with-warehouse-connection-model-and-end…
Zaimwa9 ed14ce2
test: add-warehouse-connection-view-serializer-and-permission-tests
Zaimwa9 9257a5a
feat: rename warehouse endpoint to plural and return list response
Zaimwa9 8db7e03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2dee61d
feat: lint tests
Zaimwa9 b8c4833
Merge branch 'feat/experimentation-warehouse-setup-api' of github.com…
Zaimwa9 3e78fab
feat: type
Zaimwa9 dbf0c15
feat: switched to viewset and added get single connection endpoint
Zaimwa9 307d545
feat: fixed types
Zaimwa9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -167,6 +167,7 @@ | |
| "softdelete", | ||
| "metadata", | ||
| "app_analytics", | ||
| "experimentation", | ||
| "oauth2_metadata", | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class ExperimentationConfig(AppConfig): | ||
| name = "experimentation" |
This file contains hidden or 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 @@ | ||
| WAREHOUSE_CONNECTION_FLAG = "experimentation_warehouse_connection" |
89 changes: 89 additions & 0 deletions
89
api/experimentation/migrations/0001_introduce_warehouse_connection.py
This file contains hidden or 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,89 @@ | ||
| # Generated by Django 5.2.14 on 2026-05-19 11:05 | ||
|
|
||
| import django.db.models.deletion | ||
| import django_lifecycle.mixins # type: ignore[import-untyped] | ||
| import uuid | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("environments", "0037_add_uuid_field"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="WarehouseConnection", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "deleted_at", | ||
| models.DateTimeField( | ||
| blank=True, | ||
| db_index=True, | ||
| default=None, | ||
| editable=False, | ||
| null=True, | ||
| ), | ||
| ), | ||
| ( | ||
| "uuid", | ||
| models.UUIDField(default=uuid.uuid4, editable=False, unique=True), | ||
| ), | ||
| ( | ||
| "warehouse_type", | ||
| models.CharField( | ||
| choices=[ | ||
| ("flagsmith", "Flagsmith"), | ||
| ("snowflake", "Snowflake"), | ||
| ("clickhouse", "ClickHouse"), | ||
| ], | ||
| max_length=50, | ||
| ), | ||
| ), | ||
| ( | ||
| "status", | ||
| models.CharField( | ||
| choices=[ | ||
| ("pending_connection", "Pending Connection"), | ||
| ("connected", "Connected"), | ||
| ("errored", "Errored"), | ||
| ], | ||
| default="pending_connection", | ||
| max_length=50, | ||
| ), | ||
| ), | ||
| ("name", models.CharField(max_length=255)), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ( | ||
| "environment", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="warehouse_connections", | ||
| to="environments.environment", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| condition=models.Q(("deleted_at__isnull", True)), | ||
| fields=("warehouse_type", "environment"), | ||
| name="unique_active_warehouse_per_type_and_env", | ||
| ) | ||
| ], | ||
| }, | ||
| bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model), | ||
| ), | ||
| ] | ||
Empty file.
This file contains hidden or 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 @@ | ||
| from django.db import models | ||
| from django_lifecycle import LifecycleModelMixin # type: ignore[import-untyped] | ||
|
|
||
| from core.models import SoftDeleteExportableModel | ||
| from environments.models import Environment | ||
|
|
||
|
|
||
| class WarehouseType(models.TextChoices): | ||
| FLAGSMITH = "flagsmith", "Flagsmith" | ||
| SNOWFLAKE = "snowflake", "Snowflake" | ||
| CLICKHOUSE = "clickhouse", "ClickHouse" | ||
|
|
||
|
|
||
| class WarehouseConnectionStatus(models.TextChoices): | ||
| PENDING_CONNECTION = "pending_connection", "Pending Connection" | ||
| CONNECTED = "connected", "Connected" | ||
| ERRORED = "errored", "Errored" | ||
|
|
||
|
|
||
| class WarehouseConnection(LifecycleModelMixin, SoftDeleteExportableModel): # type: ignore[misc] | ||
| environment = models.ForeignKey( | ||
| Environment, | ||
| on_delete=models.CASCADE, | ||
| related_name="warehouse_connections", | ||
| ) | ||
| warehouse_type = models.CharField( | ||
| max_length=50, | ||
| choices=WarehouseType.choices, | ||
| ) | ||
| status = models.CharField( | ||
| max_length=50, | ||
| choices=WarehouseConnectionStatus.choices, | ||
| default=WarehouseConnectionStatus.PENDING_CONNECTION, | ||
| ) | ||
| name = models.CharField(max_length=255) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| class Meta: | ||
| constraints = [ | ||
| models.UniqueConstraint( | ||
| fields=["warehouse_type", "environment"], | ||
| condition=models.Q(deleted_at__isnull=True), | ||
| name="unique_active_warehouse_per_type_and_env", | ||
| ), | ||
| ] |
This file contains hidden or 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,23 @@ | ||
| from rest_framework.permissions import BasePermission | ||
| from rest_framework.request import Request | ||
| from rest_framework.views import APIView | ||
|
|
||
| from environments.models import Environment | ||
| from experimentation.services import is_warehouse_feature_enabled | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
||
| class WarehouseConnectionPermission(BasePermission): | ||
| def has_permission(self, request: Request, view: APIView) -> bool: | ||
| try: | ||
| environment = Environment.objects.get( | ||
| api_key=view.kwargs.get("environment_api_key") | ||
| ) | ||
| except Environment.DoesNotExist: | ||
| return False | ||
|
|
||
| if not is_warehouse_feature_enabled(environment.project.organisation): | ||
| return False | ||
|
|
||
| user: FFAdminUser = request.user # type: ignore[assignment] | ||
| return user.is_environment_admin(environment) |
This file contains hidden or 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,49 @@ | ||
| from typing import Any | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from environments.models import Environment | ||
| from experimentation.models import ( | ||
| WarehouseConnection, | ||
| WarehouseConnectionStatus, | ||
| WarehouseType, | ||
| ) | ||
|
|
||
|
|
||
| class WarehouseConnectionSerializer(serializers.ModelSerializer): # type: ignore[type-arg] | ||
| class Meta: | ||
| model = WarehouseConnection | ||
| fields = ("uuid", "warehouse_type", "status", "name", "created_at") | ||
| read_only_fields = ("uuid", "status", "name", "created_at") | ||
|
|
||
| def create( | ||
| self, | ||
| validated_data: dict[str, Any], | ||
| ) -> WarehouseConnection: | ||
| environment: Environment = validated_data["environment"] | ||
| warehouse_type: str = validated_data["warehouse_type"] | ||
|
|
||
| existing: WarehouseConnection | None = ( | ||
| WarehouseConnection.objects.all_with_deleted() | ||
| .filter( | ||
| environment=environment, | ||
| warehouse_type=warehouse_type, | ||
| deleted_at__isnull=False, | ||
| ) | ||
| .first() | ||
| ) | ||
| if existing: | ||
| existing.deleted_at = None | ||
| existing.status = WarehouseConnectionStatus.PENDING_CONNECTION | ||
| existing.name = self._generate_name(warehouse_type, environment) | ||
| existing.save() | ||
| return existing | ||
|
|
||
| validated_data["name"] = self._generate_name(warehouse_type, environment) | ||
| result: WarehouseConnection = super().create(validated_data) | ||
| return result | ||
|
|
||
| @staticmethod | ||
| def _generate_name(warehouse_type: str, environment: Environment) -> str: | ||
| label = WarehouseType(warehouse_type).label | ||
| return f"{label} Warehouse - {environment.name}" |
This file contains hidden or 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,40 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import typing | ||
|
|
||
| from audit.models import AuditLog | ||
| from audit.related_object_type import RelatedObjectType | ||
| from experimentation.constants import WAREHOUSE_CONNECTION_FLAG | ||
| from integrations.flagsmith.client import get_openfeature_client | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from experimentation.models import WarehouseConnection | ||
| from organisations.models import Organisation | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
||
| def is_warehouse_feature_enabled(organisation: Organisation) -> bool: | ||
| return get_openfeature_client().get_boolean_value( | ||
| WAREHOUSE_CONNECTION_FLAG, | ||
| default_value=False, | ||
| evaluation_context=organisation.openfeature_evaluation_context, | ||
| ) | ||
|
|
||
|
|
||
| def create_warehouse_audit_log( | ||
| connection: WarehouseConnection, | ||
| user: FFAdminUser, | ||
| *, | ||
| action: str, | ||
| ) -> None: | ||
| AuditLog.objects.create( | ||
| environment=connection.environment, | ||
| project=connection.environment.project, | ||
| author=user, | ||
| related_object_id=connection.id, | ||
| related_object_type=RelatedObjectType.WAREHOUSE_CONNECTION.name, | ||
| log=( | ||
| f"Warehouse connection {action} for environment " | ||
| f"{connection.environment.name}" | ||
| ), | ||
| ) |
This file contains hidden or 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,10 @@ | ||
| from rest_framework.routers import DefaultRouter | ||
|
|
||
| from experimentation.views import WarehouseConnectionViewSet | ||
|
|
||
| app_name = "experimentation" | ||
|
|
||
| router = DefaultRouter() | ||
| router.register(r"", WarehouseConnectionViewSet, basename="warehouse-connections") | ||
|
|
||
| urlpatterns = router.urls |
This file contains hidden or 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,63 @@ | ||
| from rest_framework import mixins | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
| from rest_framework.serializers import BaseSerializer | ||
|
|
||
| from environments.views import NestedEnvironmentViewSet | ||
| from experimentation.models import WarehouseConnection | ||
| from experimentation.permissions import WarehouseConnectionPermission | ||
| from experimentation.serializers import WarehouseConnectionSerializer | ||
| from experimentation.services import create_warehouse_audit_log | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
||
| class WarehouseConnectionViewSet( | ||
| NestedEnvironmentViewSet[WarehouseConnection], | ||
| mixins.ListModelMixin, | ||
| mixins.CreateModelMixin, | ||
| mixins.RetrieveModelMixin, | ||
| mixins.DestroyModelMixin, | ||
| ): | ||
| serializer_class = WarehouseConnectionSerializer | ||
| pagination_class = None | ||
| permission_classes = [IsAuthenticated, WarehouseConnectionPermission] | ||
| model_class = WarehouseConnection | ||
| lookup_field = "uuid" | ||
| lookup_url_kwarg = "connection_id" | ||
|
|
||
| def perform_create(self, serializer: BaseSerializer[WarehouseConnection]) -> None: | ||
| connection: WarehouseConnection = serializer.save( | ||
| environment=self._get_environment() | ||
| ) | ||
| create_warehouse_audit_log( | ||
| connection, self._get_user(self.request), action="created" | ||
| ) | ||
|
|
||
| def perform_destroy(self, instance: WarehouseConnection) -> None: | ||
| create_warehouse_audit_log( | ||
| instance, self._get_user(self.request), action="deleted" | ||
| ) | ||
| instance.delete() | ||
|
|
||
| def create(self, request: Request, *args: object, **kwargs: object) -> Response: | ||
| environment = self._get_environment() | ||
| serializer = self.get_serializer(data=request.data) | ||
| serializer.is_valid(raise_exception=True) | ||
|
|
||
| warehouse_type = serializer.validated_data["warehouse_type"] | ||
| if WarehouseConnection.objects.filter( | ||
| environment=environment, | ||
| warehouse_type=warehouse_type, | ||
| ).exists(): | ||
| return Response( | ||
| {"detail": "Warehouse connection already exists."}, | ||
| status=409, | ||
| ) | ||
|
|
||
| self.perform_create(serializer) | ||
| return Response(serializer.data, status=201) | ||
|
|
||
| @staticmethod | ||
| def _get_user(request: Request) -> FFAdminUser: | ||
| return request.user # type: ignore[return-value] |
This file contains hidden or 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
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hesitated between UUID and integerID, let me know what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer int because uuid takes a lot more space, but either is fine here, doesn't make much of a difference.