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

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/service/waf"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsWafByteMatchSet() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsWafByteMatchSetCreate,
    16  		Read:   resourceAwsWafByteMatchSetRead,
    17  		Update: resourceAwsWafByteMatchSetUpdate,
    18  		Delete: resourceAwsWafByteMatchSetDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  			"byte_match_tuples": &schema.Schema{
    27  				Type:     schema.TypeSet,
    28  				Optional: true,
    29  				Elem: &schema.Resource{
    30  					Schema: map[string]*schema.Schema{
    31  						"field_to_match": {
    32  							Type:     schema.TypeSet,
    33  							Required: true,
    34  							MaxItems: 1,
    35  							Elem: &schema.Resource{
    36  								Schema: map[string]*schema.Schema{
    37  									"data": {
    38  										Type:     schema.TypeString,
    39  										Optional: true,
    40  									},
    41  									"type": {
    42  										Type:     schema.TypeString,
    43  										Required: true,
    44  									},
    45  								},
    46  							},
    47  						},
    48  						"positional_constraint": &schema.Schema{
    49  							Type:     schema.TypeString,
    50  							Required: true,
    51  						},
    52  						"target_string": &schema.Schema{
    53  							Type:     schema.TypeString,
    54  							Optional: true,
    55  						},
    56  						"text_transformation": &schema.Schema{
    57  							Type:     schema.TypeString,
    58  							Required: true,
    59  						},
    60  					},
    61  				},
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  func resourceAwsWafByteMatchSetCreate(d *schema.ResourceData, meta interface{}) error {
    68  	conn := meta.(*AWSClient).wafconn
    69  
    70  	log.Printf("[INFO] Creating ByteMatchSet: %s", d.Get("name").(string))
    71  
    72  	wr := newWafRetryer(conn, "global")
    73  	out, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
    74  		params := &waf.CreateByteMatchSetInput{
    75  			ChangeToken: token,
    76  			Name:        aws.String(d.Get("name").(string)),
    77  		}
    78  		return conn.CreateByteMatchSet(params)
    79  	})
    80  	if err != nil {
    81  		return errwrap.Wrapf("[ERROR] Error creating ByteMatchSet: {{err}}", err)
    82  	}
    83  	resp := out.(*waf.CreateByteMatchSetOutput)
    84  
    85  	d.SetId(*resp.ByteMatchSet.ByteMatchSetId)
    86  
    87  	return resourceAwsWafByteMatchSetUpdate(d, meta)
    88  }
    89  
    90  func resourceAwsWafByteMatchSetRead(d *schema.ResourceData, meta interface{}) error {
    91  	conn := meta.(*AWSClient).wafconn
    92  	log.Printf("[INFO] Reading ByteMatchSet: %s", d.Get("name").(string))
    93  	params := &waf.GetByteMatchSetInput{
    94  		ByteMatchSetId: aws.String(d.Id()),
    95  	}
    96  
    97  	resp, err := conn.GetByteMatchSet(params)
    98  	if err != nil {
    99  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
   100  			log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id())
   101  			d.SetId("")
   102  			return nil
   103  		}
   104  
   105  		return err
   106  	}
   107  
   108  	d.Set("name", resp.ByteMatchSet.Name)
   109  
   110  	return nil
   111  }
   112  
   113  func resourceAwsWafByteMatchSetUpdate(d *schema.ResourceData, meta interface{}) error {
   114  	log.Printf("[INFO] Updating ByteMatchSet: %s", d.Get("name").(string))
   115  	err := updateByteMatchSetResource(d, meta, waf.ChangeActionInsert)
   116  	if err != nil {
   117  		return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err)
   118  	}
   119  	return resourceAwsWafByteMatchSetRead(d, meta)
   120  }
   121  
   122  func resourceAwsWafByteMatchSetDelete(d *schema.ResourceData, meta interface{}) error {
   123  	conn := meta.(*AWSClient).wafconn
   124  
   125  	log.Printf("[INFO] Deleting ByteMatchSet: %s", d.Get("name").(string))
   126  	err := updateByteMatchSetResource(d, meta, waf.ChangeActionDelete)
   127  	if err != nil {
   128  		return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err)
   129  	}
   130  
   131  	wr := newWafRetryer(conn, "global")
   132  	_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
   133  		req := &waf.DeleteByteMatchSetInput{
   134  			ChangeToken:    token,
   135  			ByteMatchSetId: aws.String(d.Id()),
   136  		}
   137  		return conn.DeleteByteMatchSet(req)
   138  	})
   139  	if err != nil {
   140  		return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err)
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  func updateByteMatchSetResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error {
   147  	conn := meta.(*AWSClient).wafconn
   148  
   149  	wr := newWafRetryer(conn, "global")
   150  	_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
   151  		req := &waf.UpdateByteMatchSetInput{
   152  			ChangeToken:    token,
   153  			ByteMatchSetId: aws.String(d.Id()),
   154  		}
   155  
   156  		ByteMatchTuples := d.Get("byte_match_tuples").(*schema.Set)
   157  		for _, ByteMatchTuple := range ByteMatchTuples.List() {
   158  			ByteMatch := ByteMatchTuple.(map[string]interface{})
   159  			ByteMatchUpdate := &waf.ByteMatchSetUpdate{
   160  				Action: aws.String(ChangeAction),
   161  				ByteMatchTuple: &waf.ByteMatchTuple{
   162  					FieldToMatch:         expandFieldToMatch(ByteMatch["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})),
   163  					PositionalConstraint: aws.String(ByteMatch["positional_constraint"].(string)),
   164  					TargetString:         []byte(ByteMatch["target_string"].(string)),
   165  					TextTransformation:   aws.String(ByteMatch["text_transformation"].(string)),
   166  				},
   167  			}
   168  			req.Updates = append(req.Updates, ByteMatchUpdate)
   169  		}
   170  
   171  		return conn.UpdateByteMatchSet(req)
   172  	})
   173  	if err != nil {
   174  		return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err)
   175  	}
   176  
   177  	return nil
   178  }
   179  
   180  func expandFieldToMatch(d map[string]interface{}) *waf.FieldToMatch {
   181  	return &waf.FieldToMatch{
   182  		Type: aws.String(d["type"].(string)),
   183  		Data: aws.String(d["data"].(string)),
   184  	}
   185  }
   186  
   187  func flattenFieldToMatch(fm *waf.FieldToMatch) map[string]interface{} {
   188  	m := make(map[string]interface{})
   189  	m["data"] = *fm.Data
   190  	m["type"] = *fm.Type
   191  	return m
   192  }