Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(awssqs): Support sending messages to SQS FIFO queues #261

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/services/awssqs.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# AWS SQS
# AWS SQS

## Parameters

This notification service is capable of sending simple messages to AWS SQS queue.
This notification service is capable of sending simple messages to AWS SQS queue.

* `queue` - name of the queue you are intending to send messages to. Can be overridden with target destination annotation.
* `region` - region of the sqs queue can be provided via env variable AWS_DEFAULT_REGION
Expand Down Expand Up @@ -104,3 +104,16 @@ data:
- oncePer: obj.metadata.annotations["generation"]
```
## FIFO SQS Queues
FIFO queues require a [MessageGroupId](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html#SQS-SendMessage-request-MessageGroupId) to be sent along with every message, every message with a matching MessageGroupId will be processed one by one in order.
To send to a FIFO SQS Queue you must include a `messageGroupId` in the template such as in the example below:

```yaml
template.deployment-ready: |
message: |
Deployment {{.obj.metadata.name}} is ready!
messageGroupId: {{.obj.metadata.name}}-deployment
```
14 changes: 14 additions & 0 deletions pkg/services/awssqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

type AwsSqsNotification struct {
MessageAttributes map[string]string `json:"messageAttributes"`
MessageGroupId string `json:"messageGroupId,omitempty"`
}

type AwsSqsOptions struct {
Expand Down Expand Up @@ -130,6 +131,11 @@
}

func (n *AwsSqsNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
groupId, err := texttemplate.New(name).Funcs(f).Parse(n.MessageGroupId)
if err != nil {
return nil, err
}

Check warning on line 137 in pkg/services/awssqs.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/awssqs.go#L136-L137

Added lines #L136 - L137 were not covered by tests

return func(notification *Notification, vars map[string]interface{}) error {
if notification.AwsSqs == nil {
notification.AwsSqs = &AwsSqsNotification{}
Expand All @@ -142,6 +148,14 @@
}
}

var groupIdBuff bytes.Buffer
if err := groupId.Execute(&groupIdBuff, vars); err != nil {
return err
}

Check warning on line 154 in pkg/services/awssqs.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/awssqs.go#L153-L154

Added lines #L153 - L154 were not covered by tests
if val := groupIdBuff.String(); val != "" {
notification.AwsSqs.MessageGroupId = val
}

return nil
}, nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/services/awssqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestGetTemplater_AwsSqs(t *testing.T) {
MessageAttributes: map[string]string{
"attributeKey": "{{.messageAttributeValue}}",
},
MessageGroupId: "{{.messageGroupId}}",
},
}

Expand All @@ -33,6 +34,7 @@ func TestGetTemplater_AwsSqs(t *testing.T) {
err = templater(&notification, map[string]interface{}{
"message": "abcdef",
"messageAttributeValue": "123456",
"messageGroupId": "a1b2c3",
})

if !assert.NoError(t, err) {
Expand All @@ -42,6 +44,7 @@ func TestGetTemplater_AwsSqs(t *testing.T) {
assert.Equal(t, map[string]string{
"attributeKey": "123456",
}, notification.AwsSqs.MessageAttributes)
assert.Equal(t, "a1b2c3", notification.AwsSqs.MessageGroupId)
}

func TestSend_AwsSqs(t *testing.T) {
Expand Down
Loading