github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_dms_certificate_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 TestAccAwsDmsCertificateBasic(t *testing.T) {
    16  	resourceName := "aws_dms_certificate.dms_certificate"
    17  	randId := acctest.RandString(8)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: dmsCertificateDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: dmsCertificateConfig(randId),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					checkDmsCertificateExists(resourceName),
    28  					resource.TestCheckResourceAttrSet(resourceName, "certificate_arn"),
    29  				),
    30  			},
    31  			{
    32  				ResourceName:      resourceName,
    33  				ImportState:       true,
    34  				ImportStateVerify: true,
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func dmsCertificateDestroy(s *terraform.State) error {
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "aws_dms_certificate" {
    43  			continue
    44  		}
    45  
    46  		err := checkDmsCertificateExists(rs.Primary.ID)
    47  		if err == nil {
    48  			return fmt.Errorf("Found a certificate that was not destroyed: %s", rs.Primary.ID)
    49  		}
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func checkDmsCertificateExists(n string) resource.TestCheckFunc {
    56  	providers := []*schema.Provider{testAccProvider}
    57  	return checkDmsCertificateExistsWithProviders(n, &providers)
    58  }
    59  
    60  func checkDmsCertificateExistsWithProviders(n string, providers *[]*schema.Provider) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No ID is set")
    69  		}
    70  		for _, provider := range *providers {
    71  			// Ignore if Meta is empty, this can happen for validation providers
    72  			if provider.Meta() == nil {
    73  				continue
    74  			}
    75  
    76  			conn := provider.Meta().(*AWSClient).dmsconn
    77  			_, err := conn.DescribeCertificates(&dms.DescribeCertificatesInput{
    78  				Filters: []*dms.Filter{
    79  					{
    80  						Name:   aws.String("certificate-id"),
    81  						Values: []*string{aws.String(rs.Primary.ID)},
    82  					},
    83  				},
    84  			})
    85  
    86  			if err != nil {
    87  				return fmt.Errorf("DMS certificate error: %v", err)
    88  			}
    89  			return nil
    90  		}
    91  
    92  		return fmt.Errorf("DMS certificate not found")
    93  	}
    94  }
    95  
    96  func dmsCertificateConfig(randId string) string {
    97  	return fmt.Sprintf(`
    98  resource "aws_dms_certificate" "dms_certificate" {
    99  	certificate_id = "tf-test-dms-certificate-%[1]s"
   100      certificate_pem = "-----BEGIN CERTIFICATE-----\nMIID2jCCAsKgAwIBAgIJAJ58TJVjU7G1MA0GCSqGSIb3DQEBBQUAMFExCzAJBgNV\nBAYTAlVTMREwDwYDVQQIEwhDb2xvcmFkbzEPMA0GA1UEBxMGRGVudmVyMRAwDgYD\nVQQKEwdDaGFydGVyMQwwCgYDVQQLEwNDU0UwHhcNMTcwMTMwMTkyMDA4WhcNMjYx\nMjA5MTkyMDA4WjBRMQswCQYDVQQGEwJVUzERMA8GA1UECBMIQ29sb3JhZG8xDzAN\nBgNVBAcTBkRlbnZlcjEQMA4GA1UEChMHQ2hhcnRlcjEMMAoGA1UECxMDQ1NFMIIB\nIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv6dq6VLIImlAaTrckb5w3X6J\nWP7EGz2ChGAXlkEYto6dPCba0v5+f+8UlMOpeB25X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