add flight doc query

This commit is contained in:
xss 2022-05-11 19:50:25 +10:00
parent 6e636fd2f5
commit 6aa5bbfcb2
3 changed files with 142 additions and 4 deletions

View File

@ -42,4 +42,49 @@ resource "aws_apigatewayv2_integration" "ham_flight_doc" {
integration_uri = aws_lambda_function.ham_flight_doc.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
resource "aws_lambda_function" "ham_flight_doc_get" {
function_name = "ham-get-flight-doc"
handler = "ham_update_flight_doc.query"
s3_bucket = aws_s3_bucket_object.lambda.bucket
s3_key = aws_s3_bucket_object.lambda.key
source_code_hash = data.archive_file.lambda.output_base64sha256
publish = true
memory_size = 128
role = aws_iam_role.basic_lambda_role.arn
runtime = "python3.9"
timeout = 30
architectures = ["arm64"]
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
}
resource "aws_lambda_permission" "ham_flight_doc_get" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ham_flight_doc_get.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/amateur/flightdoc/*"
}
resource "aws_apigatewayv2_route" "ham_flight_doc_get" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /amateur/flightdoc/{payload_callsign}"
target = "integrations/${aws_apigatewayv2_integration.ham_flight_doc_get.id}"
}
resource "aws_apigatewayv2_integration" "ham_flight_doc_get" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.ham_flight_doc_get.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}

View File

@ -31,4 +31,29 @@ def lambda_handler(event, context):
es.request(json.dumps(payload),f"flight-doc/_doc","POST")
return {"statusCode": 200, "body": "^v^ updated"}
return {"statusCode": 200, "body": "^v^ updated"}
def query(event, context):
payload_callsign = event['pathParameters']['payload_callsign']
payload = {
"sort": [
{ "datetime" : {"order" : "desc"}}
],
"size": 1,
"query": {
"bool": {
"filter": [
{
"match_phrase": {
"payload_callsign.keyword": payload_callsign
}
}
]
}
}
}
results = es.request(json.dumps(payload),f"flight-doc/_search","POST")
if len(results['hits']['hits']) > 0 :
return {"statusCode": 200, "body": json.dumps(results['hits']['hits'][0]['_source'])}
else:
return {"statusCode": 404, "body": "not found"}

View File

@ -1,4 +1,69 @@
from . import *
# payload = {
# "version": "2.0",
# "routeKey": "PUT /amateur/flightdoc",
# "rawPath": "/amateur/flightdoc",
# "rawQueryString": "",
# "headers": {
# "accept": "*/*",
# "accept-encoding": "gzip, deflate, br",
# "accept-language": "en-AU,en;q=0.9",
# "authorization": "AWS4-HMAC-SHA256 Credential=ASIASC7NF3EG2IN32V5U/20220510/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=2c79b43997baee65869b98bbf8525116a297d57d90af1f311b5c1ff0e046cab0",
# "content-length": "109",
# "content-type": "application/json",
# "host": "api-raw.v2.sondehub.org",
# "origin": "http://localhost:8000",
# "referer": "http://localhost:8000/",
# "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15",
# "x-amz-content-sha256": "5cdaa82af662f6efdff22bc28a9ce534a4250025378f30f7b917506d31cbec90",
# "x-amz-date": "20220510T233932Z",
# "x-amz-security-token": "IQoJb3JpZ2luX2VjEEgaCXVzLWVhc3QtMSJIMEYCIQC1dWVTNpEB0saU5TPR9NHVmOdiBK1QfjMB1LrPZsea+AIhAIuhvNxDNF/gUaNrnmgK2de45dogn8+1C5HhEpqpKHqBKsQECCEQARoMMTQzODQxOTQxNzczIgzIMKmrFJy94ru7nrwqoQRnvyF9anCZpsqJbtc8X2aErXQvZwrKClPq26KpnHot4alk8k4WzOHjFYA1GOPDpbAPe8jpTKoNpzcU7twpKDk6znA+fo3bSnkhAqAyackmag7Tff4w5bABgysWmHafHrZBo4TIyihlRSmHSZNUo7NwFajOKg5wiHSrneF719Qxl1hcbqp5FuxUG1xWc8ySUOFarQ+gxtm1yGz33WMYqjkND1R5wANH3ii4Wz39Xd4FR5IJgm5nAdyaKVM536tW/KZOOtbyGPlMyyGjKNZYV0PDtWclHaBEPwSgCI4m+l5WRga2/WQuRtkTFAQXSn45aGcT7nUNflixbyD0tsHuHILFVOr64MVliumnTf7v60FJjUJqyTLYlG4hcSG+U+FWxFnBcWxPklILzx3QtSRj0gOCIV8rll5i6APq9vngHu3QBC9V96w06Fgz21jmyv2KF4/+lYTSR4d5DwF44+OttE9vtoibV8i7nZCZcNLwzb3F9qMyAmnJL2I2ZjcL8JeJyd9fz9IBiT1T2E/vmD1yfZt5gGN3/N+aSwj+w30CChUt8bhdpTVJqUtPhnLYdT3rOAez84HWvOmMIrZakCSy+ZRixLP6JCjmko2uQvKF3ohUeVGs97Z4d1cyJKaGYNxq7Jm48xy8oa5zsFN6b2up0fpRa7UqvFsiAMYq7C3SZKHiry67GO8ESa0zJkjke5g/SGNUf1O6IrmyuDScg/r2SjF9zzCM7uuTBjqEAjkBXGngIs6QyM2CUNEKAfGZ+MSM5D/da4EdnKIN+YJsu4KHYTepXDGtCpXuXmsI0fZkN7lxQPBYxtRnRzngph0SZaXq8cxLY/PBPievd4oQ8dnGX9Gy9lPLyq360igqlpxXAAke8C2CORr+y29pO6M8lFg7ds47Y5TXQ8wyfdUQAtXkBqklqHNEDMNJrZPFg/dJqSEGxxT/1GYNTdUKupGkNw0fHAv9XoBeewPsXoulOk4HlyiaKeEpZZWDsclpjNHtEYaASR0KzlWJ/KYPTkFaGpb8OzMg/KKL3Qvg8t8drEy5LZPhWNIOvOnKYaf+6V0nL62yS03YMIa07dADPwUTSHgA",
# "x-amz-user-agent": "aws-sdk-js/2.1130.0",
# "x-amzn-trace-id": "Root=1-627af7b4-5966222c69fc1dcb6a0d98e0",
# "x-forwarded-for": "14.203.163.173",
# "x-forwarded-port": "443",
# "x-forwarded-proto": "https"
# },
# "requestContext": {
# "accountId": "143841941773",
# "apiId": "r03szwwq41",
# "authorizer": {
# "iam": {
# "accessKey": "ASIASC7NF3EG2IN32V5U",
# "accountId": "143841941773",
# "callerId": "AROASC7NF3EGZURC7BJTC:CognitoIdentityCredentials",
# "cognitoIdentity": {
# "amr": [
# "authenticated",
# "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM",
# "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM:CognitoSignIn:61f3c171-39f9-474b-9900-9953b755b474"
# ],
# "identityId": "us-east-1:54e08cdd-84df-42b3-ab16-e568ef118e8c",
# "identityPoolId": "us-east-1:55e43eac-9626-43e1-a7d2-bbc57f5f5aa9"
# },
# "principalOrgId": None,
# "userArn": "arn:aws:sts::143841941773:assumed-role/Cognito_sondesAuth_Role/CognitoIdentityCredentials",
# "userId": "AROASC7NF3EGZURC7BJTC:CognitoIdentityCredentials"
# }
# },
# "domainName": "api-raw.v2.sondehub.org",
# "domainPrefix": "api-raw",
# "http": {
# "method": "PUT",
# "path": "/amateur/flightdoc",
# "protocol": "HTTP/1.1",
# "sourceIp": "14.203.163.173",
# "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15"
# },
# "requestId": "R7ukRiWYIAMEJ2g=",
# "routeKey": "PUT /amateur/flightdoc",
# "stage": "$default",
# "time": "10/May/2022:23:39:32 +0000",
# "timeEpoch": 1652225972793
# },
# "body": "{\"payload_callsign\":\"4FSKTEST\",\"float_expected\":false,\"peak_altitude\":30000,\"descent_rate\":5,\"ascent_rate\":5}",
# "isBase64Encoded": False
# }
payload = {
"version": "2.0",
"routeKey": "PUT /amateur/flightdoc",
@ -49,7 +114,7 @@ payload = {
"domainName": "api-raw.v2.sondehub.org",
"domainPrefix": "api-raw",
"http": {
"method": "PUT",
"method": "GET",
"path": "/amateur/flightdoc",
"protocol": "HTTP/1.1",
"sourceIp": "14.203.163.173",
@ -61,7 +126,10 @@ payload = {
"time": "10/May/2022:23:39:32 +0000",
"timeEpoch": 1652225972793
},
"body": "{\"payload_callsign\":\"4FSKTEST\",\"float_expected\":false,\"peak_altitude\":30000,\"descent_rate\":5,\"ascent_rate\":5}",
"pathParameters" : {
"payload_callsign" : "HORUS-V2"
},
"body": "",
"isBase64Encoded": False
}
print(lambda_handler(payload, {}))
print(query(payload, {}))