Skip to content

Commit

Permalink
Merge pull request #39144 from hashicorp/td-slices.Chunk
Browse files Browse the repository at this point in the history
Use Go 1.23 `slices.Chunk` as a replacement for `internal/slices.Chunks`
  • Loading branch information
ewbankkit authored Sep 5, 2024
2 parents 33b29bd + e5c24d5 commit 367b75c
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 183 deletions.
101 changes: 39 additions & 62 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports
"errors"
"fmt"
"log"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -959,15 +959,15 @@ func resourceGroup() *schema.Resource {

func instanceMaintenancePolicyDiffSupress(k, old, new string, d *schema.ResourceData) bool {
o, n := d.GetChange("instance_maintenance_policy")
oList := o.([]interface{})
nList := n.([]interface{})
oList, nList := o.([]interface{}), n.([]interface{})

if len(oList) == 0 && len(nList) != 0 {
tfMap := nList[0].(map[string]interface{})
if int64(tfMap["min_healthy_percentage"].(int)) == -1 || int64(tfMap["max_healthy_percentage"].(int)) == -1 {
return true
}
}

return false
}

Expand Down Expand Up @@ -1500,22 +1500,17 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("traffic_source") {
o, n := d.GetChange("traffic_source")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
os, ns := o.(*schema.Set), n.(*schema.Set)

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) {
_, err := conn.DetachTrafficSources(ctx, &autoscaling.DetachTrafficSourcesInput{
for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) {
input := &autoscaling.DetachTrafficSourcesInput{
AutoScalingGroupName: aws.String(d.Id()),
TrafficSources: chunk,
})
}

_, err := conn.DetachTrafficSources(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) traffic sources: %s", d.Id(), err)
Expand All @@ -1526,11 +1521,13 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) {
_, err := conn.AttachTrafficSources(ctx, &autoscaling.AttachTrafficSourcesInput{
for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) {
input := &autoscaling.AttachTrafficSourcesInput{
AutoScalingGroupName: aws.String(d.Id()),
TrafficSources: chunk,
})
}

_, err := conn.AttachTrafficSources(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) traffic sources: %s", d.Id(), err)
Expand All @@ -1544,22 +1541,17 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("load_balancers") {
o, n := d.GetChange("load_balancers")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
os, ns := o.(*schema.Set), n.(*schema.Set)

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
_, err := conn.DetachLoadBalancers(ctx, &autoscaling.DetachLoadBalancersInput{
for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
input := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(d.Id()),
LoadBalancerNames: chunk,
})
}

_, err := conn.DetachLoadBalancers(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) load balancers: %s", d.Id(), err)
Expand All @@ -1570,11 +1562,13 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
_, err := conn.AttachLoadBalancers(ctx, &autoscaling.AttachLoadBalancersInput{
for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
input := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(d.Id()),
LoadBalancerNames: chunk,
})
}

_, err := conn.AttachLoadBalancers(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) load balancers: %s", d.Id(), err)
Expand All @@ -1588,22 +1582,17 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("target_group_arns") {
o, n := d.GetChange("target_group_arns")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
os, ns := o.(*schema.Set), n.(*schema.Set)

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
_, err := conn.DetachLoadBalancerTargetGroups(ctx, &autoscaling.DetachLoadBalancerTargetGroupsInput{
for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
input := &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: chunk,
})
}

_, err := conn.DetachLoadBalancerTargetGroups(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "detaching Auto Scaling Group (%s) target groups: %s", d.Id(), err)
Expand All @@ -1614,11 +1603,13 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
_, err := conn.AttachLoadBalancerTargetGroups(ctx, &autoscaling.AttachLoadBalancerTargetGroupsInput{
for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
input := &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: chunk,
})
}

_, err := conn.AttachLoadBalancerTargetGroups(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "attaching Auto Scaling Group (%s) target groups: %s", d.Id(), err)
Expand Down Expand Up @@ -1717,14 +1708,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("enabled_metrics") {
o, n := d.GetChange("enabled_metrics")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
os, ns := o.(*schema.Set), n.(*schema.Set)

if disableMetrics := os.Difference(ns); disableMetrics.Len() != 0 {
input := &autoscaling.DisableMetricsCollectionInput{
Expand Down Expand Up @@ -1756,14 +1740,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("suspended_processes") {
o, n := d.GetChange("suspended_processes")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
os, ns := o.(*schema.Set), n.(*schema.Set)

if resumeProcesses := os.Difference(ns); resumeProcesses.Len() != 0 {
input := &autoscaling.ResumeProcessesInput{
Expand Down Expand Up @@ -1887,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,
Expand Down
8 changes: 3 additions & 5 deletions internal/service/connect/routing_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"slices"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/service/docdb/cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"reflect"
"slices"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions internal/service/kafka/scram_secret_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions internal/service/lakeformation/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (

FindDataCellsFilterByID = findDataCellsFilterByID
FindResourceLFTagByID = findResourceLFTagByID
LFTagParseResourceID = lfTagParseResourceID
)
Loading

0 comments on commit 367b75c

Please sign in to comment.