Skip to content

Commit

Permalink
Removes unneeded DiffSupressFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Jan 31, 2024
1 parent bb99e7d commit 24c51eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 79 deletions.
57 changes: 18 additions & 39 deletions internal/service/elbv2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ func ResourceListener() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authenticate_cognito": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumAuthenticateCognito),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authentication_request_extra_params": {
Expand Down Expand Up @@ -124,10 +123,9 @@ func ResourceListener() *schema.Resource {
},
},
"authenticate_oidc": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumAuthenticateOidc),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authentication_request_extra_params": {
Expand Down Expand Up @@ -185,10 +183,9 @@ func ResourceListener() *schema.Resource {
},
},
"fixed_response": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumFixedResponse),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content_type": {
Expand Down Expand Up @@ -217,10 +214,9 @@ func ResourceListener() *schema.Resource {
},
},
"forward": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumForward),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_group": {
Expand Down Expand Up @@ -274,10 +270,9 @@ func ResourceListener() *schema.Resource {
ValidateFunc: validation.IntBetween(listenerActionOrderMin, listenerActionOrderMax),
},
"redirect": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumRedirect),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Expand Down Expand Up @@ -322,10 +317,9 @@ func ResourceListener() *schema.Resource {
},
},
"target_group_arn": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot(awstypes.ActionTypeEnumForward),
ValidateFunc: verify.ValidARN,
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidARN,
},
"type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -397,21 +391,6 @@ func ResourceListener() *schema.Resource {
}
}

func suppressIfDefaultActionTypeNot(t awstypes.ActionTypeEnum) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
take := 2
i := strings.IndexFunc(k, func(r rune) bool {
if r == '.' {
take -= 1
return take == 0
}
return false
})
at := k[:i+1] + "type"
return awstypes.ActionTypeEnum(d.Get(at).(string)) != t
}
}

func resourceListenerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ELBV2Client(ctx)
Expand Down
58 changes: 18 additions & 40 deletions internal/service/elbv2/listener_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log"
"sort"
"strconv"
"strings"
"time"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -93,17 +92,15 @@ func ResourceListenerRule() *schema.Resource {
},

"target_group_arn": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumForward),
ValidateFunc: verify.ValidARN,
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidARN,
},

"forward": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumForward),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_group": {
Expand Down Expand Up @@ -152,10 +149,9 @@ func ResourceListenerRule() *schema.Resource {
},

"redirect": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumRedirect),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Expand Down Expand Up @@ -206,10 +202,9 @@ func ResourceListenerRule() *schema.Resource {
},

"fixed_response": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumFixedResponse),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content_type": {
Expand Down Expand Up @@ -241,10 +236,9 @@ func ResourceListenerRule() *schema.Resource {
},

"authenticate_cognito": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumAuthenticateCognito),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authentication_request_extra_params": {
Expand Down Expand Up @@ -291,10 +285,9 @@ func ResourceListenerRule() *schema.Resource {
},

"authenticate_oidc": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfActionTypeNot(awstypes.ActionTypeEnumAuthenticateOidc),
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authentication_request_extra_params": {
Expand Down Expand Up @@ -486,21 +479,6 @@ func ResourceListenerRule() *schema.Resource {
}
}

func suppressIfActionTypeNot(t awstypes.ActionTypeEnum) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
take := 2
i := strings.IndexFunc(k, func(r rune) bool {
if r == '.' {
take -= 1
return take == 0
}
return false
})
at := k[:i+1] + "type"
return awstypes.ActionTypeEnum(d.Get(at).(string)) != t
}
}

func resourceListenerRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ELBV2Client(ctx)
Expand Down

0 comments on commit 24c51eb

Please sign in to comment.