-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathurl-endpoint-test-lambda.py
More file actions
90 lines (78 loc) · 3.01 KB
/
url-endpoint-test-lambda.py
File metadata and controls
90 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import os
import boto3
from botocore.exceptions import ClientError
from sys import stdout
# This lambda is to be used in SIT and WL to test the ingest subscription notification process.
# This is a test lambda that gets triggered off of the interal CMR load balancer.
# It receives a notification generated by the ingest notification lambda.
# The notification is stored in a file in an S3 bucket. Users can tunnel into the CMR internal only
# load balancer and run curl http://localhost:8081/notification/tester. Make sure to use the correct port number
# that you used in your tunnel.
s3 = boto3.client('s3')
BUCKET_NAME = os.getenv("BUCKET_NAME")
FILE_NAME = 'notification-message.json'
# handler(event: Dict[str, Any], context: Any) -> None:
def handler(event, context):
"""This handler is invoked from the CMR internal load balancer from an http request.
If method is get, then read the contents of the file in the bucket and return the file contents.
The caller can then verify that the notfication that was sent by the ingest notification lambda is correct.
If method is post, then save the contents of the event body in the file located in the bucket.
The contents can be recalled by the get method to verify the sent notification."""
method = event['httpMethod']
if method == 'GET':
return handle_get()
elif method == 'POST':
body = event['body']
return handle_post(body)
else:
return {
'statusCode': 405,
'body': json.dumps('Method Not Allowed')
}
def handle_post(body):
"""This method save the contents of the request body into a file."""
try:
# Parse the incoming JSON data
body = json.loads(body)
# Save the data to S3
s3.put_object(
Bucket=BUCKET_NAME,
Key=FILE_NAME,
Body=json.dumps(body),
ContentType='application/json'
)
return {
'statusCode': 200,
'body': json.dumps('Data saved successfully')
}
except Exception as e:
print(f"Error in POST: {str(e)}")
stdout.flush()
return {
'statusCode': 500,
'body': json.dumps('Error saving data')
}
def handle_get():
"""This method retreives the contents of the file."""
try:
# Retrieve the data from S3
response = s3.get_object(Bucket=BUCKET_NAME, Key=FILE_NAME)
file_content = response['Body'].read().decode('utf-8')
return {
'statusCode': 200,
'body': file_content
}
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchKey':
return {
'statusCode': 404,
'body': json.dumps('File not found')
}
else:
print(f"Error in GET: {str(e)}")
stdout.flush()
return {
'statusCode': 500,
'body': json.dumps('Error retrieving data')
}