|
| 1 | +import boto3 |
| 2 | +import hashlib |
| 3 | +import json |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def map_dynamo_type(value: Any): |
| 10 | + if isinstance(value, str): |
| 11 | + return {"S": value} |
| 12 | + elif isinstance(value, bool): |
| 13 | + return {"BOOL": value} |
| 14 | + elif isinstance(value, (int, float, Decimal)): |
| 15 | + return {"N": str(value)} |
| 16 | + elif value is None: |
| 17 | + return {"NULL": True} |
| 18 | + elif isinstance(value, list): |
| 19 | + return {"L": [map_dynamo_type(item) for item in value]} |
| 20 | + elif isinstance(value, dict): |
| 21 | + return {"M": {k: map_dynamo_type(v) for k, v in value.items()}} |
| 22 | + elif isinstance(value, Row): |
| 23 | + return {"M": {k: map_dynamo_type(v) for k, v in value.asDict().items()}} |
| 24 | + else: |
| 25 | + logging.warning(f"Unsupported value type: {type(value)}", "Converting it to string") |
| 26 | + return {"S": value} |
| 27 | + |
| 28 | + |
| 29 | +def upload_to_s3(s3_client, bucket, filepath, dry_run=False): |
| 30 | + filename = os.path.basename(filepath) |
| 31 | + s3_key = f"manual-uploads/{filename}.json" |
| 32 | + |
| 33 | + if dry_run: |
| 34 | + print(f"[DRY RUN] Would upload {filepath} to s3://{bucket}/{s3_key}") |
| 35 | + return |
| 36 | + |
| 37 | + try: |
| 38 | + s3_client.upload_file(filepath, bucket, s3_key) |
| 39 | + print(f"Uploaded {filepath} to s3://{bucket}/{s3_key}") |
| 40 | + except Exception as e: |
| 41 | + print(f"Failed to upload {filepath}: {e}") |
| 42 | + |
| 43 | + |
| 44 | +def upload_to_dynamo(dynamo_client, table_name, filepath): |
| 45 | + with open(filepath) as f: |
| 46 | + item = json.load(f) |
| 47 | + |
| 48 | + try: |
| 49 | + dynamo_client.put_item( |
| 50 | + TableName=table_name, Item={key: map_dynamo_type(value) for key, value in item.items()} |
| 51 | + ) |
| 52 | + print(f"Uploaded {filepath} to DynamoDB table {table_name}") |
| 53 | + except Exception as e: |
| 54 | + print(f"Failed to upload {filepath}: {e}") |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument("--env") |
| 60 | + parser.add_argument("--upload-s3", type=Path) |
| 61 | + parser.add_argument("--upload-dynamo", type=Path) |
| 62 | + parser.add_argument("--region", default="eu-west-2") |
| 63 | + parser.add_argument("--s3-bucket") |
| 64 | + parser.add_argument("--dynamo-table") |
| 65 | + parser.add_argument("--dry-run", action="store_true") |
| 66 | + args = parser.parse_args() |
| 67 | + |
| 68 | + if not args.s3_bucket: |
| 69 | + args.s3_bucket = f"eligibility-signposting-api-{args.env}-eli-rules" |
| 70 | + if not args.dynamo_table: |
| 71 | + args.dynamo_table = f"eligibility-signposting-api-{args.env}-eligibility_datastore" |
| 72 | + |
| 73 | + session = boto3.Session() |
| 74 | + s3 = session.client("s3", region_name=args.region) |
| 75 | + dynamo = session.client("dynamodb", region_name=args.region) |
| 76 | + |
| 77 | + if args.upload_s3: |
| 78 | + for filepath in args.upload_s3.glob("*.json"): |
| 79 | + upload_to_s3(s3, args.s3_bucket, str(filepath), args.dry_run) |
| 80 | + |
| 81 | + if args.upload_dynamo: |
| 82 | + for filepath in args.upload_dynamo.glob("*.json"): |
| 83 | + upload_to_dynamo(dynamo, args.dynamo_table, str(filepath)) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments