diff --git a/.go-version b/.go-version index 87b26e8b1aa..49e0a31d496 100644 --- a/.go-version +++ b/.go-version @@ -1 +1 @@ -1.22.7 +1.23.1 diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 9b79e8687b0..6765fcb0f58 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -8,6 +8,7 @@ import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports "errors" "fmt" "log" + "slices" "strconv" "strings" "time" @@ -34,7 +35,6 @@ import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" "github.com/hashicorp/terraform-provider-aws/internal/sdkv2/types/nullable" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" @@ -1504,7 +1504,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter // API only supports adding or removing 10 at a time. batchSize := 10 - for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) { + for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) { input := &autoscaling.DetachTrafficSourcesInput{ AutoScalingGroupName: aws.String(d.Id()), TrafficSources: chunk, @@ -1521,7 +1521,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter } } - for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) { + for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) { input := &autoscaling.AttachTrafficSourcesInput{ AutoScalingGroupName: aws.String(d.Id()), TrafficSources: chunk, @@ -1545,7 +1545,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter // API only supports adding or removing 10 at a time. batchSize := 10 - for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) { + for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) { input := &autoscaling.DetachLoadBalancersInput{ AutoScalingGroupName: aws.String(d.Id()), LoadBalancerNames: chunk, @@ -1562,7 +1562,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter } } - for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) { + for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) { input := &autoscaling.AttachLoadBalancersInput{ AutoScalingGroupName: aws.String(d.Id()), LoadBalancerNames: chunk, @@ -1586,7 +1586,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter // API only supports adding or removing 10 at a time. batchSize := 10 - for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) { + for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) { input := &autoscaling.DetachLoadBalancerTargetGroupsInput{ AutoScalingGroupName: aws.String(d.Id()), TargetGroupARNs: chunk, @@ -1603,7 +1603,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter } } - for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) { + for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) { input := &autoscaling.AttachLoadBalancerTargetGroupsInput{ AutoScalingGroupName: aws.String(d.Id()), TargetGroupARNs: chunk, @@ -1864,7 +1864,7 @@ func drainGroup(ctx context.Context, conn *autoscaling.Client, name string, inst } } const batchSize = 50 // API limit. - for _, chunk := range tfslices.Chunks(instanceIDs, batchSize) { + for chunk := range slices.Chunk(instanceIDs, batchSize) { input := &autoscaling.SetInstanceProtectionInput{ AutoScalingGroupName: aws.String(name), InstanceIds: chunk, diff --git a/internal/service/connect/routing_profile.go b/internal/service/connect/routing_profile.go index 1ed8dd8413d..791bd07429e 100644 --- a/internal/service/connect/routing_profile.go +++ b/internal/service/connect/routing_profile.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "slices" "strings" "github.com/aws/aws-sdk-go-v2/aws" @@ -20,7 +21,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -361,8 +361,7 @@ func updateRoutingProfileQueueAssociations(ctx context.Context, conn *connect.Cl // the respective queues based on the diff detected // disassociate first since Queue and channel type combination cannot be duplicated - chunks := tfslices.Chunks(del, routingProfileQueueAssociationChunkSize) - for _, chunk := range chunks { + for chunk := range slices.Chunk(del, routingProfileQueueAssociationChunkSize) { var queueReferences []awstypes.RoutingProfileQueueReference for _, v := range chunk { if v := v.QueueReference; v != nil { @@ -385,8 +384,7 @@ func updateRoutingProfileQueueAssociations(ctx context.Context, conn *connect.Cl } } - chunks = tfslices.Chunks(add, routingProfileQueueAssociationChunkSize) - for _, chunk := range chunks { + for chunk := range slices.Chunk(add, routingProfileQueueAssociationChunkSize) { input := &connect.AssociateRoutingProfileQueuesInput{ InstanceId: aws.String(instanceID), QueueConfigs: chunk, diff --git a/internal/service/docdb/cluster_parameter_group.go b/internal/service/docdb/cluster_parameter_group.go index d530b2a85c2..910dec1a1bb 100644 --- a/internal/service/docdb/cluster_parameter_group.go +++ b/internal/service/docdb/cluster_parameter_group.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "reflect" + "slices" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -21,7 +22,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -226,7 +226,7 @@ func modifyClusterParameterGroupParameters(ctx context.Context, conn *docdb.Clie clusterParameterGroupMaxParamsBulkEdit = 20 ) // We can only modify 20 parameters at a time, so chunk them until we've got them all. - for _, chunk := range tfslices.Chunks(parameters, clusterParameterGroupMaxParamsBulkEdit) { + for chunk := range slices.Chunk(parameters, clusterParameterGroupMaxParamsBulkEdit) { input := &docdb.ModifyDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(name), Parameters: chunk, diff --git a/internal/service/kafka/scram_secret_association.go b/internal/service/kafka/scram_secret_association.go index 183e5017647..51d96cad139 100644 --- a/internal/service/kafka/scram_secret_association.go +++ b/internal/service/kafka/scram_secret_association.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "log" + "slices" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/kafka" @@ -19,7 +20,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) @@ -163,7 +163,7 @@ func findSCRAMSecretsByClusterARN(ctx context.Context, conn *kafka.Client, clust } func associateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN string, secretARNs []string) error { - for _, chunk := range tfslices.Chunks(secretARNs, scramSecretBatchSize) { + for chunk := range slices.Chunk(secretARNs, scramSecretBatchSize) { input := &kafka.BatchAssociateScramSecretInput{ ClusterArn: aws.String(clusterARN), SecretArnList: chunk, @@ -184,7 +184,7 @@ func associateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN st } func disassociateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN string, secretARNs []string) error { - for _, chunk := range tfslices.Chunks(secretARNs, scramSecretBatchSize) { + for chunk := range slices.Chunk(secretARNs, scramSecretBatchSize) { input := &kafka.BatchDisassociateScramSecretInput{ ClusterArn: aws.String(clusterARN), SecretArnList: chunk, diff --git a/internal/service/lakeformation/lf_tag.go b/internal/service/lakeformation/lf_tag.go index c083d1e9cac..507d271e1bd 100644 --- a/internal/service/lakeformation/lf_tag.go +++ b/internal/service/lakeformation/lf_tag.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "slices" "strings" "github.com/YakDriver/regexache" @@ -20,7 +21,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -83,7 +83,7 @@ func resourceLFTagCreate(ctx context.Context, d *schema.ResourceData, meta inter id := lfTagCreateResourceID(catalogID, tagKey) i := 0 - for _, chunk := range tfslices.Chunks(tagValues.List(), lfTagsValuesMaxBatchSize) { + for chunk := range slices.Chunk(tagValues.List(), lfTagsValuesMaxBatchSize) { if i == 0 { input := &lakeformation.CreateLFTagInput{ CatalogId: aws.String(catalogID), @@ -169,11 +169,11 @@ func resourceLFTagUpdate(ctx context.Context, d *schema.ResourceData, meta inter var toAddChunks, toDeleteChunks [][]interface{} if len(toAdd.List()) > 0 { - toAddChunks = tfslices.Chunks(toAdd.List(), lfTagsValuesMaxBatchSize) + toAddChunks = slices.Collect(slices.Chunk(toAdd.List(), lfTagsValuesMaxBatchSize)) } if len(toDelete.List()) > 0 { - toDeleteChunks = tfslices.Chunks(toDelete.List(), lfTagsValuesMaxBatchSize) + toDeleteChunks = slices.Collect(slices.Chunk(toDelete.List(), lfTagsValuesMaxBatchSize)) } for { diff --git a/internal/service/neptune/cluster_parameter_group.go b/internal/service/neptune/cluster_parameter_group.go index aa63ef7b8a8..b31cd660ef4 100644 --- a/internal/service/neptune/cluster_parameter_group.go +++ b/internal/service/neptune/cluster_parameter_group.go @@ -242,7 +242,7 @@ func modifyClusterParameterGroupParameters(ctx context.Context, conn *neptune.Cl clusterParameterGroupMaxParamsBulkEdit = 20 ) // We can only modify 20 parameters at a time, so chunk them until we've got them all. - for _, chunk := range tfslices.Chunks(parameters, clusterParameterGroupMaxParamsBulkEdit) { + for chunk := range slices.Chunk(parameters, clusterParameterGroupMaxParamsBulkEdit) { input := &neptune.ModifyDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(name), Parameters: chunk, diff --git a/internal/service/neptune/parameter_group.go b/internal/service/neptune/parameter_group.go index 0c8e05589a2..df81d7c11bb 100644 --- a/internal/service/neptune/parameter_group.go +++ b/internal/service/neptune/parameter_group.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "slices" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -20,7 +21,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -226,7 +226,7 @@ func resourceParameterGroupDelete(ctx context.Context, d *schema.ResourceData, m } func addDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, name string, parameters []awstypes.Parameter) error { // We can only modify 20 parameters at a time, so chunk them until we've got them all. - for _, chunk := range tfslices.Chunks(parameters, dbParameterGroupMaxParamsBulkEdit) { + for chunk := range slices.Chunk(parameters, dbParameterGroupMaxParamsBulkEdit) { input := &neptune.ModifyDBParameterGroupInput{ DBParameterGroupName: aws.String(name), Parameters: chunk, @@ -243,7 +243,7 @@ func addDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, na } func delDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, name string, parameters []awstypes.Parameter) error { // We can only modify 20 parameters at a time, so chunk them until we've got them all. - for _, chunk := range tfslices.Chunks(parameters, dbParameterGroupMaxParamsBulkEdit) { + for chunk := range slices.Chunk(parameters, dbParameterGroupMaxParamsBulkEdit) { input := &neptune.ResetDBParameterGroupInput{ DBParameterGroupName: aws.String(name), Parameters: chunk, diff --git a/internal/service/rds/cluster_parameter_group.go b/internal/service/rds/cluster_parameter_group.go index ccfdc6bfcdc..28a4a0c6ae0 100644 --- a/internal/service/rds/cluster_parameter_group.go +++ b/internal/service/rds/cluster_parameter_group.go @@ -208,7 +208,7 @@ func resourceClusterParameterGroupUpdate(ctx context.Context, d *schema.Resource o, n := d.GetChange(names.AttrParameter) os, ns := o.(*schema.Set), n.(*schema.Set) - for _, chunk := range tfslices.Chunks(expandParameters(ns.Difference(os).List()), maxParamModifyChunk) { + for chunk := range slices.Chunk(expandParameters(ns.Difference(os).List()), maxParamModifyChunk) { input := &rds.ModifyDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(d.Id()), Parameters: chunk, @@ -236,7 +236,7 @@ func resourceClusterParameterGroupUpdate(ctx context.Context, d *schema.Resource } // Reset parameters that have been removed. - for _, chunk := range tfslices.Chunks(maps.Values(toRemove), maxParamModifyChunk) { + for chunk := range slices.Chunk(maps.Values(toRemove), maxParamModifyChunk) { input := &rds.ResetDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(d.Id()), Parameters: chunk, diff --git a/internal/service/route53/zone.go b/internal/service/route53/zone.go index 15edc9f2afb..940e3bf78db 100644 --- a/internal/service/route53/zone.go +++ b/internal/service/route53/zone.go @@ -383,8 +383,7 @@ func deleteAllResourceRecordsFromHostedZone(ctx context.Context, conn *route53.C const ( chunkSize = 100 ) - chunks := tfslices.Chunks(resourceRecordSets, chunkSize) - for _, chunk := range chunks { + for chunk := range slices.Chunk(resourceRecordSets, chunkSize) { changes := tfslices.ApplyToAll(chunk, func(v awstypes.ResourceRecordSet) awstypes.Change { return awstypes.Change{ Action: awstypes.ChangeActionDelete, diff --git a/internal/service/ssm/document.go b/internal/service/ssm/document.go index e6ed152e142..ef17345d0e3 100644 --- a/internal/service/ssm/document.go +++ b/internal/service/ssm/document.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "regexp" + "slices" "strings" "time" @@ -27,7 +28,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/flex" - tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" itypes "github.com/hashicorp/terraform-provider-aws/internal/types" @@ -290,9 +290,7 @@ func resourceDocumentCreate(ctx context.Context, d *schema.ResourceData, meta in tfMap := flex.ExpandStringValueMap(v.(map[string]interface{})) if v, ok := tfMap["account_ids"]; ok && v != "" { - chunks := tfslices.Chunks(strings.Split(v, ","), documentPermissionsBatchLimit) - - for _, chunk := range chunks { + for chunk := range slices.Chunk(strings.Split(v, ","), documentPermissionsBatchLimit) { input := &ssm.ModifyDocumentPermissionInput{ AccountIdsToAdd: chunk, Name: aws.String(d.Id()), @@ -426,7 +424,7 @@ func resourceDocumentUpdate(ctx context.Context, d *schema.ResourceData, meta in } } - for _, chunk := range tfslices.Chunks(newAccountIDs.Difference(oldAccountIDs), documentPermissionsBatchLimit) { + for chunk := range slices.Chunk(newAccountIDs.Difference(oldAccountIDs), documentPermissionsBatchLimit) { input := &ssm.ModifyDocumentPermissionInput{ AccountIdsToAdd: chunk, Name: aws.String(d.Id()), @@ -440,7 +438,7 @@ func resourceDocumentUpdate(ctx context.Context, d *schema.ResourceData, meta in } } - for _, chunk := range tfslices.Chunks(oldAccountIDs.Difference(newAccountIDs), documentPermissionsBatchLimit) { + for chunk := range slices.Chunk(oldAccountIDs.Difference(newAccountIDs), documentPermissionsBatchLimit) { input := &ssm.ModifyDocumentPermissionInput{ AccountIdsToRemove: chunk, Name: aws.String(d.Id()), @@ -517,9 +515,7 @@ func resourceDocumentDelete(ctx context.Context, d *schema.ResourceData, meta in tfMap := flex.ExpandStringValueMap(v.(map[string]interface{})) if v, ok := tfMap["account_ids"]; ok && v != "" { - chunks := tfslices.Chunks(strings.Split(v, ","), documentPermissionsBatchLimit) - - for _, chunk := range chunks { + for chunk := range slices.Chunk(strings.Split(v, ","), documentPermissionsBatchLimit) { input := &ssm.ModifyDocumentPermissionInput{ AccountIdsToRemove: chunk, Name: aws.String(d.Id()), diff --git a/internal/slices/slices.go b/internal/slices/slices.go index dccfa9f3814..f35b30007b3 100644 --- a/internal/slices/slices.go +++ b/internal/slices/slices.go @@ -100,23 +100,6 @@ func Any[S ~[]E, E any](s S, f Predicate[E]) bool { return false } -// Chunks returns a slice of S, each of the specified size (or less). -func Chunks[S ~[]E, E any](s S, size int) []S { - chunks := make([]S, 0) - - for i := 0; i < len(s); i += size { - end := i + size - - if end > len(s) { - end = len(s) - } - - chunks = append(chunks, s[i:end]) - } - - return chunks -} - // AppendUnique appends unique (not already in the slice) values to a slice. func AppendUnique[S ~[]E, E comparable](s S, vs ...E) S { for _, v := range vs { diff --git a/internal/slices/slices_test.go b/internal/slices/slices_test.go index 35ea97b2621..0537d29f2cf 100644 --- a/internal/slices/slices_test.go +++ b/internal/slices/slices_test.go @@ -153,45 +153,6 @@ func TestApplyToAll(t *testing.T) { } } -func TestChunk(t *testing.T) { - t.Parallel() - - type testCase struct { - input []string - expected [][]string - } - tests := map[string]testCase{ - "three elements": { - input: []string{"one", "two", "3"}, - expected: [][]string{{"one", "two"}, {"3"}}, - }, - "two elements": { - input: []string{"aa", "bb"}, - expected: [][]string{{"aa", "bb"}}, - }, - "one element": { - input: []string{"1"}, - expected: [][]string{{"1"}}, - }, - "zero elements": { - input: []string{}, - expected: [][]string{}, - }, - } - - for name, test := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - - got := Chunks(test.input, 2) - - if diff := cmp.Diff(got, test.expected); diff != "" { - t.Errorf("unexpected diff (+wanted, -got): %s", diff) - } - }) - } -} - func TestFilter(t *testing.T) { t.Parallel()