Skip to content
Permalink
Browse files

feat(api): expose publish-to-social report content via GET /social_re…

…port
  • Loading branch information
shaftoe committed Feb 22, 2020
1 parent cbee227 commit 231cf3d4a48430e40f1d6ba867f0bf6a25effc4e
@@ -1 +1 @@
0.4.0
0.5.0b
@@ -44,23 +44,24 @@
},
)

ApiStack(
PUBLISH_TO_SOCIAL_STACK = SocialPublishStack(
APP,
'api',
lambda_notifications=NOTIFICATIONS_STACK.pushover,
'publish-to-social',
lambda_layers=LAYERS_STACK.layers,
tags={
'Managed': 'cdk',
'Name': 'api',
'Name': 'publish-to-social',
},
)

SocialPublishStack(
ApiStack(
APP,
'publish-to-social',
lambda_layers=LAYERS_STACK.layers,
'api',
lambda_notifications=NOTIFICATIONS_STACK.pushover,
social_log_group=PUBLISH_TO_SOCIAL_STACK.log_group,
tags={
'Managed': 'cdk',
'Name': 'publish-to-social',
'Name': 'api',
},
)

@@ -16,7 +16,9 @@
class ApiStack(core.Stack):

def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-builtin
lambda_notifications: aws_lambda.Function, **kwargs) -> None:
lambda_notifications: aws_lambda.IFunction,
social_log_group: aws_logs.ILogGroup,
**kwargs) -> None:

super().__init__(scope, id, **kwargs)

@@ -31,9 +33,11 @@ def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-
'PUSHOVER_USERKEY': env['PUSHOVER_USERKEY'],
'LAMBDA_FUNCTIONS_LOG_LEVEL': 'INFO',
'LAMBDA_NOTIFICATIONS': lambda_notifications.function_name,
'REPORT_LOG_GROUP_NAME': social_log_group.log_group_name,
},
)
lambda_notifications.grant_invoke(api_lambda)
social_log_group.grant(api_lambda, "logs:GetLogEvents", "logs:DescribeLogStreams")

cert = aws_certificatemanager.Certificate(
self,
@@ -1,12 +1,17 @@
import json
from os import environ as env
from typing import Mapping

import utils
import utils.aws as aws
import utils.handlers as handlers


LAMBDA_NOTIFICATIONS = env["LAMBDA_NOTIFICATIONS"]
def read_social_report(event: utils.LambdaEvent) -> Mapping: # pylint: disable=unused-argument
"""Get all events from CloudWatch REPORT_LOG_GROUP_NAME group."""
log_group_name = env["REPORT_LOG_GROUP_NAME"]

return aws.read_all_log_streams(log_group=log_group_name)


def contact(event: utils.LambdaEvent) -> str:
@@ -20,6 +25,8 @@ def contact(event: utils.LambdaEvent) -> str:
- email
- description
"""
lambda_notifications = env["LAMBDA_NOTIFICATIONS"]

body = event["body"]

utils.Log.debug("Processing body payload: %s", body)
@@ -46,7 +53,7 @@ def contact(event: utils.LambdaEvent) -> str:
utils.Log.debug("#############################")

return aws.invoke_lambda(
name=LAMBDA_NOTIFICATIONS,
name=lambda_notifications,
payload={
"title": "New /contact submission received",
"payload": msg,
@@ -60,6 +67,7 @@ def handler(event, context) -> utils.Response:
"""
router_map = {
"POST /contact": contact,
"GET /social_report": read_social_report,
}

return handlers.ApiGatewayEventHandler(name="api",
@@ -49,26 +49,26 @@ def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-
)
topic.grant_publish(lambda_publish_to_social)

# REPORT lambda and CloudWatch resources
# REPORT lambdas and CloudWatch resources
report_log_group_name = "%s-reports" % id

log_group = aws_logs.LogGroup(
self.log_group = aws_logs.LogGroup(
self,
"%s-report-log-group" % id,
log_group_name=report_log_group_name,
retention=DEFAULT_LOG_RETENTION,
)

report_lambda = get_lambda(
create_report_lambda = get_lambda(
self,
"%s-report" % id,
"%s-create-report" % id,
code=code,
handler="send_report.handler",
environment={
"REPORT_LOG_GROUP_NAME": report_log_group_name,
}
)
log_group.grant_write(report_lambda)
self.log_group.grant_write(create_report_lambda)

# SUBSCRIBE lambdas
social_lambdas = [social.lower()
@@ -78,8 +78,8 @@ def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-
if social]

for social in social_lambdas:
log_group.add_stream(
"%s-report-log-stream" % id,
self.log_group.add_stream(
"%s-%s-report-log-stream" % (id, social),
log_stream_name=social)

def build_lambda(name):
@@ -98,7 +98,7 @@ def build_lambda(name):
if var.startswith(name.upper())
or var.startswith("LAMBDA_FUNCTIONS_")
or var.startswith("GITHUB_")},
on_success=aws_lambda_destinations.LambdaDestination(report_lambda))
on_success=aws_lambda_destinations.LambdaDestination(create_report_lambda))

topic.add_subscription(aws_sns_subscriptions.LambdaSubscription(_lambda))

@@ -45,7 +45,7 @@ def put_record_to_logstream(event: utils.LambdaEvent) -> str:
def handler(event, context) -> utils.Response:
"""Lambda entry point."""
return handlers.EventHandler(
name="publish_to_social",
name="send_report",
event=utils.LambdaEvent(event),
context=utils.LambdaContext(context),
action=put_record_to_logstream,
@@ -1,5 +1,8 @@
from time import time
from typing import Mapping
from typing import (
Mapping,
Iterable,
)
import json

from . import (
@@ -141,7 +144,7 @@ def send_event_to_logstream(log_group: str, log_stream: str, message: Mapping) -
status_code=500)


def read_log_stream(log_group: str, log_stream: str):
def read_log_stream(log_group: str, log_stream: str) -> Iterable:
"""Return all events from log stream."""
Log.debug("Read events from CloudWatch LogGroup %s Stream %s",
log_group, log_stream)
@@ -153,3 +156,16 @@ def read_log_stream(log_group: str, log_stream: str):
logStreamName=log_stream)

return resp["events"]


def read_all_log_streams(log_group: str) -> Mapping:
Log.debug("Read all events from all CloudWatch LogGroup %s Streams", log_group)

boto3 = import_non_stdlib_module("boto3")
client = boto3.client("logs")
resp = client.describe_log_streams(logGroupName=log_group)

streams = [stream["logStreamName"] for stream in resp["logStreams"]]

return {stream: read_log_stream(log_group=log_group, log_stream=stream)
for stream in streams}

0 comments on commit 231cf3d

Please sign in to comment.
You can’t perform that action at this time.