github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_dms_endpoint_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 TestAccAwsDmsEndpointBasic(t *testing.T) {
    15  	resourceName := "aws_dms_endpoint.dms_endpoint"
    16  	randId := acctest.RandString(8)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: dmsEndpointDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: dmsEndpointConfig(randId),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					checkDmsEndpointExists(resourceName),
    27  					resource.TestCheckResourceAttrSet(resourceName, "endpoint_arn"),
    28  				),
    29  			},
    30  			{
    31  				ResourceName:            resourceName,
    32  				ImportState:             true,
    33  				ImportStateVerify:       true,
    34  				ImportStateVerifyIgnore: []string{"password"},
    35  			},
    36  			{
    37  				Config: dmsEndpointConfigUpdate(randId),
    38  				Check: resource.ComposeTestCheckFunc(
    39  					checkDmsEndpointExists(resourceName),
    40  					resource.TestCheckResourceAttr(resourceName, "database_name", "tf-test-dms-db-updated"),
    41  					resource.TestCheckResourceAttr(resourceName, "extra_connection_attributes", "extra"),
    42  					resource.TestCheckResourceAttr(resourceName, "password", "tftestupdate"),
    43  					resource.TestCheckResourceAttr(resourceName, "port", "3303"),
    44  					resource.TestCheckResourceAttr(resourceName, "ssl_mode", "none"),
    45  					resource.TestCheckResourceAttr(resourceName, "server_name", "tftestupdate"),
    46  					resource.TestCheckResourceAttr(resourceName, "username", "tftestupdate"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func dmsEndpointDestroy(s *terraform.State) error {
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "aws_dms_endpoint" {
    56  			continue
    57  		}
    58  
    59  		err := checkDmsEndpointExists(rs.Primary.ID)
    60  		if err == nil {
    61  			return fmt.Errorf("Found an endpoint that was not destroyed: %s", rs.Primary.ID)
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func checkDmsEndpointExists(n string) resource.TestCheckFunc {
    69  	return func(s *terraform.State) error {
    70  		rs, ok := s.RootModule().Resources[n]
    71  		if !ok {
    72  			return fmt.Errorf("Not found: %s", n)
    73  		}
    74  
    75  		if rs.Primary.ID == "" {
    76  			return fmt.Errorf("No ID is set")
    77  		}
    78  
    79  		conn := testAccProvider.Meta().(*AWSClient).dmsconn
    80  		resp, err := conn.DescribeEndpoints(&dms.DescribeEndpointsInput{
    81  			Filters: []*dms.Filter{
    82  				{
    83  					Name:   aws.String("endpoint-id"),
    84  					Values: []*string{aws.String(rs.Primary.ID)},
    85  				},
    86  			},
    87  		})
    88  
    89  		if err != nil {
    90  			return fmt.Errorf("DMS endpoint error: %v", err)
    91  		}
    92  
    93  		if resp.Endpoints == nil {
    94  			return fmt.Errorf("DMS endpoint not found")
    95  		}
    96  
    97  		return nil
    98  	}
    99  }
   100  
   101  func dmsEndpointConfig(randId string) string {
   102  	return fmt.Sprintf(`
   103  resource "aws_dms_endpoint" "dms_endpoint" {
   104  	database_name = "tf-test-dms-db"
   105  	endpoint_id = "tf-test-dms-endpoint-%[1]s"
   106  	endpoint_type = "source"
   107  	engine_name = "aurora"
   108  	extra_connection_attributes = ""
   109  	password = "tftest"
   110  	port = 3306
   111  	server_name = "tftest"
   112  	ssl_mode = "none"
   113  	tags {
   114  		Name = "tf-test-dms-endpoint-%[1]s"
   115  		Update = "to-update"
   116  		Remove = "to-remove"
   117  	}
   118  	username = "tftest"
   119  }
   120  `, randId)
   121  }
   122  
   123  func dmsEndpointConfigUpdate(randId string) string {
   124  	return fmt.Sprintf(`
   125  resource "aws_dms_endpoint" "dms_endpoint" {
   126  	database_name = "tf-test-dms-db-updated"
   127  	endpoint_id = "tf-test-dms-endpoint-%[1]s"
   128  	endpoint_type = "source"
   129  	engine_name = "aurora"
   130  	extra_connection_attributes = "extra"
   131  	password = "tftestupdate"
   132  	port = 3303
   133  	server_name = "tftestupdate"
   134  	ssl_mode = "none"
   135  	tags {
   136  		Name = "tf-test-dms-endpoint-%[1]s"
   137  		Update = "updated"
   138  		Add = "added"
   139  	}
   140  	username = "tftestupdate"
   141  }
   142  `, randId)
   143  }