github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ses_domain_identity.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/service/ses"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsSesDomainIdentity() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsSesDomainIdentityCreate,
    15  		Read:   resourceAwsSesDomainIdentityRead,
    16  		Delete: resourceAwsSesDomainIdentityDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"domain": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"verification_token": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceAwsSesDomainIdentityCreate(d *schema.ResourceData, meta interface{}) error {
    37  	conn := meta.(*AWSClient).sesConn
    38  
    39  	domainName := d.Get("domain").(string)
    40  
    41  	createOpts := &ses.VerifyDomainIdentityInput{
    42  		Domain: aws.String(domainName),
    43  	}
    44  
    45  	_, err := conn.VerifyDomainIdentity(createOpts)
    46  	if err != nil {
    47  		return fmt.Errorf("Error requesting SES domain identity verification: %s", err)
    48  	}
    49  
    50  	d.SetId(domainName)
    51  
    52  	return resourceAwsSesDomainIdentityRead(d, meta)
    53  }
    54  
    55  func resourceAwsSesDomainIdentityRead(d *schema.ResourceData, meta interface{}) error {
    56  	conn := meta.(*AWSClient).sesConn
    57  
    58  	domainName := d.Id()
    59  	d.Set("domain", domainName)
    60  
    61  	readOpts := &ses.GetIdentityVerificationAttributesInput{
    62  		Identities: []*string{
    63  			aws.String(domainName),
    64  		},
    65  	}
    66  
    67  	response, err := conn.GetIdentityVerificationAttributes(readOpts)
    68  	if err != nil {
    69  		log.Printf("[WARN] Error fetching identity verification attributes for %s: %s", d.Id(), err)
    70  		return err
    71  	}
    72  
    73  	verificationAttrs, ok := response.VerificationAttributes[domainName]
    74  	if !ok {
    75  		log.Printf("[WARN] Domain not listed in response when fetching verification attributes for %s", d.Id())
    76  		d.SetId("")
    77  		return nil
    78  	}
    79  
    80  	d.Set("verification_token", verificationAttrs.VerificationToken)
    81  	return nil
    82  }
    83  
    84  func resourceAwsSesDomainIdentityDelete(d *schema.ResourceData, meta interface{}) error {
    85  	conn := meta.(*AWSClient).sesConn
    86  
    87  	domainName := d.Get("domain").(string)
    88  
    89  	deleteOpts := &ses.DeleteIdentityInput{
    90  		Identity: aws.String(domainName),
    91  	}
    92  
    93  	_, err := conn.DeleteIdentity(deleteOpts)
    94  	if err != nil {
    95  		return fmt.Errorf("Error deleting SES domain identity: %s", err)
    96  	}
    97  
    98  	return nil
    99  }