github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_dms_replication_task_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/terraform" 12 ) 13 14 func TestAccAwsDmsReplicationTaskBasic(t *testing.T) { 15 resourceName := "aws_dms_replication_task.dms_replication_task" 16 randId := acctest.RandString(8) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: dmsReplicationTaskDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: dmsReplicationTaskConfig(randId), 25 Check: resource.ComposeTestCheckFunc( 26 checkDmsReplicationTaskExists(resourceName), 27 resource.TestCheckResourceAttrSet(resourceName, "replication_task_arn"), 28 ), 29 }, 30 { 31 ResourceName: resourceName, 32 ImportState: true, 33 ImportStateVerify: true, 34 }, 35 { 36 Config: dmsReplicationTaskConfigUpdate(randId), 37 Check: resource.ComposeTestCheckFunc( 38 checkDmsReplicationTaskExists(resourceName), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func checkDmsReplicationTaskExists(n string) resource.TestCheckFunc { 46 return func(s *terraform.State) error { 47 rs, ok := s.RootModule().Resources[n] 48 if !ok { 49 return fmt.Errorf("Not found: %s", n) 50 } 51 52 if rs.Primary.ID == "" { 53 return fmt.Errorf("No ID is set") 54 } 55 56 conn := testAccProvider.Meta().(*AWSClient).dmsconn 57 resp, err := conn.DescribeReplicationTasks(&dms.DescribeReplicationTasksInput{ 58 Filters: []*dms.Filter{ 59 { 60 Name: aws.String("replication-task-id"), 61 Values: []*string{aws.String(rs.Primary.ID)}, 62 }, 63 }, 64 }) 65 66 if err != nil { 67 return err 68 } 69 70 if resp.ReplicationTasks == nil { 71 return fmt.Errorf("DMS replication task error: %v", err) 72 } 73 return nil 74 } 75 } 76 77 func dmsReplicationTaskDestroy(s *terraform.State) error { 78 for _, rs := range s.RootModule().Resources { 79 if rs.Type != "aws_dms_replication_task" { 80 continue 81 } 82 83 conn := testAccProvider.Meta().(*AWSClient).dmsconn 84 resp, err := conn.DescribeReplicationTasks(&dms.DescribeReplicationTasksInput{ 85 Filters: []*dms.Filter{ 86 { 87 Name: aws.String("replication-task-id"), 88 Values: []*string{aws.String(rs.Primary.ID)}, 89 }, 90 }, 91 }) 92 93 if err != nil { 94 return nil 95 } 96 97 if resp != nil && len(resp.ReplicationTasks) > 0 { 98 return fmt.Errorf("DMS replication task still exists: %v", err) 99 } 100 } 101 102 return nil 103 } 104 105 func dmsReplicationTaskConfig(randId string) string { 106 return fmt.Sprintf(` 107 resource "aws_vpc" "dms_vpc" { 108 cidr_block = "10.1.0.0/16" 109 tags { 110 Name = "tf-test-dms-vpc-%[1]s" 111 } 112 } 113 114 resource "aws_subnet" "dms_subnet_1" { 115 cidr_block = "10.1.1.0/24" 116 availability_zone = "us-west-2a" 117 vpc_id = "${aws_vpc.dms_vpc.id}" 118 tags { 119 Name = "tf-test-dms-subnet-%[1]s" 120 } 121 depends_on = ["aws_vpc.dms_vpc"] 122 } 123 124 resource "aws_subnet" "dms_subnet_2" { 125 cidr_block = "10.1.2.0/24" 126 availability_zone = "us-west-2b" 127 vpc_id = "${aws_vpc.dms_vpc.id}" 128 tags { 129 Name = "tf-test-dms-subnet-%[1]s" 130 } 131 depends_on = ["aws_vpc.dms_vpc"] 132 } 133 134 resource "aws_dms_endpoint" "dms_endpoint_source" { 135 database_name = "tf-test-dms-db" 136 endpoint_id = "tf-test-dms-endpoint-source-%[1]s" 137 endpoint_type = "source" 138 engine_name = "aurora" 139 server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com" 140 port = 3306 141 username = "tftest" 142 password = "tftest" 143 } 144 145 resource "aws_dms_endpoint" "dms_endpoint_target" { 146 database_name = "tf-test-dms-db" 147 endpoint_id = "tf-test-dms-endpoint-target-%[1]s" 148 endpoint_type = "target" 149 engine_name = "aurora" 150 server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com" 151 port = 3306 152 username = "tftest" 153 password = "tftest" 154 } 155 156 resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" { 157 replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s" 158 replication_subnet_group_description = "terraform test for replication subnet group" 159 subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"] 160 } 161 162 resource "aws_dms_replication_instance" "dms_replication_instance" { 163 allocated_storage = 5 164 auto_minor_version_upgrade = true 165 replication_instance_class = "dms.t2.micro" 166 replication_instance_id = "tf-test-dms-replication-instance-%[1]s" 167 preferred_maintenance_window = "sun:00:30-sun:02:30" 168 publicly_accessible = false 169 replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}" 170 } 171 172 resource "aws_dms_replication_task" "dms_replication_task" { 173 migration_type = "full-load" 174 replication_instance_arn = "${aws_dms_replication_instance.dms_replication_instance.replication_instance_arn}" 175 replication_task_id = "tf-test-dms-replication-task-%[1]s" 176 replication_task_settings = "{\"TargetMetadata\":{\"TargetSchema\":\"\",\"SupportLobs\":true,\"FullLobMode\":false,\"LobChunkSize\":0,\"LimitedSizeLobMode\":true,\"LobMaxSize\":32,\"LoadMaxFileSize\":0,\"ParallelLoadThreads\":0,\"BatchApplyEnabled\":false},\"FullLoadSettings\":{\"FullLoadEnabled\":true,\"ApplyChangesEnabled\":false,\"TargetTablePrepMode\":\"DROP_AND_CREATE\",\"CreatePkAfterFullLoad\":false,\"StopTaskCachedChangesApplied\":false,\"StopTaskCachedChangesNotApplied\":false,\"ResumeEnabled\":false,\"ResumeMinTableSize\":100000,\"ResumeOnlyClusteredPKTables\":true,\"MaxFullLoadSubTasks\":8,\"TransactionConsistencyTimeout\":600,\"CommitRate\":10000},\"Logging\":{\"EnableLogging\":false,\"LogComponents\":[{\"Id\":\"SOURCE_UNLOAD\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TARGET_LOAD\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"SOURCE_CAPTURE\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TARGET_APPLY\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TASK_MANAGER\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"}],\"CloudWatchLogGroup\":null,\"CloudWatchLogStream\":null},\"ControlTablesSettings\":{\"historyTimeslotInMinutes\":5,\"ControlSchema\":\"\",\"HistoryTimeslotInMinutes\":5,\"HistoryTableEnabled\":false,\"SuspendedTablesTableEnabled\":false,\"StatusTableEnabled\":false},\"StreamBufferSettings\":{\"StreamBufferCount\":3,\"StreamBufferSizeInMB\":8,\"CtrlStreamBufferSizeInMB\":5},\"ChangeProcessingDdlHandlingPolicy\":{\"HandleSourceTableDropped\":true,\"HandleSourceTableTruncated\":true,\"HandleSourceTableAltered\":true},\"ErrorBehavior\":{\"DataErrorPolicy\":\"LOG_ERROR\",\"DataTruncationErrorPolicy\":\"LOG_ERROR\",\"DataErrorEscalationPolicy\":\"SUSPEND_TABLE\",\"DataErrorEscalationCount\":0,\"TableErrorPolicy\":\"SUSPEND_TABLE\",\"TableErrorEscalationPolicy\":\"STOP_TASK\",\"TableErrorEscalationCount\":0,\"RecoverableErrorCount\":-1,\"RecoverableErrorInterval\":5,\"RecoverableErrorThrottling\":true,\"RecoverableErrorThrottlingMax\":1800,\"ApplyErrorDeletePolicy\":\"IGNORE_RECORD\",\"ApplyErrorInsertPolicy\":\"LOG_ERROR\",\"ApplyErrorUpdatePolicy\":\"LOG_ERROR\",\"ApplyErrorEscalationPolicy\":\"LOG_ERROR\",\"ApplyErrorEscalationCount\":0,\"FullLoadIgnoreConflicts\":true},\"ChangeProcessingTuning\":{\"BatchApplyPreserveTransaction\":true,\"BatchApplyTimeoutMin\":1,\"BatchApplyTimeoutMax\":30,\"BatchApplyMemoryLimit\":500,\"BatchSplitSize\":0,\"MinTransactionSize\":1000,\"CommitTimeout\":1,\"MemoryLimitTotal\":1024,\"MemoryKeepTime\":60,\"StatementCacheSize\":50}}" 177 source_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_source.endpoint_arn}" 178 table_mappings = "{\"rules\":[{\"rule-type\":\"selection\",\"rule-id\":\"1\",\"rule-name\":\"1\",\"object-locator\":{\"schema-name\":\"%%\",\"table-name\":\"%%\"},\"rule-action\":\"include\"}]}" 179 tags { 180 Name = "tf-test-dms-replication-task-%[1]s" 181 Update = "to-update" 182 Remove = "to-remove" 183 } 184 target_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_target.endpoint_arn}" 185 } 186 `, randId) 187 } 188 189 func dmsReplicationTaskConfigUpdate(randId string) string { 190 return fmt.Sprintf(` 191 resource "aws_vpc" "dms_vpc" { 192 cidr_block = "10.1.0.0/16" 193 tags { 194 Name = "tf-test-dms-vpc-%[1]s" 195 } 196 } 197 198 resource "aws_subnet" "dms_subnet_1" { 199 cidr_block = "10.1.1.0/24" 200 availability_zone = "us-west-2a" 201 vpc_id = "${aws_vpc.dms_vpc.id}" 202 tags { 203 Name = "tf-test-dms-subnet-%[1]s" 204 } 205 depends_on = ["aws_vpc.dms_vpc"] 206 } 207 208 resource "aws_subnet" "dms_subnet_2" { 209 cidr_block = "10.1.2.0/24" 210 availability_zone = "us-west-2b" 211 vpc_id = "${aws_vpc.dms_vpc.id}" 212 tags { 213 Name = "tf-test-dms-subnet-%[1]s" 214 } 215 depends_on = ["aws_vpc.dms_vpc"] 216 } 217 218 resource "aws_dms_endpoint" "dms_endpoint_source" { 219 database_name = "tf-test-dms-db" 220 endpoint_id = "tf-test-dms-endpoint-source-%[1]s" 221 endpoint_type = "source" 222 engine_name = "aurora" 223 server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com" 224 port = 3306 225 username = "tftest" 226 password = "tftest" 227 } 228 229 resource "aws_dms_endpoint" "dms_endpoint_target" { 230 database_name = "tf-test-dms-db" 231 endpoint_id = "tf-test-dms-endpoint-target-%[1]s" 232 endpoint_type = "target" 233 engine_name = "aurora" 234 server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com" 235 port = 3306 236 username = "tftest" 237 password = "tftest" 238 } 239 240 resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" { 241 replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s" 242 replication_subnet_group_description = "terraform test for replication subnet group" 243 subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"] 244 } 245 246 resource "aws_dms_replication_instance" "dms_replication_instance" { 247 allocated_storage = 5 248 auto_minor_version_upgrade = true 249 replication_instance_class = "dms.t2.micro" 250 replication_instance_id = "tf-test-dms-replication-instance-%[1]s" 251 preferred_maintenance_window = "sun:00:30-sun:02:30" 252 publicly_accessible = false 253 replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}" 254 } 255 256 resource "aws_dms_replication_task" "dms_replication_task" { 257 migration_type = "full-load" 258 replication_instance_arn = "${aws_dms_replication_instance.dms_replication_instance.replication_instance_arn}" 259 replication_task_id = "tf-test-dms-replication-task-%[1]s" 260 replication_task_settings = "{\"TargetMetadata\":{\"TargetSchema\":\"\",\"SupportLobs\":true,\"FullLobMode\":false,\"LobChunkSize\":0,\"LimitedSizeLobMode\":true,\"LobMaxSize\":32,\"LoadMaxFileSize\":0,\"ParallelLoadThreads\":0,\"BatchApplyEnabled\":false},\"FullLoadSettings\":{\"FullLoadEnabled\":true,\"ApplyChangesEnabled\":false,\"TargetTablePrepMode\":\"DROP_AND_CREATE\",\"CreatePkAfterFullLoad\":false,\"StopTaskCachedChangesApplied\":false,\"StopTaskCachedChangesNotApplied\":false,\"ResumeEnabled\":false,\"ResumeMinTableSize\":100000,\"ResumeOnlyClusteredPKTables\":true,\"MaxFullLoadSubTasks\":7,\"TransactionConsistencyTimeout\":600,\"CommitRate\":10000},\"Logging\":{\"EnableLogging\":false,\"LogComponents\":[{\"Id\":\"SOURCE_UNLOAD\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TARGET_LOAD\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"SOURCE_CAPTURE\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TARGET_APPLY\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"},{\"Id\":\"TASK_MANAGER\",\"Severity\":\"LOGGER_SEVERITY_DEFAULT\"}],\"CloudWatchLogGroup\":null,\"CloudWatchLogStream\":null},\"ControlTablesSettings\":{\"historyTimeslotInMinutes\":5,\"ControlSchema\":\"\",\"HistoryTimeslotInMinutes\":5,\"HistoryTableEnabled\":false,\"SuspendedTablesTableEnabled\":false,\"StatusTableEnabled\":false},\"StreamBufferSettings\":{\"StreamBufferCount\":3,\"StreamBufferSizeInMB\":8,\"CtrlStreamBufferSizeInMB\":5},\"ChangeProcessingDdlHandlingPolicy\":{\"HandleSourceTableDropped\":true,\"HandleSourceTableTruncated\":true,\"HandleSourceTableAltered\":true},\"ErrorBehavior\":{\"DataErrorPolicy\":\"LOG_ERROR\",\"DataTruncationErrorPolicy\":\"LOG_ERROR\",\"DataErrorEscalationPolicy\":\"SUSPEND_TABLE\",\"DataErrorEscalationCount\":0,\"TableErrorPolicy\":\"SUSPEND_TABLE\",\"TableErrorEscalationPolicy\":\"STOP_TASK\",\"TableErrorEscalationCount\":0,\"RecoverableErrorCount\":-1,\"RecoverableErrorInterval\":5,\"RecoverableErrorThrottling\":true,\"RecoverableErrorThrottlingMax\":1800,\"ApplyErrorDeletePolicy\":\"IGNORE_RECORD\",\"ApplyErrorInsertPolicy\":\"LOG_ERROR\",\"ApplyErrorUpdatePolicy\":\"LOG_ERROR\",\"ApplyErrorEscalationPolicy\":\"LOG_ERROR\",\"ApplyErrorEscalationCount\":0,\"FullLoadIgnoreConflicts\":true},\"ChangeProcessingTuning\":{\"BatchApplyPreserveTransaction\":true,\"BatchApplyTimeoutMin\":1,\"BatchApplyTimeoutMax\":30,\"BatchApplyMemoryLimit\":500,\"BatchSplitSize\":0,\"MinTransactionSize\":1000,\"CommitTimeout\":1,\"MemoryLimitTotal\":1024,\"MemoryKeepTime\":60,\"StatementCacheSize\":50}}" 261 source_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_source.endpoint_arn}" 262 table_mappings = "{\"rules\":[{\"rule-type\":\"selection\",\"rule-id\":\"1\",\"rule-name\":\"1\",\"object-locator\":{\"schema-name\":\"%%\",\"table-name\":\"%%\"},\"rule-action\":\"include\"}]}" 263 tags { 264 Name = "tf-test-dms-replication-task-%[1]s" 265 Update = "updated" 266 Add = "added" 267 } 268 target_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_target.endpoint_arn}" 269 } 270 `, randId) 271 }