github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSDBEventSubscription_basicUpdate(t *testing.T) {
    16  	var v rds.EventSubscription
    17  	rInt := acctest.RandInt()
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSDBEventSubscriptionDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSDBEventSubscriptionConfig(rInt),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_db_event_subscription.bar", "enabled", "true"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_db_event_subscription.bar", "source_type", "db-instance"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_db_event_subscription.bar", "name", fmt.Sprintf("tf-acc-test-rds-event-subs-%d", rInt)),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_db_event_subscription.bar", "tags.Name", "name"),
    36  				),
    37  			},
    38  			{
    39  				Config: testAccAWSDBEventSubscriptionConfigUpdate(rInt),
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_db_event_subscription.bar", "enabled", "false"),
    44  					resource.TestCheckResourceAttr(
    45  						"aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_db_event_subscription.bar", "tags.Name", "new-name"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func TestAccAWSDBEventSubscription_withSourceIds(t *testing.T) {
    55  	var v rds.EventSubscription
    56  	rInt := acctest.RandInt()
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckAWSDBEventSubscriptionDestroy,
    62  		Steps: []resource.TestStep{
    63  			{
    64  				Config: testAccAWSDBEventSubscriptionConfigWithSourceIds(rInt),
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_db_event_subscription.bar", "enabled", "true"),
    69  					resource.TestCheckResourceAttr(
    70  						"aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
    71  					resource.TestCheckResourceAttr(
    72  						"aws_db_event_subscription.bar", "name", fmt.Sprintf("tf-acc-test-rds-event-subs-with-ids-%d", rInt)),
    73  					resource.TestCheckResourceAttr(
    74  						"aws_db_event_subscription.bar", "source_ids.#", "1"),
    75  				),
    76  			},
    77  			{
    78  				Config: testAccAWSDBEventSubscriptionConfigUpdateSourceIds(rInt),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckAWSDBEventSubscriptionExists("aws_db_event_subscription.bar", &v),
    81  					resource.TestCheckResourceAttr(
    82  						"aws_db_event_subscription.bar", "enabled", "true"),
    83  					resource.TestCheckResourceAttr(
    84  						"aws_db_event_subscription.bar", "source_type", "db-parameter-group"),
    85  					resource.TestCheckResourceAttr(
    86  						"aws_db_event_subscription.bar", "name", fmt.Sprintf("tf-acc-test-rds-event-subs-with-ids-%d", rInt)),
    87  					resource.TestCheckResourceAttr(
    88  						"aws_db_event_subscription.bar", "source_ids.#", "2"),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func testAccCheckAWSDBEventSubscriptionExists(n string, v *rds.EventSubscription) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  		rs, ok := s.RootModule().Resources[n]
    98  		if !ok {
    99  			return fmt.Errorf("Not found: %s", n)
   100  		}
   101  
   102  		if rs.Primary.ID == "" {
   103  			return fmt.Errorf("No RDS Event Subscription is set")
   104  		}
   105  
   106  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   107  
   108  		opts := rds.DescribeEventSubscriptionsInput{
   109  			SubscriptionName: aws.String(rs.Primary.ID),
   110  		}
   111  
   112  		resp, err := conn.DescribeEventSubscriptions(&opts)
   113  
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		if len(resp.EventSubscriptionsList) != 1 ||
   119  			*resp.EventSubscriptionsList[0].CustSubscriptionId != rs.Primary.ID {
   120  			return fmt.Errorf("RDS Event Subscription not found")
   121  		}
   122  
   123  		*v = *resp.EventSubscriptionsList[0]
   124  		return nil
   125  	}
   126  }
   127  
   128  func testAccCheckAWSDBEventSubscriptionDestroy(s *terraform.State) error {
   129  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   130  
   131  	for _, rs := range s.RootModule().Resources {
   132  		if rs.Type != "aws_db_event_subscription" {
   133  			continue
   134  		}
   135  
   136  		var err error
   137  		resp, err := conn.DescribeEventSubscriptions(
   138  			&rds.DescribeEventSubscriptionsInput{
   139  				SubscriptionName: aws.String(rs.Primary.ID),
   140  			})
   141  
   142  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "SubscriptionNotFound" {
   143  			continue
   144  		}
   145  
   146  		if err == nil {
   147  			if len(resp.EventSubscriptionsList) != 0 &&
   148  				*resp.EventSubscriptionsList[0].CustSubscriptionId == rs.Primary.ID {
   149  				return fmt.Errorf("Event Subscription still exists")
   150  			}
   151  		}
   152  
   153  		// Verify the error
   154  		newerr, ok := err.(awserr.Error)
   155  		if !ok {
   156  			return err
   157  		}
   158  		if newerr.Code() != "SubscriptionNotFound" {
   159  			return err
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  func testAccAWSDBEventSubscriptionConfig(rInt int) string {
   167  	return fmt.Sprintf(`
   168  resource "aws_sns_topic" "aws_sns_topic" {
   169    name = "tf-acc-test-rds-event-subs-sns-topic-%d"
   170  }
   171  
   172  resource "aws_db_event_subscription" "bar" {
   173    name = "tf-acc-test-rds-event-subs-%d"
   174    sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   175    source_type = "db-instance"
   176    event_categories = [
   177      "availability",
   178      "backup",
   179      "creation",
   180      "deletion",
   181      "maintenance"
   182    ]
   183    tags {
   184      Name = "name"
   185    }
   186  }`, rInt, rInt)
   187  }
   188  
   189  func testAccAWSDBEventSubscriptionConfigUpdate(rInt int) string {
   190  	return fmt.Sprintf(`
   191  resource "aws_sns_topic" "aws_sns_topic" {
   192    name = "tf-acc-test-rds-event-subs-sns-topic-%d"
   193  }
   194  
   195  resource "aws_db_event_subscription" "bar" {
   196    name = "tf-acc-test-rds-event-subs-%d"
   197    sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   198    enabled = false
   199    source_type = "db-parameter-group"
   200    event_categories = [
   201      "configuration change"
   202    ]
   203    tags {
   204      Name = "new-name"
   205    }
   206  }`, rInt, rInt)
   207  }
   208  
   209  func testAccAWSDBEventSubscriptionConfigWithSourceIds(rInt int) string {
   210  	return fmt.Sprintf(`
   211  resource "aws_sns_topic" "aws_sns_topic" {
   212    name = "tf-acc-test-rds-event-subs-sns-topic-%d"
   213  }
   214  
   215  resource "aws_db_parameter_group" "bar" {
   216    name = "db-parameter-group-event-%d"
   217    family = "mysql5.6"
   218    description = "Test parameter group for terraform"
   219  }
   220  
   221  resource "aws_db_event_subscription" "bar" {
   222    name = "tf-acc-test-rds-event-subs-with-ids-%d"
   223    sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   224    source_type = "db-parameter-group"
   225    source_ids = ["${aws_db_parameter_group.bar.id}"]
   226    event_categories = [
   227      "configuration change"
   228    ]
   229    tags {
   230      Name = "name"
   231    }
   232  }`, rInt, rInt, rInt)
   233  }
   234  
   235  func testAccAWSDBEventSubscriptionConfigUpdateSourceIds(rInt int) string {
   236  	return fmt.Sprintf(`
   237  	resource "aws_sns_topic" "aws_sns_topic" {
   238  		name = "tf-acc-test-rds-event-subs-sns-topic-%d"
   239  	}
   240  
   241  	resource "aws_db_parameter_group" "bar" {
   242  		name = "db-parameter-group-event-%d"
   243  		family = "mysql5.6"
   244  		description = "Test parameter group for terraform"
   245  	}
   246  
   247  	resource "aws_db_parameter_group" "foo" {
   248  		name = "db-parameter-group-event-2-%d"
   249  		family = "mysql5.6"
   250  		description = "Test parameter group for terraform"
   251  	}
   252  
   253  	resource "aws_db_event_subscription" "bar" {
   254  		name = "tf-acc-test-rds-event-subs-with-ids-%d"
   255  		sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   256  		source_type = "db-parameter-group"
   257  		source_ids = ["${aws_db_parameter_group.bar.id}","${aws_db_parameter_group.foo.id}"]
   258  		event_categories = [
   259  			"configuration change"
   260  		]
   261  		tags {
   262  			Name = "name"
   263  		}
   264  	}`, rInt, rInt, rInt, rInt)
   265  }