Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/waf_byte_match_set: update typeset with maxitems of 1 to typelist #13599

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions aws/resource_aws_waf_byte_match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -32,7 +32,7 @@ func resourceAwsWafByteMatchSet() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field_to_match": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -44,6 +44,15 @@ func resourceAwsWafByteMatchSet() *schema.Resource {
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
waf.MatchFieldTypeUri,
waf.MatchFieldTypeQueryString,
waf.MatchFieldTypeHeader,
waf.MatchFieldTypeMethod,
waf.MatchFieldTypeBody,
waf.MatchFieldTypeSingleQueryArg,
waf.MatchFieldTypeAllQueryArgs,
}, false),
},
},
},
Expand Down Expand Up @@ -85,7 +94,7 @@ func resourceAwsWafByteMatchSetCreate(d *schema.ResourceData, meta interface{})
}
resp := out.(*waf.CreateByteMatchSetOutput)

d.SetId(*resp.ByteMatchSet.ByteMatchSetId)
d.SetId(aws.StringValue(resp.ByteMatchSet.ByteMatchSetId))

return resourceAwsWafByteMatchSetUpdate(d, meta)
}
Expand All @@ -99,7 +108,7 @@ func resourceAwsWafByteMatchSetRead(d *schema.ResourceData, meta interface{}) er

resp, err := conn.GetByteMatchSet(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF IPSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand Down Expand Up @@ -185,9 +194,9 @@ func flattenWafByteMatchTuples(bmt []*waf.ByteMatchTuple) []interface{} {
if t.FieldToMatch != nil {
m["field_to_match"] = flattenFieldToMatch(t.FieldToMatch)
}
m["positional_constraint"] = *t.PositionalConstraint
m["positional_constraint"] = aws.StringValue(t.PositionalConstraint)
m["target_string"] = string(t.TargetString)
m["text_transformation"] = *t.TextTransformation
m["text_transformation"] = aws.StringValue(t.TextTransformation)

out[i] = m
}
Expand All @@ -208,7 +217,7 @@ func diffWafByteMatchSetTuples(oldT, newT []interface{}) []*waf.ByteMatchSetUpda
updates = append(updates, &waf.ByteMatchSetUpdate{
Action: aws.String(waf.ChangeActionDelete),
ByteMatchTuple: &waf.ByteMatchTuple{
FieldToMatch: expandFieldToMatch(tuple["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})),
FieldToMatch: expandFieldToMatch(tuple["field_to_match"].([]interface{})[0].(map[string]interface{})),
PositionalConstraint: aws.String(tuple["positional_constraint"].(string)),
TargetString: []byte(tuple["target_string"].(string)),
TextTransformation: aws.String(tuple["text_transformation"].(string)),
Expand All @@ -222,7 +231,7 @@ func diffWafByteMatchSetTuples(oldT, newT []interface{}) []*waf.ByteMatchSetUpda
updates = append(updates, &waf.ByteMatchSetUpdate{
Action: aws.String(waf.ChangeActionInsert),
ByteMatchTuple: &waf.ByteMatchTuple{
FieldToMatch: expandFieldToMatch(tuple["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})),
FieldToMatch: expandFieldToMatch(tuple["field_to_match"].([]interface{})[0].(map[string]interface{})),
PositionalConstraint: aws.String(tuple["positional_constraint"].(string)),
TargetString: []byte(tuple["target_string"].(string)),
TextTransformation: aws.String(tuple["text_transformation"].(string)),
Expand Down
74 changes: 37 additions & 37 deletions aws/resource_aws_waf_byte_match_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ func TestAccAWSWafByteMatchSet_basic(t *testing.T) {
testAccCheckAWSWafByteMatchSetExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "name", byteMatchSet),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.#", "2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.2991901334.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.2991901334.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.target_string", "badrefer2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.0.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.0.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.target_string", "badrefer2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.text_transformation", "NONE"),
),
},
{
Expand Down Expand Up @@ -106,18 +106,18 @@ func TestAccAWSWafByteMatchSet_changeTuples(t *testing.T) {
testAccCheckAWSWafByteMatchSetExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "name", byteMatchSetName),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.#", "2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.2991901334.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.field_to_match.2991901334.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.target_string", "badrefer2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.839525137.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.0.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.field_to_match.0.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.target_string", "badrefer2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2081155357.text_transformation", "NONE"),
),
},
{
Expand All @@ -126,18 +126,18 @@ func TestAccAWSWafByteMatchSet_changeTuples(t *testing.T) {
testAccCheckAWSWafByteMatchSetExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", byteMatchSetName),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.#", "2"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.2174619346.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.field_to_match.4253810390.data", "GET"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.field_to_match.4253810390.type", "METHOD"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.positional_constraint", "CONTAINS_WORD"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.target_string", "blah"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.4224486115.text_transformation", "URL_DECODE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.data", "referer"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.field_to_match.0.type", "HEADER"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.positional_constraint", "CONTAINS"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.target_string", "badrefer1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3483354334.text_transformation", "NONE"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.field_to_match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.field_to_match.0.data", ""),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.field_to_match.0.type", "METHOD"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.positional_constraint", "CONTAINS_WORD"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.target_string", "blah"),
resource.TestCheckResourceAttr(resourceName, "byte_match_tuples.3945246213.text_transformation", "URL_DECODE"),
),
},
{
Expand Down Expand Up @@ -386,7 +386,7 @@ resource "aws_waf_byte_match_set" "byte_set" {

field_to_match {
type = "METHOD"
data = "GET"
# data field omitted as the type is neither "HEADER" nor "SINGLE_QUERY_ARG"
}
}
}
Expand Down