Skip to content

Commit

Permalink
Merge pull request #5902 from terraform-providers/b-aws_wafregional_b…
Browse files Browse the repository at this point in the history
…yte_match_set-schema-error

resource/aws_wafregional_byte_match_set: Properly read byte_match_tuple into Terraform state
  • Loading branch information
bflad authored Sep 18, 2018
2 parents 70908a6 + 19c2279 commit ba915e0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 92 deletions.
65 changes: 33 additions & 32 deletions aws/resource_aws_wafregional_byte_match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"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/aws/aws-sdk-go/service/wafregional"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -32,7 +31,7 @@ func resourceAwsWafRegionalByteMatchSet() *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 Down Expand Up @@ -69,7 +68,7 @@ func resourceAwsWafRegionalByteMatchSet() *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 Down Expand Up @@ -139,51 +138,53 @@ func resourceAwsWafRegionalByteMatchSetRead(d *schema.ResourceData, meta interfa
}

resp, err := conn.GetByteMatchSet(params)

if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF Regional Byte Set Match (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
log.Printf("[WARN] WAF IPSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error getting WAF Regional Byte Match Set (%s): %s", d.Id(), err)
}

return err
if resp == nil || resp.ByteMatchSet == nil {
log.Printf("[WARN] WAF Regional Byte Set Match (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if _, ok := d.GetOk("byte_match_tuple"); ok {
d.Set("byte_match_tuple", flattenWafByteMatchTuplesWR(resp.ByteMatchSet.ByteMatchTuples))
if err := d.Set("byte_match_tuple", flattenWafByteMatchTuplesWR(resp.ByteMatchSet.ByteMatchTuples)); err != nil {
return fmt.Errorf("error setting byte_match_tuple: %s", err)
}
} else {
d.Set("byte_match_tuples", flattenWafByteMatchTuplesWR(resp.ByteMatchSet.ByteMatchTuples))
if err := d.Set("byte_match_tuples", flattenWafByteMatchTuplesWR(resp.ByteMatchSet.ByteMatchTuples)); err != nil {
return fmt.Errorf("error setting byte_match_tuples: %s", err)
}
}
d.Set("name", resp.ByteMatchSet.Name)

return nil
}

func flattenWafByteMatchTuplesWR(in []*waf.ByteMatchTuple) []interface{} {
tuples := make([]interface{}, len(in), len(in))
tuples := make([]interface{}, len(in))

for i, tuple := range in {
field_to_match := tuple.FieldToMatch
m := map[string]interface{}{
"type": *field_to_match.Type,
}

if field_to_match.Data == nil {
m["data"] = ""
} else {
m["data"] = *field_to_match.Data
fieldToMatchMap := map[string]interface{}{
"data": aws.StringValue(tuple.FieldToMatch.Data),
"type": aws.StringValue(tuple.FieldToMatch.Type),
}

var ms []map[string]interface{}
ms = append(ms, m)

tuple := map[string]interface{}{
"field_to_match": ms,
"positional_constraint": *tuple.PositionalConstraint,
"target_string": tuple.TargetString,
"text_transformation": *tuple.TextTransformation,
m := map[string]interface{}{
"field_to_match": []map[string]interface{}{fieldToMatchMap},
"positional_constraint": aws.StringValue(tuple.PositionalConstraint),
"target_string": string(tuple.TargetString),
"text_transformation": aws.StringValue(tuple.TextTransformation),
}
tuples[i] = tuple
tuples[i] = m
}

return tuples
Expand Down Expand Up @@ -283,7 +284,7 @@ func diffByteMatchSetTuple(oldT, newT []interface{}) []*waf.ByteMatchSetUpdate {
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 @@ -297,7 +298,7 @@ func diffByteMatchSetTuple(oldT, newT []interface{}) []*waf.ByteMatchSetUpdate {
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
Loading

0 comments on commit ba915e0

Please sign in to comment.