github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_dms_replication_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAwsDmsReplicationInstanceBasic(t *testing.T) { 16 resourceName := "aws_dms_replication_instance.dms_replication_instance" 17 randId := acctest.RandString(8) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: dmsReplicationInstanceDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: dmsReplicationInstanceConfig(randId), 26 Check: resource.ComposeTestCheckFunc( 27 checkDmsReplicationInstanceExists(resourceName), 28 resource.TestCheckResourceAttrSet(resourceName, "replication_instance_arn"), 29 ), 30 }, 31 { 32 ResourceName: resourceName, 33 ImportState: true, 34 ImportStateVerify: true, 35 }, 36 { 37 Config: dmsReplicationInstanceConfigUpdate(randId), 38 Check: resource.ComposeTestCheckFunc( 39 checkDmsReplicationInstanceExists(resourceName), 40 resource.TestCheckResourceAttrSet(resourceName, "apply_immediately"), 41 resource.TestCheckResourceAttr(resourceName, "auto_minor_version_upgrade", "false"), 42 resource.TestCheckResourceAttr(resourceName, "preferred_maintenance_window", "mon:00:30-mon:02:30"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func checkDmsReplicationInstanceExists(n string) resource.TestCheckFunc { 50 providers := []*schema.Provider{testAccProvider} 51 return checkDmsReplicationInstanceExistsWithProviders(n, &providers) 52 } 53 54 func checkDmsReplicationInstanceExistsWithProviders(n string, providers *[]*schema.Provider) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 rs, ok := s.RootModule().Resources[n] 57 if !ok { 58 return fmt.Errorf("Not found: %s", n) 59 } 60 61 if rs.Primary.ID == "" { 62 return fmt.Errorf("No ID is set") 63 } 64 for _, provider := range *providers { 65 // Ignore if Meta is empty, this can happen for validation providers 66 if provider.Meta() == nil { 67 continue 68 } 69 70 conn := provider.Meta().(*AWSClient).dmsconn 71 _, err := conn.DescribeReplicationInstances(&dms.DescribeReplicationInstancesInput{ 72 Filters: []*dms.Filter{ 73 { 74 Name: aws.String("replication-instance-id"), 75 Values: []*string{aws.String(rs.Primary.ID)}, 76 }, 77 }, 78 }) 79 80 if err != nil { 81 return fmt.Errorf("DMS replication instance error: %v", err) 82 } 83 return nil 84 } 85 86 return fmt.Errorf("DMS replication instance not found") 87 } 88 } 89 90 func dmsReplicationInstanceDestroy(s *terraform.State) error { 91 for _, rs := range s.RootModule().Resources { 92 if rs.Type != "aws_dms_replication_instance" { 93 continue 94 } 95 96 err := checkDmsReplicationInstanceExists(rs.Primary.ID) 97 if err == nil { 98 return fmt.Errorf("Found replication instance that was not destroyed: %s", rs.Primary.ID) 99 } 100 } 101 102 return nil 103 } 104 105 func dmsReplicationInstanceConfig(randId string) string { 106 return fmt.Sprintf(` 107 resource "aws_iam_role" "dms_iam_role" { 108 name = "dms-vpc-role" 109 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"dms.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}" 110 } 111 112 resource "aws_iam_role_policy_attachment" "dms_iam_role_policy" { 113 role = "${aws_iam_role.dms_iam_role.name}" 114 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole" 115 } 116 117 resource "aws_vpc" "dms_vpc" { 118 cidr_block = "10.1.0.0/16" 119 tags { 120 Name = "tf-test-dms-vpc-%[1]s" 121 } 122 depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"] 123 } 124 125 resource "aws_subnet" "dms_subnet_1" { 126 cidr_block = "10.1.1.0/24" 127 availability_zone = "us-west-2a" 128 vpc_id = "${aws_vpc.dms_vpc.id}" 129 tags { 130 Name = "tf-test-dms-subnet-%[1]s" 131 } 132 depends_on = ["aws_vpc.dms_vpc"] 133 } 134 135 resource "aws_subnet" "dms_subnet_2" { 136 cidr_block = "10.1.2.0/24" 137 availability_zone = "us-west-2b" 138 vpc_id = "${aws_vpc.dms_vpc.id}" 139 tags { 140 Name = "tf-test-dms-subnet-%[1]s" 141 } 142 depends_on = ["aws_vpc.dms_vpc"] 143 } 144 145 resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" { 146 replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s" 147 replication_subnet_group_description = "terraform test for replication subnet group" 148 subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"] 149 depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"] 150 } 151 152 resource "aws_dms_replication_instance" "dms_replication_instance" { 153 allocated_storage = 5 154 auto_minor_version_upgrade = true 155 replication_instance_class = "dms.t2.micro" 156 replication_instance_id = "tf-test-dms-replication-instance-%[1]s" 157 preferred_maintenance_window = "sun:00:30-sun:02:30" 158 publicly_accessible = false 159 replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}" 160 tags { 161 Name = "tf-test-dms-replication-instance-%[1]s" 162 Update = "to-update" 163 Remove = "to-remove" 164 } 165 } 166 `, randId) 167 } 168 169 func dmsReplicationInstanceConfigUpdate(randId string) string { 170 return fmt.Sprintf(` 171 resource "aws_iam_role" "dms_iam_role" { 172 name = "dms-vpc-role" 173 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"dms.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}" 174 } 175 176 resource "aws_iam_role_policy_attachment" "dms_iam_role_policy" { 177 role = "${aws_iam_role.dms_iam_role.name}" 178 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole" 179 } 180 181 resource "aws_vpc" "dms_vpc" { 182 cidr_block = "10.1.0.0/16" 183 tags { 184 Name = "tf-test-dms-vpc-%[1]s" 185 } 186 depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"] 187 } 188 189 resource "aws_subnet" "dms_subnet_1" { 190 cidr_block = "10.1.1.0/24" 191 availability_zone = "us-west-2a" 192 vpc_id = "${aws_vpc.dms_vpc.id}" 193 tags { 194 Name = "tf-test-dms-subnet-%[1]s" 195 } 196 depends_on = ["aws_vpc.dms_vpc"] 197 } 198 199 resource "aws_subnet" "dms_subnet_2" { 200 cidr_block = "10.1.2.0/24" 201 availability_zone = "us-west-2b" 202 vpc_id = "${aws_vpc.dms_vpc.id}" 203 tags { 204 Name = "tf-test-dms-subnet-%[1]s" 205 } 206 depends_on = ["aws_vpc.dms_vpc"] 207 } 208 209 resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" { 210 replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s" 211 replication_subnet_group_description = "terraform test for replication subnet group" 212 subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"] 213 depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"] 214 } 215 216 resource "aws_dms_replication_instance" "dms_replication_instance" { 217 allocated_storage = 5 218 apply_immediately = true 219 auto_minor_version_upgrade = false 220 replication_instance_class = "dms.t2.micro" 221 replication_instance_id = "tf-test-dms-replication-instance-%[1]s" 222 preferred_maintenance_window = "mon:00:30-mon:02:30" 223 publicly_accessible = false 224 replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}" 225 tags { 226 Name = "tf-test-dms-replication-instance-%[1]s" 227 Update = "updated" 228 Add = "added" 229 } 230 } 231 `, randId) 232 }