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

New Resource: aws_ses_identity_notification #2640

Merged
merged 33 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5e737c6
adds ses identity notification resource
Jun 22, 2017
d73a07e
fix compile issues
Jun 22, 2017
45705b7
format code
Jun 22, 2017
008bbb6
#931 adjust to comments, add validators
hussfelt Dec 12, 2017
96b0c49
#931 Adding a test-base, not yet run
hussfelt Dec 12, 2017
5bed6c7
#931 import log and strings
hussfelt Dec 12, 2017
d4da1cb
#931 add missing conn
hussfelt Dec 12, 2017
a3994c0
#931 convert to string
hussfelt Dec 12, 2017
4dd6397
#931 remove .Send() and ajust if-statement to be more compact
hussfelt Dec 12, 2017
e937e01
#931 We have NotificationAttributes, and identity not domain
hussfelt Dec 12, 2017
f67e36b
#931 we should get NotificaitonAttributes, not VerificationAttributes
hussfelt Dec 12, 2017
e96621b
#931 Adding documentation
hussfelt Mar 31, 2018
e3d7667
#931 adding reference to resourceAwsSesNotification
hussfelt Mar 31, 2018
13e39ee
#931 Fixing minor nitpicks
hussfelt Mar 31, 2018
32d15fb
#931 Force new resource on these attributes as updating it could crea…
hussfelt Mar 31, 2018
16136e0
#931 Set an ID so we can reference the resource identifier in the rea…
hussfelt Mar 31, 2018
a6eaef4
#931 Read information from the Terraform resource ID
hussfelt Mar 31, 2018
c35f6ed
#931 skip the err checking with simple *string input
hussfelt Mar 31, 2018
0523289
#931 Read information from the Terraform resource ID
hussfelt Mar 31, 2018
484648e
#931 remove additional commas
hussfelt Mar 31, 2018
6e50910
#931 fix code issues
hussfelt Apr 1, 2018
ea2faa9
#931 format code
hussfelt Apr 1, 2018
86d3bcb
#931 changed variable
hussfelt Apr 1, 2018
b9691dd
#931 Import validation
hussfelt Apr 1, 2018
beaf939
#931 Adding a case where we update the notification arn
hussfelt Apr 1, 2018
9627959
#931 use a random topic name
hussfelt Apr 3, 2018
6288102
#931 missing domain in resource
hussfelt Apr 3, 2018
d2bb12b
#931 missing aws prefix
hussfelt Apr 3, 2018
49b1e85
#931 better descriptions in howto
hussfelt Apr 3, 2018
5b4f70e
#931 use terraform fmt, easier to read
hussfelt Apr 3, 2018
20f8834
#931 only set topic if it is passed
hussfelt Apr 3, 2018
d933edc
#931 Renaming resources to align with AWS API Resource action
hussfelt Apr 4, 2018
7817071
#931 Fixing issue with passing the concatenated ID, instead of identity
hussfelt Apr 4, 2018
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ func Provider() terraform.ResourceProvider {
"aws_ses_receipt_rule_set": resourceAwsSesReceiptRuleSet(),
"aws_ses_configuration_set": resourceAwsSesConfigurationSet(),
"aws_ses_event_destination": resourceAwsSesEventDestination(),
"aws_ses_identity_notification_topic": resourceAwsSesNotificationTopic(),
"aws_ses_template": resourceAwsSesTemplate(),
"aws_s3_bucket": resourceAwsS3Bucket(),
"aws_s3_bucket_policy": resourceAwsS3BucketPolicy(),
Expand Down
136 changes: 136 additions & 0 deletions aws/resource_aws_ses_identity_notification_topic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsSesNotificationTopic() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSesNotificationTopicSet,
Read: resourceAwsSesNotificationTopicRead,
Update: resourceAwsSesNotificationTopicSet,
Delete: resourceAwsSesNotificationTopicDelete,

Schema: map[string]*schema.Schema{
"topic_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},

"notification_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
ses.NotificationTypeBounce,
ses.NotificationTypeComplaint,
ses.NotificationTypeDelivery,
}, false),
},

"identity": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
},
}
}

func resourceAwsSesNotificationTopicSet(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn
notification := d.Get("notification_type").(string)
identity := d.Get("identity").(string)

setOpts := &ses.SetIdentityNotificationTopicInput{
Identity: aws.String(identity),
NotificationType: aws.String(notification),
}

if v, ok := d.GetOk("topic_arn"); ok && v.(string) != "" {
setOpts.SnsTopic = aws.String(v.(string))
}

d.SetId(fmt.Sprintf("%s|%s", identity, notification))

log.Printf("[DEBUG] Setting SES Identity Notification Topic: %#v", setOpts)

if _, err := conn.SetIdentityNotificationTopic(setOpts); err != nil {
return fmt.Errorf("Error setting SES Identity Notification Topic: %s", err)
}

return resourceAwsSesNotificationTopicRead(d, meta)
}

func resourceAwsSesNotificationTopicRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn

identity, notificationType, err := decodeSesIdentityNotificationTopicId(d.Id())
if err != nil {
return err
}

getOpts := &ses.GetIdentityNotificationAttributesInput{
Identities: []*string{aws.String(identity)},
}

log.Printf("[DEBUG] Reading SES Identity Notification Topic Attributes: %#v", getOpts)

response, err := conn.GetIdentityNotificationAttributes(getOpts)

if err != nil {
return fmt.Errorf("Error reading SES Identity Notification Topic: %s", err)
}

