github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ses_event_destination.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 resourceAwsSesEventDestination() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsSesEventDestinationCreate,
    15  		Read:   resourceAwsSesEventDestinationRead,
    16  		Delete: resourceAwsSesEventDestinationDelete,
    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  			"configuration_set_name": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"enabled": &schema.Schema{
    35  				Type:     schema.TypeBool,
    36  				Optional: true,
    37  				Default:  false,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"matching_types": &schema.Schema{
    42  				Type:     schema.TypeSet,
    43  				Required: true,
    44  				ForceNew: true,
    45  				Set:      schema.HashString,
    46  				Elem: &schema.Schema{
    47  					Type:         schema.TypeString,
    48  					ValidateFunc: validateMatchingTypes,
    49  				},
    50  			},
    51  
    52  			"cloudwatch_destination": {
    53  				Type:          schema.TypeSet,
    54  				Optional:      true,
    55  				ForceNew:      true,
    56  				ConflictsWith: []string{"kinesis_destination"},
    57  				Elem: &schema.Resource{
    58  					Schema: map[string]*schema.Schema{
    59  						"default_value": &schema.Schema{
    60  							Type:     schema.TypeString,
    61  							Required: true,
    62  						},
    63  
    64  						"dimension_name": &schema.Schema{
    65  							Type:     schema.TypeString,
    66  							Required: true,
    67  						},
    68  
    69  						"value_source": &schema.Schema{
    70  							Type:         schema.TypeString,
    71  							Required:     true,
    72  							ValidateFunc: validateDimensionValueSource,
    73  						},
    74  					},
    75  				},
    76  			},
    77  
    78  			"kinesis_destination": {
    79  				Type:          schema.TypeSet,
    80  				Optional:      true,
    81  				ForceNew:      true,
    82  				ConflictsWith: []string{"cloudwatch_destination"},
    83  				Elem: &schema.Resource{
    84  					Schema: map[string]*schema.Schema{
    85  						"stream_arn": &schema.Schema{
    86  							Type:     schema.TypeString,
    87  							Required: true,
    88  						},
    89  
    90  						"role_arn": &schema.Schema{
    91  							Type:     schema.TypeString,
    92  							Required: true,
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  }
   100  
   101  func resourceAwsSesEventDestinationCreate(d *schema.ResourceData, meta interface{}) error {
   102  	conn := meta.(*AWSClient).sesConn
   103  
   104  	configurationSetName := d.Get("configuration_set_name").(string)
   105  	eventDestinationName := d.Get("name").(string)
   106  	enabled := d.Get("enabled").(bool)
   107  	matchingEventTypes := d.Get("matching_types").(*schema.Set).List()
   108  
   109  	createOpts := &ses.CreateConfigurationSetEventDestinationInput{
   110  		ConfigurationSetName: aws.String(configurationSetName),
   111  		EventDestination: &ses.EventDestination{
   112  			Name:               aws.String(eventDestinationName),
   113  			Enabled:            aws.Bool(enabled),
   114  			MatchingEventTypes: expandStringList(matchingEventTypes),
   115  		},
   116  	}
   117  
   118  	if v, ok := d.GetOk("cloudwatch_destination"); ok {
   119  		destination := v.(*schema.Set).List()
   120  		createOpts.EventDestination.CloudWatchDestination = &ses.CloudWatchDestination{
   121  			DimensionConfigurations: generateCloudWatchDestination(destination),
   122  		}
   123  		log.Printf("[DEBUG] Creating cloudwatch destination: %#v", destination)
   124  	}
   125  
   126  	if v, ok := d.GetOk("kinesis_destination"); ok {
   127  		destination := v.(*schema.Set).List()
   128  		if len(destination) > 1 {
   129  			return fmt.Errorf("You can only define a single kinesis destination per record")
   130  		}
   131  		kinesis := destination[0].(map[string]interface{})
   132  		createOpts.EventDestination.KinesisFirehoseDestination = &ses.KinesisFirehoseDestination{
   133  			DeliveryStreamARN: aws.String(kinesis["stream_arn"].(string)),
   134  			IAMRoleARN:        aws.String(kinesis["role_arn"].(string)),
   135  		}
   136  		log.Printf("[DEBUG] Creating kinesis destination: %#v", kinesis)
   137  	}
   138  
   139  	_, err := conn.CreateConfigurationSetEventDestination(createOpts)
   140  	if err != nil {
   141  		return fmt.Errorf("Error creating SES configuration set event destination: %s", err)
   142  	}
   143  
   144  	d.SetId(eventDestinationName)
   145  
   146  	log.Printf("[WARN] SES DONE")
   147  	return resourceAwsSesEventDestinationRead(d, meta)
   148  }
   149  
   150  func resourceAwsSesEventDestinationRead(d *schema.ResourceData, meta interface{}) error {
   151  
   152  	return nil
   153  }
   154  
   155  func resourceAwsSesEventDestinationDelete(d *schema.ResourceData, meta interface{}) error {
   156  	conn := meta.(*AWSClient).sesConn
   157  
   158  	log.Printf("[DEBUG] SES Delete Configuration Set Destination: %s", d.Id())
   159  	_, err := conn.DeleteConfigurationSetEventDestination(&ses.DeleteConfigurationSetEventDestinationInput{
   160  		ConfigurationSetName: aws.String(d.Get("configuration_set_name").(string)),
   161  		EventDestinationName: aws.String(d.Id()),
   162  	})
   163  
   164  	if err != nil {
   165  		return err
   166  	}
   167  
   168  	return nil
   169  }
   170  
   171  func validateMatchingTypes(v interface{}, k string) (ws []string, errors []error) {
   172  	value := v.(string)
   173  	matchingTypes := map[string]bool{
   174  		"send":      true,
   175  		"reject":    true,
   176  		"bounce":    true,
   177  		"complaint": true,
   178  		"delivery":  true,
   179  	}
   180  
   181  	if !matchingTypes[value] {
   182  		errors = append(errors, fmt.Errorf("%q must be a valid matching event type value: %q", k, value))
   183  	}
   184  	return
   185  }
   186  
   187  func validateDimensionValueSource(v interface{}, k string) (ws []string, errors []error) {
   188  	value := v.(string)
   189  	matchingSource := map[string]bool{
   190  		"messageTag":  true,
   191  		"emailHeader": true,
   192  	}
   193  
   194  	if !matchingSource[value] {
   195  		errors = append(errors, fmt.Errorf("%q must be a valid dimension value: %q", k, value))
   196  	}
   197  	return
   198  }
   199  
   200  func generateCloudWatchDestination(v []interface{}) []*ses.CloudWatchDimensionConfiguration {
   201  
   202  	b := make([]*ses.CloudWatchDimensionConfiguration, len(v))
   203  
   204  	for i, vI := range v {
   205  		cloudwatch := vI.(map[string]interface{})
   206  		b[i] = &ses.CloudWatchDimensionConfiguration{
   207  			DefaultDimensionValue: aws.String(cloudwatch["default_value"].(string)),
   208  			DimensionName:         aws.String(cloudwatch["dimension_name"].(string)),
   209  			DimensionValueSource:  aws.String(cloudwatch["value_source"].(string)),
   210  		}
   211  	}
   212  
   213  	return b
   214  }