github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 // ChangeToken 65 var ct *waf.GetChangeTokenInput 66 67 res, err := conn.GetChangeToken(ct) 68 if err != nil { 69 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 70 } 71 72 params := &waf.CreateSqlInjectionMatchSetInput{ 73 ChangeToken: res.ChangeToken, 74 Name: aws.String(d.Get("name").(string)), 75 } 76 77 resp, err := conn.CreateSqlInjectionMatchSet(params) 78 79 if err != nil { 80 return errwrap.Wrapf("[ERROR] Error creating SqlInjectionMatchSet: {{err}}", err) 81 } 82 83 d.SetId(*resp.SqlInjectionMatchSet.SqlInjectionMatchSetId) 84 85 return resourceAwsWafSqlInjectionMatchSetUpdate(d, meta) 86 } 87 88 func resourceAwsWafSqlInjectionMatchSetRead(d *schema.ResourceData, meta interface{}) error { 89 conn := meta.(*AWSClient).wafconn 90 log.Printf("[INFO] Reading SqlInjectionMatchSet: %s", d.Get("name").(string)) 91 params := &waf.GetSqlInjectionMatchSetInput{ 92 SqlInjectionMatchSetId: aws.String(d.Id()), 93 } 94 95 resp, err := conn.GetSqlInjectionMatchSet(params) 96 if err != nil { 97 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" { 98 log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id()) 99 d.SetId("") 100 return nil 101 } 102 103 return err 104 } 105 106 d.Set("name", resp.SqlInjectionMatchSet.Name) 107 108 return nil 109 } 110 111 func resourceAwsWafSqlInjectionMatchSetUpdate(d *schema.ResourceData, meta interface{}) error { 112 log.Printf("[INFO] Updating SqlInjectionMatchSet: %s", d.Get("name").(string)) 113 err := updateSqlInjectionMatchSetResource(d, meta, waf.ChangeActionInsert) 114 if err != nil { 115 return errwrap.Wrapf("[ERROR] Error updating SqlInjectionMatchSet: {{err}}", err) 116 } 117 return resourceAwsWafSqlInjectionMatchSetRead(d, meta) 118 } 119 120 func resourceAwsWafSqlInjectionMatchSetDelete(d *schema.ResourceData, meta interface{}) error { 121 conn := meta.(*AWSClient).wafconn 122 123 log.Printf("[INFO] Deleting SqlInjectionMatchSet: %s", d.Get("name").(string)) 124 err := updateSqlInjectionMatchSetResource(d, meta, waf.ChangeActionDelete) 125 if err != nil { 126 return errwrap.Wrapf("[ERROR] Error deleting SqlInjectionMatchSet: {{err}}", err) 127 } 128 129 var ct *waf.GetChangeTokenInput 130 131 resp, err := conn.GetChangeToken(ct) 132 133 req := &waf.DeleteSqlInjectionMatchSetInput{ 134 ChangeToken: resp.ChangeToken, 135 SqlInjectionMatchSetId: aws.String(d.Id()), 136 } 137 138 _, err = conn.DeleteSqlInjectionMatchSet(req) 139 140 if err != nil { 141 return errwrap.Wrapf("[ERROR] Error deleting SqlInjectionMatchSet: {{err}}", err) 142 } 143 144 return nil 145 } 146 147 func updateSqlInjectionMatchSetResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error { 148 conn := meta.(*AWSClient).wafconn 149 150 var ct *waf.GetChangeTokenInput 151 152 resp, err := conn.GetChangeToken(ct) 153 if err != nil { 154 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 155 } 156 157 req := &waf.UpdateSqlInjectionMatchSetInput{ 158 ChangeToken: resp.ChangeToken, 159 SqlInjectionMatchSetId: aws.String(d.Id()), 160 } 161 162 sqlInjectionMatchTuples := d.Get("sql_injection_match_tuples").(*schema.Set) 163 for _, sqlInjectionMatchTuple := range sqlInjectionMatchTuples.List() { 164 simt := sqlInjectionMatchTuple.(map[string]interface{}) 165 sizeConstraintUpdate := &waf.SqlInjectionMatchSetUpdate{ 166 Action: aws.String(ChangeAction), 167 SqlInjectionMatchTuple: &waf.SqlInjectionMatchTuple{ 168 FieldToMatch: expandFieldToMatch(simt["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})), 169 TextTransformation: aws.String(simt["text_transformation"].(string)), 170 }, 171 } 172 req.Updates = append(req.Updates, sizeConstraintUpdate) 173 } 174 175 _, err = conn.UpdateSqlInjectionMatchSet(req) 176 if err != nil { 177 return errwrap.Wrapf("[ERROR] Error updating SqlInjectionMatchSet: {{err}}", err) 178 } 179 180 return nil 181 }