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

New data source aws_lb_listener_rule #39865

Merged
merged 24 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
379906d
Delays regex compilation until actually needed
gdavison Oct 21, 2024
210aad4
Uses value of `arn` attribute instead of `id`
gdavison Oct 21, 2024
46122d7
Uses `arn` for Listener Rule transparent tagging
gdavison Oct 21, 2024
2a93c91
Registers AutoFlex logger for data sources
gdavison Oct 21, 2024
f1eb865
Adds data source `aws_lb_listener_rule`
gdavison Oct 22, 2024
21c89b8
Factors out Condition known value check
gdavison Oct 22, 2024
b3d5190
Adds lookup by listener ARN and priority
gdavison Oct 22, 2024
d8f249a
Adds tests for Condition types
gdavison Oct 22, 2024
49fea4c
Adds missing attributes to resource `authentication_cognito` test
gdavison Oct 22, 2024
e0536da
Factors out action sorting and replaces `sort.Slices` with `slices.So…
gdavison Oct 22, 2024
29d84cc
Adds tests for authenticate actions
gdavison Oct 22, 2024
28c5e81
Adds tests for remaining actions
gdavison Oct 23, 2024
441e6e1
Removes top-level `target_group_arn` from `action` in favour of `forw…
gdavison Oct 23, 2024
64daf22
Updates root-level validation
gdavison Oct 23, 2024
ff1768d
Grammar fix
gdavison Oct 23, 2024
028927a
Converts `priority` attribute from string to int32
gdavison Oct 23, 2024
92dfe94
Documents `aws_lb_listener_rule` data source
gdavison Oct 23, 2024
f2e53de
Adds tagging tests
gdavison Oct 23, 2024
7b059ad
`skaff`: Separates provider service package and sdk service package
gdavison Oct 23, 2024
86034f2
`skaff`: Uses pre-calculated resource name
gdavison Oct 23, 2024
2681cc7
Fixed constants
gdavison Oct 23, 2024
887e0fe
Adds CHANGELOG entry
gdavison Oct 23, 2024
e406d25
Documentation fixes
gdavison Oct 23, 2024
1223c8a
Replaces generated `describeRulesPages` with `NewDescribeRulesPaginator`
gdavison Oct 23, 2024
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
3 changes: 3 additions & 0 deletions .changelog/39865.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_lb_listener_rule
```
9 changes: 9 additions & 0 deletions internal/provider/fwprovider/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ func (w *wrappedDataSource) Configure(ctx context.Context, request datasource.Co
w.inner.Configure(ctx, request, response)
}

func (w *wrappedDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
if v, ok := w.inner.(datasource.DataSourceWithConfigValidators); ok {
ctx = w.bootstrapContext(ctx, w.meta)
return v.ConfigValidators(ctx)
}

return nil
}

// tagsDataSourceInterceptor implements transparent tagging for data sources.
type tagsDataSourceInterceptor struct {
tags *types.ServicePackageResourceTags
Expand Down
1 change: 1 addition & 0 deletions internal/provider/fwprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func (p *fwprovider) DataSources(ctx context.Context) []func() datasource.DataSo
if meta != nil {
ctx = tftags.NewContext(ctx, meta.DefaultTagsConfig(ctx), meta.IgnoreTagsConfig(ctx))
ctx = meta.RegisterLogger(ctx)
ctx = flex.RegisterLogger(ctx)
}

return ctx
Expand Down
1 change: 0 additions & 1 deletion internal/service/elbv2/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/listpages/main.go -ListOps=DescribeListenerCertificates -InputPaginator=Marker -OutputPaginator=NextMarker -- list_listener_certificates_pages_gen.go
//go:generate go run ../../generate/listpages/main.go -ListOps=DescribeRules -InputPaginator=Marker -OutputPaginator=NextMarker -- list_rules_pages_gen.go
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=DescribeTags -ListTagsInIDElem=ResourceArns -ListTagsInIDNeedValueSlice=yes -ListTagsOutTagsElem=TagDescriptions[0].Tags -ServiceTagsSlice -TagOp=AddTags -TagInIDElem=ResourceArns -TagInIDNeedValueSlice=yes -UntagOp=RemoveTags -UpdateTags -CreateTags -KVTValues
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
Expand Down
27 changes: 0 additions & 27 deletions internal/service/elbv2/list_rules_pages_gen.go

This file was deleted.

14 changes: 10 additions & 4 deletions internal/service/elbv2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package elbv2

import (
"cmp"
"context"
"fmt"
"log"
"slices"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -572,9 +572,9 @@ func resourceListenerRead(ctx context.Context, d *schema.ResourceData, meta inte
if len(listener.Certificates) == 1 {
d.Set(names.AttrCertificateARN, listener.Certificates[0].CertificateArn)
}
sort.Slice(listener.DefaultActions, func(i, j int) bool {
return aws.ToInt32(listener.DefaultActions[i].Order) < aws.ToInt32(listener.DefaultActions[j].Order)
})

sortListenerActions(listener.DefaultActions)

if err := d.Set(names.AttrDefaultAction, flattenListenerActions(d, names.AttrDefaultAction, listener.DefaultActions)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting default_action: %s", err)
}
Expand Down Expand Up @@ -1689,3 +1689,9 @@ func diffSuppressMissingForward(attrName string) schema.SchemaDiffSuppressFunc {
return false
}
}

func sortListenerActions(actions []awstypes.Action) {
slices.SortFunc(actions, func(a, b awstypes.Action) int {
return cmp.Compare(aws.ToInt32(a.Order), aws.ToInt32(b.Order))
})
}
7 changes: 3 additions & 4 deletions internal/service/elbv2/listener_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package elbv2

import (
"context"
"sort"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -334,9 +333,9 @@ func dataSourceListenerRead(ctx context.Context, d *schema.ResourceData, meta in
if len(listener.Certificates) == 1 {
d.Set(names.AttrCertificateARN, listener.Certificates[0].CertificateArn)
}
sort.Slice(listener.DefaultActions, func(i, j int) bool {
return aws.ToInt32(listener.DefaultActions[i].Order) < aws.ToInt32(listener.DefaultActions[j].Order)
})

sortListenerActions(listener.DefaultActions)

if err := d.Set(names.AttrDefaultAction, flattenListenerActions(d, names.AttrDefaultAction, listener.DefaultActions)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting default_action: %s", err)
}
Expand Down
61 changes: 32 additions & 29 deletions internal/service/elbv2/listener_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"log"
"slices"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -47,7 +46,7 @@ const (

// @SDKResource("aws_alb_listener_rule", name="Listener Rule")
// @SDKResource("aws_lb_listener_rule", name="Listener Rule")
// @Tags(identifierAttribute="id")
// @Tags(identifierAttribute="arn")
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types;awstypes;awstypes.Rule")
// @Testing(importIgnore="action.0.forward")
func resourceListenerRule() *schema.Resource {
Expand Down Expand Up @@ -578,9 +577,8 @@ func resourceListenerRuleRead(ctx context.Context, d *schema.ResourceData, meta
}
}

sort.Slice(rule.Actions, func(i, j int) bool {
return aws.ToInt32(rule.Actions[i].Order) < aws.ToInt32(rule.Actions[j].Order)
})
sortListenerActions(rule.Actions)

if err := d.Set(names.AttrAction, flattenListenerActions(d, names.AttrAction, rule.Actions)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting action: %s", err)
}
Expand Down Expand Up @@ -765,29 +763,24 @@ func findListenerRule(ctx context.Context, conn *elasticloadbalancingv2.Client,
func findListenerRules(ctx context.Context, conn *elasticloadbalancingv2.Client, input *elasticloadbalancingv2.DescribeRulesInput, filter tfslices.Predicate[*awstypes.Rule]) ([]awstypes.Rule, error) {
var output []awstypes.Rule

err := describeRulesPages(ctx, conn, input, func(page *elasticloadbalancingv2.DescribeRulesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
paginator := elasticloadbalancingv2.NewDescribeRulesPaginator(conn, input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if errs.IsA[*awstypes.RuleNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}

for _, v := range page.Rules {
if filter(&v) {
output = append(output, v)
}
}

return !lastPage
})

if errs.IsA[*awstypes.RuleNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
Expand All @@ -801,6 +794,16 @@ func findListenerRuleByARN(ctx context.Context, conn *elasticloadbalancingv2.Cli
return findListenerRule(ctx, conn, input, tfslices.PredicateTrue[*awstypes.Rule]())
}

func findListenerRuleByListenerAndPriority(ctx context.Context, conn *elasticloadbalancingv2.Client, listenerARN, priority string) (*awstypes.Rule, error) {
input := &elasticloadbalancingv2.DescribeRulesInput{
ListenerArn: aws.String(listenerARN),
}

return findListenerRule(ctx, conn, input, func(v *awstypes.Rule) bool {
return aws.ToString(v.Priority) == priority
})
}

func highestListenerRulePriority(ctx context.Context, conn *elasticloadbalancingv2.Client, arn string) (int32, error) {
input := &elasticloadbalancingv2.DescribeRulesInput{
ListenerArn: aws.String(arn),
Expand Down Expand Up @@ -832,15 +835,15 @@ func validListenerRulePriority(v interface{}, k string) (ws []string, errors []e
return
}

// from arn:
// arn:aws:elasticloadbalancing:us-east-1:012345678912:listener-rule/app/name/0123456789abcdef/abcdef0123456789/456789abcedf1234
// select submatches:
// (arn:aws:elasticloadbalancing:us-east-1:012345678912:listener)-rule(/app/name/0123456789abcdef/abcdef0123456789)/456789abcedf1234
// concat to become:
// arn:aws:elasticloadbalancing:us-east-1:012345678912:listener/app/name/0123456789abcdef/abcdef0123456789
var lbListenerARNFromRuleARNRegexp = regexache.MustCompile(`^(arn:.+:listener)-rule(/.+)/[^/]+$`)

func listenerARNFromRuleARN(ruleARN string) string {
// from arn:
// arn:aws:elasticloadbalancing:us-east-1:012345678912:listener-rule/app/name/0123456789abcdef/abcdef0123456789/456789abcedf1234
// select submatches:
// (arn:aws:elasticloadbalancing:us-east-1:012345678912:listener)-rule(/app/name/0123456789abcdef/abcdef0123456789)/456789abcedf1234
// concat to become:
// arn:aws:elasticloadbalancing:us-east-1:012345678912:listener/app/name/0123456789abcdef/abcdef0123456789
var lbListenerARNFromRuleARNRegexp = regexache.MustCompile(`^(arn:.+:listener)-rule(/.+)/[^/]+$`)

if arnComponents := lbListenerARNFromRuleARNRegexp.FindStringSubmatch(ruleARN); len(arnComponents) > 1 {
return arnComponents[1] + arnComponents[2]
}
Expand Down
Loading
Loading