|
| 1 | +import json |
| 2 | +from threading import Thread |
| 3 | +from typing import TYPE_CHECKING, Any, Dict, Optional |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +from moto.core.utils import gzip_compress |
| 7 | +from moto.utilities.utils import get_partition |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from moto.dynamodb.models import Table |
| 11 | + from moto.s3.models import S3Backend |
| 12 | + |
| 13 | + |
| 14 | +class TableExport(Thread): |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + s3_bucket: str, |
| 18 | + s3_prefix: str, |
| 19 | + region_name: str, |
| 20 | + account_id: str, |
| 21 | + table_arn: str, |
| 22 | + export_format: str, |
| 23 | + export_type: str, |
| 24 | + ): |
| 25 | + super().__init__() |
| 26 | + self.partition = get_partition(region_name) |
| 27 | + self.table_arn = table_arn |
| 28 | + self.arn = f"arn:{self.partition}:dynamodb:{region_name}:{account_id}:table/{table_arn}/import/{str(uuid4()).replace('-', '')}" |
| 29 | + self.s3_bucket = s3_bucket |
| 30 | + self.s3_prefix = s3_prefix |
| 31 | + self.status = "IN_PROGRESS" |
| 32 | + self.export_format = export_format |
| 33 | + self.export_type = export_type |
| 34 | + self.account_id = account_id |
| 35 | + self.region_name = region_name |
| 36 | + |
| 37 | + self.failure_code: Optional[str] = None |
| 38 | + self.failure_message: Optional[str] = None |
| 39 | + self.table_name: str = "" |
| 40 | + self.item_count = 0 |
| 41 | + self.processed_bytes = 0 |
| 42 | + self.error_count = 0 |
| 43 | + |
| 44 | + def run(self) -> None: |
| 45 | + from moto.dynamodb.models import dynamodb_backends |
| 46 | + |
| 47 | + dynamodb_backend = dynamodb_backends[self.account_id][self.region_name] |
| 48 | + try: |
| 49 | + from moto.s3.models import s3_backends |
| 50 | + |
| 51 | + s3_backend = s3_backends[self.account_id][self.partition] |
| 52 | + s3_backend.buckets[self.s3_bucket] |
| 53 | + |
| 54 | + except KeyError: |
| 55 | + self.status = "FAILED" |
| 56 | + self.failure_code = "S3NoSuchBucket" |
| 57 | + self.failure_message = "The specified bucket does not exist" |
| 58 | + return |
| 59 | + |
| 60 | + for key in dynamodb_backend.tables: |
| 61 | + if dynamodb_backend.tables[key].table_arn == self.table_arn: |
| 62 | + self.table_name = key |
| 63 | + if not self.table_name: |
| 64 | + self.status = "FAILED" |
| 65 | + self.failure_code = "DynamoDBTableNotFound" |
| 66 | + self.failure_message = "The specified table does not exist" |
| 67 | + return |
| 68 | + table = dynamodb_backend.tables[self.table_name] |
| 69 | + if ( |
| 70 | + table.continuous_backups["PointInTimeRecoveryDescription"][ |
| 71 | + "PointInTimeRecoveryStatus" |
| 72 | + ] |
| 73 | + != "ENABLED" |
| 74 | + ): |
| 75 | + self.status = "FAILED" |
| 76 | + self.failure_code = "PointInTimeRecoveryUnavailable" |
| 77 | + self.failure_message = "Point in time recovery not enabled for table" |
| 78 | + return |
| 79 | + try: |
| 80 | + self._backup_to_s3_file(s3_backend, table) |
| 81 | + except Exception as e: |
| 82 | + self.status = "FAILED" |
| 83 | + self.failure_code = "UNKNOWN" |
| 84 | + self.failure_message = str(e) |
| 85 | + |
| 86 | + def _backup_to_s3_file(self, s3_backend: "S3Backend", table: "Table") -> None: |
| 87 | + backup = [] |
| 88 | + for item in table.all_items(): |
| 89 | + json_item = item.to_json() |
| 90 | + backup.append(json_item) |
| 91 | + self.processed_bytes += len(json_item) |
| 92 | + self.item_count = len(backup) |
| 93 | + content = gzip_compress(json.dumps(backup).encode("utf-8")) |
| 94 | + s3_backend.put_object( |
| 95 | + bucket_name=self.s3_bucket, |
| 96 | + key_name=f"{self.s3_prefix}/AWSDynamoDB/{str(uuid4())}/data/{str(uuid4())}.gz", |
| 97 | + value=content, |
| 98 | + ) |
| 99 | + |
| 100 | + self.status = "COMPLETED" if self.error_count == 0 else "FAILED" |
| 101 | + |
| 102 | + def response(self) -> Dict[str, Any]: |
| 103 | + return { |
| 104 | + "ExportArn": self.arn, |
| 105 | + "ExportStatus": self.status, |
| 106 | + "FailureCode": self.failure_code, |
| 107 | + "FailureMessage": self.failure_message, |
| 108 | + "ExportFormat": self.export_format, |
| 109 | + "ExportType": self.export_type, |
| 110 | + "ItemCount": self.item_count, |
| 111 | + "BilledSizeBytes": self.processed_bytes, |
| 112 | + } |
0 commit comments