Skip to content

Commit

Permalink
#479 code improvements after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmarquezini committed Dec 19, 2023
1 parent de0ba7c commit ecb3965
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 41 deletions.
7 changes: 4 additions & 3 deletions events/cloudwatch_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"time"
)

// EventBridgeEvent is the outer structure of an event sent via EventBridge serverless service.
// For examples of events that come via EventBridge, see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html
type EventBridgeEvent struct {
// CloudWatchEvent is the outer structure of an event sent via EventBridge serverless service.
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
DetailType string `json:"detail-type"`
Expand All @@ -18,3 +17,5 @@ type EventBridgeEvent struct {
Resources []string `json:"resources"`
Detail json.RawMessage `json:"detail"`
}

type EventBridge = CloudWatchEvent
4 changes: 2 additions & 2 deletions events/cloudwatch_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCloudwatchScheduledEventIdempotency(t *testing.T) {
"\"resources\":[\"arn:aws:events:us-east-1:123456789012:rule/SampleRule\"]," +
"\"detail\":{}}")

var inputEvent EventBridgeEvent
var inputEvent CloudWatchEvent
err := json.Unmarshal(inputJSON, &inputEvent)
if err != nil {
t.Errorf("Could not unmarshal scheduled event: %v", err)
Expand All @@ -32,5 +32,5 @@ func TestCloudwatchScheduledEventIdempotency(t *testing.T) {
}

func TestCloudwatchScheduledEventRequestMalformedJson(t *testing.T) {
test.TestMalformedJson(t, EventBridgeEvent{})
test.TestMalformedJson(t, CloudWatchEvent{})
}
36 changes: 18 additions & 18 deletions events/cloudwatch_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"encoding/json"
)

// EventBridgeEventLogs represents raw data from an eventbridge logs event
type EventBridgeEventLogs struct {
AWSLogs EventBridgeLogsRawData `json:"awslogs"`
// CloudwatchLogsEvent represents raw data from a cloudwatch logs event
type CloudwatchLogsEvent struct {
AWSLogs CloudwatchLogsRawData `json:"awslogs"`
}

// EventBridgeLogsRawData contains gzipped base64 json representing the bulk
// of an eventbridge logs event
type EventBridgeLogsRawData struct {
// CloudwatchLogsRawData contains gzipped base64 json representing the bulk
// of a cloudwatch logs event
type CloudwatchLogsRawData struct {
Data string `json:"data"`
}

// Parse returns a struct representing a usable EventBridgeLogs event
func (c EventBridgeLogsRawData) Parse() (d EventBridgeLogsData, err error) {
// Parse returns a struct representing a usable CloudwatchLogs event
func (c CloudwatchLogsRawData) Parse() (d CloudwatchLogsData, err error) {
data, err := base64.StdEncoding.DecodeString(c.Data)
if err != nil {
return
Expand All @@ -37,18 +37,18 @@ func (c EventBridgeLogsRawData) Parse() (d EventBridgeLogsData, err error) {
return
}

// EventBridgeLogsData is an unmarshal'd, ungzip'd, eventbridge logs event
type EventBridgeLogsData struct {
Owner string `json:"owner"`
LogGroup string `json:"logGroup"`
LogStream string `json:"logStream"`
SubscriptionFilters []string `json:"subscriptionFilters"`
MessageType string `json:"messageType"`
LogEvents []EventBridgeLogEvent `json:"logEvents"`
// CloudwatchLogsData is an unmarshal'd, ungzip'd, cloudwatch logs event
type CloudwatchLogsData struct {
Owner string `json:"owner"`
LogGroup string `json:"logGroup"`
LogStream string `json:"logStream"`
SubscriptionFilters []string `json:"subscriptionFilters"`
MessageType string `json:"messageType"`
LogEvents []CloudwatchLogsLogEvent `json:"logEvents"`
}

// EventBridgeLogEvent represents a log entry from eventbridge logs
type EventBridgeLogEvent struct {
// CloudwatchLogsLogEvent represents a log entry from cloudwatch logs
type CloudwatchLogsLogEvent struct {
ID string `json:"id"`
Timestamp int64 `json:"timestamp"`
Message string `json:"message"`
Expand Down
16 changes: 8 additions & 8 deletions events/cloudwatch_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func TestCloudwatchLogs(t *testing.T) {
name string
eventJSON string
expectError bool
expectEventBridgeData EventBridgeEventLogs
expectEventBridgeData CloudwatchLogsEvent
}{
{"Well formed cloudwatch event",
"./testdata/cloudwatch-logs-event.json",
false,
EventBridgeEventLogs{
AWSLogs: EventBridgeLogsRawData{
CloudwatchLogsEvent{
AWSLogs: CloudwatchLogsRawData{
Data: "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA==",
},
},
Expand All @@ -29,7 +29,7 @@ func TestCloudwatchLogs(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
inputJSON := tst.ReadJSONFromFile(t, test.eventJSON)

var inputEvent EventBridgeEventLogs
var inputEvent CloudwatchLogsEvent
err := json.Unmarshal(inputJSON, &inputEvent)

if err != nil && !test.expectError {
Expand All @@ -52,20 +52,20 @@ func TestCloudwatchLogsParse(t *testing.T) {
name string
eventJSON string
expectError bool
expectCloudwatchLogsData EventBridgeLogsData
expectCloudwatchLogsData CloudwatchLogsData
}{
{"Well formed cloudwatch event",
"./testdata/cloudwatch-logs-event.json",
false,
EventBridgeLogsData{
CloudwatchLogsData{
Owner: "123456789123",
LogGroup: "testLogGroup",
LogStream: "testLogStream",
SubscriptionFilters: []string{
"testFilter",
},
MessageType: "DATA_MESSAGE",
LogEvents: []EventBridgeLogEvent{
LogEvents: []CloudwatchLogsLogEvent{
{ID: "eventId1", Timestamp: 1440442987000, Message: "[ERROR] First test message"},
{ID: "eventId2", Timestamp: 1440442987001, Message: "[ERROR], Second test message"},
},
Expand All @@ -76,7 +76,7 @@ func TestCloudwatchLogsParse(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
inputJSON := tst.ReadJSONFromFile(t, test.eventJSON)

var inputEvent EventBridgeEventLogs
var inputEvent CloudwatchLogsEvent
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
Expand Down
2 changes: 0 additions & 2 deletions events/codedeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const (
CodeDeployDeploymentStateSuccess CodeDeployDeploymentState = "SUCCESS"
)

// CodeDeployEvent is documented at:
// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html#acd_event_types
type CodeDeployEvent struct {
// AccountID is the id of the AWS account from which the event originated.
AccountID string `json:"account"`
Expand Down
6 changes: 3 additions & 3 deletions events/codepipeline_cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const (
CodePipelineActionStateCanceled CodePipelineActionState = "CANCELED"
)

// CodePipelineEvent is documented at:
// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html#codepipeline_event_type
type CodePipelineEventBridgeEvent struct {
type CodePipelineCloudWatchEvent struct {
// Version is the version of the event's schema.
Version string `json:"version"`

Expand Down Expand Up @@ -76,6 +74,8 @@ type CodePipelineEventBridgeEvent struct {
Detail CodePipelineEventDetail `json:"detail"`
}

type CodePipelineEventBridgeEvent = CodePipelineCloudWatchEvent

type CodePipelineEventDetail struct {
Pipeline string `json:"pipeline"`

Expand Down
10 changes: 5 additions & 5 deletions events/codepipeline_cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
func TestUnmarshalCodePipelineEvent(t *testing.T) {
tests := []struct {
input string
expect CodePipelineEventBridgeEvent
expect CodePipelineCloudWatchEvent
}{
{
input: "testdata/codepipeline-action-execution-stage-change-event.json",
expect: CodePipelineEventBridgeEvent{
expect: CodePipelineCloudWatchEvent{
Version: "0",
ID: "CWE-event-id",
DetailType: "CodePipeline Action Execution State Change",
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) {
},
{
input: "testdata/codepipeline-execution-stage-change-event.json",
expect: CodePipelineEventBridgeEvent{
expect: CodePipelineCloudWatchEvent{
Version: "0",
ID: "CWE-event-id",
DetailType: "CodePipeline Stage Execution State Change",
Expand All @@ -67,7 +67,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) {
},
{
input: "testdata/codepipeline-execution-state-change-event.json",
expect: CodePipelineEventBridgeEvent{
expect: CodePipelineCloudWatchEvent{
Version: "0",
ID: "CWE-event-id",
DetailType: "CodePipeline Pipeline Execution State Change",
Expand All @@ -92,7 +92,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) {
data, err := ioutil.ReadFile(testcase.input)
require.NoError(t, err)

var actual CodePipelineEventBridgeEvent
var actual CodePipelineCloudWatchEvent
require.NoError(t, json.Unmarshal(data, &actual))

require.Equal(t, testcase.expect, actual)
Expand Down

0 comments on commit ecb3965

Please sign in to comment.