From e149ab7c801b9aa3a4f01bc626ad1703aa5b857f Mon Sep 17 00:00:00 2001 From: Emanuele Date: Wed, 1 May 2024 10:42:56 +0200 Subject: [PATCH] First version of lambda config put object --- lambda_config/lambda_config.py | 63 ++++++++++++++++++++++++++------- lambda_config/redirects.json | 24 +++++++++++++ lambda_config/redirects_base.py | 4 +-- lambda_config/test.json | 38 ++++++++++++++++++++ 4 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 lambda_config/redirects.json create mode 100644 lambda_config/test.json diff --git a/lambda_config/lambda_config.py b/lambda_config/lambda_config.py index 790566f..5ada08e 100644 --- a/lambda_config/lambda_config.py +++ b/lambda_config/lambda_config.py @@ -3,7 +3,8 @@ import json import boto3 from typing import Optional from pydantic import BaseModel -from redirects_base import Redirects +import pydantic_core +from redirects_base import Content, Customer, Redirects, Tag s3_client = None bucket_config = '' @@ -44,13 +45,16 @@ def lambda_handler(event: dict, context): s3_client = boto3.client('s3') ## Download redirects file - redirects = None + redirects: Redirects try: - resp = s3_client.get_object( - Bucket=bucket_config, - Key='redirects.json' - ) - redirects = Redirects(**json.load(resp['Body'])) + #resp = s3_client.get_object( + # Bucket=bucket_config, + # Key='redirects.json' + #) + #redirects = Redirects(**json.load(resp['Body'])) + with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as f: + #redirects = Redirects(**json.load(f)) + redirects = Redirects.model_validate_json(f.read()) except s3_client.exceptions.NoSuchKey as e: print(e) # Oppure pagina "siamo spiacenti ma il contenuto non e' disponibile" @@ -65,13 +69,47 @@ def lambda_handler(event: dict, context): return False print(f"Action: {record.eventName}") - print(f"Object:{record.s3}") + print(f"Object: {record.s3}") + + # splitta la chiave per capire la directory + keys = record.s3.object.key.split('/') + file_name = keys[-1] match record.eventName: case "ObjectCreated:Put" | "ObjectCreated:Post": - print(f"Object add: {record.s3.object.key}") - key_components = record.s3.object.key.split('/') - # capire il numero di key components, aggiornare il modello + print(f"ObjectCreated: {record.s3.object.key}") + + # crea il primo utente se necessario o selezionalo + if redirects.customers is None: + redirects.customers = {keys[0]: Customer(status='active')} + c = redirects.customers[keys[0]] + + # crea un tag per l'utente, con contenuto nullo o selezionalo + if c.tags is None: + c.tags = {keys[1]: Tag(status="active", content=None)} + t = c.tags[keys[1]] + + # crea un contenuto per il tag a seconda della lunghezza della chiave + if file_name == "url.txt": + with s3_client.get_object(Bucket=bucket_data, Key=record.s3.object.key)['Body'] as url_file: + content = Content(type='url', key=file_name, url=url_file.readline().decode().strip()) + else: + content = Content(type='s3', key=file_name, url=None) + + match len(keys): + case 4: + if t.content is None: + t.content = {keys[2]: content} + elif isinstance(t.content, dict): + t.content[keys[2]] = content + case 3: + t.content = content + case _: + print("Too long keys") + + + with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'w') as f: + f.write(redirects.model_dump_json(indent=2)) return True @@ -89,4 +127,5 @@ def lambda_handler(event: dict, context): if __name__ == "__main__": - lambda_handler({}, None) \ No newline at end of file + with open('/home/emanuele/dev/StandOut/lambda_config/test.json', 'r') as f: + lambda_handler(json.load(f), None) \ No newline at end of file diff --git a/lambda_config/redirects.json b/lambda_config/redirects.json new file mode 100644 index 0000000..1b9c165 --- /dev/null +++ b/lambda_config/redirects.json @@ -0,0 +1,24 @@ +{ + "customers": { + "customer1": { + "status": "active", + "tags": { + "tag1": { + "status": "active", + "content": { + "face1": { + "type": "url", + "key": "url.txt", + "url": "https://grafana.etss.it/d/LbON5PkGz/power?orgId=1&from=now-12h&to=now&refresh=30s" + }, + "face2": { + "type": "s3", + "key": "file.txt", + "url": null + } + } + } + } + } + } +} \ No newline at end of file diff --git a/lambda_config/redirects_base.py b/lambda_config/redirects_base.py index 2a8ecbb..e41a2b6 100644 --- a/lambda_config/redirects_base.py +++ b/lambda_config/redirects_base.py @@ -4,7 +4,7 @@ from pydantic import BaseModel class Content(BaseModel): type: str key: str - url: Optional[str | None] + url: Optional[str | None] = None class Tag(BaseModel): status: str @@ -15,7 +15,7 @@ class Customer(BaseModel): tags: Dict[str, Tag] | None = None class Redirects(BaseModel): - customers: Dict[str, Customer] + customers: Dict[str, Customer] | None = None if __name__ == "__main__": r = Redirects ( diff --git a/lambda_config/test.json b/lambda_config/test.json new file mode 100644 index 0000000..1fdcc49 --- /dev/null +++ b/lambda_config/test.json @@ -0,0 +1,38 @@ +{ + "Records": [ + { + "eventVersion": "2.1", + "eventSource": "aws:s3", + "awsRegion": "eu-west-1", + "eventTime": "2024-04-25T09:20:47.735Z", + "eventName": "ObjectCreated:Put", + "userIdentity": { + "principalId": "AWS:AIDA4HZZTH2GIL7SGDHDP" + }, + "requestParameters": { + "sourceIPAddress": "93.44.141.63" + }, + "responseElements": { + "x-amz-request-id": "PKAA3E505JRZ6RD5", + "x-amz-id-2": "JS2xXacUSZPbV5zC73sizKo5A0Am1qxadYy+KiAN4j5qSC89U5HueSXWMUWjHDdPryNBntkthWxfnKZSRznWucX0knBUkUfx" + }, + "s3": { + "s3SchemaVersion": "1.0", + "configurationId": "tf-s3-lambda-20240425091712738000000001", + "bucket": { + "name": "standout-data", + "ownerIdentity": { + "principalId": "A37RIV9ZNURQAX" + }, + "arn": "arn:aws:s3:::standout-data" + }, + "object": { + "key": "customer1/tag1/face2/file.txt", + "size": 2844326, + "eTag": "7039e5338840f289d0510dc9149bf0b5", + "sequencer": "00662A206F99CD2E09" + } + } + } + ] +} \ No newline at end of file