mirror of
https://github.com/projecthorus/sondehub-infra.git
synced 2025-02-20 17:12:50 +00:00
first pass sites api
This commit is contained in:
parent
345e10400d
commit
fdaa440cf3
43
main.tf
43
main.tf
@ -646,6 +646,24 @@ resource "aws_lambda_function" "get_telem" {
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_lambda_function" "get_sites" {
|
||||
function_name = "get_sites"
|
||||
handler = "lambda_function.get_sites"
|
||||
filename = "${path.module}/build/query.zip"
|
||||
source_code_hash = data.archive_file.query.output_base64sha256
|
||||
publish = true
|
||||
memory_size = 256
|
||||
role = aws_iam_role.IAMRole5.arn
|
||||
runtime = "python3.9"
|
||||
timeout = 30
|
||||
architectures = ["arm64"]
|
||||
environment {
|
||||
variables = {
|
||||
"ES" = "es.${local.domain_name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_lambda_function" "get_listener_telemetry" {
|
||||
function_name = "get_listener_telemetry"
|
||||
handler = "lambda_function.get_listener_telemetry"
|
||||
@ -724,6 +742,13 @@ resource "aws_lambda_permission" "get_sondes" {
|
||||
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sondes"
|
||||
}
|
||||
|
||||
resource "aws_lambda_permission" "get_sites" {
|
||||
action = "lambda:InvokeFunction"
|
||||
function_name = aws_lambda_function.get_sites.arn
|
||||
principal = "apigateway.amazonaws.com"
|
||||
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sites"
|
||||
}
|
||||
|
||||
resource "aws_lambda_permission" "listeners" {
|
||||
action = "lambda:InvokeFunction"
|
||||
function_name = aws_lambda_function.listeners.arn
|
||||
@ -890,6 +915,14 @@ resource "aws_apigatewayv2_route" "get_sondes" {
|
||||
target = "integrations/${aws_apigatewayv2_integration.get_sondes.id}"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_route" "get_sites" {
|
||||
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
|
||||
api_key_required = false
|
||||
authorization_type = "NONE"
|
||||
route_key = "GET /sites"
|
||||
target = "integrations/${aws_apigatewayv2_integration.get_sites.id}"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_route" "listeners" {
|
||||
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
|
||||
api_key_required = false
|
||||
@ -954,6 +987,16 @@ resource "aws_apigatewayv2_integration" "get_sondes" {
|
||||
payload_format_version = "2.0"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_integration" "get_sites" {
|
||||
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
|
||||
connection_type = "INTERNET"
|
||||
integration_method = "POST"
|
||||
integration_type = "AWS_PROXY"
|
||||
integration_uri = aws_lambda_function.get_sites.arn
|
||||
timeout_milliseconds = 30000
|
||||
payload_format_version = "2.0"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_integration" "listeners" {
|
||||
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
|
||||
connection_type = "INTERNET"
|
||||
|
@ -347,6 +347,53 @@ def get_listener_telemetry(event, context):
|
||||
|
||||
}
|
||||
|
||||
def get_sites(event, context):
|
||||
|
||||
path = "sites/_search"
|
||||
payload = {
|
||||
"version": True,
|
||||
"size": 10000,
|
||||
"_source": {
|
||||
"excludes": []
|
||||
},
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"match_all": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
if "queryStringParameters" in event:
|
||||
if "station" in event["queryStringParameters"]:
|
||||
payload["query"]["bool"]["filter"].append(
|
||||
{
|
||||
"match_phrase": {
|
||||
"station": str(event["queryStringParameters"]["station"])
|
||||
}
|
||||
}
|
||||
)
|
||||
results = es_request(payload, path, "POST")
|
||||
output = {x['_source']['station']: x['_source'] for x in results['hits']['hits']}
|
||||
|
||||
compressed = BytesIO()
|
||||
with gzip.GzipFile(fileobj=compressed, mode='w') as f:
|
||||
json_response = json.dumps(output)
|
||||
f.write(json_response.encode('utf-8'))
|
||||
|
||||
gzippedResponse = compressed.getvalue()
|
||||
return {
|
||||
"body": base64.b64encode(gzippedResponse).decode(),
|
||||
"isBase64Encoded": True,
|
||||
"statusCode": 200,
|
||||
"headers": {
|
||||
"Content-Encoding": "gzip",
|
||||
"content-type": "application/json"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def get_listeners(event, context):
|
||||
|
||||
@ -489,16 +536,17 @@ if __name__ == "__main__":
|
||||
# {},
|
||||
# )
|
||||
# )
|
||||
print(
|
||||
get_telem(
|
||||
{
|
||||
"queryStringParameters": {
|
||||
"duration": "3d",
|
||||
"serial": "P4120469"
|
||||
}},{}
|
||||
print(get_sites({},{}))
|
||||
# print(
|
||||
# get_telem(
|
||||
# {
|
||||
# "queryStringParameters": {
|
||||
# "duration": "3d",
|
||||
# "serial": "P4120469"
|
||||
# }},{}
|
||||
|
||||
)
|
||||
)
|
||||
# )
|
||||
# )
|
||||
# print (
|
||||
# get_chase(
|
||||
# {"queryStringParameters": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user