-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnections.py
More file actions
320 lines (274 loc) · 10.9 KB
/
connections.py
File metadata and controls
320 lines (274 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
# SPDX-License-Identifier: Apache-2.0
from collections.abc import Sequence
from http.client import NO_CONTENT
from fastapi import APIRouter, Depends, Query
from pydantic import TypeAdapter
from syncmaster.db.models import Connection, ConnectionType, Transfer, User
from syncmaster.db.utils import Permission
from syncmaster.errors.registration import get_error_responses
from syncmaster.exceptions import ActionNotAllowedError
from syncmaster.exceptions.connection import (
ConnectionAuthDataUpdateError,
ConnectionDeleteError,
ConnectionNotFoundError,
ConnectionTypeUpdateError,
)
from syncmaster.exceptions.credentials import AuthDataNotFoundError
from syncmaster.exceptions.group import GroupNotFoundError
from syncmaster.schemas.v1.connection_types import CONNECTION_TYPES
from syncmaster.schemas.v1.connections.connection import (
ConnectionCopySchema,
ConnectionPageSchema,
CreateConnectionSchema,
ReadConnectionSchema,
UpdateConnectionSchema,
)
from syncmaster.schemas.v1.page import MetaPageSchema
from syncmaster.server.services.get_user import get_user
from syncmaster.server.services.unit_of_work import UnitOfWork
router = APIRouter(tags=["Connections"], responses=get_error_responses())
@router.get("/connections")
async def read_connections(
group_id: int,
page: int = Query(gt=0, default=1),
page_size: int = Query(gt=0, le=50, default=20), # noqa: WPS432
type: list[ConnectionType] | None = Query(None),
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
search_query: str | None = Query(
None,
title="Search Query",
description="full-text search for connections",
),
) -> ConnectionPageSchema:
"""Return connections in page format"""
resource_role = await unit_of_work.connection.get_group_permission(
user=current_user,
group_id=group_id,
)
if resource_role == Permission.NONE:
raise GroupNotFoundError
connection_str_type = None if type is None else [ct.value for ct in type]
pagination = await unit_of_work.connection.paginate(
page=page,
page_size=page_size,
group_id=group_id,
search_query=search_query,
connection_type=connection_str_type,
)
items: list[ReadConnectionSchema] = []
if pagination.items:
credentials = await unit_of_work.credentials.read_bulk([item.id for item in pagination.items])
items = [
TypeAdapter(ReadConnectionSchema).validate_python(
{
"id": item.id,
"group_id": item.group_id,
"name": item.name,
"description": item.description,
"type": item.type,
"data": item.data,
"auth_data": credentials.get(item.id, None),
},
)
for item in pagination.items
]
return ConnectionPageSchema(
meta=MetaPageSchema(
page=pagination.page,
pages=pagination.pages,
total=pagination.total,
page_size=pagination.page_size,
has_next=pagination.has_next,
has_previous=pagination.has_previous,
next_page=pagination.next_page,
previous_page=pagination.previous_page,
),
items=items,
)
@router.post("/connections")
async def create_connection(
connection_data: CreateConnectionSchema,
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
) -> ReadConnectionSchema:
"""Create new connection"""
group_permission = await unit_of_work.connection.get_group_permission(
user=current_user,
group_id=connection_data.group_id,
)
if group_permission == Permission.NONE:
raise GroupNotFoundError
if group_permission < Permission.WRITE:
raise ActionNotAllowedError
async with unit_of_work:
connection = await unit_of_work.connection.create(
name=connection_data.name,
type=connection_data.type,
description=connection_data.description,
group_id=connection_data.group_id,
data=connection_data.data.model_dump(),
)
await unit_of_work.credentials.create(
connection_id=connection.id,
data=connection_data.auth_data.model_dump(),
)
credentials = await unit_of_work.credentials.read(connection.id)
return TypeAdapter(ReadConnectionSchema).validate_python(
{
"id": connection.id,
"group_id": connection.group_id,
"name": connection.name,
"description": connection.description,
"type": connection.type,
"data": connection.data,
"auth_data": credentials,
},
)
@router.get("/connections/known_types", dependencies=[Depends(get_user(is_active=True))])
async def read_connection_types() -> list[str]:
return CONNECTION_TYPES
@router.get("/connections/{connection_id}")
async def read_connection(
connection_id: int,
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
) -> ReadConnectionSchema:
resource_role = await unit_of_work.connection.get_resource_permission(
user=current_user,
resource_id=connection_id,
)
if resource_role == Permission.NONE:
raise ConnectionNotFoundError
connection = await unit_of_work.connection.read_by_id(connection_id)
try:
credentials = await unit_of_work.credentials.read(connection.id)
except AuthDataNotFoundError:
credentials = None
return TypeAdapter(ReadConnectionSchema).validate_python(
{
"id": connection.id,
"group_id": connection.group_id,
"name": connection.name,
"description": connection.description,
"type": connection.type,
"data": connection.data,
"auth_data": credentials,
},
)
@router.put("/connections/{connection_id}")
async def update_connection( # noqa: WPS217, WPS238
connection_id: int,
connection_data: UpdateConnectionSchema,
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
) -> ReadConnectionSchema:
resource_role = await unit_of_work.connection.get_resource_permission(
user=current_user,
resource_id=connection_id,
)
if resource_role == Permission.NONE:
raise ConnectionNotFoundError
if resource_role < Permission.WRITE:
raise ActionNotAllowedError
async with unit_of_work:
existing_connection: Connection = await unit_of_work.connection.read_by_id(connection_id=connection_id)
if connection_data.type != existing_connection.type:
linked_transfers: Sequence[Transfer] = await unit_of_work.transfer.list_by_connection_id(connection_id)
if linked_transfers:
raise ConnectionTypeUpdateError
existing_credentials = await unit_of_work.credentials.read(connection_id=connection_id)
auth_data = connection_data.auth_data.model_dump()
for secret_field in connection_data.auth_data.get_secret_fields():
if auth_data[secret_field] is None:
if existing_credentials["type"] != auth_data["type"]:
raise ConnectionAuthDataUpdateError
auth_data[secret_field] = existing_credentials[secret_field]
connection = await unit_of_work.connection.update(
connection_id=connection_id,
name=connection_data.name,
type=connection_data.type,
description=connection_data.description,
data=connection_data.data.model_dump(),
)
await unit_of_work.credentials.update(
connection_id=connection_id,
data=auth_data,
)
credentials = await unit_of_work.credentials.read(connection_id)
return TypeAdapter(ReadConnectionSchema).validate_python(
{
"id": connection.id,
"group_id": connection.group_id,
"name": connection.name,
"description": connection.description,
"type": connection.type,
"data": connection.data,
"auth_data": credentials,
},
)
@router.delete("/connections/{connection_id}", status_code=NO_CONTENT)
async def delete_connection(
connection_id: int,
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
):
resource_role = await unit_of_work.connection.get_resource_permission(
user=current_user,
resource_id=connection_id,
)
if resource_role == Permission.NONE:
raise ConnectionNotFoundError
if resource_role < Permission.DELETE:
raise ActionNotAllowedError
connection = await unit_of_work.connection.read_by_id(connection_id)
transfers = await unit_of_work.transfer.list_by_connection_id(connection.id)
if transfers:
raise ConnectionDeleteError(
f"The connection has an associated transfers. Number of the connected transfers: {len(transfers)}",
)
async with unit_of_work:
await unit_of_work.connection.delete(connection_id)
@router.post("/connections/{connection_id}/copy_connection")
async def copy_connection( # noqa: WPS238
connection_id: int,
copy_connection_data: ConnectionCopySchema,
current_user: User = Depends(get_user(is_active=True)),
unit_of_work: UnitOfWork = Depends(UnitOfWork),
) -> ReadConnectionSchema:
resource_role = await unit_of_work.connection.get_resource_permission(
user=current_user,
resource_id=connection_id,
)
if resource_role == Permission.NONE:
raise ConnectionNotFoundError
if copy_connection_data.remove_source and resource_role < Permission.DELETE:
raise ActionNotAllowedError
target_group_role = await unit_of_work.connection.get_group_permission(
user=current_user,
group_id=copy_connection_data.new_group_id,
)
if target_group_role == Permission.NONE:
raise GroupNotFoundError
if target_group_role < Permission.WRITE:
raise ActionNotAllowedError
async with unit_of_work:
connection = await unit_of_work.connection.copy(
connection_id=connection_id,
new_group_id=copy_connection_data.new_group_id,
new_name=copy_connection_data.new_name,
)
if copy_connection_data.remove_source:
await unit_of_work.connection.delete(connection_id)
return TypeAdapter(ReadConnectionSchema).validate_python(
{
"id": connection.id,
"group_id": connection.group_id,
"name": connection.name,
"description": connection.description,
"type": connection.type,
"data": connection.data,
"auth_data": None,
},
)