Skip to content

Commit e9e6220

Browse files
authored
Define CloudWatch Alarm Payload structure (#296)
* Added CloudWatchAlarmSnsPayload struct and nested structs within the payload * Added test for the unmarshaling of CloudWatchAlarmSnsPayload * Added sample event json files * Adding module files * Fixed json tag * Initialisms and acronyms changed to caps. * Sanitized the example json payload * Removed unused dependency * Fix the new type name using caps * Changed all CloudWatch types to not being pointers for consistency
1 parent 0b4291a commit e9e6220

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

events/sns.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,59 @@ type SNSEntity struct {
3030
UnsubscribeURL string `json:"UnsubscribeUrl"`
3131
Subject string `json:"Subject"`
3232
}
33+
34+
type CloudWatchAlarmSNSPayload struct {
35+
AlarmName string `json:"AlarmName"`
36+
AlarmDescription string `json:"AlarmDescription"`
37+
AWSAccountID string `json:"AWSAccountId"`
38+
NewStateValue string `json:"NewStateValue"`
39+
NewStateReason string `json:"NewStateReason"`
40+
StateChangeTime string `json:"StateChangeTime"`
41+
Region string `json:"Region"`
42+
AlarmARN string `json:"AlarmArn"`
43+
OldStateValue string `json:"OldStateValue"`
44+
Trigger CloudWatchAlarmTrigger `json:"Trigger"`
45+
}
46+
47+
type CloudWatchAlarmTrigger struct {
48+
Period int64 `json:"Period"`
49+
EvaluationPeriods int64 `json:"EvaluationPeriods"`
50+
ComparisonOperator string `json:"ComparisonOperator"`
51+
Threshold float64 `json:"Threshold"`
52+
TreatMissingData string `json:"TreatMissingData"`
53+
EvaluateLowSampleCountPercentile string `json:"EvaluateLowSampleCountPercentile"`
54+
Metrics []CloudWatchMetricDataQuery `json:"Metrics,omitempty"`
55+
MetricName string `json:"MetricName,omitempty"`
56+
Namespace string `json:"Namespace,omitempty"`
57+
StatisticType string `json:"StatisticType,omitempty"`
58+
Statistic string `json:"Statistic,omitempty"`
59+
Unit string `json:"Unit,omitempty"`
60+
Dimensions []CloudWatchDimension `json:"Dimensions,omitempty"`
61+
}
62+
63+
type CloudWatchMetricDataQuery struct {
64+
Expression string `json:"Expression,omitempty"`
65+
ID string `json:"Id"`
66+
Label string `json:"Label,omitempty"`
67+
MetricStat CloudWatchMetricStat `json:"MetricStat,omitempty"`
68+
Period int64 `json:"Period,omitempty"`
69+
ReturnData bool `json:"ReturnData,omitempty"`
70+
}
71+
72+
type CloudWatchMetricStat struct {
73+
Metric CloudWatchMetric `json:"Metric"`
74+
Period int64 `json:"Period"`
75+
Stat string `json:"Stat"`
76+
Unit string `json:"Unit,omitempty"`
77+
}
78+
79+
type CloudWatchMetric struct {
80+
Dimensions []CloudWatchDimension `json:"Dimensions,omitempty"`
81+
MetricName string `json:"MetricName,omitempty"`
82+
Namespace string `json:"Namespace,omitempty"`
83+
}
84+
85+
type CloudWatchDimension struct {
86+
Name string `json:"name"`
87+
Value string `json:"value"`
88+
}

events/sns_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,32 @@ func TestSnsEventMarshaling(t *testing.T) {
3333
func TestSnsMarshalingMalformedJson(t *testing.T) {
3434
test.TestMalformedJson(t, SNSEvent{})
3535
}
36+
37+
func TestCloudWatchAlarmSNSPayloadMarshaling(t *testing.T) {
38+
// 1. read JSON from file
39+
inputJson := test.ReadJSONFromFile(t, "./testdata/cloudwatch-alarm-sns-payload-single-metric.json")
40+
41+
// 2. de-serialize into Go object
42+
var inputEvent SNSEvent
43+
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
44+
t.Errorf("could not unmarshal event. details: %v", err)
45+
}
46+
47+
// 3.retrieve message from the Go object
48+
var message = inputEvent.Records[0].SNS.Message
49+
50+
// 4. de-serialize message into Go object
51+
var inputCloudWatchPayload CloudWatchAlarmSNSPayload
52+
if err := json.Unmarshal([]byte(message), &inputCloudWatchPayload); err != nil {
53+
t.Errorf("could not unmarshal event. details: %v", err)
54+
}
55+
56+
// 5. serialize message to JSON
57+
outputJson, err := json.Marshal(inputCloudWatchPayload)
58+
if err != nil {
59+
t.Errorf("could not marshal event. details: %v", err)
60+
}
61+
62+
// 4. check result
63+
assert.JSONEq(t, string(message), string(outputJson))
64+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
10+
"TopicArn": "arn:aws:sns:EXAMPLE",
11+
"Subject": "TestInvoke",
12+
"Message": "{\"AlarmName\":\"EXAMPLE\",\"AlarmDescription\":\"EXAMPLE\",\"AWSAccountId\":\"EXAMPLE\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 out of the last 1 datapoints [1234.0 (06/03/15 17:43:27)] was greater than the threshold (0.0) (minimum 1 datapoint for OK -> ALARM transition).\",\"StateChangeTime\":\"2015-06-03T17:43:27.123+0000\",\"Region\":\"EXAMPLE\",\"AlarmArn\":\"arn:aws:cloudwatch:REGION:ACCOUNT_NUMBER:alarm:EXAMPLE\",\"OldStateValue\":\"INSUFFICIENT_DATA\",\"Trigger\":{\"Period\":60,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanThreshold\",\"Threshold\":0.0,\"TreatMissingData\":\"- TreatMissingData: missing\",\"EvaluateLowSampleCountPercentile\":\"\",\"Metrics\":[{\"Expression\":\"m1*1\",\"Id\":\"e1\",\"Label\":\"Expression1\",\"ReturnData\":true},{\"Id\":\"m1\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"TestInstance\",\"name\":\"InstanceId\"}],\"MetricName\":\"NetworkOut\",\"Namespace\":\"AWS/EC2\"},\"Period\":60,\"Stat\":\"Average\"},\"ReturnData\":false}]}}",
13+
"Timestamp": "2015-06-03T17:43:27.123Z",
14+
"SignatureVersion": "1",
15+
"Signature": "EXAMPLE",
16+
"SigningCertUrl": "EXAMPLE",
17+
"UnsubscribeUrl": "EXAMPLE",
18+
"MessageAttributes": {}
19+
}
20+
}
21+
]
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
10+
"TopicArn": "arn:aws:sns:EXAMPLE",
11+
"Subject": "TestInvoke",
12+
"Message": "{\"AlarmName\": \"EXAMPLE\",\"AlarmDescription\": \"EXAMPLE\",\"AWSAccountId\": \"123456789012\",\"NewStateValue\": \"ALARM\",\"NewStateReason\": \"Threshold Crossed: 1 out of the last 1 datapoints [1234.0 (06/03/15 17:43:27)] was greater than the threshold (0.0) (minimum 1 datapoint for OK -> ALARM transition).\",\"StateChangeTime\": \"2015-06-03T17:43:27.123+0000\",\"Region\": \"EXAMPLE\",\"AlarmArn\": \"arn:aws:cloudwatch:REGION:ACCOUNT_NUMBER:alarm:EXAMPLE\",\"OldStateValue\": \"INSUFFICIENT_DATA\",\"Trigger\": {\"MetricName\": \"NetworkOut\",\"Namespace\": \"AWS/EC2\",\"StatisticType\": \"Statistic\",\"Statistic\": \"AVERAGE\",\"Unit\": \"Bytes\",\"Dimensions\": [{\"value\": \"TestInstance\",\"name\": \"InstanceId\"}],\"Period\": 60,\"EvaluationPeriods\": 1,\"ComparisonOperator\": \"GreaterThanThreshold\",\"Threshold\": 0.0,\"TreatMissingData\": \"- TreatMissingData: missing\",\"EvaluateLowSampleCountPercentile\": \"\"}}",
13+
"Timestamp": "2015-06-03T17:43:27.123Z",
14+
"SignatureVersion": "1",
15+
"Signature": "EXAMPLE",
16+
"SigningCertUrl": "EXAMPLE",
17+
"UnsubscribeUrl": "EXAMPLE",
18+
"MessageAttributes": {}
19+
}
20+
}
21+
]
22+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/aws/aws-lambda-go
33
go 1.12
44

55
require (
6-
github.com/stretchr/testify v1.4.0
6+
github.com/stretchr/testify v1.5.1
77
github.com/urfave/cli/v2 v2.1.1
88
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/aws/aws-sdk-go v1.31.12 h1:SxRRGyhlCagI0DYkhOg+FgdXGXzRTE3vEX/gsgFaiKQ=
3+
github.com/aws/aws-sdk-go v1.31.12/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
24
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
35
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
46
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
9+
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
10+
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
11+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
612
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
713
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
814
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
@@ -13,8 +19,14 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1319
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1420
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1521
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
22+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
23+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1624
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
1725
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
26+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
27+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
28+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
29+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1830
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1931
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2032
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

0 commit comments

Comments
 (0)