|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | from fastapi import APIRouter, Depends, Query, status |
5 | | -from kombu.exceptions import KombuError |
6 | 5 |
|
7 | 6 | from syncmaster.backend.api.deps import UnitOfWorkMarker |
8 | 7 | from syncmaster.backend.services import UnitOfWork, get_user |
9 | | -from syncmaster.db.models import Status, User |
| 8 | +from syncmaster.db.models import User |
10 | 9 | from syncmaster.db.utils import Permission |
11 | 10 | from syncmaster.errors.registration import get_error_responses |
12 | 11 | from syncmaster.exceptions.base import ActionNotAllowedError |
13 | 12 | from syncmaster.exceptions.connection import ConnectionNotFoundError |
14 | 13 | from syncmaster.exceptions.group import GroupNotFoundError |
15 | 14 | from syncmaster.exceptions.queue import DifferentTransferAndQueueGroupError |
16 | | -from syncmaster.exceptions.run import CannotConnectToTaskQueueError |
17 | 15 | from syncmaster.exceptions.transfer import ( |
18 | 16 | DifferentTransferAndConnectionsGroupsError, |
19 | 17 | DifferentTypeConnectionsAndParamsError, |
20 | 18 | TransferNotFoundError, |
21 | 19 | ) |
22 | 20 | from syncmaster.schemas.v1.connection_types import ConnectionType |
23 | | -from syncmaster.schemas.v1.connections.connection import ReadAuthDataSchema |
24 | 21 | from syncmaster.schemas.v1.status import ( |
25 | 22 | StatusCopyTransferResponseSchema, |
26 | 23 | StatusResponseSchema, |
|
32 | 29 | TransferPageSchema, |
33 | 30 | UpdateTransferSchema, |
34 | 31 | ) |
35 | | -from syncmaster.schemas.v1.transfers.run import ( |
36 | | - CreateRunSchema, |
37 | | - ReadRunSchema, |
38 | | - RunPageSchema, |
39 | | -) |
40 | | -from syncmaster.worker.config import celery |
41 | 32 |
|
42 | 33 | router = APIRouter(tags=["Transfers"], responses=get_error_responses()) |
43 | 34 |
|
@@ -368,123 +359,3 @@ async def delete_transfer( |
368 | 359 | status_code=status.HTTP_200_OK, |
369 | 360 | message="Transfer was deleted", |
370 | 361 | ) |
371 | | - |
372 | | - |
373 | | -@router.get("/runs") |
374 | | -async def read_runs( |
375 | | - transfer_id: int, |
376 | | - page: int = Query(gt=0, default=1), |
377 | | - page_size: int = Query(gt=0, le=200, default=20), |
378 | | - current_user: User = Depends(get_user(is_active=True)), |
379 | | - unit_of_work: UnitOfWork = Depends(UnitOfWorkMarker), |
380 | | -) -> RunPageSchema: |
381 | | - """Return runs of transfer with pagination""" |
382 | | - resource_rule = await unit_of_work.transfer.get_resource_permission( |
383 | | - user=current_user, |
384 | | - resource_id=transfer_id, |
385 | | - ) |
386 | | - |
387 | | - if resource_rule == Permission.NONE: |
388 | | - raise TransferNotFoundError |
389 | | - |
390 | | - pagination = await unit_of_work.run.paginate( |
391 | | - transfer_id=transfer_id, |
392 | | - page=page, |
393 | | - page_size=page_size, |
394 | | - ) |
395 | | - |
396 | | - return RunPageSchema.from_pagination(pagination=pagination) |
397 | | - |
398 | | - |
399 | | -@router.get("/runs/{run_id}") |
400 | | -async def read_run( |
401 | | - run_id: int, |
402 | | - current_user: User = Depends(get_user(is_active=True)), |
403 | | - unit_of_work: UnitOfWork = Depends(UnitOfWorkMarker), |
404 | | -) -> ReadRunSchema: |
405 | | - run = await unit_of_work.run.read_by_id(run_id=run_id) |
406 | | - |
407 | | - resource_role = await unit_of_work.transfer.get_resource_permission( |
408 | | - user=current_user, |
409 | | - resource_id=run.transfer_id, |
410 | | - ) |
411 | | - |
412 | | - if resource_role == Permission.NONE: |
413 | | - raise TransferNotFoundError |
414 | | - |
415 | | - return ReadRunSchema.from_orm(run) |
416 | | - |
417 | | - |
418 | | -@router.post("/runs") |
419 | | -async def start_run( |
420 | | - create_run_data: CreateRunSchema, |
421 | | - current_user: User = Depends(get_user(is_active=True)), |
422 | | - unit_of_work: UnitOfWork = Depends(UnitOfWorkMarker), |
423 | | -) -> ReadRunSchema: |
424 | | - # Check: user can start transfer |
425 | | - resource_rule = await unit_of_work.transfer.get_resource_permission( |
426 | | - user=current_user, |
427 | | - resource_id=create_run_data.transfer_id, |
428 | | - ) |
429 | | - |
430 | | - if resource_rule == Permission.NONE: |
431 | | - raise TransferNotFoundError |
432 | | - |
433 | | - if resource_rule < Permission.WRITE: |
434 | | - raise ActionNotAllowedError |
435 | | - |
436 | | - transfer = await unit_of_work.transfer.read_by_id(transfer_id=create_run_data.transfer_id) |
437 | | - |
438 | | - # The credentials.read method is used rather than credentials.read_bulk deliberately |
439 | | - # it’s more convenient to transfer credits in this place |
440 | | - credentials_source = await unit_of_work.credentials.read( |
441 | | - transfer.source_connection_id, |
442 | | - ) |
443 | | - credentials_target = await unit_of_work.credentials.read( |
444 | | - transfer.target_connection_id, |
445 | | - ) |
446 | | - |
447 | | - async with unit_of_work: |
448 | | - run = await unit_of_work.run.create( |
449 | | - transfer_id=create_run_data.transfer_id, |
450 | | - # Since fields with credentials may have different names (for example, S3 and Postgres have different names) |
451 | | - # the work of checking fields and removing passwords is delegated to the ReadAuthDataSchema class |
452 | | - source_creds=ReadAuthDataSchema(auth_data=credentials_source).dict(), |
453 | | - target_creds=ReadAuthDataSchema(auth_data=credentials_target).dict(), |
454 | | - ) |
455 | | - try: |
456 | | - celery.send_task("run_transfer_task", kwargs={"run_id": run.id}, queue=transfer.queue.name) |
457 | | - except KombuError as e: |
458 | | - async with unit_of_work: |
459 | | - run = await unit_of_work.run.update( |
460 | | - run_id=run.id, |
461 | | - status=Status.FAILED, |
462 | | - ) |
463 | | - raise CannotConnectToTaskQueueError(run_id=run.id) from e |
464 | | - return ReadRunSchema.from_orm(run) |
465 | | - |
466 | | - |
467 | | -@router.post("/runs/{run_id}/stop") |
468 | | -async def stop_run( |
469 | | - run_id: int, |
470 | | - current_user: User = Depends(get_user(is_active=True)), |
471 | | - unit_of_work: UnitOfWork = Depends(UnitOfWorkMarker), |
472 | | -) -> ReadRunSchema: |
473 | | - run = await unit_of_work.run.read_by_id(run_id=run_id) |
474 | | - |
475 | | - # Check: user can stop transfer |
476 | | - resource_rule = await unit_of_work.transfer.get_resource_permission( |
477 | | - user=current_user, |
478 | | - resource_id=run.transfer_id, |
479 | | - ) |
480 | | - |
481 | | - if resource_rule == Permission.NONE: |
482 | | - raise TransferNotFoundError |
483 | | - |
484 | | - if resource_rule < Permission.WRITE: |
485 | | - raise ActionNotAllowedError |
486 | | - |
487 | | - async with unit_of_work: |
488 | | - run = await unit_of_work.run.stop(run_id=run_id) |
489 | | - # TODO: add immdiate stop transfer after stop Run |
490 | | - return ReadRunSchema.from_orm(run) |
0 commit comments