diff --git a/lambda_config/lambda_config.py b/lambda_config/lambda_config.py index 5ada08e..d21b739 100644 --- a/lambda_config/lambda_config.py +++ b/lambda_config/lambda_config.py @@ -47,14 +47,15 @@ def lambda_handler(event: dict, context): ## Download redirects file redirects: Redirects try: - #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()) + if context is not None: + resp = s3_client.get_object( + Bucket=bucket_config, + Key='redirects.json' + ) + redirects = Redirects.model_validate_json(resp['Body'].read()) + else: + with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'r') as 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" @@ -73,23 +74,38 @@ def lambda_handler(event: dict, context): # splitta la chiave per capire la directory keys = record.s3.object.key.split('/') - file_name = keys[-1] + keys.reverse() match record.eventName: - case "ObjectCreated:Put" | "ObjectCreated:Post": + case "ObjectCreated:Put" | "ObjectCreated:CompleteMultipartUpload": print(f"ObjectCreated: {record.s3.object.key}") # crea il primo utente se necessario o selezionalo + cust_key = keys.pop() if redirects.customers is None: - redirects.customers = {keys[0]: Customer(status='active')} - c = redirects.customers[keys[0]] + redirects.customers = {cust_key: Customer(status="active")} + if cust_key not in redirects.customers.keys(): + redirects.customers[cust_key] = Customer(status="active") + + # Aggiunto solo un cliente + if len(keys) == 0: + break + c = redirects.customers[cust_key] # crea un tag per l'utente, con contenuto nullo o selezionalo + tag_key = keys.pop() if c.tags is None: - c.tags = {keys[1]: Tag(status="active", content=None)} - t = c.tags[keys[1]] + c.tags = {tag_key: Tag(status="active", content=None)} + if tag_key not in c.tags.keys(): + c.tags[tag_key] = Tag(status="active", content=None) + + # Aggiunta anche una chiave + if len(keys) == 0: + break + t = c.tags[tag_key] # crea un contenuto per il tag a seconda della lunghezza della chiave + file_name = keys[0] 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()) @@ -97,33 +113,35 @@ def lambda_handler(event: dict, context): content = Content(type='s3', key=file_name, url=None) match len(keys): - case 4: - if t.content is None: - t.content = {keys[2]: content} + case 2: + if t.content is None or isinstance(t.content, Content): + t.content = {keys[1]: content} elif isinstance(t.content, dict): - t.content[keys[2]] = content - case 3: + t.content[keys[1]] = content + case 1: 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 - - + case "ObjectCreated:Copy": print(f"Object copy: {record.s3.object.key}") - return True - - case "s3:ObjectRemoved:*": + case "s3:ObjectRemoved:Delete": print(f"Object remove: {record.s3.object.key}") - return True case _: print("Unknown action") + + if context is not None: + resp = s3_client.put_object(Bucket=bucket_config, + Key='redirects.json', + Body=redirects.model_dump_json(indent=2)) + print(f"New redirects version: {resp['ETag']}") + else: + with open('/home/emanuele/dev/StandOut/lambda_config/redirects.json', 'w') as f: + f.write(redirects.model_dump_json(indent=2)) + + return True if __name__ == "__main__": diff --git a/lambda_config/redirects.json b/lambda_config/redirects.json index 1b9c165..1902a21 100644 --- a/lambda_config/redirects.json +++ b/lambda_config/redirects.json @@ -1,17 +1,33 @@ { "customers": { "customer1": { + "status": "active", + "tags": { + "tag1": { + "status": "active", + "content": { + "type": "url", + "key": "url.txt", + "url": "https://grafana.etss.it/d/LbON5PkGz/power?orgId=1&from=now-12h&to=now&refresh=30s" + } + }, + "tag2": { + "status": "active", + "content": { + "type": "s3", + "key": "file.txt", + "url": null + } + } + } + }, + "customer2": { "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 @@ -19,6 +35,19 @@ } } } + }, + "customer3": { + "status": "active", + "tags": { + "tag1": { + "status": "active", + "content": { + "type": "s3", + "key": "VID20240116160134.mp4", + "url": null + } + } + } } } } \ No newline at end of file diff --git a/lambda_config/test.json b/lambda_config/test.json index 1fdcc49..6493d0a 100644 --- a/lambda_config/test.json +++ b/lambda_config/test.json @@ -27,7 +27,7 @@ "arn": "arn:aws:s3:::standout-data" }, "object": { - "key": "customer1/tag1/face2/file.txt", + "key": "customer1/tag2/file.txt", "size": 2844326, "eTag": "7039e5338840f289d0510dc9149bf0b5", "sequencer": "00662A206F99CD2E09" diff --git a/master.tf b/master.tf index 0c92fed..2ea8d7e 100644 --- a/master.tf +++ b/master.tf @@ -150,6 +150,18 @@ data "archive_file" "lambda_standout_config_code" { output_path = "./lambda_zip/standout_lambda_config.zip" } +data "archive_file" "lambda_layer_deps" { + type = "zip" + source_dir = "./lambda_layer" + output_path = "./lambda_zip/lambda_layer.zip" +} + +resource "aws_lambda_layer_version" "lambda_layer" { + filename = "./lambda_zip/lambda_layer.zip" + layer_name = "lambda_deps" + compatible_runtimes = ["python3.12"] +} + resource "aws_lambda_function" "lambda_standout_redirect" { # If the file is not in the current working directory you will need to include a # path.module in the filename. @@ -162,6 +174,8 @@ resource "aws_lambda_function" "lambda_standout_redirect" { runtime = "python3.12" + layers = [aws_lambda_layer_version.lambda_layer.arn] + timeout = 10 environment { @@ -184,6 +198,8 @@ resource "aws_lambda_function" "lambda_standout_config" { runtime = "python3.12" + layers = [aws_lambda_layer_version.lambda_layer.arn] + timeout = 10 environment {