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

add custom diff for LBListener forward options #1528

Merged
Merged
Changes from 2 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
69 changes: 67 additions & 2 deletions config/elbv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

package elbv2

import "github.com/crossplane/upjet/pkg/config"
import (
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/crossplane/upjet/pkg/config"
)

// Configure adds configurations for the elbv2 group.
func Configure(p *config.Provider) {
func Configure(p *config.Provider) { //nolint:gocyclo
p.AddResourceConfigurator("aws_lb", func(r *config.Resource) {
r.ExternalName.OmittedFields = append(r.ExternalName.OmittedFields, "name_prefix")
r.References = config.References{
Expand Down Expand Up @@ -44,6 +51,64 @@ func Configure(p *config.Provider) {
TerraformName: "aws_lb_target_group",
},
}

// lb_listener schema allows to configure "default_action" with type
// "forward", in 2 different ways.
// 1. you can specify, default_action.0.forward, which allows configuring
// multiple target groups.
// 2. you can specify default_action.0.target_group_arn if you want
// to configure only one target group.
// Former is a more general way, and latter is more of a shortcut for
// a specific case, which can already be expressed with #1.
// TF implementation instructs to specify either #1 or #2, not both.
// However, they both end up in the state redundantly and cause
// unnecessary diff.
r.TerraformCustomDiff = func(diff *terraform.InstanceDiff, _ *terraform.InstanceState, _ *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { //nolint:gocyclo
// skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}
duplicatedAction := ""
for k, attrDiff := range diff.Attributes {
// user specified the "default_action.0.target_group_arn" and
// "default_action.0.forward" is not specified in config.
// In that case, default_action.0.forward is populated
// by the AWS API, which can cause an unnecessary diff,
// trying to remove that "auto-populated" element.
if regexp.MustCompile(`^default_action\.\d+\.forward\.#$`).MatchString(k) &&
attrDiff.New == "0" && attrDiff.Old == "1" {
delete(diff.Attributes, k)
// save this attribute path to remove remaining diffs
// of its nested fields if any in a second pass
duplicatedAction = strings.TrimSuffix(k, ".#")
}
// this is the same case as above
if regexp.MustCompile(`^default_action\.\d+\.forward\.\d+\.target_group\.#$`).MatchString(k) &&
attrDiff.New == "0" && attrDiff.Old == "1" {
delete(diff.Attributes, k)
}
// this is the same case but vice versa. user specified
// forward target via default_action.0.forward, and
// default_action.0.target_group_arn is omitted.
// In that case, default_action.0.target_group_arn is populated
// by the AWS API and ends up in the state, causing
// an unnecessary diff, which we omit here.
if regexp.MustCompile(`^default_action\.\d+\.target_group_arn$`).MatchString(k) &&
attrDiff.New == "" && attrDiff.Old != "" && attrDiff.NewRemoved {
delete(diff.Attributes, k)
}
}
// if we have caught an unnecessary diff for default_action.0.forward, remove
// any sub-element diffs of it.
if duplicatedAction != "" {
for k := range diff.Attributes {
if strings.HasPrefix(k, duplicatedAction) {
delete(diff.Attributes, k)
}
}
}
return diff, nil
}
})

p.AddResourceConfigurator("aws_lb_target_group", func(r *config.Resource) {
Expand Down
Loading