88 WarehouseConnectionStatus ,
99 WarehouseType ,
1010)
11+ from experimentation .types import SNOWFLAKE_DEFAULTS , SnowflakeConfig
1112
1213
1314class WarehouseConnectionSerializer (serializers .ModelSerializer ): # type: ignore[type-arg]
15+ name = serializers .CharField (max_length = 255 , required = False )
16+ config = serializers .JSONField (default = None , required = False , allow_null = True )
17+
1418 class Meta :
1519 model = WarehouseConnection
16- fields = ("id" , "warehouse_type" , "status" , "name" , "created_at" )
17- read_only_fields = ("id" , "status" , "name" , "created_at" )
20+ fields = ("id" , "warehouse_type" , "status" , "name" , "config" , "created_at" )
21+ read_only_fields = ("id" , "status" , "created_at" )
22+
23+ def validate (self , attrs : dict [str , Any ]) -> dict [str , Any ]:
24+ warehouse_type : str = attrs .get (
25+ "warehouse_type" ,
26+ getattr (self .instance , "warehouse_type" , "" ),
27+ )
28+
29+ if "config" not in attrs and self .instance is not None :
30+ return attrs
31+
32+ config : dict [str , Any ] | None = attrs .get ("config" )
33+
34+ if warehouse_type == WarehouseType .SNOWFLAKE :
35+ attrs ["config" ] = self ._validate_snowflake_config (config or {})
36+ elif warehouse_type == WarehouseType .FLAGSMITH :
37+ if config :
38+ raise serializers .ValidationError (
39+ {"config" : "Flagsmith warehouse does not accept configuration." }
40+ )
41+ attrs ["config" ] = None
42+ return attrs
1843
1944 def create (
2045 self ,
@@ -23,6 +48,9 @@ def create(
2348 environment : Environment = validated_data ["environment" ]
2449 warehouse_type : str = validated_data ["warehouse_type" ]
2550
51+ if not validated_data .get ("name" ):
52+ validated_data ["name" ] = self ._generate_name (warehouse_type , environment )
53+
2654 existing : WarehouseConnection | None = (
2755 WarehouseConnection .objects .all_with_deleted ()
2856 .filter (
@@ -34,16 +62,29 @@ def create(
3462 )
3563 if existing :
3664 existing .deleted_at = None
37- existing .status = WarehouseConnectionStatus .PENDING_CONNECTION
38- existing .name = self ._generate_name (warehouse_type , environment )
65+ existing .status = WarehouseConnectionStatus .CREATED
66+ existing .name = validated_data ["name" ]
67+ existing .config = validated_data .get ("config" )
3968 existing .save ()
4069 return existing
4170
42- validated_data ["name" ] = self ._generate_name (warehouse_type , environment )
4371 result : WarehouseConnection = super ().create (validated_data )
4472 return result
4573
4674 @staticmethod
4775 def _generate_name (warehouse_type : str , environment : Environment ) -> str :
4876 label = WarehouseType (warehouse_type ).label
4977 return f"{ label } Warehouse - { environment .name } "
78+
79+ @staticmethod
80+ def _validate_snowflake_config (config : dict [str , Any ]) -> SnowflakeConfig :
81+ account_identifier = config .get ("account_identifier" , "" )
82+ if not account_identifier :
83+ raise serializers .ValidationError (
84+ {"config" : {"account_identifier" : "This field is required." }}
85+ )
86+ merged : SnowflakeConfig = {
87+ ** SNOWFLAKE_DEFAULTS ,
88+ ** config , # type: ignore[typeddict-item]
89+ }
90+ return merged
0 commit comments