github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_db_event_subscription_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/rds"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSDBEventSubscription_basicUpdate(t *testing.T) {
    15  	var v rds.EventSubscription
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSDBEventSubscriptionDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSDBEventSubscriptionConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_db_event_subscription.bar", "enabled", "true"),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_db_event_subscription.bar", "source_type", "db-instance"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_db_event_subscription.bar", "name", "tf-acc-test-rds-event-subs"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_db_event_subscription.bar", "tags.Name", "name"),
    34  				),
    35  			},
    36  			resource.TestStep{
    37  				Config: testAccAWSDBEventSubscriptionConfigUpdate,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_db_event_subscription.bar", "enabled", "false"),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
    44  					resource.TestCheckResourceAttr(
    45  						"aws_db_event_subscription.bar", "tags.Name", "new-name"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func testAccCheckAWSDBEventSubscriptionExists(n string, v *rds.EventSubscription) resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		rs, ok := s.RootModule().Resources[n]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No RDS Event Subscription is set")
    61  		}
    62  
    63  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
    64  
    65  		opts := rds.DescribeEventSubscriptionsInput{
    66  			SubscriptionName: aws.String(rs.Primary.ID),
    67  		}
    68  
    69  		resp, err := conn.DescribeEventSubscriptions(&opts)
    70  
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		if len(resp.EventSubscriptionsList) != 1 ||
    76  			*resp.EventSubscriptionsList[0].CustSubscriptionId != rs.Primary.ID {
    77  			return fmt.Errorf("RDS Event Subscription not found")
    78  		}
    79  
    80  		*v = *resp.EventSubscriptionsList[0]
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccCheckAWSDBEventSubscriptionDestroy(s *terraform.State) error {
    86  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "aws_db_event_subscription" {
    90  			continue
    91  		}
    92  
    93  		var err error
    94  		resp, err := conn.DescribeEventSubscriptions(
    95  			&rds.DescribeEventSubscriptionsInput{
    96  				SubscriptionName: aws.String(rs.Primary.ID),
    97  			})
    98  
    99  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "SubscriptionNotFound" {
   100  			continue
   101  		}
   102  
   103  		if err == nil {
   104  			if len(resp.EventSubscriptionsList) != 0 &&
   105  				*resp.EventSubscriptionsList[0].CustSubscriptionId == rs.Primary.ID {
   106  				return fmt.Errorf("Event Subscription still exists")
   107  			}
   108  		}
   109  
   110  		// Verify the error
   111  		newerr, ok := err.(awserr.Error)
   112  		if !ok {
   113  			return err
   114  		}
   115  		if newerr.Code() != "SubscriptionNotFound" {
   116  			return err
   117  		}
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  var testAccAWSDBEventSubscriptionConfig = `
   124  resource "aws_sns_topic" "aws_sns_topic" {
   125    name = "tf-acc-test-rds-event-subs-sns-topic"
   126  }
   127  
   128  resource "aws_db_event_subscription" "bar" {
   129    name = "tf-acc-test-rds-event-subs"
   130    sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   131    source_type = "db-instance"
   132    event_categories = [
   133      "availability",
   134      "backup",
   135      "creation",
   136      "deletion",
   137      "maintenance"
   138    ]
   139    tags {
   140      Name = "name"
   141    }
   142  }
   143  `
   144  
   145  var testAccAWSDBEventSubscriptionConfigUpdate = `
   146  resource "aws_sns_topic" "aws_sns_topic" {
   147    name = "tf-acc-test-rds-event-subs-sns-topic"
   148  }
   149  
   150  resource "aws_db_event_subscription" "bar" {
   151    name = "tf-acc-test-rds-event-subs"
   152    sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   153    enabled = false
   154    source_type = "db-parameter-group"
   155    event_categories = [
   156      "configuration change"
   157    ]
   158    tags {
   159      Name = "new-name"
   160    }
   161  }
   162  `