github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 // ChangeToken 73 var ct *waf.GetChangeTokenInput 74 75 res, err := conn.GetChangeToken(ct) 76 if err != nil { 77 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 78 } 79 80 params := &waf.CreateByteMatchSetInput{ 81 ChangeToken: res.ChangeToken, 82 Name: aws.String(d.Get("name").(string)), 83 } 84 85 resp, err := conn.CreateByteMatchSet(params) 86 87 if err != nil { 88 return errwrap.Wrapf("[ERROR] Error creating ByteMatchSet: {{err}}", err) 89 } 90 91 d.SetId(*resp.ByteMatchSet.ByteMatchSetId) 92 93 return resourceAwsWafByteMatchSetUpdate(d, meta) 94 } 95 96 func resourceAwsWafByteMatchSetRead(d *schema.ResourceData, meta interface{}) error { 97 conn := meta.(*AWSClient).wafconn 98 log.Printf("[INFO] Reading ByteMatchSet: %s", d.Get("name").(string)) 99 params := &waf.GetByteMatchSetInput{ 100 ByteMatchSetId: aws.String(d.Id()), 101 } 102 103 resp, err := conn.GetByteMatchSet(params) 104 if err != nil { 105 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" { 106 log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id()) 107 d.SetId("") 108 return nil 109 } 110 111 return err 112 } 113 114 d.Set("name", resp.ByteMatchSet.Name) 115 116 return nil 117 } 118 119 func resourceAwsWafByteMatchSetUpdate(d *schema.ResourceData, meta interface{}) error { 120 log.Printf("[INFO] Updating ByteMatchSet: %s", d.Get("name").(string)) 121 err := updateByteMatchSetResource(d, meta, waf.ChangeActionInsert) 122 if err != nil { 123 return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err) 124 } 125 return resourceAwsWafByteMatchSetRead(d, meta) 126 } 127 128 func resourceAwsWafByteMatchSetDelete(d *schema.ResourceData, meta interface{}) error { 129 conn := meta.(*AWSClient).wafconn 130 131 log.Printf("[INFO] Deleting ByteMatchSet: %s", d.Get("name").(string)) 132 err := updateByteMatchSetResource(d, meta, waf.ChangeActionDelete) 133 if err != nil { 134 return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err) 135 } 136 137 var ct *waf.GetChangeTokenInput 138 139 resp, err := conn.GetChangeToken(ct) 140 141 req := &waf.DeleteByteMatchSetInput{ 142 ChangeToken: resp.ChangeToken, 143 ByteMatchSetId: aws.String(d.Id()), 144 } 145 146 _, err = conn.DeleteByteMatchSet(req) 147 148 if err != nil { 149 return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err) 150 } 151 152 return nil 153 } 154 155 func updateByteMatchSetResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error { 156 conn := meta.(*AWSClient).wafconn 157 158 var ct *waf.GetChangeTokenInput 159 160 resp, err := conn.GetChangeToken(ct) 161 if err != nil { 162 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 163 } 164 165 req := &waf.UpdateByteMatchSetInput{ 166 ChangeToken: resp.ChangeToken, 167 ByteMatchSetId: aws.String(d.Id()), 168 } 169 170 ByteMatchTuples := d.Get("byte_match_tuples").(*schema.Set) 171 for _, ByteMatchTuple := range ByteMatchTuples.List() { 172 ByteMatch := ByteMatchTuple.(map[string]interface{}) 173 ByteMatchUpdate := &waf.ByteMatchSetUpdate{ 174 Action: aws.String(ChangeAction), 175 ByteMatchTuple: &waf.ByteMatchTuple{ 176 FieldToMatch: expandFieldToMatch(ByteMatch["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})), 177 PositionalConstraint: aws.String(ByteMatch["positional_constraint"].(string)), 178 TargetString: []byte(ByteMatch["target_string"].(string)), 179 TextTransformation: aws.String(ByteMatch["text_transformation"].(string)), 180 }, 181 } 182 req.Updates = append(req.Updates, ByteMatchUpdate) 183 } 184 185 _, err = conn.UpdateByteMatchSet(req) 186 if err != nil { 187 return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err) 188 } 189 190 return nil 191 } 192 193 func expandFieldToMatch(d map[string]interface{}) *waf.FieldToMatch { 194 return &waf.FieldToMatch{ 195 Type: aws.String(d["type"].(string)), 196 Data: aws.String(d["data"].(string)), 197 } 198 } 199 200 func flattenFieldToMatch(fm *waf.FieldToMatch) map[string]interface{} { 201 m := make(map[string]interface{}) 202 m["data"] = *fm.Data 203 m["type"] = *fm.Type 204 return m 205 }