mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Merge pull request #3003 from alphagov/staging-preview-cbc
Non-production environments invoke CBC Proxy during broadcast event creation
This commit is contained in:
@@ -23,6 +23,7 @@ from werkzeug.local import LocalProxy
|
||||
|
||||
from app.celery.celery import NotifyCelery
|
||||
from app.clients import Clients
|
||||
from app.clients.cbc_proxy import CBCProxyClient, CBCProxyNoopClient
|
||||
from app.clients.document_download import DocumentDownloadClient
|
||||
from app.clients.email.aws_ses import AwsSesClient
|
||||
from app.clients.email.aws_ses_stub import AwsSesStubClient
|
||||
@@ -60,6 +61,7 @@ zendesk_client = ZendeskClient()
|
||||
statsd_client = StatsdClient()
|
||||
redis_store = RedisClient()
|
||||
performance_platform_client = PerformancePlatformClient()
|
||||
cbc_proxy_client = CBCProxyNoopClient()
|
||||
document_download_client = DocumentDownloadClient()
|
||||
metrics = GDSMetrics()
|
||||
|
||||
@@ -112,6 +114,11 @@ def create_app(application):
|
||||
performance_platform_client.init_app(application)
|
||||
document_download_client.init_app(application)
|
||||
|
||||
global cbc_proxy_client
|
||||
if application.config['CBC_PROXY_AWS_ACCESS_KEY_ID']:
|
||||
cbc_proxy_client = CBCProxyClient()
|
||||
cbc_proxy_client.init_app(application)
|
||||
|
||||
register_blueprint(application)
|
||||
register_v2_blueprints(application)
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@ import requests
|
||||
from flask import current_app
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
|
||||
from app import notify_celery
|
||||
from app import cbc_proxy_client, notify_celery
|
||||
|
||||
from app.models import BroadcastEventMessageType
|
||||
from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
|
||||
|
||||
|
||||
@@ -12,6 +13,19 @@ from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id
|
||||
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
|
||||
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
|
||||
|
||||
if broadcast_event.message_type == BroadcastEventMessageType.ALERT:
|
||||
current_app.logger.info(
|
||||
f'invoking cbc proxy to send '
|
||||
f'broadcast_event {broadcast_event.reference} '
|
||||
f'msgType {broadcast_event.message_type} to {provider}'
|
||||
)
|
||||
|
||||
cbc_proxy_client.create_and_send_broadcast(
|
||||
identifier=str(broadcast_event.id),
|
||||
headline="GOV.UK Notify Broadcast",
|
||||
description=broadcast_event.transmitted_content['body'],
|
||||
)
|
||||
|
||||
current_app.logger.info(
|
||||
f'sending broadcast_event {broadcast_event.reference} '
|
||||
f'msgType {broadcast_event.message_type} to {provider}'
|
||||
|
||||
90
app/clients/cbc_proxy.py
Normal file
90
app/clients/cbc_proxy.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import json
|
||||
|
||||
import boto3
|
||||
|
||||
# The variable names in this file have specific meaning in a CAP message
|
||||
#
|
||||
# identifier is a unique field for each CAP message
|
||||
#
|
||||
# headline is a field which we are not sure if we will use
|
||||
#
|
||||
# description is the body of the message
|
||||
#
|
||||
# references is a whitespace separated list of message identifiers
|
||||
# where each identifier is a previous sent message
|
||||
# ie a Cancel message would have a unique identifier but have the identifier of
|
||||
# the preceeding Alert message in the references field
|
||||
|
||||
|
||||
# Noop = no operation
|
||||
class CBCProxyNoopClient:
|
||||
|
||||
def init_app(self, app):
|
||||
pass
|
||||
|
||||
def create_and_send_broadcast(
|
||||
self,
|
||||
identifier, headline, description,
|
||||
):
|
||||
pass
|
||||
|
||||
# We have not implementated updating a broadcast
|
||||
def update_and_send_broadcast(
|
||||
self,
|
||||
identifier, references, headline, description,
|
||||
):
|
||||
pass
|
||||
|
||||
# We have not implemented cancelling a broadcast
|
||||
def cancel_broadcast(
|
||||
self,
|
||||
identifier, references, headline, description,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class CBCProxyClient:
|
||||
|
||||
def init_app(self, app):
|
||||
self._lambda_client = boto3.client(
|
||||
'lambda',
|
||||
region_name='eu-west-2',
|
||||
aws_access_key_id=app.config['CBC_PROXY_AWS_ACCESS_KEY_ID'],
|
||||
aws_secret_access_key=app.config['CBC_PROXY_AWS_SECRET_ACCESS_KEY'],
|
||||
)
|
||||
|
||||
def create_and_send_broadcast(
|
||||
self,
|
||||
identifier, headline, description,
|
||||
):
|
||||
payload_bytes = bytes(json.dumps({
|
||||
'identifier': identifier,
|
||||
'headline': headline,
|
||||
'description': description,
|
||||
}), encoding='utf8')
|
||||
|
||||
result = self._lambda_client.invoke(
|
||||
FunctionName='bt-ee-1-proxy',
|
||||
InvocationType='RequestResponse',
|
||||
Payload=payload_bytes,
|
||||
)
|
||||
|
||||
if result['StatusCode'] > 299:
|
||||
raise Exception('Could not invoke lambda')
|
||||
|
||||
if 'FunctionError' in result:
|
||||
raise Exception('Function exited with unhandled exception')
|
||||
|
||||
# We have not implementated updating a broadcast
|
||||
def update_and_send_broadcast(
|
||||
self,
|
||||
identifier, references, headline, description,
|
||||
):
|
||||
pass
|
||||
|
||||
# We have not implemented cancelling a broadcast
|
||||
def cancel_broadcast(
|
||||
self,
|
||||
identifier, references, headline, description,
|
||||
):
|
||||
pass
|
||||
@@ -353,6 +353,11 @@ class Config(object):
|
||||
|
||||
AWS_REGION = 'eu-west-1'
|
||||
|
||||
# CBC Proxy
|
||||
# if the access keys are empty then noop client is used
|
||||
CBC_PROXY_AWS_ACCESS_KEY_ID = os.environ.get('CBC_PROXY_AWS_ACCESS_KEY_ID', '')
|
||||
CBC_PROXY_AWS_SECRET_ACCESS_KEY = os.environ.get('CBC_PROXY_AWS_SECRET_ACCESS_KEY', '')
|
||||
|
||||
|
||||
######################
|
||||
# Config overrides ###
|
||||
|
||||
Reference in New Issue
Block a user