github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_waf_sql_injection_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 resourceAwsWafSqlInjectionMatchSet() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsWafSqlInjectionMatchSetCreate, 16 Read: resourceAwsWafSqlInjectionMatchSetRead, 17 Update: resourceAwsWafSqlInjectionMatchSetUpdate, 18 Delete: resourceAwsWafSqlInjectionMatchSetDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 "sql_injection_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 "text_transformation": &schema.Schema{ 49 Type: schema.TypeString, 50 Required: true, 51 }, 52 }, 53 }, 54 }, 55 }, 56 } 57 } 58 59 func resourceAwsWafSqlInjectionMatchSetCreate(d *schema.ResourceData, meta interface{}) error { 60 conn := meta.(*AWSClient).wafconn 61 62 log.Printf("[INFO] Creating SqlInjectionMatchSet: %s", d.Get("name").(string)) 63 64 wr := newWafRetryer(conn, "global") 65 out, err := wr.RetryWithToken(func(token *string) (interface{}, error) { 66 params := &waf.CreateSqlInjectionMatchSetInput{ 67 ChangeToken: token, 68 Name: aws.String(d.Get("name").(string)), 69 } 70 71 return conn.CreateSqlInjectionMatchSet(params) 72 }) 73 if err != nil { 74 return errwrap.Wrapf("[ERROR] Error creating SqlInjectionMatchSet: {{err}}", err) 75 } 76 resp := out.(*waf.CreateSqlInjectionMatchSetOutput) 77 d.SetId(*resp.SqlInjectionMatchSet.SqlInjectionMatchSetId) 78 79 return resourceAwsWafSqlInjectionMatchSetUpdate(d, meta) 80 } 81 82 func resourceAwsWafSqlInjectionMatchSetRead(d *schema.ResourceData, meta interface{}) error { 83 conn := meta.(*AWSClient).wafconn 84 log.Printf("[INFO] Reading SqlInjectionMatchSet: %s", d.Get("name").(string)) 85 params := &waf.GetSqlInjectionMatchSetInput{ 86 SqlInjectionMatchSetId: aws.String(d.Id()), 87 } 88 89 resp, err := conn.GetSqlInjectionMatchSet(params) 90 if err != nil { 91 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" { 92 log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id()) 93 d.SetId("") 94 return nil 95 } 96 97 return err 98 } 99 100 d.Set("name", resp.SqlInjectionMatchSet.Name) 101 102 return nil 103 } 104 105 func resourceAwsWafSqlInjectionMatchSetUpdate(d *schema.ResourceData, meta interface{}) error { 106 log.Printf("[INFO] Updating SqlInjectionMatchSet: %s", d.Get("name").(string)) 107 err := updateSqlInjectionMatchSetResource(d, meta, waf.ChangeActionInsert) 108 if err != nil { 109 return errwrap.Wrapf("[ERROR] Error updating SqlInjectionMatchSet: {{err}}", err) 110 } 111 return resourceAwsWafSqlInjectionMatchSetRead(d, meta) 112 } 113 114 func resourceAwsWafSqlInjectionMatchSetDelete(d *schema.ResourceData, meta interface{}) error { 115 conn := meta.(*AWSClient).wafconn 116 117 log.Printf("[INFO] Deleting SqlInjectionMatchSet: %s", d.Get("name").(string)) 118 err := updateSqlInjectionMatchSetResource(d, meta, waf.ChangeActionDelete) 119 if err != nil { 120 return errwrap.Wrapf("[ERROR] Error deleting SqlInjectionMatchSet: {{err}}", err) 121 } 122 123 wr := newWafRetryer(conn, "global") 124 _, err = wr.RetryWithToken(func(token *string) (interface{}, error) { 125 req := &waf.DeleteSqlInjectionMatchSetInput{ 126 ChangeToken: token, 127 SqlInjectionMatchSetId: aws.String(d.Id()), 128 } 129 130 return conn.DeleteSqlInjectionMatchSet(req) 131 }) 132 if err != nil { 133 return errwrap.Wrapf("[ERROR] Error deleting SqlInjectionMatchSet: {{err}}", err) 134 } 135 136 return nil 137 } 138 139 func updateSqlInjectionMatchSetResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error { 140 conn := meta.(*AWSClient).wafconn 141 142 wr := newWafRetryer(conn, "global") 143 _, err := wr.RetryWithToken(func(token *string) (interface{}, error) { 144 req := &waf.UpdateSqlInjectionMatchSetInput{ 145 ChangeToken: token, 146 SqlInjectionMatchSetId: aws.String(d.Id()), 147 } 148 149 sqlInjectionMatchTuples := d.Get("sql_injection_match_tuples").(*schema.Set) 150 for _, sqlInjectionMatchTuple := range sqlInjectionMatchTuples.List() { 151 simt := sqlInjectionMatchTuple.(map[string]interface{}) 152 sizeConstraintUpdate := &waf.SqlInjectionMatchSetUpdate{ 153 Action: aws.String(ChangeAction), 154 SqlInjectionMatchTuple: &waf.SqlInjectionMatchTuple{ 155 FieldToMatch: expandFieldToMatch(simt["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})), 156 TextTransformation: aws.String(simt["text_transformation"].(string)), 157 }, 158 } 159 req.Updates = append(req.Updates, sizeConstraintUpdate) 160 } 161 162 return conn.UpdateSqlInjectionMatchSet(req) 163 }) 164 if err != nil { 165 return errwrap.Wrapf("[ERROR] Error updating SqlInjectionMatchSet: {{err}}", err) 166 } 167 168 return nil 169 }