Skip to content

Commit

Permalink
Merge pull request #4151 from terraform-providers/b-aws_db_event_subs…
Browse files Browse the repository at this point in the history
…cription-tags

resource/aws_db_event_subscription: Fix tag ARN handling
  • Loading branch information
bflad authored Apr 11, 2018
2 parents fdc94d0 + 5909bf6 commit 1d41a8e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
108 changes: 50 additions & 58 deletions aws/resource_aws_db_event_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/rds"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -23,7 +20,16 @@ func resourceAwsDbEventSubscription() *schema.Resource {
Importer: &schema.ResourceImporter{
State: resourceAwsDbEventSubscriptionImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(40 * time.Minute),
Delete: schema.DefaultTimeout(40 * time.Minute),
Update: schema.DefaultTimeout(40 * time.Minute),
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -67,7 +73,7 @@ func resourceAwsDbEventSubscription() *schema.Resource {
}

func resourceAwsDbEventSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
conn := meta.(*AWSClient).rdsconn
name := d.Get("name").(string)
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))

Expand Down Expand Up @@ -95,19 +101,25 @@ func resourceAwsDbEventSubscriptionCreate(d *schema.ResourceData, meta interface

log.Println("[DEBUG] Create RDS Event Subscription:", request)

_, err := rdsconn.CreateEventSubscription(request)
if err != nil {
output, err := conn.CreateEventSubscription(request)
if err != nil || output.EventSubscription == nil {
return fmt.Errorf("Error creating RDS Event Subscription %s: %s", name, err)
}

d.SetId(aws.StringValue(output.EventSubscription.CustSubscriptionId))

if err := setTagsRDS(conn, d, aws.StringValue(output.EventSubscription.EventSubscriptionArn)); err != nil {
return fmt.Errorf("Error creating RDS Event Subscription (%s) tags: %s", d.Id(), err)
}

log.Println(
"[INFO] Waiting for RDS Event Subscription to be ready")

stateConf := &resource.StateChangeConf{
Pending: []string{"creating"},
Target: []string{"active"},
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d, meta.(*AWSClient).rdsconn),
Timeout: 40 * time.Minute,
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second, // Wait 30 secs before starting
}
Expand All @@ -122,16 +134,19 @@ func resourceAwsDbEventSubscriptionCreate(d *schema.ResourceData, meta interface
}

func resourceAwsDbEventSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
sub, err := resourceAwsDbEventSubscriptionRetrieve(d.Get("name").(string), meta.(*AWSClient).rdsconn)
conn := meta.(*AWSClient).rdsconn

sub, err := resourceAwsDbEventSubscriptionRetrieve(d.Get("name").(string), conn)
if err != nil {
return fmt.Errorf("Error retrieving RDS Event Subscription %s: %s", d.Id(), err)
}
if sub == nil {
log.Printf("[WARN] RDS Event Subscription (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

d.SetId(*sub.CustSubscriptionId)
d.Set("arn", sub.EventSubscriptionArn)
if err := d.Set("name", sub.CustSubscriptionId); err != nil {
return err
}
Expand All @@ -155,43 +170,34 @@ func resourceAwsDbEventSubscriptionRead(d *schema.ResourceData, meta interface{}
}

// list tags for resource
// set tags
conn := meta.(*AWSClient).rdsconn

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "rds",
Region: meta.(*AWSClient).region,
AccountID: d.Get("customer_aws_id").(string),
Resource: fmt.Sprintf("subgrp:%s", d.Id()),
}.String()
resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: aws.String(arn),
ResourceName: sub.EventSubscriptionArn,
})

if err != nil {
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", aws.StringValue(sub.EventSubscriptionArn))
}

