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

autoscaling: Migrate to AWS SDK v2 #36638

Merged
merged 30 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
15aa5e5
autoscaling: Migrate to AWS SDK v2
mattburgess Mar 28, 2024
eb5fd3a
Merge branch 'main' into HEAD
ewbankkit Apr 7, 2024
a6679f2
Merge branch 'main' into HEAD
ewbankkit Apr 8, 2024
efa4beb
r/aws_autoscaling_attachment: Reduce visibility.
ewbankkit Apr 8, 2024
8a2580c
r/aws_autoscaling_group_tag: Reduce visibility.
ewbankkit Apr 8, 2024
652a854
d/aws_autoscaling_groups: Reduce visibility.
ewbankkit Apr 8, 2024
cc41998
r/aws_autoscaling_group: Reduce visibility.
ewbankkit Apr 8, 2024
a07fc6a
r/aws_autoscaling_group: Reduce visibility.
ewbankkit Apr 8, 2024
9e7c3d0
Fix 'findWarmPool'.
ewbankkit Apr 8, 2024
cdcef0b
r/aws_launch_configuration: Reduce visibility.
ewbankkit Apr 9, 2024
07880a7
d/aws_launch_configuration: Reduce visibility.
ewbankkit Apr 9, 2024
e1f1b46
'autoscaling/list_pages_gen.go' not used.
ewbankkit Apr 9, 2024
c1cd557
r/aws_autoscaling_lifecycle_hook: Reduce visibility.
ewbankkit Apr 9, 2024
a734cce
autoscaling/flex: Reduce visibility.
ewbankkit Apr 9, 2024
363725d
r/aws_autoscaling_notification: Reduce visibility.
ewbankkit Apr 9, 2024
2821277
r/aws_autoscaling_policy: Reduce visibility.
ewbankkit Apr 9, 2024
6439715
r/aws_autoscaling_schedule: Reduce visibility.
ewbankkit Apr 9, 2024
864f7b0
r/aws_autoscaling_traffic_source_attachment: Reduce visibility.
ewbankkit Apr 9, 2024
3487576
autoscaling: Fixup acceptance tests.
ewbankkit Apr 9, 2024
68ae5a3
Tidy up 'expandStepAdjustments' and 'flattenStepAdjustments'.
ewbankkit Apr 9, 2024
007ce0a
autoscaling: Remove 'flex.go'.
ewbankkit Apr 9, 2024
ea5dd33
Fix golangci-lint 'unconvert'.
ewbankkit Apr 9, 2024
588866e
Fix golangci-lint 'whitespace'.
ewbankkit Apr 9, 2024
6520a1e
autoscaling: 'aws-sdk-go-base/v2/awsv1shim/v2/tfawserr' -> 'aws-sdk-g…
ewbankkit Apr 9, 2024
b1c9a4b
r/aws_autoscaling_notification: Handle 'ValidationError: Notification…
ewbankkit Apr 9, 2024
7e1a136
Fix panic in 'flattenInstanceRequirements'.
ewbankkit Apr 9, 2024
357193d
Fix 'TestAccAutoScalingGroup_withTrafficSourceELB_toTargetGroup'.
ewbankkit Apr 9, 2024
69c0753
r/aws_autoscaling_schedule: Handle 'ValidationError: Group ... not fo…
ewbankkit Apr 9, 2024
e5a1f50
r/aws_autoscaling_attachment: Handle 'ValidationError: Trying to remo…
ewbankkit Apr 9, 2024
57a6b3d
autoscaling: Fixup acceptance tests.
ewbankkit Apr 9, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.4
github.com/aws/aws-sdk-go-v2/service/athena v1.40.4
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.4
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5
github.com/aws/aws-sdk-go-v2/service/autoscalingplans v1.20.4
github.com/aws/aws-sdk-go-v2/service/batch v1.36.1
github.com/aws/aws-sdk-go-v2/service/bcmdataexports v1.3.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/aws/aws-sdk-go-v2/service/athena v1.40.4 h1:tiHIjFXSyb5DbNfnu3ql2r86s
github.com/aws/aws-sdk-go-v2/service/athena v1.40.4/go.mod h1:6OHesqDfYPNzYI+VaXtmylYLyppuUy9SwRk4CH/pQA4=
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.4 h1:45+KYpnG8ZKoqLkQSIg8hnU52rbBRyIYHqaSf+02P3I=
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.4/go.mod h1:WHURzIps29VZSUz9jxpGeShOhGuf/SqQmNJLs3Ytfns=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5 h1:vhdJymxlWS2qftzLiuCjSswjXBRLGfzo/BEE9LDveBA=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5/go.mod h1:ZErgk/bPaaZIpj+lUWGlwI1A0UFhSIscgnCPzTLnb2s=
github.com/aws/aws-sdk-go-v2/service/autoscalingplans v1.20.4 h1:LIQN+2GGZHwWksR6cVQtEF7xdLZUEsmUqy+4fStrZcE=
github.com/aws/aws-sdk-go-v2/service/autoscalingplans v1.20.4/go.mod h1:5orNWB4auLR1UJ6MUrRpAwne7uZ84Y6cq/0sB6F6LA4=
github.com/aws/aws-sdk-go-v2/service/batch v1.36.1 h1:OY9+Dt4FkK6q2VHKAB8zY4nEKOhkziVOtxCPnS94leM=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ func FlattenInt64Set(list []*int64) *schema.Set {
return schema.NewSet(schema.HashInt, FlattenInt64List(list))
}

