github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ses_configuration_set.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 resourceAwsSesConfigurationSet() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsSesConfigurationSetCreate,
    15  		Read:   resourceAwsSesConfigurationSetRead,
    16  		Delete: resourceAwsSesConfigurationSetDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  		},
    28  	}
    29  }
    30  
    31  func resourceAwsSesConfigurationSetCreate(d *schema.ResourceData, meta interface{}) error {
    32  	conn := meta.(*AWSClient).sesConn
    33  
    34  	configurationSetName := d.Get("name").(string)
    35  
    36  	createOpts := &ses.CreateConfigurationSetInput{
    37  		ConfigurationSet: &ses.ConfigurationSet{
    38  			Name: aws.String(configurationSetName),
    39  		},
    40  	}
    41  
    42  	_, err := conn.CreateConfigurationSet(createOpts)
    43  	if err != nil {
    44  		return fmt.Errorf("Error creating SES configuration set: %s", err)
    45  	}
    46  
    47  	d.SetId(configurationSetName)
    48  
    49  	return resourceAwsSesConfigurationSetRead(d, meta)
    50  }
    51  
    52  func resourceAwsSesConfigurationSetRead(d *schema.ResourceData, meta interface{}) error {
    53  	configurationSetExists, err := findConfigurationSet(d.Id(), nil, meta)
    54  
    55  	if !configurationSetExists {
    56  		log.Printf("[WARN] SES Configuration Set (%s) not found", d.Id())
    57  		d.SetId("")
    58  		return nil
    59  	}
    60  
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	d.Set("name", d.Id())
    66  
    67  	return nil
    68  }
    69  
    70  func resourceAwsSesConfigurationSetDelete(d *schema.ResourceData, meta interface{}) error {
    71  	conn := meta.(*AWSClient).sesConn
    72  
    73  	log.Printf("[DEBUG] SES Delete Configuration Rule Set: %s", d.Id())
    74  	_, err := conn.DeleteConfigurationSet(&ses.DeleteConfigurationSetInput{
    75  		ConfigurationSetName: aws.String(d.Id()),
    76  	})
    77  
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func findConfigurationSet(name string, token *string, meta interface{}) (bool, error) {
    86  	conn := meta.(*AWSClient).sesConn
    87  
    88  	configurationSetExists := false
    89  
    90  	listOpts := &ses.ListConfigurationSetsInput{
    91  		NextToken: token,
    92  	}
    93  
    94  	response, err := conn.ListConfigurationSets(listOpts)
    95  	for _, element := range response.ConfigurationSets {
    96  		if *element.Name == name {
    97  			configurationSetExists = true
    98  		}
    99  	}
   100  
   101  	if err != nil && !configurationSetExists && response.NextToken != nil {
   102  		configurationSetExists, err = findConfigurationSet(name, response.NextToken, meta)
   103  	}
   104  
   105  	if err != nil {
   106  		return false, err
   107  	}
   108  
   109  	return configurationSetExists, nil
   110  }