@@ -125,10 +125,11 @@ async def create_connection(
125125 data = connection_data .data .model_dump (),
126126 )
127127
128- await unit_of_work .credentials .create (
129- connection_id = connection .id ,
130- data = connection_data .auth_data .model_dump (),
131- )
128+ if connection_data .auth_data :
129+ await unit_of_work .credentials .create (
130+ connection_id = connection .id ,
131+ data = connection_data .auth_data .model_dump (),
132+ )
132133
133134 credentials = await unit_of_work .credentials .read (connection .id )
134135 return TypeAdapter (ReadConnectionSchema ).validate_python (
@@ -183,7 +184,7 @@ async def read_connection(
183184
184185
185186@router .put ("/connections/{connection_id}" )
186- async def update_connection ( # noqa: WPS217, WPS238
187+ async def update_connection ( # noqa: WPS217, WPS238, WPS231
187188 connection_id : int ,
188189 connection_data : UpdateConnectionSchema ,
189190 current_user : User = Depends (get_user (is_active = True )),
@@ -200,36 +201,49 @@ async def update_connection( # noqa: WPS217, WPS238
200201 if resource_role < Permission .WRITE :
201202 raise ActionNotAllowedError
202203
203- async with unit_of_work :
204- existing_connection : Connection = await unit_of_work .connection .read_by_id (connection_id = connection_id )
205- if connection_data .type != existing_connection .type :
206- linked_transfers : Sequence [Transfer ] = await unit_of_work .transfer .list_by_connection_id (connection_id )
207- if linked_transfers :
208- raise ConnectionTypeUpdateError
209-
210- existing_credentials = await unit_of_work .credentials .read (connection_id = connection_id )
211- auth_data = connection_data .auth_data .model_dump ()
204+ existing_connection : Connection = await unit_of_work .connection .read_by_id (connection_id = connection_id )
205+ if connection_data .type != existing_connection .type :
206+ linked_transfers : Sequence [Transfer ] = await unit_of_work .transfer .list_by_connection_id (connection_id )
207+ if linked_transfers :
208+ raise ConnectionTypeUpdateError
209+
210+ existing_credentials = await unit_of_work .credentials .read (connection_id = connection_id )
211+ new_credentials : dict | None = None
212+ if connection_data .auth_data :
213+ new_credentials = connection_data .auth_data .model_dump ()
212214 secret_field = connection_data .auth_data .secret_field
215+ if new_credentials [secret_field ] is None :
213216
214- if auth_data [ secret_field ] is None :
215- if existing_credentials ["type" ] != auth_data ["type" ]:
217+ # We don't return secret_field to client, so default field value means using existing secret
218+ if not existing_credentials or existing_credentials ["type" ] != new_credentials ["type" ]:
216219 raise ConnectionAuthDataUpdateError
217220
218- auth_data [secret_field ] = existing_credentials [secret_field ]
221+ new_credentials [secret_field ] = existing_credentials [secret_field ]
219222
223+ async with unit_of_work :
220224 connection = await unit_of_work .connection .update (
221225 connection_id = connection_id ,
222226 name = connection_data .name ,
223227 type = connection_data .type ,
224228 description = connection_data .description ,
225229 data = connection_data .data .model_dump (),
226230 )
227- await unit_of_work .credentials .update (
228- connection_id = connection_id ,
229- data = auth_data ,
230- )
231231
232- credentials = await unit_of_work .credentials .read (connection_id )
232+ if existing_credentials and new_credentials :
233+ await unit_of_work .credentials .update (
234+ connection_id = connection_id ,
235+ data = new_credentials ,
236+ )
237+ elif new_credentials :
238+ await unit_of_work .credentials .create (
239+ connection_id = connection .id ,
240+ data = new_credentials ,
241+ )
242+ elif existing_credentials :
243+ await unit_of_work .credentials .delete (
244+ connection_id = connection_id ,
245+ )
246+
233247 return TypeAdapter (ReadConnectionSchema ).validate_python (
234248 {
235249 "id" : connection .id ,
@@ -238,7 +252,7 @@ async def update_connection( # noqa: WPS217, WPS238
238252 "description" : connection .description ,
239253 "type" : connection .type ,
240254 "data" : connection .data ,
241- "auth_data" : credentials ,
255+ "auth_data" : new_credentials ,
242256 },
243257 )
244258
0 commit comments