Skip to content

Commit

Permalink
sns support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispruitt committed Nov 30, 2021
1 parent b47078f commit ca03f64
Show file tree
Hide file tree
Showing 134 changed files with 218 additions and 13,319 deletions.
61 changes: 61 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
project_name: aws-event-mock

builds:
- env:
- CGO_ENABLED=0
goos:
- darwin
- windows
- linux
binary: aws-event-mock

archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ .Tag }}"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

release:
disable: false

brews:
-
tap:
owner: chrispruitt
name: homebrew-tap
folder: Formula
commit_author:
name: goreleaserbot
email: [email protected]
homepage: "https://github.com/chrispruitt/aws-event-mock"
license: "Mozilla Public License Version 2.0"
signs:
- artifacts: checksum
args:
- "--batch"
- "--local-user"
- "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key
- "--output"
- "${signature}"
- "--detach-sign"
- "${artifact}"
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
test:
go run main.go create --type cloudwatch-log --message '{"message":"i am a cloudwatch log event. send me to lambda!"}'
go run main.go create --type sns --message '{"message":"i am an sns message. send me to lambda!"}'

generate-enums:
go-enum --names --file lib/event_type_enum.go

unit:
# Execute test recursivly
go test -v ./...
go test -v ./...

release:
goreleaser --rm-dist
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# aws-event-mock

A simple tool to quickly mockup aws test events like cloudwatch logs
A simple tool to quickly mockup aws test events like cloudwatch logs or sns events

## Usage

Expand Down Expand Up @@ -36,16 +36,43 @@ aws-event-mock create --type cloudwatch-log --message "@/some/filepath"
"data": "H4sIAAAAAAAA/0SNT0+EMBBHv8vv3E1atrq7vZGIXPQEN0MMyoQ0obRpB40hfHfDH93jmzfzZob/HinCQGVn/fB4ud5UdobA4Psy+inAgCnxyx9upuJIrburgwXS9JE+ow1s/fhsB6aYYN62tR3RCDhKqe2p/gkEg6e8zt9fi6rKy2KvF1808no3w3bHE1pntlMQYOsocesCjNJaap3drhcp5X8YBqcTr/WlWX4BAAD//wEAAP//obAJk+EAAAA="
}
}

aws-event-mock create --type sns --message '{"message":"i am an sns message. send me to lambda!"}' | jq
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
"EventSource": "aws.sns",
"Sns": {
"Signature": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Type": "Notification",
"TopicArn": "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "teststringvalue"
}
},
"SignatureVersion": "1",
"Timestamp": "2021-11-30T22:08:23.379513Z",
"SigningCertUrl": "EXAMPLE",
"Message": "{\"message\":\"i am an sns message. send me to lambda!\"}",
"UnsubscribeUrl": "EXAMPLE",
"Subject": "example subject"
}
}
]
}
```

### Roadmap
-

- [x] Pass --message as file (use the '@' like `--message "@<filename>"` format to determine if its a file or not like `curl --data-binary` of `--message fileb://<filename>` like lambda
- [ ] Add more event types (SNS, S3, Dynamo, API Gateway, IoT)
- [ ] Add more event types
- [x] Cloudwatch Log
- [ ] Cloudwatch
- [ ] SNS
- [x] SNS
- [ ] S3
- [ ] Dynamo
- [ ] API Gateway
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (

func init() {
createCmd.PersistentFlags().StringVarP(&event.Message, "message", "m", "", "Message of the event. (Required)")
createCmd.PersistentFlags().StringVarP(&eventType, "type", "t", lib.EventTypeEnumCloudwatchLog.String(), "Event Type. (default: cloudwatch-log)")
createCmd.PersistentFlags().StringVarP(&eventType, "type", "t", lib.EventTypeEnumCloudwatchLog.String(), "Event Type. (default: sns)")

// runCmd.PersistentFlags().StringVarP(&task.Cluster, "cluster", "", "", "ECS cluster")
// runCmd.PersistentFlags().StringVarP(&task.Name, "name", "n", "ephemeral-task-from-ecs-cli", "Assign a name to the task")
Expand Down
9 changes: 6 additions & 3 deletions lib/event_type_enum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package lib

// EventTypeEnum is an enumeration of applicable aws event types.
// ENUM(
// cloudwatch-log
// )
/*
ENUM(
cloudwatch-log
sns
)
*/
type EventTypeEnum int
9 changes: 7 additions & 2 deletions lib/event_type_enum_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ func GetEvent(event *Event, eventType EventTypeEnum) (resp string, err error) {
switch eventType {
case EventTypeEnumCloudwatchLog:
resp, err = event.GetCloudwatchLogEvent()
case EventTypeEnumSns:
resp, err = event.GetSNSEvent()
}

return
Expand Down
67 changes: 67 additions & 0 deletions lib/sns_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package lib

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"
"unicode/utf8"

"github.com/aws/aws-lambda-go/events"
)

func (event *Event) GetSNSEvent() (resp string, err error) {

// read from file if first char is @
if string(event.Message[0]) == "@" {
_, i := utf8.DecodeRuneInString(event.Message)
var b []byte
b, err = ioutil.ReadFile(event.Message[i:])
if err != nil {
err = fmt.Errorf("Error: unable to parse file: %s", err)
return
}
event.Message = string(b)
}

// Remove whitespaces
event.Message = strings.ReplaceAll(event.Message, "\n", "")

result := events.SNSEvent{
Records: []events.SNSEventRecord{
{
EventSource: "aws.sns",
EventVersion: "1.0",
EventSubscriptionArn: "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
SNS: events.SNSEntity{
Type: "Notification",
MessageID: "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
TopicArn: "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
Subject: "example subject",
Timestamp: time.Now().UTC(),
SignatureVersion: "1",
Signature: "EXAMPLE",
SigningCertURL: "EXAMPLE",
UnsubscribeURL: "EXAMPLE",
MessageAttributes: map[string]interface{}{
"Test": struct {
Type string `json:"Type"`
Value string `json:"Value"`
}{"String", "teststringvalue"},
},
Message: event.Message,
},
},
},
}

marshal, err := json.Marshal(result)
if err != nil {
return
}

resp = string(marshal)

return
}
36 changes: 36 additions & 0 deletions lib/sns_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lib

import (
"encoding/json"
"fmt"
"reflect"
"testing"

"github.com/aws/aws-lambda-go/events"
)

func TestGetSNSEvent(t *testing.T) {
testMessage := "{\"greet\":\"hello\",\n\"name\":\"bobby bob\"}"

event := Event{
Message: testMessage,
}

response, err := event.GetSNSEvent()
if err != nil {
t.Errorf("Error on GetSNSEvent, %s", err)
}

var snsEventResponse events.SNSEvent
err = json.Unmarshal([]byte(response), &snsEventResponse)
if err != nil {
t.Errorf("Unable to unmarshal SNS Event string, %s", err)
}

expected := "{\"greet\":\"hello\",\"name\":\"bobby bob\"}"

if !reflect.DeepEqual(snsEventResponse.Records[0].SNS.Message, expected) {
fmt.Printf("Expected %v, got %v", expected, snsEventResponse.Records[0].SNS.Message)
t.Error("SNS is not equal to expected response")
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var version = "development"
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "aws-event-mock",
Short: "aws-event-mock is used as a starter cli project",
Short: "aws-event-mock is to generate mock up aws notifications in json format",
}

func main() {
Expand Down
Loading

0 comments on commit ca03f64

Please sign in to comment.