-
Notifications
You must be signed in to change notification settings - Fork 1
Initial setup to trigger log upload lambda for broadcast alerts #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AlecAshmore
wants to merge
29
commits into
main
Choose a base branch
from
Trigger-Lambda-MNO-Portal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6074fe8
Initial setup to trigger log upload lambda for broadcast alerts
AlecAshmore 70bda68
Added in the name of the lambda to be triggered on alert broadcast
AlecAshmore 59a6c5e
Updated taskname for sending broadcast event on the trigger lambda
AlecAshmore ace88a2
Corrected import argument for the request_logs_task
AlecAshmore 9dab139
Moved lambda invocation logic into its own file
AlecAshmore 9d28bdc
Aligned correct naming convention for the TaskName
AlecAshmore 4b253a3
Incorrect import statement causing errors, updated
AlecAshmore 45a03f1
update the tests to account for the new request-log-ingest task being…
AlecAshmore f3fa805
Hardcoded emails, for initial testing before moving to MNO contact in…
AlecAshmore 6cc0f56
Ensure log ingest task is being picked up
AlecAshmore c39b88e
addressed issues of lists and arrays colliding on task_imports
AlecAshmore 7be7d2c
addressed issues of lists and arrays colliding on task_imports
AlecAshmore 2cbd9c2
Identified error relating to .broadcast not recognised - changed to b…
AlecAshmore a0e99ad
Changed to broadcast_event for the mno contact function
AlecAshmore ebdb5d9
Ensured matching lambda names for the log upload to be invoked
AlecAshmore d8af239
Logging to aid debugging
AlecAshmore 5a5866c
explicitly listed lambda arn for cross account access
AlecAshmore 758bb99
Added additonal test email to represent MNO
AlecAshmore 8648cf1
Remove Mehmet from mno contact
AlecAshmore dbca19c
Merge branch 'main' of https://github.com/alphagov/emergency-alerts-a…
leylayaltiligil 139943d
MNO contact information
AlecAshmore ac16586
Merge branch 'Trigger-Lambda-MNO-Portal' of https://github.com/alphag…
leylayaltiligil 8234ca5
Merge remote-tracking branch 'origin/trigger-lambda-mno-portal' into …
AlecAshmore 1ab78a1
Expanded logging on mno contact function
AlecAshmore a28de9f
Adjusted helper from boto3 to using ecs task def/config
AlecAshmore 9470b8b
Merge branch 'Trigger-Lambda-MNO-Portal' of https://github.com/alphag…
leylayaltiligil 64ac50d
Formatting
leylayaltiligil b269fce
Removed the need for mno emails being grabbed from param store, just …
AlecAshmore 389a061
Adjusted lambda arn to default as empty
AlecAshmore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import json | ||
|
|
||
| import boto3 | ||
| import botocore.exceptions | ||
| from emergency_alerts_utils.celery import TaskNames | ||
| from flask import current_app | ||
|
|
||
| from app import notify_celery | ||
| from app.models import BroadcastEvent | ||
|
|
||
|
|
||
| @notify_celery.task(name=TaskNames.REQUEST_LOG_INGEST) | ||
| def request_log_ingest_task(broadcast_event_id): | ||
| """ | ||
| Invokes the operator portal log upload Lambda to send invite emails to MNOs. | ||
| """ | ||
| try: | ||
| current_app.logger.info("Starting request_log_ingest_task", extra={"broadcast_event_id": broadcast_event_id}) | ||
|
|
||
| broadcast_event = BroadcastEvent.query.get(broadcast_event_id) | ||
| if not broadcast_event: | ||
| current_app.logger.error( | ||
| f"Broadcast event {broadcast_event_id} not found", extra={"broadcast_event_id": broadcast_event_id} | ||
| ) | ||
| return False | ||
|
|
||
| broadcast = broadcast_event.broadcast_message | ||
|
|
||
| payload = { | ||
| "alert_reference": broadcast.reference, | ||
| "environment": current_app.config.get("ENVIRONMENT"), | ||
| "broadcast_start": ( | ||
| broadcast_event.transmitted_starts_at.isoformat() if broadcast_event.transmitted_starts_at else None | ||
| ), | ||
| "broadcast_end": ( | ||
| broadcast_event.transmitted_finishes_at.isoformat() if broadcast_event.transmitted_finishes_at else None | ||
| ), | ||
| "mnos": [ | ||
| {"mno_id": provider.upper()} for provider in broadcast_event.service.get_available_broadcast_providers() | ||
| ], | ||
| } | ||
|
|
||
| current_app.logger.info( | ||
| "Built Lambda payload", extra={"alert_reference": broadcast.reference, "payload": json.dumps(payload)} | ||
| ) | ||
|
|
||
| lambda_arn = current_app.config.get("LOG_UPLOAD_LAMBDA_ARN") | ||
|
|
||
| current_app.logger.info( | ||
| "About to invoke Lambda", extra={"lambda_arn": lambda_arn, "has_lambda_arn": lambda_arn is not None} | ||
| ) | ||
|
|
||
| if not lambda_arn: | ||
| current_app.logger.error("LOG_UPLOAD_LAMBDA_ARN not configured!") | ||
| return False | ||
|
|
||
| success = _invoke_log_upload_lambda(lambda_arn, payload) | ||
|
|
||
| if success: | ||
| current_app.logger.info( | ||
| "Successfully invoked log upload Lambda", | ||
| extra={"broadcast_reference": broadcast.reference, "broadcast_event_id": broadcast_event_id}, | ||
| ) | ||
| else: | ||
| current_app.logger.error( | ||
| "Failed to invoke log upload Lambda", | ||
| extra={"broadcast_reference": broadcast.reference, "broadcast_event_id": broadcast_event_id}, | ||
| ) | ||
|
|
||
| return success | ||
|
|
||
| except Exception as e: | ||
| current_app.logger.exception( | ||
| f"Error in request_log_ingest_task for broadcast_event {broadcast_event_id}", | ||
| extra={ | ||
| "exception_type": type(e).__name__, | ||
| "python_module": __name__, | ||
| "exception": str(e), | ||
| "broadcast_event_id": broadcast_event_id, | ||
| }, | ||
| ) | ||
| raise | ||
|
|
||
|
|
||
| def _invoke_log_upload_lambda(lambda_name, payload): | ||
| """ | ||
| Invoke the log upload Lambda function | ||
| """ | ||
| lambda_client = boto3.client("lambda", region_name="eu-west-2") | ||
| payload_bytes = bytes(json.dumps(payload), encoding="utf8") | ||
|
|
||
| try: | ||
| current_app.logger.info( | ||
| f"Calling log upload lambda {lambda_name}", | ||
| extra={ | ||
| "lambda_payload": str(payload), | ||
| "lambda_invocation_type": "Event", | ||
| }, | ||
| ) | ||
|
|
||
| response = lambda_client.invoke( | ||
| FunctionName=lambda_name, | ||
| InvocationType="Event", | ||
| Payload=payload_bytes, | ||
| ) | ||
|
|
||
| if response["StatusCode"] == 202: | ||
| current_app.logger.info( | ||
| f"Successfully invoked log upload lambda {lambda_name}", | ||
| extra={ | ||
| "status_code": response["StatusCode"], | ||
| }, | ||
| ) | ||
| return True | ||
| else: | ||
| current_app.logger.error( | ||
| f"Error invoking log upload lambda {lambda_name}", | ||
| extra={ | ||
| "status_code": response["StatusCode"], | ||
| }, | ||
| ) | ||
| return False | ||
|
|
||
| except botocore.exceptions.ClientError as e: | ||
| current_app.logger.error( | ||
| f"Boto3 ClientError calling log upload lambda {lambda_name}", | ||
| extra={ | ||
| "python_module": __name__, | ||
| "error": str(e), | ||
| }, | ||
| ) | ||
| return False | ||
|
|
||
| except Exception as e: | ||
| current_app.logger.error( | ||
| f"Unexpected error calling log upload lambda {lambda_name}", | ||
| extra={ | ||
| "python_module": __name__, | ||
| "error": str(e), | ||
| }, | ||
| ) | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.