Skip to content

Commit

Permalink
Alerting Contact Points: Add SNS notifier (#1295)
Browse files Browse the repository at this point in the history
* Alerting Contact Points: Add SNS notifier
Closes #1075
Tested on Amazon Managed Grafana

* go generate
  • Loading branch information
julienduchesne authored Jan 24, 2024
1 parent b58bc6c commit 44f5cda
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/resources/contact_point.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ resource "grafana_contact_point" "my_contact_point" {
- `pushover` (Block Set) A contact point that sends notifications to Pushover. (see [below for nested schema](#nestedblock--pushover))
- `sensugo` (Block Set) A contact point that sends notifications to SensuGo. (see [below for nested schema](#nestedblock--sensugo))
- `slack` (Block Set) A contact point that sends notifications to Slack. (see [below for nested schema](#nestedblock--slack))
- `sns` (Block Set) A contact point that sends notifications to Amazon SNS. Requires Amazon Managed Grafana. (see [below for nested schema](#nestedblock--sns))
- `teams` (Block Set) A contact point that sends notifications to Microsoft Teams. (see [below for nested schema](#nestedblock--teams))
- `telegram` (Block Set) A contact point that sends notifications to Telegram. (see [below for nested schema](#nestedblock--telegram))
- `threema` (Block Set) A contact point that sends notifications to Threema. (see [below for nested schema](#nestedblock--threema))
Expand Down Expand Up @@ -375,6 +376,31 @@ Read-Only:
- `uid` (String) The UID of the contact point.


<a id="nestedblock--sns"></a>
### Nested Schema for `sns`

Required:

- `topic` (String) The Amazon SNS topic to send notifications to.

Optional:

- `access_key` (String, Sensitive) AWS access key ID used to authenticate with Amazon SNS.
- `assume_role_arn` (String) The Amazon Resource Name (ARN) of the role to assume to send notifications to Amazon SNS.
- `auth_provider` (String) The authentication provider to use. Valid values are `default`, `arn` and `keys`. Default is `default`. Defaults to `default`.
- `body` (String)
- `disable_resolve_message` (Boolean) Whether to disable sending resolve messages. Defaults to `false`.
- `external_id` (String) The external ID to use when assuming the role.
- `message_format` (String) The format of the message to send. Valid values are `text`, `body` and `json`. Default is `text`. Defaults to `text`.
- `secret_key` (String, Sensitive) AWS secret access key used to authenticate with Amazon SNS.
- `settings` (Map of String, Sensitive) Additional custom properties to attach to the notifier. Defaults to `map[]`.
- `subject` (String)

Read-Only:

- `uid` (String) The UID of the contact point.


<a id="nestedblock--teams"></a>
### Nested Schema for `teams`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var notifiers = []notifier{
pushoverNotifier{},
sensugoNotifier{},
slackNotifier{},
snsNotifier{},
teamsNotifier{},
telegramNotifier{},
threemaNotifier{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,118 @@ func (s slackNotifier) unpack(raw interface{}, name string) *models.EmbeddedCont
}
}

type snsNotifier struct{}

var _ notifier = (*snsNotifier)(nil)

func (s snsNotifier) meta() notifierMeta {
return notifierMeta{
field: "sns",
typeStr: "sns",
desc: "A contact point that sends notifications to Amazon SNS. Requires Amazon Managed Grafana.",
secureFields: []string{"access_key", "secret_key"},
}
}

func (s snsNotifier) schema() *schema.Resource {
r := commonNotifierResource()
r.Schema["topic"] = &schema.Schema{
Type: schema.TypeString,
Description: "The Amazon SNS topic to send notifications to.",
Required: true,
}
r.Schema["auth_provider"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The authentication provider to use. Valid values are `default`, `arn` and `keys`. Default is `default`.",
Default: "default",
ValidateFunc: validation.StringInSlice([]string{"default", "arn", "keys"}, false),
}
r.Schema["access_key"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "AWS access key ID used to authenticate with Amazon SNS.",
}
r.Schema["secret_key"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "AWS secret access key used to authenticate with Amazon SNS.",
}
r.Schema["assume_role_arn"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The Amazon Resource Name (ARN) of the role to assume to send notifications to Amazon SNS.",
}
r.Schema["message_format"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The format of the message to send. Valid values are `text`, `body` and `json`. Default is `text`.",
ValidateFunc: validation.StringInSlice([]string{"text", "body", "json"}, false),
Default: "text",
}
r.Schema["body"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
}
r.Schema["subject"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
}
r.Schema["external_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The external ID to use when assuming the role.",
}

return r
}

func (s snsNotifier) pack(p *models.EmbeddedContactPoint, data *schema.ResourceData) (interface{}, error) {
notifier := packCommonNotifierFields(p)
settings := p.Settings.(map[string]interface{})

packNotifierStringField(&settings, &notifier, "topic", "topic")
packNotifierStringField(&settings, &notifier, "authProvider", "auth_provider")
packNotifierStringField(&settings, &notifier, "accessKey", "access_key")
packNotifierStringField(&settings, &notifier, "secretKey", "secret_key")
packNotifierStringField(&settings, &notifier, "assumeRoleARN", "assume_role_arn")
packNotifierStringField(&settings, &notifier, "messageFormat", "message_format")
packNotifierStringField(&settings, &notifier, "body", "body")
packNotifierStringField(&settings, &notifier, "subject", "subject")
packNotifierStringField(&settings, &notifier, "externalId", "external_id")

packSecureFields(notifier, getNotifierConfigFromStateWithUID(data, s, p.UID), s.meta().secureFields)

notifier["settings"] = packSettings(p)

return notifier, nil
}

func (s snsNotifier) unpack(raw interface{}, name string) *models.EmbeddedContactPoint {
json := raw.(map[string]interface{})
uid, disableResolve, settings := unpackCommonNotifierFields(json)

unpackNotifierStringField(&json, &settings, "topic", "topic")
unpackNotifierStringField(&json, &settings, "auth_provider", "authProvider")
unpackNotifierStringField(&json, &settings, "access_key", "accessKey")
unpackNotifierStringField(&json, &settings, "secret_key", "secretKey")
unpackNotifierStringField(&json, &settings, "assume_role_arn", "assumeRoleARN")
unpackNotifierStringField(&json, &settings, "message_format", "messageFormat")
unpackNotifierStringField(&json, &settings, "body", "body")
unpackNotifierStringField(&json, &settings, "subject", "subject")
unpackNotifierStringField(&json, &settings, "external_id", "externalId")

return &models.EmbeddedContactPoint{
UID: uid,
Name: name,
Type: common.Ref(s.meta().typeStr),
DisableResolveMessage: disableResolve,
Settings: settings,
}
}

type teamsNotifier struct{}

var _ notifier = (*teamsNotifier)(nil)
Expand Down

0 comments on commit 44f5cda

Please sign in to comment.