// Takes the result of flatmap.Expand for an array of int32
// and returns a []int32
func ExpandInt32ValueList(configured []interface{}) []int32 {
return tfslices.ApplyToAll(configured, func(v any) int32 {
return int32(v.(int))
})
}

// Takes the result of flatmap.Expand for an array of int64
// and returns a []*int64
func ExpandInt64List(configured []interface{}) []*int64 {
Expand Down Expand Up @@ -358,6 +366,11 @@ func IntValueToString(v int) *string {
return aws.String(strconv.Itoa(v))
}

// Int64ToStringValue converts an int64 pointer to a Go string value.
func Int32ToStringValue(v *int32) string {
return strconv.FormatInt(int64(aws.Int32Value(v)), 10)
}

// Int64ToStringValue converts an int64 pointer to a Go string value.
func Int64ToStringValue(v *int64) string {
return strconv.FormatInt(aws.Int64Value(v), 10)
Expand Down
1 change: 1 addition & 0 deletions internal/generate/tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func main() {
SkipTypesImp: *skipTypesImp,
TfLogPkg: *updateTags,
TfResourcePkg: *getTag || *waitForPropagation || *retryTagsListTagsType != "",
TfSlicesPkg: *serviceTagsSlice && *tagTypeIDElem != "" && *tagTypeAddBoolElem != "",
TimePkg: *waitForPropagation || *retryTagsListTagsType != "",

CreateTagsFunc: createTagsFunc,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
// []*SERVICE.Tag handling

{{ if and ( .TagTypeIDElem ) ( .TagTypeAddBoolElem ) }}
// ListOfMap returns a list of {{ .ServicePackage }} in flattened map.
// listOfMap returns a list of {{ .ServicePackage }} tags in a flattened map.
//
// Compatible with setting Terraform state for strongly typed configuration blocks.
//
// This function strips tag resource identifier and type. Generally, this is
// the desired behavior so the tag schema does not require those attributes.
// Use (tftags.KeyValueTags).ListOfMap() for full tag information.
func ListOfMap(tags tftags.KeyValueTags) []any {
var result []any

for _, key := range tags.Keys() {
m := map[string]any{
func listOfMap(tags tftags.KeyValueTags) []any {
return tfslices.ApplyToAll(tags.Keys(), func (key string) any {
return map[string]any{
"key": key,
"value": aws.ToString(tags.KeyValue(key)),
{{ if .TagTypeAddBoolElem }}
{{- if .TagTypeAddBoolElem }}
"{{ .TagTypeAddBoolElemSnake }}": aws.ToBool(tags.KeyAdditionalBoolValue(key, "{{ .TagTypeAddBoolElem }}")),
{{ end }}
}

result = append(result, m)
}

return result
})
}
{{- end }}

Expand Down
57 changes: 32 additions & 25 deletions internal/service/autoscaling/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand All @@ -19,8 +20,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

// @SDKResource("aws_autoscaling_attachment")
func ResourceAttachment() *schema.Resource {
// @SDKResource("aws_autoscaling_attachment", name="Attachment")
func resourceAttachment() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAttachmentCreate,
ReadWithoutTimeout: resourceAttachmentRead,
Expand Down Expand Up @@ -50,19 +51,19 @@ func ResourceAttachment() *schema.Resource {

func resourceAttachmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AutoScalingConn(ctx)
conn := meta.(*conns.AWSClient).AutoScalingClient(ctx)
asgName := d.Get("autoscaling_group_name").(string)

if v, ok := d.GetOk("elb"); ok {
lbName := v.(string)
input := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: aws.StringSlice([]string{lbName}),
LoadBalancerNames: []string{lbName},
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, d.Timeout(schema.TimeoutCreate),
func() (interface{}, error) {
return conn.AttachLoadBalancersWithContext(ctx, input)
return conn.AttachLoadBalancers(ctx, input)
},
// ValidationError: Trying to update too many Load Balancers/Target Groups at once. The limit is 10
errCodeValidationError, "update too many")
Expand All @@ -71,19 +72,20 @@ func resourceAttachmentCreate(ctx context.Context, d *schema.ResourceData, meta
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) load balancer (%s): %s", asgName, lbName, err)
}
} else {
lbTargetGroupARN := d.Get("lb_target_group_arn").(string)
input := &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: aws.StringSlice([]string{d.Get("lb_target_group_arn").(string)}),
TargetGroupARNs: []string{lbTargetGroupARN},
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, d.Timeout(schema.TimeoutCreate),
func() (interface{}, error) {
return conn.AttachLoadBalancerTargetGroupsWithContext(ctx, input)
return conn.AttachLoadBalancerTargetGroups(ctx, input)
},
errCodeValidationError, "update too many")

if err != nil {
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) target group (%s): %s", asgName, d.Get("lb_target_group_arn").(string), err)
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) target group (%s): %s", asgName, lbTargetGroupARN, err)
}
}

Expand All @@ -95,15 +97,15 @@ func resourceAttachmentCreate(ctx context.Context, d *schema.ResourceData, meta

func resourceAttachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AutoScalingConn(ctx)
conn := meta.(*conns.AWSClient).AutoScalingClient(ctx)
asgName := d.Get("autoscaling_group_name").(string)

var err error

if v, ok := d.GetOk("elb"); ok {
err = FindAttachmentByLoadBalancerName(ctx, conn, asgName, v.(string))
err = findAttachmentByLoadBalancerName(ctx, conn, asgName, v.(string))
} else {
err = FindAttachmentByTargetGroupARN(ctx, conn, asgName, d.Get("lb_target_group_arn").(string))
err = findAttachmentByTargetGroupARN(ctx, conn, asgName, d.Get("lb_target_group_arn").(string))
}

if !d.IsNewResource() && tfresource.NotFound(err) {
Expand All @@ -121,54 +123,59 @@ func resourceAttachmentRead(ctx context.Context, d *schema.ResourceData, meta in

func resourceAttachmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AutoScalingConn(ctx)
conn := meta.(*conns.AWSClient).AutoScalingClient(ctx)
asgName := d.Get("autoscaling_group_name").(string)

if v, ok := d.GetOk("elb"); ok {
lbName := v.(string)
input := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: aws.StringSlice([]string{lbName}),
LoadBalancerNames: []string{lbName},
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, d.Timeout(schema.TimeoutCreate),
func() (interface{}, error) {
return conn.DetachLoadBalancersWithContext(ctx, input)
return conn.DetachLoadBalancers(ctx, input)
},
errCodeValidationError, "update too many")

if tfawserr.ErrMessageContains(err, errCodeValidationError, "Trying to remove Load Balancers that are not part of the group") {
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) load balancer (%s): %s", asgName, lbName, err)
}
} else {
lbTargetGroupARN := d.Get("lb_target_group_arn").(string)
input := &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: aws.StringSlice([]string{d.Get("lb_target_group_arn").(string)}),
TargetGroupARNs: []string{lbTargetGroupARN},
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, d.Timeout(schema.TimeoutCreate),
func() (interface{}, error) {
return conn.DetachLoadBalancerTargetGroupsWithContext(ctx, input)
return conn.DetachLoadBalancerTargetGroups(ctx, input)
},
errCodeValidationError, "update too many")

if err != nil {
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) target group (%s): %s", asgName, d.Get("lb_target_group_arn").(string), err)
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) target group (%s): %s", asgName, lbTargetGroupARN, err)
}
}

