github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_dms_certificate.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	dms "github.com/aws/aws-sdk-go/service/databasemigrationservice"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsDmsCertificate() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsDmsCertificateCreate,
    16  		Read:   resourceAwsDmsCertificateRead,
    17  		Delete: resourceAwsDmsCertificateDelete,
    18  
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"certificate_arn": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  			"certificate_id": {
    29  				Type:         schema.TypeString,
    30  				Required:     true,
    31  				ForceNew:     true,
    32  				ValidateFunc: validateDmsCertificateId,
    33  			},
    34  			"certificate_pem": {
    35  				Type:      schema.TypeString,
    36  				Optional:  true,
    37  				ForceNew:  true,
    38  				Sensitive: true,
    39  			},
    40  			"certificate_wallet": {
    41  				Type:      schema.TypeString,
    42  				Optional:  true,
    43  				ForceNew:  true,
    44  				Sensitive: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceAwsDmsCertificateCreate(d *schema.ResourceData, meta interface{}) error {
    51  	conn := meta.(*AWSClient).dmsconn
    52  
    53  	request := &dms.ImportCertificateInput{
    54  		CertificateIdentifier: aws.String(d.Get("certificate_id").(string)),
    55  	}
    56  
    57  	pem, pemSet := d.GetOk("certificate_pem")
    58  	wallet, walletSet := d.GetOk("certificate_wallet")
    59  
    60  	if !pemSet && !walletSet {
    61  		return fmt.Errorf("Must set either certificate_pem and certificate_wallet.")
    62  	}
    63  	if pemSet && walletSet {
    64  		return fmt.Errorf("Cannot set both certificate_pem and certificate_wallet.")
    65  	}
    66  
    67  	if pemSet {
    68  		request.CertificatePem = aws.String(pem.(string))
    69  	}
    70  	if walletSet {
    71  		request.CertificateWallet = []byte(wallet.(string))
    72  	}
    73  
    74  	log.Println("[DEBUG] DMS import certificate:", request)
    75  
    76  	_, err := conn.ImportCertificate(request)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	d.SetId(d.Get("certificate_id").(string))
    82  	return resourceAwsDmsCertificateRead(d, meta)
    83  }
    84  
    85  func resourceAwsDmsCertificateRead(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).dmsconn
    87  
    88  	response, err := conn.DescribeCertificates(&dms.DescribeCertificatesInput{
    89  		Filters: []*dms.Filter{
    90  			{
    91  				Name:   aws.String("certificate-id"),
    92  				Values: []*string{aws.String(d.Id())}, // Must use d.Id() to work with import.
    93  			},
    94  		},
    95  	})
    96  	if err != nil {
    97  		if dmserr, ok := err.(awserr.Error); ok && dmserr.Code() == "ResourceNotFoundFault" {
    98  			d.SetId("")
    99  			return nil
   100  		}
   101  		return err
   102  	}
   103  
   104  	return resourceAwsDmsCertificateSetState(d, response.Certificates[0])
   105  }
   106  
   107  func resourceAwsDmsCertificateDelete(d *schema.ResourceData, meta interface{}) error {
   108  	conn := meta.(*AWSClient).dmsconn
   109  
   110  	request := &dms.DeleteCertificateInput{
   111  		CertificateArn: aws.String(d.Get("certificate_arn").(string)),
   112  	}
   113  
   114  	log.Printf("[DEBUG] DMS delete certificate: %#v", request)
   115  
   116  	_, err := conn.DeleteCertificate(request)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func resourceAwsDmsCertificateSetState(d *schema.ResourceData, cert *dms.Certificate) error {
   125  	d.SetId(*cert.CertificateIdentifier)
   126  
   127  	d.Set("certificate_id", cert.CertificateIdentifier)
   128  	d.Set("certificate_arn", cert.CertificateArn)
   129  
   130  	if cert.CertificatePem != nil && *cert.CertificatePem != "" {
   131  		d.Set("certificate_pem", cert.CertificatePem)
   132  	}
   133  	if cert.CertificateWallet != nil && len(cert.CertificateWallet) == 0 {
   134  		d.Set("certificate_wallet", cert.CertificateWallet)
   135  	}
   136  
   137  	return nil
   138  }