github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/terraform"
    12  )
    13  
    14  func TestAccAwsDmsReplicationInstanceBasic(t *testing.T) {
    15  	resourceName := "aws_dms_replication_instance.dms_replication_instance"
    16  	randId := acctest.RandString(8)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: dmsReplicationInstanceDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: dmsReplicationInstanceConfig(randId),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					checkDmsReplicationInstanceExists(resourceName),
    27  					resource.TestCheckResourceAttrSet(resourceName, "replication_instance_arn"),
    28  				),
    29  			},
    30  			{
    31  				ResourceName:      resourceName,
    32  				ImportState:       true,
    33  				ImportStateVerify: true,
    34  			},
    35  			{
    36  				Config: dmsReplicationInstanceConfigUpdate(randId),
    37  				Check: resource.ComposeTestCheckFunc(
    38  					checkDmsReplicationInstanceExists(resourceName),
    39  					resource.TestCheckResourceAttrSet(resourceName, "apply_immediately"),
    40  					resource.TestCheckResourceAttr(resourceName, "auto_minor_version_upgrade", "false"),
    41  					resource.TestCheckResourceAttr(resourceName, "preferred_maintenance_window", "mon:00:30-mon:02:30"),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func checkDmsReplicationInstanceExists(n string) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		rs, ok := s.RootModule().Resources[n]
    51  		if !ok {
    52  			return fmt.Errorf("Not found: %s", n)
    53  		}
    54  
    55  		if rs.Primary.ID == "" {
    56  			return fmt.Errorf("No ID is set")
    57  		}
    58  		conn := testAccProvider.Meta().(*AWSClient).dmsconn
    59  		resp, err := conn.DescribeReplicationInstances(&dms.DescribeReplicationInstancesInput{
    60  			Filters: []*dms.Filter{
    61  				{
    62  					Name:   aws.String("replication-instance-id"),
    63  					Values: []*string{aws.String(rs.Primary.ID)},
    64  				},
    65  			},
    66  		})
    67  
    68  		if err != nil {
    69  			return fmt.Errorf("DMS replication instance error: %v", err)
    70  		}
    71  		if resp.ReplicationInstances == nil {
    72  			return fmt.Errorf("DMS replication instance not found")
    73  		}
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  func dmsReplicationInstanceDestroy(s *terraform.State) error {
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "aws_dms_replication_instance" {
    82  			continue
    83  		}
    84  
    85  		err := checkDmsReplicationInstanceExists(rs.Primary.ID)
    86  		if err == nil {
    87  			return fmt.Errorf("Found replication instance that was not destroyed: %s", rs.Primary.ID)
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  func dmsReplicationInstanceConfig(randId string) string {
    95  	return fmt.Sprintf(`
    96  resource "aws_vpc" "dms_vpc" {
    97  	cidr_block = "10.1.0.0/16"
    98  	tags {
    99  		Name = "tf-test-dms-vpc-%[1]s"
   100  	}
   101  }
   102  
   103  resource "aws_subnet" "dms_subnet_1" {
   104  	cidr_block = "10.1.1.0/24"
   105  	availability_zone = "us-west-2a"
   106  	vpc_id = "${aws_vpc.dms_vpc.id}"
   107  	tags {
   108  		Name = "tf-test-dms-subnet-%[1]s"
   109  	}
   110  	depends_on = ["aws_vpc.dms_vpc"]
   111  }
   112  
   113  resource "aws_subnet" "dms_subnet_2" {
   114  	cidr_block = "10.1.2.0/24"
   115  	availability_zone = "us-west-2b"
   116  	vpc_id = "${aws_vpc.dms_vpc.id}"
   117  	tags {
   118  		Name = "tf-test-dms-subnet-%[1]s"
   119  	}
   120  	depends_on = ["aws_vpc.dms_vpc"]
   121  }
   122  
   123  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   124  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   125  	replication_subnet_group_description = "terraform test for replication subnet group"
   126  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"]
   127  }
   128  
   129  resource "aws_dms_replication_instance" "dms_replication_instance" {
   130  	allocated_storage = 5
   131  	auto_minor_version_upgrade = true
   132  	replication_instance_class = "dms.t2.micro"
   133  	replication_instance_id = "tf-test-dms-replication-instance-%[1]s"
   134  	preferred_maintenance_window = "sun:00:30-sun:02:30"
   135  	publicly_accessible = false
   136  	replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}"
   137  	tags {
   138  		Name = "tf-test-dms-replication-instance-%[1]s"
   139  		Update = "to-update"
   140  		Remove = "to-remove"
   141  	}
   142  }
   143  `, randId)
   144  }
   145  
   146  func dmsReplicationInstanceConfigUpdate(randId string) string {
   147  	return fmt.Sprintf(`
   148  resource "aws_vpc" "dms_vpc" {
   149  	cidr_block = "10.1.0.0/16"
   150  	tags {
   151  		Name = "tf-test-dms-vpc-%[1]s"
   152  	}
   153  }
   154  
   155  resource "aws_subnet" "dms_subnet_1" {
   156  	cidr_block = "10.1.1.0/24"
   157  	availability_zone = "us-west-2a"
   158  	vpc_id = "${aws_vpc.dms_vpc.id}"
   159  	tags {
   160  		Name = "tf-test-dms-subnet-%[1]s"
   161  	}
   162  	depends_on = ["aws_vpc.dms_vpc"]
   163  }
   164  
   165  resource "aws_subnet" "dms_subnet_2" {
   166  	cidr_block = "10.1.2.0/24"
   167  	availability_zone = "us-west-2b"
   168  	vpc_id = "${aws_vpc.dms_vpc.id}"
   169  	tags {
   170  		Name = "tf-test-dms-subnet-%[1]s"
   171  	}
   172  	depends_on = ["aws_vpc.dms_vpc"]
   173  }
   174  
   175  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   176  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   177  	replication_subnet_group_description = "terraform test for replication subnet group"
   178  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"]
   179  }
   180  
   181  resource "aws_dms_replication_instance" "dms_replication_instance" {
   182  	allocated_storage = 5
   183  	apply_immediately = true
   184  	auto_minor_version_upgrade = false
   185  	replication_instance_class = "dms.t2.micro"
   186  	replication_instance_id = "tf-test-dms-replication-instance-%[1]s"
   187  	preferred_maintenance_window = "mon:00:30-mon:02:30"
   188  	publicly_accessible = false
   189  	replication_subnet_group_id = "${aws_dms_replication_subnet_group.dms_replication_subnet_group.replication_subnet_group_id}"
   190  	tags {
   191  		Name = "tf-test-dms-replication-instance-%[1]s"
   192  		Update = "updated"
   193  		Add = "added"
   194  	}
   195  }
   196  `, randId)
   197  }