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