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