var dt []*rds.Tag
if len(resp.TagList) > 0 {
dt = resp.TagList
}
d.Set("tags", tagsToMapRDS(dt))
if err := d.Set("tags", tagsToMapRDS(dt)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

func resourceAwsDbEventSubscriptionRetrieve(
name string, rdsconn *rds.RDS) (*rds.EventSubscription, error) {
func resourceAwsDbEventSubscriptionRetrieve(name string, conn *rds.RDS) (*rds.EventSubscription, error) {

request := &rds.DescribeEventSubscriptionsInput{
SubscriptionName: aws.String(name),
}

describeResp, err := rdsconn.DescribeEventSubscriptions(request)
describeResp, err := conn.DescribeEventSubscriptions(request)
if err != nil {
if rdserr, ok := err.(awserr.Error); ok && rdserr.Code() == "SubscriptionNotFound" {
if isAWSErr(err, rds.ErrCodeSubscriptionNotFoundFault, "") {
log.Printf("[WARN] No RDS Event Subscription by name (%s) found", name)
return nil, nil
}
Expand All @@ -206,7 +212,7 @@ func resourceAwsDbEventSubscriptionRetrieve(
}

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

d.Partial(true)
requestUpdate := false
Expand Down Expand Up @@ -243,7 +249,7 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
log.Printf("[DEBUG] Send RDS Event Subscription modification request: %#v", requestUpdate)
if requestUpdate {
log.Printf("[DEBUG] RDS Event Subscription modification request: %#v", req)
_, err := rdsconn.ModifyEventSubscription(req)
_, err := conn.ModifyEventSubscription(req)
if err != nil {
return fmt.Errorf("Modifying RDS Event Subscription %s failed: %s", d.Id(), err)
}
Expand All @@ -254,8 +260,8 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
stateConf := &resource.StateChangeConf{
Pending: []string{"modifying"},
Target: []string{"active"},
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d, meta.(*AWSClient).rdsconn),
Timeout: 40 * time.Minute,
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutUpdate),
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second, // Wait 30 secs before starting
}
Expand All @@ -271,14 +277,7 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
d.SetPartial("source_type")
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "rds",
Region: meta.(*AWSClient).region,
AccountID: d.Get("customer_aws_id").(string),
Resource: fmt.Sprintf("subgrp:%s", d.Id()),
}.String()
if err := setTagsRDS(rdsconn, d, arn); err != nil {
if err := setTagsRDS(conn, d, d.Get("arn").(string)); err != nil {
return err
} else {
d.SetPartial("tags")
Expand All @@ -301,7 +300,7 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
if len(remove) > 0 {
for _, removing := range remove {
log.Printf("[INFO] Removing %s as a Source Identifier from %q", *removing, d.Id())
_, err := rdsconn.RemoveSourceIdentifierFromSubscription(&rds.RemoveSourceIdentifierFromSubscriptionInput{
_, err := conn.RemoveSourceIdentifierFromSubscription(&rds.RemoveSourceIdentifierFromSubscriptionInput{
SourceIdentifier: removing,
SubscriptionName: aws.String(d.Id()),
})
Expand All @@ -314,7 +313,7 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
if len(add) > 0 {
for _, adding := range add {
log.Printf("[INFO] Adding %s as a Source Identifier to %q", *adding, d.Id())
_, err := rdsconn.AddSourceIdentifierToSubscription(&rds.AddSourceIdentifierToSubscriptionInput{
_, err := conn.AddSourceIdentifierToSubscription(&rds.AddSourceIdentifierToSubscriptionInput{
SourceIdentifier: adding,
SubscriptionName: aws.String(d.Id()),
})
Expand All @@ -332,28 +331,23 @@ func resourceAwsDbEventSubscriptionUpdate(d *schema.ResourceData, meta interface
}

func resourceAwsDbEventSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
conn := meta.(*AWSClient).rdsconn
deleteOpts := rds.DeleteEventSubscriptionInput{
SubscriptionName: aws.String(d.Id()),
}

if _, err := rdsconn.DeleteEventSubscription(&deleteOpts); err != nil {
rdserr, ok := err.(awserr.Error)
if !ok {
return fmt.Errorf("Error deleting RDS Event Subscription %s: %s", d.Id(), err)
}

if rdserr.Code() != "DBEventSubscriptionNotFoundFault" {
log.Printf("[WARN] RDS Event Subscription %s missing during delete", d.Id())
return fmt.Errorf("Error deleting RDS Event Subscription %s: %s", d.Id(), err)
if _, err := conn.DeleteEventSubscription(&deleteOpts); err != nil {
if isAWSErr(err, rds.ErrCodeSubscriptionNotFoundFault, "") {
return nil
}
return fmt.Errorf("Error deleting RDS Event Subscription %s: %s", d.Id(), err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"deleting"},
Target: []string{},
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d, meta.(*AWSClient).rdsconn),
Timeout: 40 * time.Minute,
Refresh: resourceAwsDbEventSubscriptionRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second, // Wait 30 secs before starting
}
Expand All @@ -364,12 +358,10 @@ func resourceAwsDbEventSubscriptionDelete(d *schema.ResourceData, meta interface
return err
}

func resourceAwsDbEventSubscriptionRefreshFunc(
d *schema.ResourceData,
rdsconn *rds.RDS) resource.StateRefreshFunc {
func resourceAwsDbEventSubscriptionRefreshFunc(name string, conn *rds.RDS) resource.StateRefreshFunc {

return func() (interface{}, string, error) {
sub, err := resourceAwsDbEventSubscriptionRetrieve(d.Get("name").(string), rdsconn)
sub, err := resourceAwsDbEventSubscriptionRetrieve(name, conn)

if err != nil {
log.Printf("Error on retrieving DB Event Subscription when waiting: %s", err)
Expand All @@ -381,7 +373,7 @@ func resourceAwsDbEventSubscriptionRefreshFunc(
}

if sub.Status != nil {
log.Printf("[DEBUG] DB Event Subscription status for %s: %s", d.Id(), *sub.Status)
log.Printf("[DEBUG] DB Event Subscription status for %s: %s", name, *sub.Status)
}

return sub, *sub.Status, nil
Expand Down
26 changes: 12 additions & 14 deletions aws/resource_aws_db_event_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -15,6 +16,7 @@ import (
func TestAccAWSDBEventSubscription_basicUpdate(t *testing.T) {
var v rds.EventSubscription
rInt := acctest.RandInt()
rName := fmt.Sprintf("tf-acc-test-rds-event-subs-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -25,26 +27,22 @@ func TestAccAWSDBEventSubscription_basicUpdate(t *testing.T) {
Config: testAccAWSDBEventSubscriptionConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "enabled", "true"),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "source_type", "db-instance"),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "name", fmt.Sprintf("tf-acc-test-rds-event-subs-%d", rInt)),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "tags.Name", "name"),
resource.TestMatchResourceAttr("aws_db_event_subscription.bar", "arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:rds:[^:]+:[^:]+:es:%s$", rName))),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "enabled", "true"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "source_type", "db-instance"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "name", rName),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "tags.Name", "name"),
),
},
{
Config: testAccAWSDBEventSubscriptionConfigUpdate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "enabled", "false"),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
resource.TestCheckResourceAttr(
"aws_db_event_subscription.bar", "tags.Name", "new-name"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "enabled", "false"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_db_event_subscription.bar", "tags.Name", "new-name"),
),
},
},
Expand Down
16 changes: 16 additions & 0 deletions website/docs/r/db_event_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ The following arguments are supported:
* `enabled` - (Optional) A boolean flag to enable/disable the subscription. Defaults to true.
* `tags` - (Optional) A mapping of tags to assign to the resource.

## Attributes

The following additional atttributes are provided:

* `id` - The name of the RDS event notification subscription
* `arn` - The Amazon Resource Name of the RDS event notification subscription
* `customer_aws_id` - The AWS customer account associated with the RDS event notification subscription

## Timeouts

`aws_db_event_subscription` provides the following [Timeouts](/docs/configuration/resources.html#timeouts)
configuration options:

- `create` - (Default `40m`) How long to wait for a RDS event notification subscription to be ready.
- `delete` - (Default `40m`) How long to wait for a RDS event notification subscription to be deleted.
- `update` - (Default `40m`) How long to wait for a RDS event notification subscription to be updated.

## Import

Expand Down

0 comments on commit 1d41a8e

Please sign in to comment.