notificationAttributes := response.NotificationAttributes[identity]
switch notificationType {
case ses.NotificationTypeBounce:
d.Set("topic_arn", notificationAttributes.BounceTopic)
case ses.NotificationTypeComplaint:
d.Set("topic_arn", notificationAttributes.ComplaintTopic)
case ses.NotificationTypeDelivery:
d.Set("topic_arn", notificationAttributes.DeliveryTopic)
}

return nil
}

func resourceAwsSesNotificationTopicDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn

identity, notificationType, err := decodeSesIdentityNotificationTopicId(d.Id())
if err != nil {
return err
}

setOpts := &ses.SetIdentityNotificationTopicInput{
Identity: aws.String(identity),
NotificationType: aws.String(notificationType),
SnsTopic: nil,
}

log.Printf("[DEBUG] Deleting SES Identity Notification Topic: %#v", setOpts)

if _, err := conn.SetIdentityNotificationTopic(setOpts); err != nil {
return fmt.Errorf("Error deleting SES Identity Notification Topic: %s", err)
}

return resourceAwsSesNotificationTopicRead(d, meta)
}

func decodeSesIdentityNotificationTopicId(id string) (string, string, error) {
parts := strings.Split(id, "|")
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected format of ID (%q), expected IDENTITY|TYPE", id)
}
return parts[0], parts[1], nil
}
129 changes: 129 additions & 0 deletions aws/resource_aws_ses_identity_notification_topic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package aws

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAwsSESIdentityNotificationTopic_basic(t *testing.T) {
domain := fmt.Sprintf(
"%s.terraformtesting.com",
acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
topicName := fmt.Sprintf("test-topic-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsSESIdentityNotificationTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccAwsSESIdentityNotificationTopicConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsSESIdentityNotificationTopicExists("aws_ses_identity_notification_topic.test"),
),
},
resource.TestStep{
Config: fmt.Sprintf(testAccAwsSESIdentityNotificationTopicConfig_update, domain, topicName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsSESIdentityNotificationTopicExists("aws_ses_identity_notification_topic.test"),
),
},
},
})
}

func testAccCheckAwsSESIdentityNotificationTopicDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sesConn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ses_identity_notification_topic" {
continue
}

identity := rs.Primary.Attributes["identity"]
params := &ses.GetIdentityNotificationAttributesInput{
Identities: []*string{aws.String(identity)},
}

log.Printf("[DEBUG] Testing SES Identity Notification Topic Destroy: %#v", params)

response, err := conn.GetIdentityNotificationAttributes(params)
if err != nil {
return err
}

if response.NotificationAttributes[identity] != nil {
return fmt.Errorf("SES Identity Notification Topic %s still exists. Failing!", identity)
}
}

return nil
}

func testAccCheckAwsSESIdentityNotificationTopicExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("SES Identity Notification Topic not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("SES Identity Notification Topic identity not set")
}

identity := rs.Primary.Attributes["identity"]
conn := testAccProvider.Meta().(*AWSClient).sesConn

params := &ses.GetIdentityNotificationAttributesInput{
Identities: []*string{aws.String(identity)},
}

log.Printf("[DEBUG] Testing SES Identity Notification Topic Exists: %#v", params)

response, err := conn.GetIdentityNotificationAttributes(params)
if err != nil {
return err
}

if response.NotificationAttributes[identity] == nil {
return fmt.Errorf("SES Identity Notification Topic %s not found in AWS", identity)
}

return nil
}
}

const testAccAwsSESIdentityNotificationTopicConfig_basic = `
resource "aws_ses_identity_notification_topic" "test" {
identity = "${aws_ses_domain_identity.test.arn}"
notification_type = "Complaint"
}

resource "aws_ses_domain_identity" "test" {
domain = "%s"
}
`
const testAccAwsSESIdentityNotificationTopicConfig_update = `
resource "aws_ses_identity_notification_topic" "test" {
topic_arn = "${aws_sns_topic.test.arn}"
identity = "${aws_ses_domain_identity.test.arn}"
notification_type = "Complaint"
}

resource "aws_ses_domain_identity" "test" {
domain = "%s"
}

resource "aws_sns_topic" "test" {
name = "%s"
}
`
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,10 @@
<a href="/docs/providers/aws/r/ses_event_destination.html">aws_ses_event_destination</a>
</li>

<li<%= sidebar_current("docs-aws-resource-ses-identity-notification-topic") %>>
<a href="/docs/providers/aws/r/ses_identity_notification_topic.html">aws_ses_identity_notification_topic</a>
</li>

<li<%= sidebar_current("docs-aws-resource-ses-template") %>>
<a href="/docs/providers/aws/r/ses_template.html">aws_ses_template</a>
</li>
Expand Down
29 changes: 29 additions & 0 deletions website/docs/r/ses_identity_notification_topic.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "aws"
page_title: "AWS: aws_ses_identity_notification_topic"
sidebar_current: "docs-aws-resource-ses-identity-notification-topic"
description: |-
Setting AWS SES Identity Notification Topic
---

# ses_identity_notification_topic

Resource for managing SES Identity Notification Topics

## Example Usage

```hcl
resource "aws_ses_identity_notification_topic" "test" {
topic_arn = "${aws_sns_topic.example.arn}"
notification_type = "Bounce"
identity = "${aws_ses_domain_identity.example.domain}"
}
```

## Argument Reference

The following arguments are supported:

* `topic_arn` - (Optional) The Amazon Resource Name (ARN) of the Amazon SNS topic. Can be set to "" (an empty string) to disable publishing.
* `notification_type` - (Required) The type of notifications that will be published to the specified Amazon SNS topic. Valid Values: *Bounce*, *Complaint* or *Delivery*.
* `identity` - (Required) The identity for which the Amazon SNS topic will be set. You can specify an identity by using its name or by using its Amazon Resource Name (ARN).