github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_dms_replication_subnet_group_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 TestAccAwsDmsReplicationSubnetGroupBasic(t *testing.T) {
    16  	resourceName := "aws_dms_replication_subnet_group.dms_replication_subnet_group"
    17  	randId := acctest.RandString(8)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: dmsReplicationSubnetGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: dmsReplicationSubnetGroupConfig(randId),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					checkDmsReplicationSubnetGroupExists(resourceName),
    28  					resource.TestCheckResourceAttrSet(resourceName, "vpc_id"),
    29  				),
    30  			},
    31  			{
    32  				ResourceName:      resourceName,
    33  				ImportState:       true,
    34  				ImportStateVerify: true,
    35  			},
    36  			{
    37  				Config: dmsReplicationSubnetGroupConfigUpdate(randId),
    38  				Check: resource.ComposeTestCheckFunc(
    39  					checkDmsReplicationSubnetGroupExists(resourceName),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func checkDmsReplicationSubnetGroupExists(n string) resource.TestCheckFunc {
    47  	providers := []*schema.Provider{testAccProvider}
    48  	return checkDmsReplicationSubnetGroupExistsWithProviders(n, &providers)
    49  }
    50  
    51  func checkDmsReplicationSubnetGroupExistsWithProviders(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.DescribeReplicationSubnetGroups(&dms.DescribeReplicationSubnetGroupsInput{
    69  				Filters: []*dms.Filter{
    70  					{
    71  						Name:   aws.String("replication-subnet-group-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 dmsReplicationSubnetGroupDestroy(s *terraform.State) error {
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "aws_dms_replication_subnet_group" {
    90  			continue
    91  		}
    92  
    93  		err := checkDmsReplicationSubnetGroupExists(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 dmsReplicationSubnetGroupConfig(randId string) string {
   103  	return fmt.Sprintf(`
   104  resource "aws_vpc" "dms_vpc" {
   105  	cidr_block = "10.1.0.0/16"
   106  	tags {
   107  		Name = "tf-test-dms-vpc-%[1]s"
   108  	}
   109  }
   110  
   111  resource "aws_subnet" "dms_subnet_1" {
   112  	cidr_block = "10.1.1.0/24"
   113  	availability_zone = "us-west-2a"
   114  	vpc_id = "${aws_vpc.dms_vpc.id}"
   115  	tags {
   116  		Name = "tf-test-dms-subnet-%[1]s"
   117  	}
   118  	depends_on = ["aws_vpc.dms_vpc"]
   119  }
   120  
   121  resource "aws_subnet" "dms_subnet_2" {
   122  	cidr_block = "10.1.2.0/24"
   123  	availability_zone = "us-west-2b"
   124  	vpc_id = "${aws_vpc.dms_vpc.id}"
   125  	tags {
   126  		Name = "tf-test-dms-subnet-%[1]s"
   127  	}
   128  	depends_on = ["aws_vpc.dms_vpc"]
   129  }
   130  
   131  resource "aws_subnet" "dms_subnet_3" {
   132  	cidr_block = "10.1.3.0/24"
   133  	availability_zone = "us-west-2b"
   134  	vpc_id = "${aws_vpc.dms_vpc.id}"
   135  	tags {
   136  		Name = "tf-test-dms-subnet-%[1]s"
   137  	}
   138  	depends_on = ["aws_vpc.dms_vpc"]
   139  }
   140  
   141  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   142  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   143  	replication_subnet_group_description = "terraform test for replication subnet group"
   144  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_2.id}"]
   145  	tags {
   146  		Name = "tf-test-dms-replication-subnet-group-%[1]s"
   147  		Update = "to-update"
   148  		Remove = "to-remove"
   149  	}
   150  }
   151  `, randId)
   152  }
   153  
   154  func dmsReplicationSubnetGroupConfigUpdate(randId string) string {
   155  	return fmt.Sprintf(`
   156  resource "aws_vpc" "dms_vpc" {
   157  	cidr_block = "10.1.0.0/16"
   158  	tags {
   159  		Name = "tf-test-dms-vpc-%[1]s"
   160  	}
   161  }
   162  
   163  resource "aws_subnet" "dms_subnet_1" {
   164  	cidr_block = "10.1.1.0/24"
   165  	availability_zone = "us-west-2a"
   166  	vpc_id = "${aws_vpc.dms_vpc.id}"
   167  	tags {
   168  		Name = "tf-test-dms-subnet-%[1]s"
   169  	}
   170  	depends_on = ["aws_vpc.dms_vpc"]
   171  }
   172  
   173  resource "aws_subnet" "dms_subnet_2" {
   174  	cidr_block = "10.1.2.0/24"
   175  	availability_zone = "us-west-2b"
   176  	vpc_id = "${aws_vpc.dms_vpc.id}"
   177  	tags {
   178  		Name = "tf-test-dms-subnet-%[1]s"
   179  	}
   180  	depends_on = ["aws_vpc.dms_vpc"]
   181  }
   182  
   183  resource "aws_subnet" "dms_subnet_3" {
   184  	cidr_block = "10.1.3.0/24"
   185  	availability_zone = "us-west-2b"
   186  	vpc_id = "${aws_vpc.dms_vpc.id}"
   187  	tags {
   188  		Name = "tf-test-dms-subnet-%[1]s"
   189  	}
   190  	depends_on = ["aws_vpc.dms_vpc"]
   191  }
   192  
   193  resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
   194  	replication_subnet_group_id = "tf-test-dms-replication-subnet-group-%[1]s"
   195  	replication_subnet_group_description = "terraform test for replication subnet group"
   196  	subnet_ids = ["${aws_subnet.dms_subnet_1.id}", "${aws_subnet.dms_subnet_3.id}"]
   197  	tags {
   198  		Name = "tf-test-dms-replication-subnet-group-%[1]s"
   199  		Update = "updated"
   200  		Add = "added"
   201  	}
   202  }
   203  `, randId)
   204  }