github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/helper/schema"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAwsDmsReplicationTaskBasic(t *testing.T) {
    16  	resourceName := "aws_dms_replication_task.dms_replication_task"
    17  	randId := acctest.RandString(8)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: dmsReplicationTaskDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: dmsReplicationTaskConfig(randId),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					checkDmsReplicationTaskExists(resourceName),
    28  					resource.TestCheckResourceAttrSet(resourceName, "replication_task_arn"),
    29  				),
    30  			},
    31  			{
    32  				ResourceName:      resourceName,
    33  				ImportState:       true,
    34  				ImportStateVerify: true,
    35  			},
    36  			{
    37  				Config: dmsReplicationTaskConfigUpdate(randId),
    38  				Check: resource.ComposeTestCheckFunc(
    39  					checkDmsReplicationTaskExists(resourceName),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func checkDmsReplicationTaskExists(n string) resource.TestCheckFunc {
    47  	providers := []*schema.Provider{testAccProvider}
    48  	return checkDmsReplicationTaskExistsWithProviders(n, &providers)
    49  }
    50  
    51  func checkDmsReplicationTaskExistsWithProviders(n string, providers *[]*schema.Provider) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[n]
    54  		if !ok {
    55  			return fmt.Errorf("Not found: %s", n)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No ID is set")
    60  		}
    61  		for _, provider := range *providers {
    62  			// Ignore if Meta is empty, this can happen for validation providers
    63  			if provider.Meta() == nil {
    64  				continue
    65  			}
    66  
    67  			conn := provider.Meta().(*AWSClient).dmsconn
    68  			_, err := conn.DescribeReplicationTasks(&dms.DescribeReplicationTasksInput{
    69  				Filters: []*dms.Filter{
    70  					{
    71  						Name:   aws.String("replication-task-id"),
    72  						Values: []*string{aws.String(rs.Primary.ID)},
    73  					},
    74  				},
    75  			})
    76  
    77  			if err != nil {
    78  				return fmt.Errorf("DMS replication subnet group error: %v", err)
    79  			}
    80  			return nil
    81  		}
    82  
    83  		return fmt.Errorf("DMS replication subnet group not found")
    84  	}
    85  }
    86  
    87  func dmsReplicationTaskDestroy(s *terraform.State) error {
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "aws_dms_replication_task" {
    90  			continue
    91  		}
    92  
    93  		err := checkDmsReplicationTaskExists(rs.Primary.ID)
    94  		if err == nil {
    95  			return fmt.Errorf("Found replication subnet group that was not destroyed: %s", rs.Primary.ID)
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func dmsReplicationTaskConfig(randId string) string {
   103  	return fmt.Sprintf(`
   104  resource "aws_iam_role" "dms_iam_role" {
   105    name = "dms-vpc-role"
   106    assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"dms.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
   107  }
   108  
   109  resource "aws_iam_role_policy_attachment" "dms_iam_role_policy" {
   110    role = "${aws_iam_role.dms_iam_role.name}"
   111    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole"
   112  }
   113  
   114  resource "aws_vpc" "dms_vpc" {
   115  	cidr_block = "10.1.0.0/16"
   116  	tags {
   117  		Name = "tf-test-dms-vpc-%[1]s"
   118  	}
   119  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   120  }
   121  
   122  resource "aws_subnet" "dms_subnet_1" {
   123  	cidr_block = "10.1.1.0/24"
   124  	availability_zone = "us-west-2a"
   125  	vpc_id = "${aws_vpc.dms_vpc.id}"
   126  	tags {
   127  		Name = "tf-test-dms-subnet-%[1]s"
   128  	}
   129  	depends_on = ["aws_vpc.dms_vpc"]
   130  }
   131  
   132  resource "aws_subnet" "dms_subnet_2" {
   133  	cidr_block = "10.1.2.0/24"
   134  	availability_zone = "us-west-2b"
   135  	vpc_id = "${aws_vpc.dms_vpc.id}"
   136  	tags {
   137  		Name = "tf-test-dms-subnet-%[1]s"
   138  	}
   139  	depends_on = ["aws_vpc.dms_vpc"]
   140  }
   141  
   142  resource "aws_dms_endpoint" "dms_endpoint_source" {
   143  	database_name = "tf-test-dms-db"
   144  	endpoint_id = "tf-test-dms-endpoint-source-%[1]s"
   145  	endpoint_type = "source"
   146  	engine_name = "aurora"
   147  	server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com"
   148  	port = 3306
   149  	username = "tftest"
   150  	password = "tftest"
   151  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   152  }
   153  
   154  resource "aws_dms_endpoint" "dms_endpoint_target" {
   155  	database_name = "tf-test-dms-db"
   156  	endpoint_id = "tf-test-dms-endpoint-target-%[1]s"
   157  	endpoint_type = "target"
   158  	engine_name = "aurora"
   159  	server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com"
   160  	port = 3306
   161  	username = "tftest"
   162  	password = "tftest"
   163  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   164  }
   165  
   166  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   167  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   168  	replication_subnet_group_description = "terraform test for replication subnet group"
   169  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"]
   170  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   171  }
   172  
   173  resource "aws_dms_replication_instance" "dms_replication_instance" {
   174  	allocated_storage = 5
   175  	auto_minor_version_upgrade = true
   176  	replication_instance_class = "dms.t2.micro"
   177  	replication_instance_id = "tf-test-dms-replication-instance-%[1]s"
   178  	preferred_maintenance_window = "sun:00:30-sun:02:30"
   179  	publicly_accessible = false
   180  	replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}"
   181  }
   182  
   183  resource "aws_dms_replication_task" "dms_replication_task" {
   184  	migration_type = "full-load"
   185  	replication_instance_arn = "${aws_dms_replication_instance.dms_replication_instance.replication_instance_arn}"
   186  	replication_task_id = "tf-test-dms-replication-task-%[1]s"
   187  	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}}"
   188  	source_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_source.endpoint_arn}"
   189  	table_mappings = "{\"rules\":[{\"rule-type\":\"selection\",\"rule-id\":\"1\",\"rule-name\":\"1\",\"object-locator\":{\"schema-name\":\"%%\",\"table-name\":\"%%\"},\"rule-action\":\"include\"}]}"
   190  	tags {
   191  		Name = "tf-test-dms-replication-task-%[1]s"
   192  		Update = "to-update"
   193  		Remove = "to-remove"
   194  	}
   195  	target_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_target.endpoint_arn}"
   196  }
   197  `, randId)
   198  }
   199  
   200  func dmsReplicationTaskConfigUpdate(randId string) string {
   201  	return fmt.Sprintf(`
   202  resource "aws_iam_role" "dms_iam_role" {
   203    name = "dms-vpc-role"
   204    assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"dms.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
   205  }
   206  
   207  resource "aws_iam_role_policy_attachment" "dms_iam_role_policy" {
   208    role = "${aws_iam_role.dms_iam_role.name}"
   209    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole"
   210  }
   211  
   212  resource "aws_vpc" "dms_vpc" {
   213  	cidr_block = "10.1.0.0/16"
   214  	tags {
   215  		Name = "tf-test-dms-vpc-%[1]s"
   216  	}
   217  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   218  }
   219  
   220  resource "aws_subnet" "dms_subnet_1" {
   221  	cidr_block = "10.1.1.0/24"
   222  	availability_zone = "us-west-2a"
   223  	vpc_id = "${aws_vpc.dms_vpc.id}"
   224  	tags {
   225  		Name = "tf-test-dms-subnet-%[1]s"
   226  	}
   227  	depends_on = ["aws_vpc.dms_vpc"]
   228  }
   229  
   230  resource "aws_subnet" "dms_subnet_2" {
   231  	cidr_block = "10.1.2.0/24"
   232  	availability_zone = "us-west-2b"
   233  	vpc_id = "${aws_vpc.dms_vpc.id}"
   234  	tags {
   235  		Name = "tf-test-dms-subnet-%[1]s"
   236  	}
   237  	depends_on = ["aws_vpc.dms_vpc"]
   238  }
   239  
   240  resource "aws_dms_endpoint" "dms_endpoint_source" {
   241  	database_name = "tf-test-dms-db"
   242  	endpoint_id = "tf-test-dms-endpoint-source-%[1]s"
   243  	endpoint_type = "source"
   244  	engine_name = "aurora"
   245  	server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com"
   246  	port = 3306
   247  	username = "tftest"
   248  	password = "tftest"
   249  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   250  }
   251  
   252  resource "aws_dms_endpoint" "dms_endpoint_target" {
   253  	database_name = "tf-test-dms-db"
   254  	endpoint_id = "tf-test-dms-endpoint-target-%[1]s"
   255  	endpoint_type = "target"
   256  	engine_name = "aurora"
   257  	server_name = "tf-test-cluster.cluster-xxxxxxx.us-west-2.rds.amazonaws.com"
   258  	port = 3306
   259  	username = "tftest"
   260  	password = "tftest"
   261  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   262  }
   263  
   264  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   265  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   266  	replication_subnet_group_description = "terraform test for replication subnet group"
   267  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"]
   268  	depends_on = ["aws_iam_role_policy_attachment.dms_iam_role_policy"]
   269  }
   270  
   271  resource "aws_dms_replication_instance" "dms_replication_instance" {
   272  	allocated_storage = 5
   273  	auto_minor_version_upgrade = true
   274  	replication_instance_class = "dms.t2.micro"
   275  	replication_instance_id = "tf-test-dms-replication-instance-%[1]s"
   276  	preferred_maintenance_window = "sun:00:30-sun:02:30"
   277  	publicly_accessible = false
   278  	replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}"
   279  }
   280  
   281  resource "aws_dms_replication_task" "dms_replication_task" {
   282  	migration_type = "full-load"
   283  	replication_instance_arn = "${aws_dms_replication_instance.dms_replication_instance.replication_instance_arn}"
   284  	replication_task_id = "tf-test-dms-replication-task-%[1]s"
   285  	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}}"
   286  	source_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_source.endpoint_arn}"
   287  	table_mappings = "{\"rules\":[{\"rule-type\":\"selection\",\"rule-id\":\"1\",\"rule-name\":\"1\",\"object-locator\":{\"schema-name\":\"%%\",\"table-name\":\"%%\"},\"rule-action\":\"include\"}]}"
   288  	tags {
   289  		Name = "tf-test-dms-replication-task-%[1]s"
   290  		Update = "updated"
   291  		Add = "added"
   292  	}
   293  	target_endpoint_arn = "${aws_dms_endpoint.dms_endpoint_target.endpoint_arn}"
   294  }
   295  `, randId)
   296  }