return diags
}

func FindAttachmentByLoadBalancerName(ctx context.Context, conn *autoscaling.AutoScaling, asgName, loadBalancerName string) error {
asg, err := FindGroupByName(ctx, conn, asgName)
func findAttachmentByLoadBalancerName(ctx context.Context, conn *autoscaling.Client, asgName, loadBalancerName string) error {
asg, err := findGroupByName(ctx, conn, asgName)

if err != nil {
return err
}

for _, v := range asg.LoadBalancerNames {
if aws.StringValue(v) == loadBalancerName {
if v == loadBalancerName {
return nil
}
}
Expand All @@ -178,15 +185,15 @@ func FindAttachmentByLoadBalancerName(ctx context.Context, conn *autoscaling.Aut
}
}

func FindAttachmentByTargetGroupARN(ctx context.Context, conn *autoscaling.AutoScaling, asgName, targetGroupARN string) error {
asg, err := FindGroupByName(ctx, conn, asgName)
func findAttachmentByTargetGroupARN(ctx context.Context, conn *autoscaling.Client, asgName, targetGroupARN string) error {
asg, err := findGroupByName(ctx, conn, asgName)

if err != nil {
return err
}

for _, v := range asg.TargetGroupARNs {
if aws.StringValue(v) == targetGroupARN {
if v == targetGroupARN {
return nil
}
}
Expand Down
29 changes: 26 additions & 3 deletions internal/service/autoscaling/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,32 @@ func TestAccAutoScalingAttachment_multipleALBTargetGroups(t *testing.T) {
})
}

func TestAccAutoScalingAttachment_disappears(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_autoscaling_attachment.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.AutoScalingServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAttachmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAttachmentConfig_elb(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAttachmentByLoadBalancerNameExists(ctx, resourceName),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfautoscaling.ResourceAttachment(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAttachmentDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_autoscaling_attachment" {
Expand Down Expand Up @@ -159,7 +182,7 @@ func testAccCheckAttachmentByLoadBalancerNameExists(ctx context.Context, n strin
return fmt.Errorf("Not found: %s", n)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingClient(ctx)

return tfautoscaling.FindAttachmentByLoadBalancerName(ctx, conn, rs.Primary.Attributes["autoscaling_group_name"], rs.Primary.Attributes["elb"])
}
Expand All @@ -172,7 +195,7 @@ func testAccCheckAttachmentByTargetGroupARNExists(ctx context.Context, n string)
return fmt.Errorf("Not found: %s", n)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingClient(ctx)

return tfautoscaling.FindAttachmentByTargetGroupARN(ctx, conn, rs.Primary.Attributes["autoscaling_group_name"], rs.Primary.Attributes["lb_target_group_arn"])
}
Expand Down
Loading
Loading