github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_ses_receipt_rule_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 resourceAwsSesReceiptRuleSet() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsSesReceiptRuleSetCreate,
    15  		Read:   resourceAwsSesReceiptRuleSetRead,
    16  		Delete: resourceAwsSesReceiptRuleSetDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"rule_set_name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  		},
    28  	}
    29  }
    30  
    31  func resourceAwsSesReceiptRuleSetCreate(d *schema.ResourceData, meta interface{}) error {
    32  	conn := meta.(*AWSClient).sesConn
    33  
    34  	ruleSetName := d.Get("rule_set_name").(string)
    35  
    36  	createOpts := &ses.CreateReceiptRuleSetInput{
    37  		RuleSetName: aws.String(ruleSetName),
    38  	}
    39  
    40  	_, err := conn.CreateReceiptRuleSet(createOpts)
    41  	if err != nil {
    42  		return fmt.Errorf("Error creating SES rule set: %s", err)
    43  	}
    44  
    45  	d.SetId(ruleSetName)
    46  
    47  	return resourceAwsSesReceiptRuleSetRead(d, meta)
    48  }
    49  
    50  func resourceAwsSesReceiptRuleSetRead(d *schema.ResourceData, meta interface{}) error {
    51  	ruleSetExists, err := findRuleSet(d.Id(), nil, meta)
    52  
    53  	if !ruleSetExists {
    54  		log.Printf("[WARN] SES Receipt Rule Set (%s) not found", d.Id())
    55  		d.SetId("")
    56  		return nil
    57  	}
    58  
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	d.Set("rule_set_name", d.Id())
    64  
    65  	return nil
    66  }
    67  
    68  func resourceAwsSesReceiptRuleSetDelete(d *schema.ResourceData, meta interface{}) error {
    69  	conn := meta.(*AWSClient).sesConn
    70  
    71  	log.Printf("[DEBUG] SES Delete Receipt Rule Set: %s", d.Id())
    72  	_, err := conn.DeleteReceiptRuleSet(&ses.DeleteReceiptRuleSetInput{
    73  		RuleSetName: aws.String(d.Id()),
    74  	})
    75  
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func findRuleSet(name string, token *string, meta interface{}) (bool, error) {
    84  	conn := meta.(*AWSClient).sesConn
    85  
    86  	ruleSetExists := false
    87  
    88  	listOpts := &ses.ListReceiptRuleSetsInput{
    89  		NextToken: token,
    90  	}
    91  
    92  	response, err := conn.ListReceiptRuleSets(listOpts)
    93  	for _, element := range response.RuleSets {
    94  		if *element.Name == name {
    95  			ruleSetExists = true
    96  		}
    97  	}
    98  
    99  	if err != nil && !ruleSetExists && response.NextToken != nil {
   100  		ruleSetExists, err = findRuleSet(name, response.NextToken, meta)
   101  	}
   102  
   103  	if err != nil {
   104  		return false, err
   105  	}
   106  
   107  	return ruleSetExists, nil
   108  }