diff --git a/.changelog/35991.txt b/.changelog/35991.txt new file mode 100644 index 00000000000..951446948bc --- /dev/null +++ b/.changelog/35991.txt @@ -0,0 +1,11 @@ +```release-note:new-resource +aws_globalaccelerator_cross_account_attachment +``` + +```release-note:enhancement +resource/aws_eip: Add `arn` attribute +``` + +```release-note:enhancement +data-source/aws_eip: Add `arn` attribute +``` \ No newline at end of file diff --git a/go.mod b/go.mod index c28905da16a..98d1c47a611 100644 --- a/go.mod +++ b/go.mod @@ -89,6 +89,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/fis v1.24.2 github.com/aws/aws-sdk-go-v2/service/fms v1.31.4 github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4 + github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1 github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0 github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.0 github.com/aws/aws-sdk-go-v2/service/iam v1.32.0 diff --git a/go.sum b/go.sum index 7094bb13a27..7e43fbff103 100644 --- a/go.sum +++ b/go.sum @@ -198,6 +198,8 @@ github.com/aws/aws-sdk-go-v2/service/fms v1.31.4 h1:gY+Dp2QdphY6m5IVkETmsNauYztd github.com/aws/aws-sdk-go-v2/service/fms v1.31.4/go.mod h1:X4DjA4sm8cobhR9DtHn947+dLYxU1oWq3zwRZUmFSLo= github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4 h1:y0/RN8LwIbyDTPe/dnDBdsCw89ko8ZNFPW4vStye4aE= github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4/go.mod h1:8ofkOuh1SZLKR5EdfxPhQ1UgaQuCBAZzUwbeIBmeKIM= +github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1 h1:E48tPAIKptyIb8OFOAsZ3xSzjwou8A63f40ao1H3tVU= +github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1/go.mod h1:6morRSCgJD400qAu5DCEtvoaAC1owS5t6oq8ddLLwxw= github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0 h1:joAdQdtfg8Yy/e5Pq5qwAe0hjH3+EJUzd1jPrlXE3SA= github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0/go.mod h1:yGNTqdu48YxjsCyLpalmwHCQF52GGERK7+J3nTcvJ3A= github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.0 h1:HRcwttYPNlE5mv6uDlcsk9m4oV3fxV8+a+V4U3jIMd4= diff --git a/internal/conns/awsclient_gen.go b/internal/conns/awsclient_gen.go index ad4fe435f1b..b67f6a932f3 100644 --- a/internal/conns/awsclient_gen.go +++ b/internal/conns/awsclient_gen.go @@ -81,6 +81,7 @@ import ( fis_sdkv2 "github.com/aws/aws-sdk-go-v2/service/fis" fms_sdkv2 "github.com/aws/aws-sdk-go-v2/service/fms" glacier_sdkv2 "github.com/aws/aws-sdk-go-v2/service/glacier" + globalaccelerator_sdkv2 "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" groundstation_sdkv2 "github.com/aws/aws-sdk-go-v2/service/groundstation" healthlake_sdkv2 "github.com/aws/aws-sdk-go-v2/service/healthlake" iam_sdkv2 "github.com/aws/aws-sdk-go-v2/service/iam" @@ -195,7 +196,6 @@ import ( emrcontainers_sdkv1 "github.com/aws/aws-sdk-go/service/emrcontainers" fsx_sdkv1 "github.com/aws/aws-sdk-go/service/fsx" gamelift_sdkv1 "github.com/aws/aws-sdk-go/service/gamelift" - globalaccelerator_sdkv1 "github.com/aws/aws-sdk-go/service/globalaccelerator" glue_sdkv1 "github.com/aws/aws-sdk-go/service/glue" greengrass_sdkv1 "github.com/aws/aws-sdk-go/service/greengrass" guardduty_sdkv1 "github.com/aws/aws-sdk-go/service/guardduty" @@ -698,8 +698,8 @@ func (c *AWSClient) GlacierClient(ctx context.Context) *glacier_sdkv2.Client { return errs.Must(client[*glacier_sdkv2.Client](ctx, c, names.Glacier, make(map[string]any))) } -func (c *AWSClient) GlobalAcceleratorConn(ctx context.Context) *globalaccelerator_sdkv1.GlobalAccelerator { - return errs.Must(conn[*globalaccelerator_sdkv1.GlobalAccelerator](ctx, c, names.GlobalAccelerator, make(map[string]any))) +func (c *AWSClient) GlobalAcceleratorClient(ctx context.Context) *globalaccelerator_sdkv2.Client { + return errs.Must(client[*globalaccelerator_sdkv2.Client](ctx, c, names.GlobalAccelerator, make(map[string]any))) } func (c *AWSClient) GlueConn(ctx context.Context) *glue_sdkv1.Glue { diff --git a/internal/flex/flex.go b/internal/flex/flex.go index 79407d007a3..f0301723438 100644 --- a/internal/flex/flex.go +++ b/internal/flex/flex.go @@ -5,6 +5,7 @@ package flex import ( "fmt" + "slices" "strconv" "strings" "time" @@ -413,24 +414,6 @@ func ResourceIdPartCount(id string) int { return len(idParts) } -type Set[T comparable] []T - -// Difference find the elements in two sets that are not similar. -func (s Set[T]) Difference(ns Set[T]) Set[T] { - m := make(map[T]struct{}) - for _, v := range ns { - m[v] = struct{}{} - } - - var result []T - for _, v := range s { - if _, ok := m[v]; !ok { - result = append(result, v) - } - } - return result -} - // DiffStringMaps returns the set of keys and values that must be created, the set of keys // and values that must be destroyed, and the set of keys and values that are unchanged. func DiffStringMaps(oldMap, newMap map[string]interface{}) (map[string]*string, map[string]*string, map[string]*string) { @@ -478,3 +461,25 @@ func DiffStringValueMaps(oldMap, newMap map[string]interface{}) (map[string]stri return add, remove, unchanged } + +func DiffSlices[E any](old []E, new []E, eq func(E, E) bool) ([]E, []E, []E) { + // First, we're creating everything we have. + add := new + + // Build the slices of what to remove and what is unchanged. + remove := make([]E, 0) + unchanged := make([]E, 0) + for _, e := range old { + eq := func(v E) bool { return eq(v, e) } + if !slices.ContainsFunc(new, eq) { + // Delete it! + remove = append(remove, e) + } else { + unchanged = append(unchanged, e) + // Already present, so remove from new. + add = slices.DeleteFunc(add, eq) + } + } + + return add, remove, unchanged +} diff --git a/internal/flex/flex_test.go b/internal/flex/flex_test.go index 3e12ee7a578..e07c7c227db 100644 --- a/internal/flex/flex_test.go +++ b/internal/flex/flex_test.go @@ -466,3 +466,62 @@ func TestDiffStringValueMaps(t *testing.T) { } } } + +func TestDiffSlices(t *testing.T) { + t.Parallel() + + type x struct { + A string + B int + } + + cases := []struct { + Old, New []x + Create, Remove, Unchanged []x + }{ + // Add + { + Old: []x{{A: "foo", B: 1}}, + New: []x{{A: "foo", B: 1}, {A: "bar", B: 2}}, + Create: []x{{A: "bar", B: 2}}, + Remove: []x{}, + Unchanged: []x{{A: "foo", B: 1}}, + }, + // Modify + { + Old: []x{{A: "foo", B: 1}}, + New: []x{{A: "foo", B: 2}}, + Create: []x{{A: "foo", B: 2}}, + Remove: []x{{A: "foo", B: 1}}, + Unchanged: []x{}, + }, + // Overlap + { + Old: []x{{A: "foo", B: 1}, {A: "bar", B: 2}}, + New: []x{{A: "foo", B: 3}, {A: "bar", B: 2}}, + Create: []x{{A: "foo", B: 3}}, + Remove: []x{{A: "foo", B: 1}}, + Unchanged: []x{{A: "bar", B: 2}}, + }, + // Remove + { + Old: []x{{A: "foo", B: 1}, {A: "bar", B: 2}}, + New: []x{{A: "foo", B: 1}}, + Create: []x{}, + Remove: []x{{A: "bar", B: 2}}, + Unchanged: []x{{A: "foo", B: 1}}, + }, + } + for _, tc := range cases { + c, r, u := DiffSlices(tc.Old, tc.New, func(x1, x2 x) bool { return x1.A == x2.A && x1.B == x2.B }) + if diff := cmp.Diff(c, tc.Create); diff != "" { + t.Errorf("unexpected diff (+wanted, -got): %s", diff) + } + if diff := cmp.Diff(r, tc.Remove); diff != "" { + t.Errorf("unexpected diff (+wanted, -got): %s", diff) + } + if diff := cmp.Diff(u, tc.Unchanged); diff != "" { + t.Errorf("unexpected diff (+wanted, -got): %s", diff) + } + } +} diff --git a/internal/framework/flex/set.go b/internal/framework/flex/set.go index f1665577d24..08b55107ba2 100644 --- a/internal/framework/flex/set.go +++ b/internal/framework/flex/set.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" + itypes "github.com/hashicorp/terraform-provider-aws/internal/types" ) func ExpandFrameworkStringSet(ctx context.Context, v basetypes.SetValuable) []*string { @@ -20,7 +21,7 @@ func ExpandFrameworkStringSet(ctx context.Context, v basetypes.SetValuable) []*s return output } -func ExpandFrameworkStringValueSet(ctx context.Context, v basetypes.SetValuable) Set[string] { +func ExpandFrameworkStringValueSet(ctx context.Context, v basetypes.SetValuable) itypes.Set[string] { var output []string must(Expand(ctx, v, &output)) diff --git a/internal/framework/flex/set_test.go b/internal/framework/flex/set_test.go index 9247e259191..74afc03eb14 100644 --- a/internal/framework/flex/set_test.go +++ b/internal/framework/flex/set_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + itypes "github.com/hashicorp/terraform-provider-aws/internal/types" ) func TestExpandFrameworkStringSet(t *testing.T) { @@ -68,7 +69,7 @@ func TestExpandFrameworkStringValueSet(t *testing.T) { type testCase struct { input types.Set - expected flex.Set[string] + expected itypes.Set[string] } tests := map[string]testCase{ "null": { diff --git a/internal/service/ec2/ec2_eip.go b/internal/service/ec2/ec2_eip.go index 1476bd416f3..2d143005c78 100644 --- a/internal/service/ec2/ec2_eip.go +++ b/internal/service/ec2/ec2_eip.go @@ -12,6 +12,7 @@ import ( "time" "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/arn" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" @@ -57,6 +58,10 @@ func resourceEIP() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, "associate_with_private_ip": { Type: schema.TypeString, Optional: true, @@ -226,7 +231,9 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface } address := outputRaw.(*types.Address) - d.Set("allocation_id", address.AllocationId) + allocationID := aws.ToString(address.AllocationId) + d.Set("allocation_id", allocationID) + d.Set("arn", eipARN(meta.(*conns.AWSClient), allocationID)) d.Set("association_id", address.AssociationId) d.Set("carrier_ip", address.CarrierIp) d.Set("customer_owned_ip", address.CustomerOwnedIp) @@ -405,3 +412,13 @@ func disassociateEIP(ctx context.Context, conn *ec2.Client, associationID string return nil } + +func eipARN(c *conns.AWSClient, allocationID string) string { + return arn.ARN{ + Partition: c.Partition, + Service: names.EC2, + Region: c.Region, + AccountID: c.AccountID, + Resource: "elastic-ip/" + allocationID, + }.String() +} diff --git a/internal/service/ec2/ec2_eip_data_source.go b/internal/service/ec2/ec2_eip_data_source.go index d919acc9252..2949716431a 100644 --- a/internal/service/ec2/ec2_eip_data_source.go +++ b/internal/service/ec2/ec2_eip_data_source.go @@ -30,6 +30,10 @@ func dataSourceEIP() *schema.Resource { }, Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, "association_id": { Type: schema.TypeString, Computed: true, @@ -132,7 +136,9 @@ func dataSourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interfa } if eip.Domain == types.DomainTypeVpc { - d.SetId(aws.ToString(eip.AllocationId)) + allocationID := aws.ToString(eip.AllocationId) + d.SetId(allocationID) + d.Set("arn", eipARN(meta.(*conns.AWSClient), allocationID)) addressAttr, err := findEIPDomainNameAttributeByAllocationID(ctx, conn, d.Id()) @@ -146,6 +152,7 @@ func dataSourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interfa } } else { d.SetId(aws.ToString(eip.PublicIp)) + d.Set("arn", nil) d.Set("ptr_record", nil) } d.Set("association_id", eip.AssociationId) diff --git a/internal/service/ec2/ec2_eip_data_source_test.go b/internal/service/ec2/ec2_eip_data_source_test.go index 40f8b6bdcdd..c9003a04c5e 100644 --- a/internal/service/ec2/ec2_eip_data_source_test.go +++ b/internal/service/ec2/ec2_eip_data_source_test.go @@ -27,6 +27,7 @@ func TestAccEC2EIPDataSource_filter(t *testing.T) { { Config: testAccEIPDataSourceConfig_filter(rName), Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"), resource.TestCheckResourceAttrPair(dataSourceName, "public_dns", resourceName, "public_dns"), resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"), diff --git a/internal/service/ec2/ec2_eip_test.go b/internal/service/ec2/ec2_eip_test.go index 12507de6854..5bdab0c6e6b 100644 --- a/internal/service/ec2/ec2_eip_test.go +++ b/internal/service/ec2/ec2_eip_test.go @@ -36,6 +36,7 @@ func TestAccEC2EIP_basic(t *testing.T) { Config: testAccEIPConfig_basic, Check: resource.ComposeTestCheckFunc( testAccCheckEIPExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttrSet(resourceName, "arn"), resource.TestCheckResourceAttr(resourceName, "domain", "vpc"), resource.TestCheckResourceAttr(resourceName, "ptr_record", ""), resource.TestCheckResourceAttrSet(resourceName, "public_ip"), diff --git a/internal/service/globalaccelerator/accelerator.go b/internal/service/globalaccelerator/accelerator.go index fda5db63211..0ac9acc97f3 100644 --- a/internal/service/globalaccelerator/accelerator.go +++ b/internal/service/globalaccelerator/accelerator.go @@ -9,15 +9,17 @@ import ( "time" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "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" "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" @@ -28,7 +30,7 @@ import ( // @SDKResource("aws_globalaccelerator_accelerator", name="Accelerator") // @Tags(identifierAttribute="id") -func ResourceAccelerator() *schema.Resource { +func resourceAccelerator() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAcceleratorCreate, ReadWithoutTimeout: resourceAcceleratorRead, @@ -88,10 +90,10 @@ func ResourceAccelerator() *schema.Resource { Computed: true, }, "ip_address_type": { - Type: schema.TypeString, - Optional: true, - Default: globalaccelerator.IpAddressTypeIpv4, - ValidateFunc: validation.StringInSlice(globalaccelerator.IpAddressType_Values(), false), + Type: schema.TypeString, + Optional: true, + Default: awstypes.IpAddressTypeIpv4, + ValidateDiagFunc: enum.Validate[awstypes.IpAddressType](), }, "ip_addresses": { Type: schema.TypeList, @@ -136,8 +138,7 @@ func ResourceAccelerator() *schema.Resource { func resourceAcceleratorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) name := d.Get("name").(string) input := &globalaccelerator.CreateAcceleratorInput{ @@ -148,37 +149,37 @@ func resourceAcceleratorCreate(ctx context.Context, d *schema.ResourceData, meta } if v, ok := d.GetOk("ip_address_type"); ok { - input.IpAddressType = aws.String(v.(string)) + input.IpAddressType = awstypes.IpAddressType(v.(string)) } if v, ok := d.GetOk("ip_addresses"); ok && len(v.([]interface{})) > 0 { - input.IpAddresses = flex.ExpandStringList(v.([]interface{})) + input.IpAddresses = flex.ExpandStringValueList(v.([]interface{})) } - output, err := conn.CreateAcceleratorWithContext(ctx, input) + output, err := conn.CreateAccelerator(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Accelerator (%s): %s", name, err) } - d.SetId(aws.StringValue(output.Accelerator.AcceleratorArn)) + d.SetId(aws.ToString(output.Accelerator.AcceleratorArn)) if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } if v, ok := d.GetOk("attributes"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { input := expandUpdateAcceleratorAttributesInput(v.([]interface{})[0].(map[string]interface{})) input.AcceleratorArn = aws.String(d.Id()) - _, err := conn.UpdateAcceleratorAttributesWithContext(ctx, input) + _, err := conn.UpdateAcceleratorAttributes(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } } @@ -187,10 +188,9 @@ func resourceAcceleratorCreate(ctx context.Context, d *schema.ResourceData, meta func resourceAcceleratorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - - accelerator, err := FindAcceleratorByARN(ctx, conn, d.Id()) + accelerator, err := findAcceleratorByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator Accelerator (%s) not found, removing from state", d.Id()) @@ -212,7 +212,7 @@ func resourceAcceleratorRead(ctx context.Context, d *schema.ResourceData, meta i } d.Set("name", accelerator.Name) - acceleratorAttributes, err := FindAcceleratorAttributesByARN(ctx, conn, d.Id()) + acceleratorAttributes, err := findAcceleratorAttributesByARN(ctx, conn, d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Accelerator (%s) attributes: %s", d.Id(), err) @@ -227,8 +227,7 @@ func resourceAcceleratorRead(ctx context.Context, d *schema.ResourceData, meta i func resourceAcceleratorUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) if d.HasChanges("name", "ip_address_type", "enabled") { input := &globalaccelerator.UpdateAcceleratorInput{ @@ -238,17 +237,17 @@ func resourceAcceleratorUpdate(ctx context.Context, d *schema.ResourceData, meta } if v, ok := d.GetOk("ip_address_type"); ok { - input.IpAddressType = aws.String(v.(string)) + input.IpAddressType = awstypes.IpAddressType(v.(string)) } - _, err := conn.UpdateAcceleratorWithContext(ctx, input) + _, err := conn.UpdateAccelerator(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Accelerator (%s): %s", d.Id(), err) } if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } } @@ -262,28 +261,28 @@ func resourceAcceleratorUpdate(ctx context.Context, d *schema.ResourceData, meta nInput.AcceleratorArn = aws.String(d.Id()) // To change flow logs bucket and prefix attributes while flows are enabled, first disable flow logs. - if aws.BoolValue(oInput.FlowLogsEnabled) && aws.BoolValue(nInput.FlowLogsEnabled) { + if aws.ToBool(oInput.FlowLogsEnabled) && aws.ToBool(nInput.FlowLogsEnabled) { oInput.FlowLogsEnabled = aws.Bool(false) - _, err := conn.UpdateAcceleratorAttributesWithContext(ctx, oInput) + _, err := conn.UpdateAcceleratorAttributes(ctx, oInput) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } } - _, err := conn.UpdateAcceleratorAttributesWithContext(ctx, nInput) + _, err := conn.UpdateAcceleratorAttributes(ctx, nInput) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } } } @@ -294,17 +293,16 @@ func resourceAcceleratorUpdate(ctx context.Context, d *schema.ResourceData, meta func resourceAcceleratorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.UpdateAcceleratorInput{ AcceleratorArn: aws.String(d.Id()), Enabled: aws.Bool(false), } - _, err := conn.UpdateAcceleratorWithContext(ctx, input) + _, err := conn.UpdateAccelerator(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return diags } @@ -313,15 +311,15 @@ func resourceAcceleratorDelete(ctx context.Context, d *schema.ResourceData, meta } if _, err := waitAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", d.Id(), err) } log.Printf("[DEBUG] Deleting Global Accelerator Accelerator: %s", d.Id()) - _, err = conn.DeleteAcceleratorWithContext(ctx, &globalaccelerator.DeleteAcceleratorInput{ + _, err = conn.DeleteAccelerator(ctx, &globalaccelerator.DeleteAcceleratorInput{ AcceleratorArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return diags } @@ -332,18 +330,14 @@ func resourceAcceleratorDelete(ctx context.Context, d *schema.ResourceData, meta return diags } -func FindAcceleratorByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.Accelerator, error) { +func findAcceleratorByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.Accelerator, error) { input := &globalaccelerator.DescribeAcceleratorInput{ AcceleratorArn: aws.String(arn), } - return findAccelerator(ctx, conn, input) -} + output, err := conn.DescribeAccelerator(ctx, input) -func findAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeAcceleratorInput) (*globalaccelerator.Accelerator, error) { - output, err := conn.DescribeAcceleratorWithContext(ctx, input) - - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -361,18 +355,14 @@ func findAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccelera return output.Accelerator, nil } -func FindAcceleratorAttributesByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.AcceleratorAttributes, error) { +func findAcceleratorAttributesByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.AcceleratorAttributes, error) { input := &globalaccelerator.DescribeAcceleratorAttributesInput{ AcceleratorArn: aws.String(arn), } - return findAcceleratorAttributes(ctx, conn, input) -} - -func findAcceleratorAttributes(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeAcceleratorAttributesInput) (*globalaccelerator.AcceleratorAttributes, error) { - output, err := conn.DescribeAcceleratorAttributesWithContext(ctx, input) + output, err := conn.DescribeAcceleratorAttributes(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -390,9 +380,9 @@ func findAcceleratorAttributes(ctx context.Context, conn *globalaccelerator.Glob return output.AcceleratorAttributes, nil } -func statusAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) retry.StateRefreshFunc { +func statusAccelerator(ctx context.Context, conn *globalaccelerator.Client, arn string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - accelerator, err := FindAcceleratorByARN(ctx, conn, arn) + accelerator, err := findAcceleratorByARN(ctx, conn, arn) if tfresource.NotFound(err) { return nil, "", nil @@ -402,21 +392,21 @@ func statusAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccele return nil, "", err } - return accelerator, aws.StringValue(accelerator.Status), nil + return accelerator, string(accelerator.Status), nil } } -func waitAcceleratorDeployed(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string, timeout time.Duration) (*globalaccelerator.Accelerator, error) { //nolint:unparam +func waitAcceleratorDeployed(ctx context.Context, conn *globalaccelerator.Client, arn string, timeout time.Duration) (*awstypes.Accelerator, error) { //nolint:unparam stateConf := &retry.StateChangeConf{ - Pending: []string{globalaccelerator.AcceleratorStatusInProgress}, - Target: []string{globalaccelerator.AcceleratorStatusDeployed}, + Pending: enum.Slice(awstypes.AcceleratorStatusInProgress), + Target: enum.Slice(awstypes.AcceleratorStatusDeployed), Refresh: statusAccelerator(ctx, conn, arn), Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*globalaccelerator.Accelerator); ok { + if output, ok := outputRaw.(*awstypes.Accelerator); ok { return output, err } @@ -445,7 +435,7 @@ func expandUpdateAcceleratorAttributesInput(tfMap map[string]interface{}) *globa return apiObject } -func flattenIPSet(apiObject *globalaccelerator.IpSet) map[string]interface{} { +func flattenIPSet(apiObject *awstypes.IpSet) map[string]interface{} { if apiObject == nil { return nil } @@ -453,17 +443,17 @@ func flattenIPSet(apiObject *globalaccelerator.IpSet) map[string]interface{} { tfMap := map[string]interface{}{} if v := apiObject.IpAddresses; v != nil { - tfMap["ip_addresses"] = aws.StringValueSlice(v) + tfMap["ip_addresses"] = v } if v := apiObject.IpFamily; v != nil { - tfMap["ip_family"] = aws.StringValue(v) + tfMap["ip_family"] = aws.ToString(v) } return tfMap } -func flattenIPSets(apiObjects []*globalaccelerator.IpSet) []interface{} { +func flattenIPSets(apiObjects []awstypes.IpSet) []interface{} { if len(apiObjects) == 0 { return nil } @@ -471,17 +461,13 @@ func flattenIPSets(apiObjects []*globalaccelerator.IpSet) []interface{} { var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenIPSet(apiObject)) + tfList = append(tfList, flattenIPSet(&apiObject)) } return tfList } -func flattenAcceleratorAttributes(apiObject *globalaccelerator.AcceleratorAttributes) map[string]interface{} { +func flattenAcceleratorAttributes(apiObject *awstypes.AcceleratorAttributes) map[string]interface{} { if apiObject == nil { return nil } @@ -489,15 +475,15 @@ func flattenAcceleratorAttributes(apiObject *globalaccelerator.AcceleratorAttrib tfMap := map[string]interface{}{} if v := apiObject.FlowLogsEnabled; v != nil { - tfMap["flow_logs_enabled"] = aws.BoolValue(v) + tfMap["flow_logs_enabled"] = aws.ToBool(v) } if v := apiObject.FlowLogsS3Bucket; v != nil { - tfMap["flow_logs_s3_bucket"] = aws.StringValue(v) + tfMap["flow_logs_s3_bucket"] = aws.ToString(v) } if v := apiObject.FlowLogsS3Prefix; v != nil { - tfMap["flow_logs_s3_prefix"] = aws.StringValue(v) + tfMap["flow_logs_s3_prefix"] = aws.ToString(v) } return tfMap diff --git a/internal/service/globalaccelerator/accelerator_data_source.go b/internal/service/globalaccelerator/accelerator_data_source.go index 043cf82226d..bff370d14c3 100644 --- a/internal/service/globalaccelerator/accelerator_data_source.go +++ b/internal/service/globalaccelerator/accelerator_data_source.go @@ -6,8 +6,9 @@ package globalaccelerator import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -16,35 +17,34 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceAccelerator(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceAccelerator{} +// @FrameworkDataSource(name="Accelerator") +func newAcceleratorDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &acceleratorDataSource{} return d, nil } -type dataSourceAccelerator struct { +type acceleratorDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceAccelerator) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { +func (*acceleratorDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { response.TypeName = "aws_globalaccelerator_accelerator" } -// Schema returns the schema for this data source. -func (d *dataSourceAccelerator) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *acceleratorDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ - "arn": schema.StringAttribute{ + names.AttrARN: schema.StringAttribute{ CustomType: fwtypes.ARNType, Optional: true, Computed: true, }, "attributes": schema.ListAttribute{ + Computed: true, ElementType: types.ObjectType{ AttrTypes: map[string]attr.Type{ "flow_logs_enabled": types.BoolType, @@ -52,7 +52,6 @@ func (d *dataSourceAccelerator) Schema(ctx context.Context, req datasource.Schem "flow_logs_s3_prefix": types.StringType, }, }, - Computed: true, }, "dns_name": schema.StringAttribute{ Computed: true, @@ -66,7 +65,7 @@ func (d *dataSourceAccelerator) Schema(ctx context.Context, req datasource.Schem "hosted_zone_id": schema.StringAttribute{ Computed: true, }, - "id": schema.StringAttribute{ + names.AttrID: schema.StringAttribute{ Optional: true, Computed: true, }, @@ -74,13 +73,13 @@ func (d *dataSourceAccelerator) Schema(ctx context.Context, req datasource.Schem Computed: true, }, "ip_sets": schema.ListAttribute{ + Computed: true, ElementType: types.ObjectType{ AttrTypes: map[string]attr.Type{ "ip_addresses": types.ListType{ElemType: types.StringType}, "ip_family": types.StringType, }, }, - Computed: true, }, "name": schema.StringAttribute{ Optional: true, @@ -91,49 +90,38 @@ func (d *dataSourceAccelerator) Schema(ctx context.Context, req datasource.Schem } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceAccelerator) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceAcceleratorData - +func (d *acceleratorDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data acceleratorDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } - conn := d.Meta().GlobalAcceleratorConn(ctx) + conn := d.Meta().GlobalAcceleratorClient(ctx) ignoreTagsConfig := d.Meta().IgnoreTagsConfig - var results []*globalaccelerator.Accelerator - err := conn.ListAcceleratorsPagesWithContext(ctx, &globalaccelerator.ListAcceleratorsInput{}, func(page *globalaccelerator.ListAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } + var results []awstypes.Accelerator + pages := globalaccelerator.NewListAcceleratorsPaginator(conn, &globalaccelerator.ListAcceleratorsInput{}) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) - for _, accelerator := range page.Accelerators { - if accelerator == nil { - continue - } + if err != nil { + response.Diagnostics.AddError("listing Global Accelerator Accelerators", err.Error()) - if !data.ARN.IsNull() && data.ARN.ValueString() != aws.StringValue(accelerator.AcceleratorArn) { + return + } + + for _, v := range page.Accelerators { + if !data.ARN.IsNull() && data.ARN.ValueString() != aws.ToString(v.AcceleratorArn) { continue } - if !data.Name.IsNull() && data.Name.ValueString() != aws.StringValue(accelerator.Name) { + if !data.Name.IsNull() && data.Name.ValueString() != aws.ToString(v.Name) { continue } - results = append(results, accelerator) + results = append(results, v) } - - return !lastPage - }) - - if err != nil { - response.Diagnostics.AddError("listing Global Accelerator Accelerators", err.Error()) - - return } if n := len(results); n == 0 { @@ -147,18 +135,18 @@ func (d *dataSourceAccelerator) Read(ctx context.Context, request datasource.Rea } accelerator := results[0] - acceleratorARN := aws.StringValue(accelerator.AcceleratorArn) + acceleratorARN := aws.ToString(accelerator.AcceleratorArn) data.ARN = flex.StringToFrameworkARN(ctx, accelerator.AcceleratorArn) data.DnsName = flex.StringToFrameworkLegacy(ctx, accelerator.DnsName) data.DualStackDNSName = flex.StringToFrameworkLegacy(ctx, accelerator.DualStackDnsName) data.Enabled = flex.BoolToFrameworkLegacy(ctx, accelerator.Enabled) data.HostedZoneID = types.StringValue(d.Meta().GlobalAcceleratorHostedZoneID(ctx)) data.ID = types.StringValue(acceleratorARN) - data.IpAddressType = flex.StringToFrameworkLegacy(ctx, accelerator.IpAddressType) + data.IpAddressType = flex.StringValueToFrameworkLegacy(ctx, accelerator.IpAddressType) data.IpSets = d.flattenIPSetsFramework(ctx, accelerator.IpSets) data.Name = flex.StringToFrameworkLegacy(ctx, accelerator.Name) - attributes, err := FindAcceleratorAttributesByARN(ctx, conn, acceleratorARN) + attributes, err := findAcceleratorAttributesByARN(ctx, conn, acceleratorARN) if err != nil { response.Diagnostics.AddError("reading Global Accelerator Accelerator attributes", err.Error()) @@ -181,7 +169,7 @@ func (d *dataSourceAccelerator) Read(ctx context.Context, request datasource.Rea response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -func (d *dataSourceAccelerator) flattenIPSetFramework(ctx context.Context, apiObject *globalaccelerator.IpSet) types.Object { +func (d *acceleratorDataSource) flattenIPSetFramework(ctx context.Context, apiObject *awstypes.IpSet) types.Object { attributeTypes := map[string]attr.Type{ "ip_addresses": types.ListType{ElemType: types.StringType}, "ip_family": types.StringType, @@ -192,14 +180,14 @@ func (d *dataSourceAccelerator) flattenIPSetFramework(ctx context.Context, apiOb } attributes := map[string]attr.Value{ - "ip_addresses": flex.FlattenFrameworkStringListLegacy(ctx, apiObject.IpAddresses), + "ip_addresses": flex.FlattenFrameworkStringValueListLegacy(ctx, apiObject.IpAddresses), "ip_family": flex.StringToFrameworkLegacy(ctx, apiObject.IpFamily), } return types.ObjectValueMust(attributeTypes, attributes) } -func (d *dataSourceAccelerator) flattenIPSetsFramework(ctx context.Context, apiObjects []*globalaccelerator.IpSet) types.List { +func (d *acceleratorDataSource) flattenIPSetsFramework(ctx context.Context, apiObjects []awstypes.IpSet) types.List { elementType := types.ObjectType{AttrTypes: map[string]attr.Type{ "ip_addresses": types.ListType{ElemType: types.StringType}, "ip_family": types.StringType, @@ -207,17 +195,13 @@ func (d *dataSourceAccelerator) flattenIPSetsFramework(ctx context.Context, apiO var elements []attr.Value for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - elements = append(elements, d.flattenIPSetFramework(ctx, apiObject)) + elements = append(elements, d.flattenIPSetFramework(ctx, &apiObject)) } return types.ListValueMust(elementType, elements) } -func (d *dataSourceAccelerator) flattenAcceleratorAttributesFramework(ctx context.Context, apiObject *globalaccelerator.AcceleratorAttributes) types.List { +func (d *acceleratorDataSource) flattenAcceleratorAttributesFramework(ctx context.Context, apiObject *awstypes.AcceleratorAttributes) types.List { attributeTypes := map[string]attr.Type{ "flow_logs_enabled": types.BoolType, "flow_logs_s3_bucket": types.StringType, @@ -240,7 +224,7 @@ func (d *dataSourceAccelerator) flattenAcceleratorAttributesFramework(ctx contex return types.ListValueMust(elementType, []attr.Value{types.ObjectValueMust(attributeTypes, attributes)}) } -type dataSourceAcceleratorData struct { +type acceleratorDataSourceModel struct { ARN fwtypes.ARN `tfsdk:"arn"` Attributes types.List `tfsdk:"attributes"` DnsName types.String `tfsdk:"dns_name"` diff --git a/internal/service/globalaccelerator/accelerator_test.go b/internal/service/globalaccelerator/accelerator_test.go index 30e73e9554c..53e7aceb61f 100644 --- a/internal/service/globalaccelerator/accelerator_test.go +++ b/internal/service/globalaccelerator/accelerator_test.go @@ -11,7 +11,9 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/service/globalaccelerator" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -331,11 +333,11 @@ func TestAccGlobalAcceleratorAccelerator_tags(t *testing.T) { } func testAccPreCheck(ctx context.Context, t *testing.T) { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListAcceleratorsInput{} - _, err := conn.ListAcceleratorsWithContext(ctx, input) + _, err := conn.ListAccelerators(ctx, input) if acctest.PreCheckSkipError(err) { t.Skipf("skipping acceptance testing: %s", err) @@ -355,19 +357,24 @@ func testAccCheckBYOIPExists(ctx context.Context, t *testing.T) { parsedAddr := net.ParseIP(requestedAddr) - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListByoipCidrsInput{} - cidrs := make([]*globalaccelerator.ByoipCidr, 0) + cidrs := make([]awstypes.ByoipCidr, 0) - err := conn.ListByoipCidrsPagesWithContext(ctx, input, - func(page *globalaccelerator.ListByoipCidrsOutput, lastPage bool) bool { - cidrs = append(cidrs, page.ByoipCidrs...) - return !lastPage - }) + pages := globalaccelerator.NewListByoipCidrsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) - if acctest.PreCheckSkipError(err) { - t.Skipf("skipping acceptance testing: %s", err) + if acctest.PreCheckSkipError(err) { + t.Skipf("skipping acceptance testing: %s", err) + } + + if err != nil { + t.Fatalf("unexpected PreCheck error: %s", err) + } + + cidrs = append(cidrs, page.ByoipCidrs...) } if len(cidrs) == 0 { @@ -377,7 +384,7 @@ func testAccCheckBYOIPExists(ctx context.Context, t *testing.T) { matches := false for _, cidr := range cidrs { - _, network, _ := net.ParseCIDR(*cidr.Cidr) + _, network, _ := net.ParseCIDR(aws.ToString(cidr.Cidr)) if network.Contains(parsedAddr) { matches = true break @@ -387,24 +394,16 @@ func testAccCheckBYOIPExists(ctx context.Context, t *testing.T) { if !matches { t.Skipf("skipping acceptance testing: requested address %s not available via BYOIP", requestedAddr) } - - if err != nil { - t.Fatalf("unexpected PreCheck error: %s", err) - } } func testAccCheckAcceleratorExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Accelerator ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) _, err := tfglobalaccelerator.FindAcceleratorByARN(ctx, conn, rs.Primary.ID) @@ -414,7 +413,7 @@ func testAccCheckAcceleratorExists(ctx context.Context, n string) resource.TestC func testAccCheckAcceleratorDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_accelerator" { diff --git a/internal/service/globalaccelerator/arn.go b/internal/service/globalaccelerator/arn.go index e02a0830622..d842e5c2208 100644 --- a/internal/service/globalaccelerator/arn.go +++ b/internal/service/globalaccelerator/arn.go @@ -7,28 +7,28 @@ import ( "fmt" "strings" - "github.com/aws/aws-sdk-go/aws/arn" + "github.com/aws/aws-sdk-go-v2/aws/arn" ) const ( - ARNSeparator = "/" - ARNService = "globalaccelerator" + arnSeparator = "/" + arnService = "globalaccelerator" ) -// EndpointGroupARNToListenerARN converts an endpoint group ARN to a listener ARN. +// endpointGroupARNToListenerARN converts an endpoint group ARN to a listener ARN. // See https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsglobalaccelerator.html#awsglobalaccelerator-resources-for-iam-policies. -func EndpointGroupARNToListenerARN(inputARN string) (string, error) { +func endpointGroupARNToListenerARN(inputARN string) (string, error) { parsedARN, err := arn.Parse(inputARN) if err != nil { return "", fmt.Errorf("parsing ARN (%s): %w", inputARN, err) } - if actual, expected := parsedARN.Service, ARNService; actual != expected { + if actual, expected := parsedARN.Service, arnService; actual != expected { return "", fmt.Errorf("expected service %s in ARN (%s), got: %s", expected, inputARN, actual) } - resourceParts := strings.Split(parsedARN.Resource, ARNSeparator) + resourceParts := strings.Split(parsedARN.Resource, arnSeparator) if actual, expected := len(resourceParts), 6; actual < expected { return "", fmt.Errorf("expected at least %d resource parts in ARN (%s), got: %d", expected, inputARN, actual) @@ -39,26 +39,26 @@ func EndpointGroupARNToListenerARN(inputARN string) (string, error) { Service: parsedARN.Service, Region: parsedARN.Region, AccountID: parsedARN.AccountID, - Resource: strings.Join(resourceParts[0:4], ARNSeparator), + Resource: strings.Join(resourceParts[0:4], arnSeparator), }.String() return outputARN, nil } -// ListenerOrEndpointGroupARNToAcceleratorARN converts a listener or endpoint group ARN to an accelerator ARN. +// listenerOrEndpointGroupARNToAcceleratorARN converts a listener or endpoint group ARN to an accelerator ARN. // See https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsglobalaccelerator.html#awsglobalaccelerator-resources-for-iam-policies. -func ListenerOrEndpointGroupARNToAcceleratorARN(inputARN string) (string, error) { +func listenerOrEndpointGroupARNToAcceleratorARN(inputARN string) (string, error) { parsedARN, err := arn.Parse(inputARN) if err != nil { return "", fmt.Errorf("parsing ARN (%s): %w", inputARN, err) } - if actual, expected := parsedARN.Service, ARNService; actual != expected { + if actual, expected := parsedARN.Service, arnService; actual != expected { return "", fmt.Errorf("expected service %s in ARN (%s), got: %s", expected, inputARN, actual) } - resourceParts := strings.Split(parsedARN.Resource, ARNSeparator) + resourceParts := strings.Split(parsedARN.Resource, arnSeparator) if actual, expected := len(resourceParts), 4; actual < expected { return "", fmt.Errorf("expected at least %d resource parts in ARN (%s), got: %d", expected, inputARN, actual) @@ -69,7 +69,7 @@ func ListenerOrEndpointGroupARNToAcceleratorARN(inputARN string) (string, error) Service: parsedARN.Service, Region: parsedARN.Region, AccountID: parsedARN.AccountID, - Resource: strings.Join(resourceParts[0:2], ARNSeparator), + Resource: strings.Join(resourceParts[0:2], arnSeparator), }.String() return outputARN, nil diff --git a/internal/service/globalaccelerator/arn_test.go b/internal/service/globalaccelerator/arn_test.go index 567bdd92a29..52b533ec04d 100644 --- a/internal/service/globalaccelerator/arn_test.go +++ b/internal/service/globalaccelerator/arn_test.go @@ -1,14 +1,13 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -package globalaccelerator_test +package globalaccelerator import ( "regexp" "testing" "github.com/YakDriver/regexache" - tfglobalaccelerator "github.com/hashicorp/terraform-provider-aws/internal/service/globalaccelerator" ) func TestEndpointGroupARNToListenerARN(t *testing.T) { @@ -52,7 +51,7 @@ func TestEndpointGroupARNToListenerARN(t *testing.T) { t.Run(testCase.TestName, func(t *testing.T) { t.Parallel() - got, err := tfglobalaccelerator.EndpointGroupARNToListenerARN(testCase.InputARN) + got, err := endpointGroupARNToListenerARN(testCase.InputARN) if err == nil && testCase.ExpectedError != nil { t.Fatalf("expected error %s, got no error", testCase.ExpectedError.String()) @@ -119,7 +118,7 @@ func TestListenerOrEndpointGroupARNToAcceleratorARN(t *testing.T) { t.Run(testCase.TestName, func(t *testing.T) { t.Parallel() - got, err := tfglobalaccelerator.ListenerOrEndpointGroupARNToAcceleratorARN(testCase.InputARN) + got, err := listenerOrEndpointGroupARNToAcceleratorARN(testCase.InputARN) if err == nil && testCase.ExpectedError != nil { t.Fatalf("expected error %s, got no error", testCase.ExpectedError.String()) diff --git a/internal/service/globalaccelerator/cross_account_attachment.go b/internal/service/globalaccelerator/cross_account_attachment.go new file mode 100644 index 00000000000..5bc7b2eaa41 --- /dev/null +++ b/internal/service/globalaccelerator/cross_account_attachment.go @@ -0,0 +1,327 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package globalaccelerator + +import ( + "context" + "fmt" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" + "github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/errs" + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" + "github.com/hashicorp/terraform-provider-aws/internal/flex" + "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" + 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/names" +) + +// @FrameworkResource(name="Cross-account Attachment") +// @Tags(identifierAttribute="id") +func newCrossAccountAttachmentResource(_ context.Context) (resource.ResourceWithConfigure, error) { + r := &crossAccountAttachmentResource{} + + return r, nil +} + +type crossAccountAttachmentResource struct { + framework.ResourceWithConfigure + framework.WithImportByID +} + +func (*crossAccountAttachmentResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { + response.TypeName = "aws_globalaccelerator_cross_account_attachment" +} + +func (r *crossAccountAttachmentResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) { + response.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + names.AttrARN: framework.ARNAttributeComputedOnly(), + "created_time": schema.StringAttribute{ + CustomType: timetypes.RFC3339Type{}, + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + names.AttrID: framework.IDAttribute(), + "last_modified_time": schema.StringAttribute{ + CustomType: timetypes.RFC3339Type{}, + Computed: true, + }, + "name": schema.StringAttribute{ + Required: true, + }, + "principals": schema.SetAttribute{ + CustomType: fwtypes.SetOfStringType, + Optional: true, + ElementType: types.StringType, + }, + names.AttrTags: tftags.TagsAttribute(), + names.AttrTagsAll: tftags.TagsAttributeComputedOnly(), + }, + Blocks: map[string]schema.Block{ + "resource": schema.SetNestedBlock{ + CustomType: fwtypes.NewSetNestedObjectTypeOf[resourceModel](ctx), + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]schema.Attribute{ + "endpoint_id": schema.StringAttribute{ + Optional: true, + }, + "region": schema.StringAttribute{ + Optional: true, + }, + }, + }, + }, + }, + } +} + +func (r *crossAccountAttachmentResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { + var data crossAccountAttachmentResourceModel + response.Diagnostics.Append(request.Plan.Get(ctx, &data)...) + if response.Diagnostics.HasError() { + return + } + + conn := r.Meta().GlobalAcceleratorClient(ctx) + + input := &globalaccelerator.CreateCrossAccountAttachmentInput{} + response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...) + if response.Diagnostics.HasError() { + return + } + + input.IdempotencyToken = aws.String(id.UniqueId()) + input.Tags = getTagsIn(ctx) + + output, err := conn.CreateCrossAccountAttachment(ctx, input) + + if err != nil { + response.Diagnostics.AddError("creating Global Accelerator Cross-account Attachment", err.Error()) + + return + } + + // Set values for unknowns. + data.AttachmentARN = fwflex.StringToFramework(ctx, output.CrossAccountAttachment.AttachmentArn) + data.CreatedTime = fwflex.TimeToFramework(ctx, output.CrossAccountAttachment.CreatedTime) + data.LastModifiedTime = fwflex.TimeToFramework(ctx, output.CrossAccountAttachment.LastModifiedTime) + data.setID() + + response.Diagnostics.Append(response.State.Set(ctx, &data)...) +} + +func (r *crossAccountAttachmentResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { + var data crossAccountAttachmentResourceModel + response.Diagnostics.Append(request.State.Get(ctx, &data)...) + if response.Diagnostics.HasError() { + return + } + + if err := data.InitFromID(); err != nil { + response.Diagnostics.AddError("parsing resource ID", err.Error()) + + return + } + + conn := r.Meta().GlobalAcceleratorClient(ctx) + + output, err := findCrossAccountAttachmentByARN(ctx, conn, data.ID.ValueString()) + + if tfresource.NotFound(err) { + response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err)) + response.State.RemoveResource(ctx) + + return + } + + if err != nil { + response.Diagnostics.AddError(fmt.Sprintf("reading Global Accelerator Cross-account Attachment (%s)", data.ID.ValueString()), err.Error()) + + return + } + + // Normalize return value. + if data.Principals.IsNull() && len(output.Principals) == 0 { + output.Principals = nil + } + + response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...) + if response.Diagnostics.HasError() { + return + } + + response.Diagnostics.Append(response.State.Set(ctx, &data)...) +} + +func (r *crossAccountAttachmentResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { + var old, new crossAccountAttachmentResourceModel + response.Diagnostics.Append(request.State.Get(ctx, &old)...) + if response.Diagnostics.HasError() { + return + } + response.Diagnostics.Append(request.Plan.Get(ctx, &new)...) + if response.Diagnostics.HasError() { + return + } + + conn := r.Meta().GlobalAcceleratorClient(ctx) + + if !new.Name.Equal(old.Name) || + !new.Principals.Equal(old.Principals) || + !new.Resources.Equal(old.Resources) { + input := &globalaccelerator.UpdateCrossAccountAttachmentInput{ + AttachmentArn: fwflex.StringFromFramework(ctx, new.ID), + } + + if !new.Name.Equal(old.Name) { + input.Name = fwflex.StringFromFramework(ctx, new.Name) + } + + if !new.Principals.Equal(old.Principals) { + oldPrincipals, newPrincipals := fwflex.ExpandFrameworkStringValueSet(ctx, old.Principals), fwflex.ExpandFrameworkStringValueSet(ctx, new.Principals) + input.AddPrincipals, input.RemovePrincipals = newPrincipals.Difference(oldPrincipals), oldPrincipals.Difference(newPrincipals) + } + + if !new.Resources.Equal(old.Resources) { + oldResources, diags := old.Resources.ToSlice(ctx) + response.Diagnostics.Append(diags...) + if response.Diagnostics.HasError() { + return + } + + newResources, diags := new.Resources.ToSlice(ctx) + response.Diagnostics.Append(diags...) + if response.Diagnostics.HasError() { + return + } + + add, remove, _ := flex.DiffSlices(oldResources, newResources, func(v1, v2 *resourceModel) bool { + return v1.EndpointID.Equal(v2.EndpointID) && v1.Region.Equal(v2.Region) + }) + + input.AddResources = tfslices.ApplyToAll(add, func(v *resourceModel) awstypes.Resource { + return awstypes.Resource{ + EndpointId: fwflex.StringFromFramework(ctx, v.EndpointID), + Region: fwflex.StringFromFramework(ctx, v.Region), + } + }) + input.RemoveResources = tfslices.ApplyToAll(remove, func(v *resourceModel) awstypes.Resource { + return awstypes.Resource{ + EndpointId: fwflex.StringFromFramework(ctx, v.EndpointID), + Region: fwflex.StringFromFramework(ctx, v.Region), + } + }) + } + + output, err := conn.UpdateCrossAccountAttachment(ctx, input) + + if err != nil { + response.Diagnostics.AddError(fmt.Sprintf("updating Global Accelerator Cross-account Attachment (%s)", new.ID.ValueString()), err.Error()) + + return + } + + new.LastModifiedTime = fwflex.TimeToFramework(ctx, output.CrossAccountAttachment.LastModifiedTime) + } else { + new.LastModifiedTime = old.LastModifiedTime + } + + response.Diagnostics.Append(response.State.Set(ctx, &new)...) +} + +func (r *crossAccountAttachmentResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { + var data crossAccountAttachmentResourceModel + response.Diagnostics.Append(request.State.Get(ctx, &data)...) + if response.Diagnostics.HasError() { + return + } + + conn := r.Meta().GlobalAcceleratorClient(ctx) + + _, err := conn.DeleteCrossAccountAttachment(ctx, &globalaccelerator.DeleteCrossAccountAttachmentInput{ + AttachmentArn: fwflex.StringFromFramework(ctx, data.ID), + }) + + if errs.IsA[*awstypes.AttachmentNotFoundException](err) { + return + } + + if err != nil { + response.Diagnostics.AddError(fmt.Sprintf("deleting Global Accelerator Cross-account Attachment (%s)", data.ID.ValueString()), err.Error()) + + return + } +} + +func (r *crossAccountAttachmentResource) ModifyPlan(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) { + r.SetTagsAll(ctx, request, response) +} + +func findCrossAccountAttachmentByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.Attachment, error) { + input := &globalaccelerator.DescribeCrossAccountAttachmentInput{ + AttachmentArn: aws.String(arn), + } + + output, err := conn.DescribeCrossAccountAttachment(ctx, input) + + if errs.IsA[*awstypes.AttachmentNotFoundException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil || output.CrossAccountAttachment == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.CrossAccountAttachment, nil +} + +type crossAccountAttachmentResourceModel struct { + AttachmentARN types.String `tfsdk:"arn"` + CreatedTime timetypes.RFC3339 `tfsdk:"created_time"` + ID types.String `tfsdk:"id"` + LastModifiedTime timetypes.RFC3339 `tfsdk:"last_modified_time"` + Name types.String `tfsdk:"name"` + Principals fwtypes.SetValueOf[types.String] `tfsdk:"principals"` + Resources fwtypes.SetNestedObjectValueOf[resourceModel] `tfsdk:"resource"` + Tags types.Map `tfsdk:"tags"` + TagsAll types.Map `tfsdk:"tags_all"` +} + +func (m *crossAccountAttachmentResourceModel) InitFromID() error { + m.AttachmentARN = m.ID + + return nil +} + +func (m *crossAccountAttachmentResourceModel) setID() { + m.ID = m.AttachmentARN +} + +type resourceModel struct { + EndpointID types.String `tfsdk:"endpoint_id"` + Region types.String `tfsdk:"region"` +} diff --git a/internal/service/globalaccelerator/cross_account_attachment_test.go b/internal/service/globalaccelerator/cross_account_attachment_test.go new file mode 100644 index 00000000000..2b01ae97178 --- /dev/null +++ b/internal/service/globalaccelerator/cross_account_attachment_test.go @@ -0,0 +1,328 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package globalaccelerator_test + +import ( + "context" + "fmt" + "testing" + + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" + sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tfglobalaccelerator "github.com/hashicorp/terraform-provider-aws/internal/service/globalaccelerator" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func TestAccGlobalAcceleratorCrossAccountAttachment_basic(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_globalaccelerator_cross_account_attachment.test" + rName1 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + var v awstypes.Attachment + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.GlobalAcceleratorServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckCrossAccountAttachmentDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccCrossAccountAttachmentConfig_basic(rName1), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttrSet(resourceName, "arn"), + resource.TestCheckResourceAttrSet(resourceName, "created_time"), + resource.TestCheckResourceAttrSet(resourceName, "last_modified_time"), + resource.TestCheckResourceAttr(resourceName, "name", rName1), + resource.TestCheckResourceAttr(resourceName, "principals.#", "0"), + resource.TestCheckResourceAttr(resourceName, "resource.#", "0"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccCrossAccountAttachmentConfig_basic(rName2), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "name", rName2), + ), + }, + }, + }) +} +func TestAccGlobalAcceleratorCrossAccountAttachment_principals(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_globalaccelerator_cross_account_attachment.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + rAccountID1 := sdkacctest.RandStringFromCharSet(12, "012346789") + rAccountID2 := sdkacctest.RandStringFromCharSet(12, "012346789") + var v awstypes.Attachment + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.GlobalAcceleratorServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckCrossAccountAttachmentDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccCrossAccountAttachmentConfig_principals(rName, rAccountID1), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "principals.#", "1"), + resource.TestCheckTypeSetElemAttr(resourceName, "principals.*", rAccountID1), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccCrossAccountAttachmentConfig_principals(rName, rAccountID2), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "principals.#", "1"), + resource.TestCheckTypeSetElemAttr(resourceName, "principals.*", rAccountID2), + ), + }, + }, + }) +} + +func TestAccGlobalAcceleratorCrossAccountAttachment_resources(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_globalaccelerator_cross_account_attachment.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.GlobalAcceleratorServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckCrossAccountAttachmentDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccCrossAccountAttachmentConfig_resources(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "resource.#", "1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccCrossAccountAttachmentConfig_resourcesUpdated(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "resource.#", "1"), + ), + }, + }, + }) +} + +func TestAccGlobalAcceleratorCrossAccountAttachment_disappears(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_globalaccelerator_cross_account_attachment.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + var v awstypes.Attachment + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.GlobalAcceleratorServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckCrossAccountAttachmentDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccCrossAccountAttachmentConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfglobalaccelerator.ResourceCrossAccountAttachment, resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccGlobalAcceleratorCrossAccountAttachment_tags(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_globalaccelerator_cross_account_attachment.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + var v awstypes.Attachment + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.GlobalAcceleratorServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckAcceleratorDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccCrossAccountAttachmentConfig_tags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccCrossAccountAttachmentConfig_tags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccCrossAccountAttachmentConfig_tags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckCrossAccountAttachmentExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + }, + }) +} + +func testAccCheckCrossAccountAttachmentDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_globalaccelerator_cross_account_attachment" { + continue + } + + _, err := tfglobalaccelerator.FindCrossAccountAttachmentByARN(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Global Accelerator Cross-account Attachment %s still exists", rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckCrossAccountAttachmentExists(ctx context.Context, n string, v *awstypes.Attachment) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) + + output, err := tfglobalaccelerator.FindCrossAccountAttachmentByARN(ctx, conn, rs.Primary.ID) + + if err != nil { + return err + } + + *v = *output + + return nil + } +} + +func testAccCrossAccountAttachmentConfig_basic(rName string) string { + return fmt.Sprintf(` +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q +} +`, rName) +} + +func testAccCrossAccountAttachmentConfig_principals(rName, accountID string) string { + return fmt.Sprintf(` +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q + principals = [%[2]q] +} +`, rName, accountID) +} + +func testAccCrossAccountAttachmentConfig_resources(rName string) string { + return acctest.ConfigCompose(testAccEndpointGroupConfig_baseALB(rName), fmt.Sprintf(` +resource "aws_eip" "test" { + tags = { + Name = %[1]q + } +} + +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q + + resource { + endpoint_id = aws_lb.test.id + } +} +`, rName)) +} + +func testAccCrossAccountAttachmentConfig_resourcesUpdated(rName string) string { + return acctest.ConfigCompose(testAccEndpointGroupConfig_baseALB(rName), fmt.Sprintf(` +resource "aws_eip" "test" { + tags = { + Name = %[1]q + } +} + +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q + + resource { + endpoint_id = aws_eip.test.arn + } +} +`, rName)) +} + +func testAccCrossAccountAttachmentConfig_tags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccCrossAccountAttachmentConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_globalaccelerator_cross_account_attachment" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} diff --git a/internal/service/globalaccelerator/custom_routing_accelerator.go b/internal/service/globalaccelerator/custom_routing_accelerator.go index 4563c437c63..444de99c2ca 100644 --- a/internal/service/globalaccelerator/custom_routing_accelerator.go +++ b/internal/service/globalaccelerator/custom_routing_accelerator.go @@ -9,15 +9,17 @@ import ( "time" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "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" "github.com/hashicorp/terraform-provider-aws/internal/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" @@ -28,7 +30,7 @@ import ( // @SDKResource("aws_globalaccelerator_custom_routing_accelerator", name="Custom Routing Accelerator") // @Tags(identifierAttribute="id") -func ResourceCustomRoutingAccelerator() *schema.Resource { +func resourceCustomRoutingAccelerator() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceCustomRoutingAcceleratorCreate, ReadWithoutTimeout: resourceCustomRoutingAcceleratorRead, @@ -84,10 +86,10 @@ func ResourceCustomRoutingAccelerator() *schema.Resource { Computed: true, }, "ip_address_type": { - Type: schema.TypeString, - Optional: true, - Default: globalaccelerator.IpAddressTypeIpv4, - ValidateFunc: validation.StringInSlice(globalaccelerator.IpAddressType_Values(), false), + Type: schema.TypeString, + Optional: true, + Default: awstypes.IpAddressTypeIpv4, + ValidateDiagFunc: enum.Validate[awstypes.IpAddressType](), }, "ip_addresses": { Type: schema.TypeList, @@ -132,46 +134,46 @@ func ResourceCustomRoutingAccelerator() *schema.Resource { func resourceCustomRoutingAcceleratorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) name := d.Get("name").(string) input := &globalaccelerator.CreateCustomRoutingAcceleratorInput{ + Enabled: aws.Bool(d.Get("enabled").(bool)), Name: aws.String(name), IdempotencyToken: aws.String(id.UniqueId()), - Enabled: aws.Bool(d.Get("enabled").(bool)), Tags: getTagsIn(ctx), } if v, ok := d.GetOk("ip_address_type"); ok { - input.IpAddressType = aws.String(v.(string)) + input.IpAddressType = awstypes.IpAddressType(v.(string)) } if v, ok := d.GetOk("ip_addresses"); ok && len(v.([]interface{})) > 0 { - input.IpAddresses = flex.ExpandStringList(v.([]interface{})) + input.IpAddresses = flex.ExpandStringValueList(v.([]interface{})) } - output, err := conn.CreateCustomRoutingAcceleratorWithContext(ctx, input) + output, err := conn.CreateCustomRoutingAccelerator(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Custom Routing Accelerator (%s): %s", name, err) } - d.SetId(aws.StringValue(output.Accelerator.AcceleratorArn)) + d.SetId(aws.ToString(output.Accelerator.AcceleratorArn)) if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } if v, ok := d.GetOk("attributes"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { input := expandUpdateAcceleratorAttributesInput(v.([]interface{})[0].(map[string]interface{})) input.AcceleratorArn = aws.String(d.Id()) - if _, err := conn.UpdateAcceleratorAttributesWithContext(ctx, input); err != nil { + if _, err := conn.UpdateAcceleratorAttributes(ctx, input); err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Custom Routing Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } } @@ -180,9 +182,9 @@ func resourceCustomRoutingAcceleratorCreate(ctx context.Context, d *schema.Resou func resourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - accelerator, err := FindCustomRoutingAcceleratorByARN(ctx, conn, d.Id()) + accelerator, err := findCustomRoutingAcceleratorByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator Custom Routing Accelerator (%s) not found, removing from state", d.Id()) @@ -203,7 +205,7 @@ func resourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.Resourc } d.Set("name", accelerator.Name) - acceleratorAttributes, err := FindCustomRoutingAcceleratorAttributesByARN(ctx, conn, d.Id()) + acceleratorAttributes, err := findCustomRoutingAcceleratorAttributesByARN(ctx, conn, d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Custom Routing Accelerator (%s) attributes: %s", d.Id(), err) @@ -218,7 +220,7 @@ func resourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.Resourc func resourceCustomRoutingAcceleratorUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) if d.HasChanges("name", "ip_address_type", "enabled") { input := &globalaccelerator.UpdateCustomRoutingAcceleratorInput{ @@ -228,17 +230,17 @@ func resourceCustomRoutingAcceleratorUpdate(ctx context.Context, d *schema.Resou } if v, ok := d.GetOk("ip_address_type"); ok { - input.IpAddressType = aws.String(v.(string)) + input.IpAddressType = awstypes.IpAddressType(v.(string)) } - _, err := conn.UpdateCustomRoutingAcceleratorWithContext(ctx, input) + _, err := conn.UpdateCustomRoutingAccelerator(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Custom Routing Accelerator (%s): %s", d.Id(), err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } } @@ -252,28 +254,28 @@ func resourceCustomRoutingAcceleratorUpdate(ctx context.Context, d *schema.Resou nInput.AcceleratorArn = aws.String(d.Id()) // To change flow logs bucket and prefix attributes while flows are enabled, first disable flow logs. - if aws.BoolValue(oInput.FlowLogsEnabled) && aws.BoolValue(nInput.FlowLogsEnabled) { + if aws.ToBool(oInput.FlowLogsEnabled) && aws.ToBool(nInput.FlowLogsEnabled) { oInput.FlowLogsEnabled = aws.Bool(false) - _, err := conn.UpdateCustomRoutingAcceleratorAttributesWithContext(ctx, oInput) + _, err := conn.UpdateCustomRoutingAcceleratorAttributes(ctx, oInput) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Custom Routing Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } } - _, err := conn.UpdateCustomRoutingAcceleratorAttributesWithContext(ctx, nInput) + _, err := conn.UpdateCustomRoutingAcceleratorAttributes(ctx, nInput) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Custom Routing Accelerator (%s) attributes: %s", d.Id(), err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } } } @@ -284,16 +286,16 @@ func resourceCustomRoutingAcceleratorUpdate(ctx context.Context, d *schema.Resou func resourceCustomRoutingAcceleratorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.UpdateCustomRoutingAcceleratorInput{ AcceleratorArn: aws.String(d.Id()), Enabled: aws.Bool(false), } - _, err := conn.UpdateCustomRoutingAcceleratorWithContext(ctx, input) + _, err := conn.UpdateCustomRoutingAccelerator(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return diags } @@ -302,15 +304,15 @@ func resourceCustomRoutingAcceleratorDelete(ctx context.Context, d *schema.Resou } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", d.Id(), err) } log.Printf("[DEBUG] Deleting Global Accelerator Custom Routing Accelerator (%s)", d.Id()) - _, err = conn.DeleteCustomRoutingAcceleratorWithContext(ctx, &globalaccelerator.DeleteCustomRoutingAcceleratorInput{ + _, err = conn.DeleteCustomRoutingAccelerator(ctx, &globalaccelerator.DeleteCustomRoutingAcceleratorInput{ AcceleratorArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return diags } @@ -321,18 +323,14 @@ func resourceCustomRoutingAcceleratorDelete(ctx context.Context, d *schema.Resou return diags } -func FindCustomRoutingAcceleratorByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.CustomRoutingAccelerator, error) { +func findCustomRoutingAcceleratorByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.CustomRoutingAccelerator, error) { input := &globalaccelerator.DescribeCustomRoutingAcceleratorInput{ AcceleratorArn: aws.String(arn), } - return findCustomRoutingAccelerator(ctx, conn, input) -} + output, err := conn.DescribeCustomRoutingAccelerator(ctx, input) -func findCustomRoutingAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeCustomRoutingAcceleratorInput) (*globalaccelerator.CustomRoutingAccelerator, error) { - output, err := conn.DescribeCustomRoutingAcceleratorWithContext(ctx, input) - - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -350,18 +348,14 @@ func findCustomRoutingAccelerator(ctx context.Context, conn *globalaccelerator.G return output.Accelerator, nil } -func FindCustomRoutingAcceleratorAttributesByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.CustomRoutingAcceleratorAttributes, error) { +func findCustomRoutingAcceleratorAttributesByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.CustomRoutingAcceleratorAttributes, error) { input := &globalaccelerator.DescribeCustomRoutingAcceleratorAttributesInput{ AcceleratorArn: aws.String(arn), } - return findCustomRoutingAcceleratorAttributes(ctx, conn, input) -} - -func findCustomRoutingAcceleratorAttributes(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeCustomRoutingAcceleratorAttributesInput) (*globalaccelerator.CustomRoutingAcceleratorAttributes, error) { - output, err := conn.DescribeCustomRoutingAcceleratorAttributesWithContext(ctx, input) + output, err := conn.DescribeCustomRoutingAcceleratorAttributes(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeAcceleratorNotFoundException) { + if errs.IsA[*awstypes.AcceleratorNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -379,9 +373,9 @@ func findCustomRoutingAcceleratorAttributes(ctx context.Context, conn *globalacc return output.AcceleratorAttributes, nil } -func statusCustomRoutingAccelerator(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) retry.StateRefreshFunc { +func statusCustomRoutingAccelerator(ctx context.Context, conn *globalaccelerator.Client, arn string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - accelerator, err := FindCustomRoutingAcceleratorByARN(ctx, conn, arn) + accelerator, err := findCustomRoutingAcceleratorByARN(ctx, conn, arn) if tfresource.NotFound(err) { return nil, "", nil @@ -391,21 +385,21 @@ func statusCustomRoutingAccelerator(ctx context.Context, conn *globalaccelerator return nil, "", err } - return accelerator, aws.StringValue(accelerator.Status), nil + return accelerator, string(accelerator.Status), nil } } -func waitCustomRoutingAcceleratorDeployed(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string, timeout time.Duration) (*globalaccelerator.CustomRoutingAccelerator, error) { //nolint:unparam +func waitCustomRoutingAcceleratorDeployed(ctx context.Context, conn *globalaccelerator.Client, arn string, timeout time.Duration) (*awstypes.CustomRoutingAccelerator, error) { //nolint:unparam stateConf := &retry.StateChangeConf{ - Pending: []string{globalaccelerator.AcceleratorStatusInProgress}, - Target: []string{globalaccelerator.AcceleratorStatusDeployed}, + Pending: enum.Slice(awstypes.AcceleratorStatusInProgress), + Target: enum.Slice(awstypes.AcceleratorStatusDeployed), Refresh: statusCustomRoutingAccelerator(ctx, conn, arn), Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*globalaccelerator.CustomRoutingAccelerator); ok { + if output, ok := outputRaw.(*awstypes.CustomRoutingAccelerator); ok { return output, err } @@ -416,6 +410,6 @@ func expandUpdateCustomRoutingAcceleratorAttributesInput(tfMap map[string]interf return (*globalaccelerator.UpdateCustomRoutingAcceleratorAttributesInput)(expandUpdateAcceleratorAttributesInput(tfMap)) } -func flattenCustomRoutingAcceleratorAttributes(apiObject *globalaccelerator.CustomRoutingAcceleratorAttributes) map[string]interface{} { - return flattenAcceleratorAttributes((*globalaccelerator.AcceleratorAttributes)(apiObject)) +func flattenCustomRoutingAcceleratorAttributes(apiObject *awstypes.CustomRoutingAcceleratorAttributes) map[string]interface{} { + return flattenAcceleratorAttributes((*awstypes.AcceleratorAttributes)(apiObject)) } diff --git a/internal/service/globalaccelerator/custom_routing_accelerator_data_source.go b/internal/service/globalaccelerator/custom_routing_accelerator_data_source.go index 4a10c5ef5a0..7d22888cf8c 100644 --- a/internal/service/globalaccelerator/custom_routing_accelerator_data_source.go +++ b/internal/service/globalaccelerator/custom_routing_accelerator_data_source.go @@ -6,8 +6,9 @@ package globalaccelerator import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -15,8 +16,8 @@ import ( tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" ) -// @SDKDataSource("aws_globalaccelerator_custom_routing_accelerator") -func DataSourceCustomRoutingAccelerator() *schema.Resource { +// @SDKDataSource("aws_globalaccelerator_custom_routing_accelerator", name="Custom Routing Accelerator") +func dataSourceCustomRoutingAccelerator() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceCustomRoutingAcceleratorRead, @@ -91,37 +92,29 @@ func DataSourceCustomRoutingAccelerator() *schema.Resource { func dataSourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - var results []*globalaccelerator.CustomRoutingAccelerator + var results []awstypes.CustomRoutingAccelerator + pages := globalaccelerator.NewListCustomRoutingAcceleratorsPaginator(conn, &globalaccelerator.ListCustomRoutingAcceleratorsInput{}) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) - err := conn.ListCustomRoutingAcceleratorsPagesWithContext(ctx, &globalaccelerator.ListCustomRoutingAcceleratorsInput{}, func(page *globalaccelerator.ListCustomRoutingAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + if err != nil { + return sdkdiag.AppendErrorf(diags, "listing Global Accelerator Custom Routing Accelerators: %s", err) } - for _, l := range page.Accelerators { - if l == nil { + for _, accelerator := range page.Accelerators { + if v, ok := d.GetOk("arn"); ok && v.(string) != aws.ToString(accelerator.AcceleratorArn) { continue } - if v, ok := d.GetOk("arn"); ok && v.(string) != aws.StringValue(l.AcceleratorArn) { + if v, ok := d.GetOk("name"); ok && v.(string) != aws.ToString(accelerator.Name) { continue } - if v, ok := d.GetOk("name"); ok && v.(string) != aws.StringValue(l.Name) { - continue - } - - results = append(results, l) + results = append(results, accelerator) } - - return !lastPage - }) - - if err != nil { - return sdkdiag.AppendErrorf(diags, "listing Global Accelerator Custom Routing Accelerators: %s", err) } if count := len(results); count != 1 { @@ -129,7 +122,7 @@ func dataSourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.Resou } accelerator := results[0] - d.SetId(aws.StringValue(accelerator.AcceleratorArn)) + d.SetId(aws.ToString(accelerator.AcceleratorArn)) d.Set("arn", accelerator.AcceleratorArn) d.Set("dns_name", accelerator.DnsName) d.Set("enabled", accelerator.Enabled) @@ -140,7 +133,7 @@ func dataSourceCustomRoutingAcceleratorRead(ctx context.Context, d *schema.Resou } d.Set("name", accelerator.Name) - acceleratorAttributes, err := FindCustomRoutingAcceleratorAttributesByARN(ctx, conn, d.Id()) + acceleratorAttributes, err := findCustomRoutingAcceleratorAttributesByARN(ctx, conn, d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Custom Routing Accelerator (%s) attributes: %s", d.Id(), err) diff --git a/internal/service/globalaccelerator/custom_routing_accelerator_test.go b/internal/service/globalaccelerator/custom_routing_accelerator_test.go index 845f39f5612..dff9a140613 100644 --- a/internal/service/globalaccelerator/custom_routing_accelerator_test.go +++ b/internal/service/globalaccelerator/custom_routing_accelerator_test.go @@ -168,16 +168,12 @@ func TestAccGlobalAcceleratorCustomRoutingAccelerator_update(t *testing.T) { func testAccCheckCustomRoutingAcceleratorExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Custom Routing Accelerator ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) _, err := tfglobalaccelerator.FindCustomRoutingAcceleratorByARN(ctx, conn, rs.Primary.ID) @@ -187,7 +183,7 @@ func testAccCheckCustomRoutingAcceleratorExists(ctx context.Context, n string) r func testAccCheckCustomRoutingAcceleratorDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_custom_routing_accelerator" { diff --git a/internal/service/globalaccelerator/custom_routing_endpoint_group.go b/internal/service/globalaccelerator/custom_routing_endpoint_group.go index 4b7e2f58980..ab59747d2b3 100644 --- a/internal/service/globalaccelerator/custom_routing_endpoint_group.go +++ b/internal/service/globalaccelerator/custom_routing_endpoint_group.go @@ -8,23 +8,25 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "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" "github.com/hashicorp/terraform-provider-aws/internal/flex" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -// @SDKResource("aws_globalaccelerator_custom_routing_endpoint_group") -func ResourceCustomRoutingEndpointGroup() *schema.Resource { +// @SDKResource("aws_globalaccelerator_custom_routing_endpoint_group", name="Custom Routing Endpoint Group") +func resourceCustomRoutingEndpointGroup() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceCustomRoutingEndpointGroupCreate, ReadWithoutTimeout: resourceCustomRoutingEndpointGroupRead, @@ -59,8 +61,8 @@ func ResourceCustomRoutingEndpointGroup() *schema.Resource { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{ - Type: schema.TypeString, - ValidateFunc: validation.StringInSlice(globalaccelerator.CustomRoutingProtocol_Values(), false), + Type: schema.TypeString, + ValidateDiagFunc: enum.Validate[awstypes.CustomRoutingProtocol](), }, }, "to_port": { @@ -104,7 +106,7 @@ func ResourceCustomRoutingEndpointGroup() *schema.Resource { func resourceCustomRoutingEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.CreateCustomRoutingEndpointGroupInput{ DestinationConfigurations: expandCustomRoutingDestinationConfigurations(d.Get("destination_configuration").(*schema.Set).List()), @@ -117,38 +119,37 @@ func resourceCustomRoutingEndpointGroupCreate(ctx context.Context, d *schema.Res input.EndpointGroupRegion = aws.String(v.(string)) } - output, err := conn.CreateCustomRoutingEndpointGroupWithContext(ctx, input) + output, err := conn.CreateCustomRoutingEndpointGroup(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Custom Routing Endpoint Group: %s", err) } - d.SetId(aws.StringValue(output.EndpointGroup.EndpointGroupArn)) - - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) + d.SetId(aws.ToString(output.EndpointGroup.EndpointGroupArn)) + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } if v, ok := d.GetOk("endpoint_configuration"); ok { input := &globalaccelerator.AddCustomRoutingEndpointsInput{ - EndpointGroupArn: aws.String(d.Id()), EndpointConfigurations: expandCustomRoutingEndpointConfigurations(v.(*schema.Set).List()), + EndpointGroupArn: aws.String(d.Id()), } - _, err := conn.AddCustomRoutingEndpointsWithContext(ctx, input) + _, err := conn.AddCustomRoutingEndpoints(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "adding Global Accelerator Custom Routing Endpoint Group (%s) endpoints: %s", d.Id(), err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } } @@ -157,9 +158,9 @@ func resourceCustomRoutingEndpointGroupCreate(ctx context.Context, d *schema.Res func resourceCustomRoutingEndpointGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - endpointGroup, err := FindCustomRoutingEndpointGroupByARN(ctx, conn, d.Id()) + endpointGroup, err := findCustomRoutingEndpointGroupByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator Custom Routing Endpoint Group (%s) not found, removing from state", d.Id()) @@ -171,8 +172,7 @@ func resourceCustomRoutingEndpointGroupRead(ctx context.Context, d *schema.Resou return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Custom Routing Endpoint Group (%s): %s", d.Id(), err) } - listenerARN, err := EndpointGroupARNToListenerARN(d.Id()) - + listenerARN, err := endpointGroupARNToListenerARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -192,14 +192,14 @@ func resourceCustomRoutingEndpointGroupRead(ctx context.Context, d *schema.Resou func resourceCustomRoutingEndpointGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) log.Printf("[DEBUG] Deleting Global Accelerator Custom Routing Endpoint Group (%s)", d.Id()) - _, err := conn.DeleteCustomRoutingEndpointGroupWithContext(ctx, &globalaccelerator.DeleteCustomRoutingEndpointGroupInput{ + _, err := conn.DeleteCustomRoutingEndpointGroup(ctx, &globalaccelerator.DeleteCustomRoutingEndpointGroupInput{ EndpointGroupArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeEndpointGroupNotFoundException) { + if errs.IsA[*awstypes.EndpointGroupNotFoundException](err) { return diags } @@ -207,31 +207,26 @@ func resourceCustomRoutingEndpointGroupDelete(ctx context.Context, d *schema.Res return sdkdiag.AppendErrorf(diags, "deleting Global Accelerator Custom Routing Endpoint Group (%s): %s", d.Id(), err) } - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) - + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutDelete)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } return diags } -func FindCustomRoutingEndpointGroupByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.CustomRoutingEndpointGroup, error) { +func findCustomRoutingEndpointGroupByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.CustomRoutingEndpointGroup, error) { input := &globalaccelerator.DescribeCustomRoutingEndpointGroupInput{ EndpointGroupArn: aws.String(arn), } - return findCustomRoutingEndpointGroup(ctx, conn, input) -} - -func findCustomRoutingEndpointGroup(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeCustomRoutingEndpointGroupInput) (*globalaccelerator.CustomRoutingEndpointGroup, error) { - output, err := conn.DescribeCustomRoutingEndpointGroupWithContext(ctx, input) + output, err := conn.DescribeCustomRoutingEndpointGroup(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeEndpointGroupNotFoundException) { + if errs.IsA[*awstypes.EndpointGroupNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -249,34 +244,34 @@ func findCustomRoutingEndpointGroup(ctx context.Context, conn *globalaccelerator return output.EndpointGroup, nil } -func expandCustomRoutingEndpointDestinationConfiguration(tfMap map[string]interface{}) *globalaccelerator.CustomRoutingDestinationConfiguration { +func expandCustomRoutingEndpointDestinationConfiguration(tfMap map[string]interface{}) *awstypes.CustomRoutingDestinationConfiguration { if tfMap == nil { return nil } - apiObject := &globalaccelerator.CustomRoutingDestinationConfiguration{} + apiObject := &awstypes.CustomRoutingDestinationConfiguration{} if v, ok := tfMap["from_port"].(int); ok && v != 0 { - apiObject.FromPort = aws.Int64(int64(v)) + apiObject.FromPort = aws.Int32(int32(v)) } if v, ok := tfMap["protocols"].(*schema.Set); ok { - apiObject.Protocols = flex.ExpandStringSet(v) + apiObject.Protocols = flex.ExpandStringyValueSet[awstypes.CustomRoutingProtocol](v) } if v, ok := tfMap["to_port"].(int); ok && v != 0 { - apiObject.ToPort = aws.Int64(int64(v)) + apiObject.ToPort = aws.Int32(int32(v)) } return apiObject } -func expandCustomRoutingDestinationConfigurations(tfList []interface{}) []*globalaccelerator.CustomRoutingDestinationConfiguration { +func expandCustomRoutingDestinationConfigurations(tfList []interface{}) []awstypes.CustomRoutingDestinationConfiguration { if len(tfList) == 0 { return nil } - var apiObjects []*globalaccelerator.CustomRoutingDestinationConfiguration + var apiObjects []awstypes.CustomRoutingDestinationConfiguration for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -291,18 +286,18 @@ func expandCustomRoutingDestinationConfigurations(tfList []interface{}) []*globa continue } - apiObjects = append(apiObjects, apiObject) + apiObjects = append(apiObjects, *apiObject) } return apiObjects } -func expandCustomRoutingEndpointConfiguration(tfMap map[string]interface{}) *globalaccelerator.CustomRoutingEndpointConfiguration { +func expandCustomRoutingEndpointConfiguration(tfMap map[string]interface{}) *awstypes.CustomRoutingEndpointConfiguration { if tfMap == nil { return nil } - apiObject := &globalaccelerator.CustomRoutingEndpointConfiguration{} + apiObject := &awstypes.CustomRoutingEndpointConfiguration{} if v, ok := tfMap["endpoint_id"].(string); ok && v != "" { apiObject.EndpointId = aws.String(v) @@ -311,12 +306,12 @@ func expandCustomRoutingEndpointConfiguration(tfMap map[string]interface{}) *glo return apiObject } -func expandCustomRoutingEndpointConfigurations(tfList []interface{}) []*globalaccelerator.CustomRoutingEndpointConfiguration { +func expandCustomRoutingEndpointConfigurations(tfList []interface{}) []awstypes.CustomRoutingEndpointConfiguration { if len(tfList) == 0 { return nil } - var apiObjects []*globalaccelerator.CustomRoutingEndpointConfiguration + var apiObjects []awstypes.CustomRoutingEndpointConfiguration for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -331,13 +326,13 @@ func expandCustomRoutingEndpointConfigurations(tfList []interface{}) []*globalac continue } - apiObjects = append(apiObjects, apiObject) + apiObjects = append(apiObjects, *apiObject) } return apiObjects } -func flattenCustomRoutingDestinationDescription(apiObject *globalaccelerator.CustomRoutingDestinationDescription) map[string]interface{} { +func flattenCustomRoutingDestinationDescription(apiObject *awstypes.CustomRoutingDestinationDescription) map[string]interface{} { if apiObject == nil { return nil } @@ -345,21 +340,21 @@ func flattenCustomRoutingDestinationDescription(apiObject *globalaccelerator.Cus tfMap := map[string]interface{}{} if v := apiObject.FromPort; v != nil { - tfMap["from_port"] = aws.Int64Value(v) + tfMap["from_port"] = aws.ToInt32(v) } if v := apiObject.Protocols; v != nil { - tfMap["protocols"] = aws.StringValueSlice(v) + tfMap["protocols"] = v } if v := apiObject.ToPort; v != nil { - tfMap["to_port"] = aws.Int64Value(v) + tfMap["to_port"] = aws.ToInt32(v) } return tfMap } -func flattenCustomRoutingDestinationDescriptions(apiObjects []*globalaccelerator.CustomRoutingDestinationDescription) []interface{} { +func flattenCustomRoutingDestinationDescriptions(apiObjects []awstypes.CustomRoutingDestinationDescription) []interface{} { if len(apiObjects) == 0 { return nil } @@ -367,17 +362,13 @@ func flattenCustomRoutingDestinationDescriptions(apiObjects []*globalaccelerator var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenCustomRoutingDestinationDescription(apiObject)) + tfList = append(tfList, flattenCustomRoutingDestinationDescription(&apiObject)) } return tfList } -func flattenCustomRoutingEndpointDescription(apiObject *globalaccelerator.CustomRoutingEndpointDescription) map[string]interface{} { +func flattenCustomRoutingEndpointDescription(apiObject *awstypes.CustomRoutingEndpointDescription) map[string]interface{} { if apiObject == nil { return nil } @@ -385,13 +376,13 @@ func flattenCustomRoutingEndpointDescription(apiObject *globalaccelerator.Custom tfMap := map[string]interface{}{} if v := apiObject.EndpointId; v != nil { - tfMap["endpoint_id"] = aws.StringValue(v) + tfMap["endpoint_id"] = aws.ToString(v) } return tfMap } -func flattenCustomRoutingEndpointDescriptions(apiObjects []*globalaccelerator.CustomRoutingEndpointDescription) []interface{} { +func flattenCustomRoutingEndpointDescriptions(apiObjects []awstypes.CustomRoutingEndpointDescription) []interface{} { if len(apiObjects) == 0 { return nil } @@ -399,11 +390,7 @@ func flattenCustomRoutingEndpointDescriptions(apiObjects []*globalaccelerator.Cu var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenCustomRoutingEndpointDescription(apiObject)) + tfList = append(tfList, flattenCustomRoutingEndpointDescription(&apiObject)) } return tfList diff --git a/internal/service/globalaccelerator/custom_routing_endpoint_group_test.go b/internal/service/globalaccelerator/custom_routing_endpoint_group_test.go index 2fcb3d9bc74..6e3aaf43d73 100644 --- a/internal/service/globalaccelerator/custom_routing_endpoint_group_test.go +++ b/internal/service/globalaccelerator/custom_routing_endpoint_group_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -21,7 +21,7 @@ import ( func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_basic(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingEndpointGroup + var v awstypes.CustomRoutingEndpointGroup resourceName := "aws_globalaccelerator_custom_routing_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -56,7 +56,7 @@ func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_basic(t *testing.T) { func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_disappears(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingEndpointGroup + var v awstypes.CustomRoutingEndpointGroup resourceName := "aws_globalaccelerator_custom_routing_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -80,7 +80,7 @@ func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_disappears(t *testing.T) func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_endpointConfiguration(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingEndpointGroup + var v awstypes.CustomRoutingEndpointGroup resourceName := "aws_globalaccelerator_custom_routing_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -116,7 +116,7 @@ func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_endpointConfiguration(t func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_endpointGroupRegion(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingEndpointGroup + var v awstypes.CustomRoutingEndpointGroup resourceName := "aws_globalaccelerator_custom_routing_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -149,18 +149,14 @@ func TestAccGlobalAcceleratorCustomRoutingEndpointGroup_endpointGroupRegion(t *t }) } -func testAccCheckCustomRoutingEndpointGroupExists(ctx context.Context, n string, v *globalaccelerator.CustomRoutingEndpointGroup) resource.TestCheckFunc { +func testAccCheckCustomRoutingEndpointGroupExists(ctx context.Context, n string, v *awstypes.CustomRoutingEndpointGroup) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Custom Routing Endpoint Group ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) output, err := tfglobalaccelerator.FindCustomRoutingEndpointGroupByARN(ctx, conn, rs.Primary.ID) @@ -176,7 +172,7 @@ func testAccCheckCustomRoutingEndpointGroupExists(ctx context.Context, n string, func testAccCheckCustomRoutingEndpointGroupDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_custom_routing_endpoint_group" { diff --git a/internal/service/globalaccelerator/custom_routing_listener.go b/internal/service/globalaccelerator/custom_routing_listener.go index 6862da3a2e6..3f4e50137ca 100644 --- a/internal/service/globalaccelerator/custom_routing_listener.go +++ b/internal/service/globalaccelerator/custom_routing_listener.go @@ -8,21 +8,22 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -// @SDKResource("aws_globalaccelerator_custom_routing_listener") -func ResourceCustomRoutingListener() *schema.Resource { +// @SDKResource("aws_globalaccelerator_custom_routing_listener", name="Custom Routing Listener") +func resourceCustomRoutingListener() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceCustomRoutingListenerCreate, ReadWithoutTimeout: resourceCustomRoutingListenerRead, @@ -71,7 +72,7 @@ func ResourceCustomRoutingListener() *schema.Resource { func resourceCustomRoutingListenerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) acceleratorARN := d.Get("accelerator_arn").(string) input := &globalaccelerator.CreateCustomRoutingListenerInput{ @@ -80,17 +81,17 @@ func resourceCustomRoutingListenerCreate(ctx context.Context, d *schema.Resource PortRanges: expandPortRanges(d.Get("port_range").(*schema.Set).List()), } - output, err := conn.CreateCustomRoutingListenerWithContext(ctx, input) + output, err := conn.CreateCustomRoutingListener(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Custom Routing Listener: %s", err) } - d.SetId(aws.StringValue(output.Listener.ListenerArn)) + d.SetId(aws.ToString(output.Listener.ListenerArn)) // Creating a listener triggers the accelerator to change status to InPending. if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceCustomRoutingListenerRead(ctx, d, meta)...) @@ -98,9 +99,9 @@ func resourceCustomRoutingListenerCreate(ctx context.Context, d *schema.Resource func resourceCustomRoutingListenerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - listener, err := FindCustomRoutingListenerByARN(ctx, conn, d.Id()) + listener, err := findCustomRoutingListenerByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator Custom Routing Listener (%s) not found, removing from state", d.Id()) @@ -112,8 +113,7 @@ func resourceCustomRoutingListenerRead(ctx context.Context, d *schema.ResourceDa return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Custom Routing Listener (%s): %s", d.Id(), err) } - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) - + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -128,15 +128,15 @@ func resourceCustomRoutingListenerRead(ctx context.Context, d *schema.ResourceDa func resourceCustomRoutingListenerUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - acceleratorARN := d.Get("accelerator_arn").(string) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) + acceleratorARN := d.Get("accelerator_arn").(string) input := &globalaccelerator.UpdateCustomRoutingListenerInput{ ListenerArn: aws.String(d.Id()), PortRanges: expandPortRanges(d.Get("port_range").(*schema.Set).List()), } - _, err := conn.UpdateCustomRoutingListenerWithContext(ctx, input) + _, err := conn.UpdateCustomRoutingListener(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Custom Routing Listener (%s): %s", d.Id(), err) @@ -144,7 +144,7 @@ func resourceCustomRoutingListenerUpdate(ctx context.Context, d *schema.Resource // Updating a listener triggers the accelerator to change status to InPending. if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceCustomRoutingListenerRead(ctx, d, meta)...) @@ -152,16 +152,14 @@ func resourceCustomRoutingListenerUpdate(ctx context.Context, d *schema.Resource func resourceCustomRoutingListenerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - - acceleratorARN := d.Get("accelerator_arn").(string) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) log.Printf("[DEBUG] Deleting Global Accelerator Custom Routing Listener (%s)", d.Id()) - _, err := conn.DeleteCustomRoutingListenerWithContext(ctx, &globalaccelerator.DeleteCustomRoutingListenerInput{ + _, err := conn.DeleteCustomRoutingListener(ctx, &globalaccelerator.DeleteCustomRoutingListenerInput{ ListenerArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeListenerNotFoundException) { + if errs.IsA[*awstypes.ListenerNotFoundException](err) { return diags } @@ -170,25 +168,22 @@ func resourceCustomRoutingListenerDelete(ctx context.Context, d *schema.Resource } // Deleting a listener triggers the accelerator to change status to InPending. + acceleratorARN := d.Get("accelerator_arn").(string) if _, err := waitCustomRoutingAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutDelete)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Custom Routing Accelerator (%s) deploy: %s", acceleratorARN, err) } return diags } -func FindCustomRoutingListenerByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.CustomRoutingListener, error) { +func findCustomRoutingListenerByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.CustomRoutingListener, error) { input := &globalaccelerator.DescribeCustomRoutingListenerInput{ ListenerArn: aws.String(arn), } - return findCustomRoutingListener(ctx, conn, input) -} - -func findCustomRoutingListener(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeCustomRoutingListenerInput) (*globalaccelerator.CustomRoutingListener, error) { - output, err := conn.DescribeCustomRoutingListenerWithContext(ctx, input) + output, err := conn.DescribeCustomRoutingListener(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeListenerNotFoundException) { + if errs.IsA[*awstypes.ListenerNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, diff --git a/internal/service/globalaccelerator/custom_routing_listener_test.go b/internal/service/globalaccelerator/custom_routing_listener_test.go index 426776943ae..157500351cb 100644 --- a/internal/service/globalaccelerator/custom_routing_listener_test.go +++ b/internal/service/globalaccelerator/custom_routing_listener_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -21,7 +21,7 @@ import ( func TestAccGlobalAcceleratorCustomRoutingListener_basic(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingListener + var v awstypes.CustomRoutingListener resourceName := "aws_globalaccelerator_custom_routing_listener.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -57,7 +57,7 @@ func TestAccGlobalAcceleratorCustomRoutingListener_basic(t *testing.T) { func TestAccGlobalAcceleratorCustomRoutingListener_disappears(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.CustomRoutingListener + var v awstypes.CustomRoutingListener resourceName := "aws_globalaccelerator_custom_routing_listener.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -79,18 +79,14 @@ func TestAccGlobalAcceleratorCustomRoutingListener_disappears(t *testing.T) { }) } -func testAccCheckCustomRoutingListenerExists(ctx context.Context, n string, v *globalaccelerator.CustomRoutingListener) resource.TestCheckFunc { +func testAccCheckCustomRoutingListenerExists(ctx context.Context, n string, v *awstypes.CustomRoutingListener) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Custom Routing Listener ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) output, err := tfglobalaccelerator.FindCustomRoutingListenerByARN(ctx, conn, rs.Primary.ID) @@ -106,7 +102,7 @@ func testAccCheckCustomRoutingListenerExists(ctx context.Context, n string, v *g func testAccCheckCustomRoutingListenerDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_custom_routing_listener" { diff --git a/internal/service/globalaccelerator/endpoint_group.go b/internal/service/globalaccelerator/endpoint_group.go index 7cb4e21839f..271baf23c57 100644 --- a/internal/service/globalaccelerator/endpoint_group.go +++ b/internal/service/globalaccelerator/endpoint_group.go @@ -8,22 +8,24 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "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" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -// @SDKResource("aws_globalaccelerator_endpoint_group") -func ResourceEndpointGroup() *schema.Resource { +// @SDKResource("aws_globalaccelerator_endpoint_group", name="Endpoint Group") +func resourceEndpointGroup() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceEndpointGroupCreate, ReadWithoutTimeout: resourceEndpointGroupRead, @@ -94,10 +96,10 @@ func ResourceEndpointGroup() *schema.Resource { ValidateFunc: validation.IsPortNumber, }, "health_check_protocol": { - Type: schema.TypeString, - Optional: true, - Default: globalaccelerator.HealthCheckProtocolTcp, - ValidateFunc: validation.StringInSlice(globalaccelerator.HealthCheckProtocol_Values(), false), + Type: schema.TypeString, + Optional: true, + Default: awstypes.HealthCheckProtocolTcp, + ValidateDiagFunc: enum.Validate[awstypes.HealthCheckProtocol](), }, "listener_arn": { Type: schema.TypeString, @@ -142,8 +144,7 @@ func ResourceEndpointGroup() *schema.Resource { func resourceEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.CreateEndpointGroupInput{ EndpointGroupRegion: aws.String(meta.(*conns.AWSClient).Region), @@ -160,7 +161,7 @@ func resourceEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, me } if v, ok := d.GetOk("health_check_interval_seconds"); ok { - input.HealthCheckIntervalSeconds = aws.Int64(int64(v.(int))) + input.HealthCheckIntervalSeconds = aws.Int32(int32(v.(int))) } if v, ok := d.GetOk("health_check_path"); ok { @@ -168,11 +169,11 @@ func resourceEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, me } if v, ok := d.GetOk("health_check_port"); ok { - input.HealthCheckPort = aws.Int64(int64(v.(int))) + input.HealthCheckPort = aws.Int32(int32(v.(int))) } if v, ok := d.GetOk("health_check_protocol"); ok { - input.HealthCheckProtocol = aws.String(v.(string)) + input.HealthCheckProtocol = awstypes.HealthCheckProtocol(v.(string)) } if v, ok := d.GetOk("port_override"); ok && v.(*schema.Set).Len() > 0 { @@ -180,29 +181,28 @@ func resourceEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, me } if v, ok := d.GetOk("threshold_count"); ok { - input.ThresholdCount = aws.Int64(int64(v.(int))) + input.ThresholdCount = aws.Int32(int32(v.(int))) } if v, ok := d.Get("traffic_dial_percentage").(float64); ok { - input.TrafficDialPercentage = aws.Float64(v) + input.TrafficDialPercentage = aws.Float32(float32(v)) } - resp, err := conn.CreateEndpointGroupWithContext(ctx, input) + output, err := conn.CreateEndpointGroup(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Endpoint Group: %s", err) } - d.SetId(aws.StringValue(resp.EndpointGroup.EndpointGroupArn)) - - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) + d.SetId(aws.ToString(output.EndpointGroup.EndpointGroupArn)) + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceEndpointGroupRead(ctx, d, meta)...) @@ -210,10 +210,9 @@ func resourceEndpointGroupCreate(ctx context.Context, d *schema.ResourceData, me func resourceEndpointGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - - endpointGroup, err := FindEndpointGroupByARN(ctx, conn, d.Id()) + endpointGroup, err := findEndpointGroupByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator endpoint group (%s) not found, removing from state", d.Id()) @@ -225,8 +224,7 @@ func resourceEndpointGroupRead(ctx context.Context, d *schema.ResourceData, meta return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Endpoint Group (%s): %s", d.Id(), err) } - listenerARN, err := EndpointGroupARNToListenerARN(d.Id()) - + listenerARN, err := endpointGroupARNToListenerARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -252,8 +250,7 @@ func resourceEndpointGroupRead(ctx context.Context, d *schema.ResourceData, meta func resourceEndpointGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) input := &globalaccelerator.UpdateEndpointGroupInput{ EndpointGroupArn: aws.String(d.Id()), @@ -262,11 +259,11 @@ func resourceEndpointGroupUpdate(ctx context.Context, d *schema.ResourceData, me if v, ok := d.GetOk("endpoint_configuration"); ok && v.(*schema.Set).Len() > 0 { input.EndpointConfigurations = expandEndpointConfigurations(v.(*schema.Set).List()) } else { - input.EndpointConfigurations = []*globalaccelerator.EndpointConfiguration{} + input.EndpointConfigurations = []awstypes.EndpointConfiguration{} } if v, ok := d.GetOk("health_check_interval_seconds"); ok { - input.HealthCheckIntervalSeconds = aws.Int64(int64(v.(int))) + input.HealthCheckIntervalSeconds = aws.Int32(int32(v.(int))) } if v, ok := d.GetOk("health_check_path"); ok { @@ -274,41 +271,40 @@ func resourceEndpointGroupUpdate(ctx context.Context, d *schema.ResourceData, me } if v, ok := d.GetOk("health_check_port"); ok { - input.HealthCheckPort = aws.Int64(int64(v.(int))) + input.HealthCheckPort = aws.Int32(int32(v.(int))) } if v, ok := d.GetOk("health_check_protocol"); ok { - input.HealthCheckProtocol = aws.String(v.(string)) + input.HealthCheckProtocol = awstypes.HealthCheckProtocol(v.(string)) } if v, ok := d.GetOk("port_override"); ok && v.(*schema.Set).Len() > 0 { input.PortOverrides = expandPortOverrides(v.(*schema.Set).List()) } else { - input.PortOverrides = []*globalaccelerator.PortOverride{} + input.PortOverrides = []awstypes.PortOverride{} } if v, ok := d.GetOk("threshold_count"); ok { - input.ThresholdCount = aws.Int64(int64(v.(int))) + input.ThresholdCount = aws.Int32(int32(v.(int))) } if v, ok := d.Get("traffic_dial_percentage").(float64); ok { - input.TrafficDialPercentage = aws.Float64(v) + input.TrafficDialPercentage = aws.Float32(float32(v)) } - _, err := conn.UpdateEndpointGroupWithContext(ctx, input) + _, err := conn.UpdateEndpointGroup(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Endpoint Group (%s): %s", d.Id(), err) } - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) - + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceEndpointGroupRead(ctx, d, meta)...) @@ -316,15 +312,14 @@ func resourceEndpointGroupUpdate(ctx context.Context, d *schema.ResourceData, me func resourceEndpointGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) log.Printf("[DEBUG] Deleting Global Accelerator Endpoint Group: %s", d.Id()) - _, err := conn.DeleteEndpointGroupWithContext(ctx, &globalaccelerator.DeleteEndpointGroupInput{ + _, err := conn.DeleteEndpointGroup(ctx, &globalaccelerator.DeleteEndpointGroupInput{ EndpointGroupArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeEndpointGroupNotFoundException) { + if errs.IsA[*awstypes.EndpointGroupNotFoundException](err) { return diags } @@ -332,31 +327,26 @@ func resourceEndpointGroupDelete(ctx context.Context, d *schema.ResourceData, me return sdkdiag.AppendErrorf(diags, "deleting Global Accelerator Endpoint Group (%s): %s", d.Id(), err) } - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) - + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutDelete)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return diags } -func FindEndpointGroupByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.EndpointGroup, error) { +func findEndpointGroupByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.EndpointGroup, error) { input := &globalaccelerator.DescribeEndpointGroupInput{ EndpointGroupArn: aws.String(arn), } - return findEndpointGroup(ctx, conn, input) -} - -func findEndpointGroup(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeEndpointGroupInput) (*globalaccelerator.EndpointGroup, error) { - output, err := conn.DescribeEndpointGroupWithContext(ctx, input) + output, err := conn.DescribeEndpointGroup(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeEndpointGroupNotFoundException) { + if errs.IsA[*awstypes.EndpointGroupNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -374,12 +364,12 @@ func findEndpointGroup(ctx context.Context, conn *globalaccelerator.GlobalAccele return output.EndpointGroup, nil } -func expandEndpointConfiguration(tfMap map[string]interface{}) *globalaccelerator.EndpointConfiguration { +func expandEndpointConfiguration(tfMap map[string]interface{}) *awstypes.EndpointConfiguration { if tfMap == nil { return nil } - apiObject := &globalaccelerator.EndpointConfiguration{} + apiObject := &awstypes.EndpointConfiguration{} if v, ok := tfMap["client_ip_preservation_enabled"].(bool); ok { apiObject.ClientIPPreservationEnabled = aws.Bool(v) @@ -390,18 +380,18 @@ func expandEndpointConfiguration(tfMap map[string]interface{}) *globalaccelerato } if v, ok := tfMap["weight"].(int); ok { - apiObject.Weight = aws.Int64(int64(v)) + apiObject.Weight = aws.Int32(int32(v)) } return apiObject } -func expandEndpointConfigurations(tfList []interface{}) []*globalaccelerator.EndpointConfiguration { +func expandEndpointConfigurations(tfList []interface{}) []awstypes.EndpointConfiguration { if len(tfList) == 0 { return nil } - var apiObjects []*globalaccelerator.EndpointConfiguration + var apiObjects []awstypes.EndpointConfiguration for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -416,36 +406,36 @@ func expandEndpointConfigurations(tfList []interface{}) []*globalaccelerator.End continue } - apiObjects = append(apiObjects, apiObject) + apiObjects = append(apiObjects, *apiObject) } return apiObjects } -func expandPortOverride(tfMap map[string]interface{}) *globalaccelerator.PortOverride { +func expandPortOverride(tfMap map[string]interface{}) *awstypes.PortOverride { if tfMap == nil { return nil } - apiObject := &globalaccelerator.PortOverride{} + apiObject := &awstypes.PortOverride{} if v, ok := tfMap["endpoint_port"].(int); ok && v != 0 { - apiObject.EndpointPort = aws.Int64(int64(v)) + apiObject.EndpointPort = aws.Int32(int32(v)) } if v, ok := tfMap["listener_port"].(int); ok && v != 0 { - apiObject.ListenerPort = aws.Int64(int64(v)) + apiObject.ListenerPort = aws.Int32(int32(v)) } return apiObject } -func expandPortOverrides(tfList []interface{}) []*globalaccelerator.PortOverride { +func expandPortOverrides(tfList []interface{}) []awstypes.PortOverride { if len(tfList) == 0 { return nil } - var apiObjects []*globalaccelerator.PortOverride + var apiObjects []awstypes.PortOverride for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -460,13 +450,13 @@ func expandPortOverrides(tfList []interface{}) []*globalaccelerator.PortOverride continue } - apiObjects = append(apiObjects, apiObject) + apiObjects = append(apiObjects, *apiObject) } return apiObjects } -func flattenEndpointDescription(apiObject *globalaccelerator.EndpointDescription) map[string]interface{} { +func flattenEndpointDescription(apiObject *awstypes.EndpointDescription) map[string]interface{} { if apiObject == nil { return nil } @@ -474,21 +464,21 @@ func flattenEndpointDescription(apiObject *globalaccelerator.EndpointDescription tfMap := map[string]interface{}{} if v := apiObject.ClientIPPreservationEnabled; v != nil { - tfMap["client_ip_preservation_enabled"] = aws.BoolValue(v) + tfMap["client_ip_preservation_enabled"] = aws.ToBool(v) } if v := apiObject.EndpointId; v != nil { - tfMap["endpoint_id"] = aws.StringValue(v) + tfMap["endpoint_id"] = aws.ToString(v) } if v := apiObject.Weight; v != nil { - tfMap["weight"] = aws.Int64Value(v) + tfMap["weight"] = aws.ToInt32(v) } return tfMap } -func flattenEndpointDescriptions(apiObjects []*globalaccelerator.EndpointDescription) []interface{} { +func flattenEndpointDescriptions(apiObjects []awstypes.EndpointDescription) []interface{} { if len(apiObjects) == 0 { return nil } @@ -496,17 +486,13 @@ func flattenEndpointDescriptions(apiObjects []*globalaccelerator.EndpointDescrip var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenEndpointDescription(apiObject)) + tfList = append(tfList, flattenEndpointDescription(&apiObject)) } return tfList } -func flattenPortOverride(apiObject *globalaccelerator.PortOverride) map[string]interface{} { +func flattenPortOverride(apiObject *awstypes.PortOverride) map[string]interface{} { if apiObject == nil { return nil } @@ -514,17 +500,17 @@ func flattenPortOverride(apiObject *globalaccelerator.PortOverride) map[string]i tfMap := map[string]interface{}{} if v := apiObject.EndpointPort; v != nil { - tfMap["endpoint_port"] = aws.Int64Value(v) + tfMap["endpoint_port"] = aws.ToInt32(v) } if v := apiObject.ListenerPort; v != nil { - tfMap["listener_port"] = aws.Int64Value(v) + tfMap["listener_port"] = aws.ToInt32(v) } return tfMap } -func flattenPortOverrides(apiObjects []*globalaccelerator.PortOverride) []interface{} { +func flattenPortOverrides(apiObjects []awstypes.PortOverride) []interface{} { if len(apiObjects) == 0 { return nil } @@ -532,11 +518,7 @@ func flattenPortOverrides(apiObjects []*globalaccelerator.PortOverride) []interf var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenPortOverride(apiObject)) + tfList = append(tfList, flattenPortOverride(&apiObject)) } return tfList diff --git a/internal/service/globalaccelerator/endpoint_group_test.go b/internal/service/globalaccelerator/endpoint_group_test.go index 3ba36305ae6..daba75804e6 100644 --- a/internal/service/globalaccelerator/endpoint_group_test.go +++ b/internal/service/globalaccelerator/endpoint_group_test.go @@ -9,9 +9,9 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/aws/aws-sdk-go/service/globalaccelerator" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -25,7 +25,7 @@ import ( func TestAccGlobalAcceleratorEndpointGroup_basic(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -63,7 +63,7 @@ func TestAccGlobalAcceleratorEndpointGroup_basic(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_disappears(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -87,7 +87,7 @@ func TestAccGlobalAcceleratorEndpointGroup_disappears(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_ALBEndpoint_clientIP(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup var vpc ec2.Vpc resourceName := "aws_globalaccelerator_endpoint_group.test" albResourceName := "aws_lb.test" @@ -162,7 +162,7 @@ func TestAccGlobalAcceleratorEndpointGroup_ALBEndpoint_clientIP(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_instanceEndpoint(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup var vpc ec2.Vpc resourceName := "aws_globalaccelerator_endpoint_group.test" instanceResourceName := "aws_instance.test" @@ -215,7 +215,7 @@ func TestAccGlobalAcceleratorEndpointGroup_instanceEndpoint(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_multiRegion(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" eipResourceName := "aws_eip.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -259,7 +259,7 @@ func TestAccGlobalAcceleratorEndpointGroup_multiRegion(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_portOverrides(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -326,7 +326,7 @@ func TestAccGlobalAcceleratorEndpointGroup_portOverrides(t *testing.T) { func TestAccGlobalAcceleratorEndpointGroup_tcpHealthCheckProtocol(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" eipResourceName := "aws_eip.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -370,7 +370,7 @@ func TestAccGlobalAcceleratorEndpointGroup_tcpHealthCheckProtocol(t *testing.T) func TestAccGlobalAcceleratorEndpointGroup_update(t *testing.T) { ctx := acctest.Context(t) - var v globalaccelerator.EndpointGroup + var v awstypes.EndpointGroup resourceName := "aws_globalaccelerator_endpoint_group.test" eipResourceName := "aws_eip.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -429,18 +429,14 @@ func TestAccGlobalAcceleratorEndpointGroup_update(t *testing.T) { }) } -func testAccCheckEndpointGroupExists(ctx context.Context, name string, v *globalaccelerator.EndpointGroup) resource.TestCheckFunc { +func testAccCheckEndpointGroupExists(ctx context.Context, name string, v *awstypes.EndpointGroup) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Endpoint Group ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) endpointGroup, err := tfglobalaccelerator.FindEndpointGroupByARN(ctx, conn, rs.Primary.ID) @@ -456,7 +452,7 @@ func testAccCheckEndpointGroupExists(ctx context.Context, name string, v *global func testAccCheckEndpointGroupDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_endpoint_group" { @@ -486,7 +482,7 @@ func testAccCheckEndpointGroupDeleteSecurityGroup(ctx context.Context, vpc *ec2. meta := acctest.Provider.Meta() conn := meta.(*conns.AWSClient).EC2Conn(ctx) - v, err := tfec2.FindSecurityGroupByNameAndVPCIDAndOwnerID(ctx, conn, "GlobalAccelerator", aws.StringValue(vpc.VpcId), aws.StringValue(vpc.OwnerId)) + v, err := tfec2.FindSecurityGroupByNameAndVPCIDAndOwnerID(ctx, conn, "GlobalAccelerator", aws.ToString(vpc.VpcId), aws.ToString(vpc.OwnerId)) if tfresource.NotFound(err) { // Already gone. @@ -499,7 +495,7 @@ func testAccCheckEndpointGroupDeleteSecurityGroup(ctx context.Context, vpc *ec2. r := tfec2.ResourceSecurityGroup() d := r.Data(nil) - d.SetId(aws.StringValue(v.GroupId)) + d.SetId(aws.ToString(v.GroupId)) d.Set("revoke_rules_on_delete", true) err = acctest.DeleteResource(ctx, r, d, meta) @@ -532,7 +528,7 @@ resource "aws_globalaccelerator_endpoint_group" "test" { `, rName) } -func testAccEndpointGroupConfig_albClientIP(rName string, clientIP bool, weight int) string { +func testAccEndpointGroupConfig_baseALB(rName string) string { return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 2), fmt.Sprintf(` resource "aws_lb" "test" { name = %[1]q @@ -578,7 +574,11 @@ resource "aws_internet_gateway" "test" { Name = %[1]q } } +`, rName)) +} +func testAccEndpointGroupConfig_albClientIP(rName string, clientIP bool, weight int) string { + return acctest.ConfigCompose(testAccEndpointGroupConfig_baseALB(rName), fmt.Sprintf(` resource "aws_globalaccelerator_accelerator" "test" { name = %[1]q ip_address_type = "IPV4" diff --git a/internal/service/globalaccelerator/exports_test.go b/internal/service/globalaccelerator/exports_test.go new file mode 100644 index 00000000000..2a5c6fee917 --- /dev/null +++ b/internal/service/globalaccelerator/exports_test.go @@ -0,0 +1,23 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package globalaccelerator + +// Exports for use in tests only. +var ( + ResourceAccelerator = resourceAccelerator + ResourceCrossAccountAttachment = newCrossAccountAttachmentResource + ResourceCustomRoutingAccelerator = resourceCustomRoutingAccelerator + ResourceCustomRoutingEndpointGroup = resourceCustomRoutingEndpointGroup + ResourceCustomRoutingListener = resourceCustomRoutingListener + ResourceEndpointGroup = resourceEndpointGroup + ResourceListener = resourceListener + + FindAcceleratorByARN = findAcceleratorByARN + FindCrossAccountAttachmentByARN = findCrossAccountAttachmentByARN + FindCustomRoutingAcceleratorByARN = findCustomRoutingAcceleratorByARN + FindCustomRoutingEndpointGroupByARN = findCustomRoutingEndpointGroupByARN + FindCustomRoutingListenerByARN = findCustomRoutingListenerByARN + FindEndpointGroupByARN = findEndpointGroupByARN + FindListenerByARN = findListenerByARN +) diff --git a/internal/service/globalaccelerator/generate.go b/internal/service/globalaccelerator/generate.go index e3f90ea20bd..77472c95ab3 100644 --- a/internal/service/globalaccelerator/generate.go +++ b/internal/service/globalaccelerator/generate.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsSlice -UpdateTags +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsSlice -UpdateTags //go:generate go run ../../generate/servicepackage/main.go // ONLY generate directives and package declaration! Do not add anything else to this file. diff --git a/internal/service/globalaccelerator/listener.go b/internal/service/globalaccelerator/listener.go index 07895de892d..dfb5a322329 100644 --- a/internal/service/globalaccelerator/listener.go +++ b/internal/service/globalaccelerator/listener.go @@ -8,21 +8,23 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "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" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "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" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -// @SDKResource("aws_globalaccelerator_listener") -func ResourceListener() *schema.Resource { +// @SDKResource("aws_globalaccelerator_listener", name="Listener") +func resourceListener() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceListenerCreate, ReadWithoutTimeout: resourceListenerRead, @@ -46,10 +48,10 @@ func ResourceListener() *schema.Resource { ForceNew: true, }, "client_affinity": { - Type: schema.TypeString, - Optional: true, - Default: globalaccelerator.ClientAffinityNone, - ValidateFunc: validation.StringInSlice(globalaccelerator.ClientAffinity_Values(), false), + Type: schema.TypeString, + Optional: true, + Default: awstypes.ClientAffinityNone, + ValidateDiagFunc: enum.Validate[awstypes.ClientAffinity](), }, "port_range": { Type: schema.TypeSet, @@ -72,9 +74,9 @@ func ResourceListener() *schema.Resource { }, }, "protocol": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice(globalaccelerator.Protocol_Values(), false), + Type: schema.TypeString, + Required: true, + ValidateDiagFunc: enum.Validate[awstypes.Protocol](), }, }, } @@ -82,29 +84,28 @@ func ResourceListener() *schema.Resource { func resourceListenerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) acceleratorARN := d.Get("accelerator_arn").(string) - input := &globalaccelerator.CreateListenerInput{ AcceleratorArn: aws.String(acceleratorARN), - ClientAffinity: aws.String(d.Get("client_affinity").(string)), + ClientAffinity: awstypes.ClientAffinity(d.Get("client_affinity").(string)), IdempotencyToken: aws.String(id.UniqueId()), PortRanges: expandPortRanges(d.Get("port_range").(*schema.Set).List()), - Protocol: aws.String(d.Get("protocol").(string)), + Protocol: awstypes.Protocol(d.Get("protocol").(string)), } - resp, err := conn.CreateListenerWithContext(ctx, input) + output, err := conn.CreateListener(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Global Accelerator Listener: %s", err) } - d.SetId(aws.StringValue(resp.Listener.ListenerArn)) + d.SetId(aws.ToString(output.Listener.ListenerArn)) // Creating a listener triggers the accelerator to change status to InPending. if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceListenerRead(ctx, d, meta)...) @@ -112,10 +113,9 @@ func resourceListenerCreate(ctx context.Context, d *schema.ResourceData, meta in func resourceListenerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - - listener, err := FindListenerByARN(ctx, conn, d.Id()) + listener, err := findListenerByARN(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Global Accelerator Listener (%s) not found, removing from state", d.Id()) @@ -127,8 +127,7 @@ func resourceListenerRead(ctx context.Context, d *schema.ResourceData, meta inte return sdkdiag.AppendErrorf(diags, "reading Global Accelerator Listener (%s): %s", d.Id(), err) } - acceleratorARN, err := ListenerOrEndpointGroupARNToAcceleratorARN(d.Id()) - + acceleratorARN, err := listenerOrEndpointGroupARNToAcceleratorARN(d.Id()) if err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -145,18 +144,17 @@ func resourceListenerRead(ctx context.Context, d *schema.ResourceData, meta inte func resourceListenerUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) acceleratorARN := d.Get("accelerator_arn").(string) - input := &globalaccelerator.UpdateListenerInput{ - ClientAffinity: aws.String(d.Get("client_affinity").(string)), + ClientAffinity: awstypes.ClientAffinity(d.Get("client_affinity").(string)), ListenerArn: aws.String(d.Id()), PortRanges: expandPortRanges(d.Get("port_range").(*schema.Set).List()), - Protocol: aws.String(d.Get("protocol").(string)), + Protocol: awstypes.Protocol(d.Get("protocol").(string)), } - _, err := conn.UpdateListenerWithContext(ctx, input) + _, err := conn.UpdateListener(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Global Accelerator Listener (%s): %s", d.Id(), err) @@ -164,7 +162,7 @@ func resourceListenerUpdate(ctx context.Context, d *schema.ResourceData, meta in // Updating a listener triggers the accelerator to change status to InPending. if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return append(diags, resourceListenerRead(ctx, d, meta)...) @@ -172,16 +170,14 @@ func resourceListenerUpdate(ctx context.Context, d *schema.ResourceData, meta in func resourceListenerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - - conn := meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx) - acceleratorARN := d.Get("accelerator_arn").(string) + conn := meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx) log.Printf("[DEBUG] Deleting Global Accelerator Listener: %s", d.Id()) - _, err := conn.DeleteListenerWithContext(ctx, &globalaccelerator.DeleteListenerInput{ + _, err := conn.DeleteListener(ctx, &globalaccelerator.DeleteListenerInput{ ListenerArn: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeListenerNotFoundException) { + if errs.IsA[*awstypes.ListenerNotFoundException](err) { return diags } @@ -190,25 +186,22 @@ func resourceListenerDelete(ctx context.Context, d *schema.ResourceData, meta in } // Deleting a listener triggers the accelerator to change status to InPending. + acceleratorARN := d.Get("accelerator_arn").(string) if _, err := waitAcceleratorDeployed(ctx, conn, acceleratorARN, d.Timeout(schema.TimeoutDelete)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deployment: %s", acceleratorARN, err) + return sdkdiag.AppendErrorf(diags, "waiting for Global Accelerator Accelerator (%s) deploy: %s", acceleratorARN, err) } return diags } -func FindListenerByARN(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, arn string) (*globalaccelerator.Listener, error) { +func findListenerByARN(ctx context.Context, conn *globalaccelerator.Client, arn string) (*awstypes.Listener, error) { input := &globalaccelerator.DescribeListenerInput{ ListenerArn: aws.String(arn), } - return findListener(ctx, conn, input) -} - -func findListener(ctx context.Context, conn *globalaccelerator.GlobalAccelerator, input *globalaccelerator.DescribeListenerInput) (*globalaccelerator.Listener, error) { - output, err := conn.DescribeListenerWithContext(ctx, input) + output, err := conn.DescribeListener(ctx, input) - if tfawserr.ErrCodeEquals(err, globalaccelerator.ErrCodeListenerNotFoundException) { + if errs.IsA[*awstypes.ListenerNotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -226,30 +219,30 @@ func findListener(ctx context.Context, conn *globalaccelerator.GlobalAccelerator return output.Listener, nil } -func expandPortRange(tfMap map[string]interface{}) *globalaccelerator.PortRange { +func expandPortRange(tfMap map[string]interface{}) *awstypes.PortRange { if tfMap == nil { return nil } - apiObject := &globalaccelerator.PortRange{} + apiObject := &awstypes.PortRange{} if v, ok := tfMap["from_port"].(int); ok && v != 0 { - apiObject.FromPort = aws.Int64(int64(v)) + apiObject.FromPort = aws.Int32(int32(v)) } if v, ok := tfMap["to_port"].(int); ok && v != 0 { - apiObject.ToPort = aws.Int64(int64(v)) + apiObject.ToPort = aws.Int32(int32(v)) } return apiObject } -func expandPortRanges(tfList []interface{}) []*globalaccelerator.PortRange { +func expandPortRanges(tfList []interface{}) []awstypes.PortRange { if len(tfList) == 0 { return nil } - var apiObjects []*globalaccelerator.PortRange + var apiObjects []awstypes.PortRange for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]interface{}) @@ -264,13 +257,13 @@ func expandPortRanges(tfList []interface{}) []*globalaccelerator.PortRange { continue } - apiObjects = append(apiObjects, apiObject) + apiObjects = append(apiObjects, *apiObject) } return apiObjects } -func flattenPortRange(apiObject *globalaccelerator.PortRange) map[string]interface{} { +func flattenPortRange(apiObject *awstypes.PortRange) map[string]interface{} { if apiObject == nil { return nil } @@ -278,17 +271,17 @@ func flattenPortRange(apiObject *globalaccelerator.PortRange) map[string]interfa tfMap := map[string]interface{}{} if v := apiObject.FromPort; v != nil { - tfMap["from_port"] = aws.Int64Value(v) + tfMap["from_port"] = aws.ToInt32(v) } if v := apiObject.ToPort; v != nil { - tfMap["to_port"] = aws.Int64Value(v) + tfMap["to_port"] = aws.ToInt32(v) } return tfMap } -func flattenPortRanges(apiObjects []*globalaccelerator.PortRange) []interface{} { +func flattenPortRanges(apiObjects []awstypes.PortRange) []interface{} { if len(apiObjects) == 0 { return nil } @@ -296,11 +289,7 @@ func flattenPortRanges(apiObjects []*globalaccelerator.PortRange) []interface{} var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, flattenPortRange(apiObject)) + tfList = append(tfList, flattenPortRange(&apiObject)) } return tfList diff --git a/internal/service/globalaccelerator/listener_test.go b/internal/service/globalaccelerator/listener_test.go index 0d39c5f000a..7cb988d6aaf 100644 --- a/internal/service/globalaccelerator/listener_test.go +++ b/internal/service/globalaccelerator/listener_test.go @@ -112,16 +112,12 @@ func TestAccGlobalAcceleratorListener_update(t *testing.T) { func testAccCheckListenerExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) - rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Global Accelerator Listener ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) _, err := tfglobalaccelerator.FindListenerByARN(ctx, conn, rs.Primary.ID) @@ -131,7 +127,7 @@ func testAccCheckListenerExists(ctx context.Context, n string) resource.TestChec func testAccCheckListenerDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).GlobalAcceleratorClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_globalaccelerator_listener" { diff --git a/internal/service/globalaccelerator/service_endpoints_gen_test.go b/internal/service/globalaccelerator/service_endpoints_gen_test.go index beddae7df67..0d337e6ad70 100644 --- a/internal/service/globalaccelerator/service_endpoints_gen_test.go +++ b/internal/service/globalaccelerator/service_endpoints_gen_test.go @@ -4,17 +4,17 @@ package globalaccelerator_test import ( "context" + "errors" "fmt" "maps" - "net/url" "os" "path/filepath" "reflect" "strings" "testing" - "github.com/aws/aws-sdk-go/aws/endpoints" - globalaccelerator_sdkv1 "github.com/aws/aws-sdk-go/service/globalaccelerator" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + globalaccelerator_sdkv2 "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" "github.com/aws/smithy-go/middleware" smithyhttp "github.com/aws/smithy-go/transport/http" "github.com/google/go-cmp/cmp" @@ -212,34 +212,42 @@ func TestEndpointConfiguration(t *testing.T) { //nolint:paralleltest // uses t.S } func defaultEndpoint(region string) string { - r := endpoints.DefaultResolver() + r := globalaccelerator_sdkv2.NewDefaultEndpointResolverV2() - ep, err := r.EndpointFor(globalaccelerator_sdkv1.EndpointsID, region, func(opt *endpoints.Options) { - opt.ResolveUnknownService = true + ep, err := r.ResolveEndpoint(context.Background(), globalaccelerator_sdkv2.EndpointParameters{ + Region: aws_sdkv2.String(region), }) if err != nil { return err.Error() } - url, _ := url.Parse(ep.URL) - - if url.Path == "" { - url.Path = "/" + if ep.URI.Path == "" { + ep.URI.Path = "/" } - return url.String() + return ep.URI.String() } func callService(ctx context.Context, t *testing.T, meta *conns.AWSClient) string { t.Helper() - client := meta.GlobalAcceleratorConn(ctx) - - req, _ := client.ListAcceleratorsRequest(&globalaccelerator_sdkv1.ListAcceleratorsInput{}) + var endpoint string - req.HTTPRequest.URL.Path = "/" + client := meta.GlobalAcceleratorClient(ctx) - endpoint := req.HTTPRequest.URL.String() + _, err := client.ListAccelerators(ctx, &globalaccelerator_sdkv2.ListAcceleratorsInput{}, + func(opts *globalaccelerator_sdkv2.Options) { + opts.APIOptions = append(opts.APIOptions, + addRetrieveEndpointURLMiddleware(t, &endpoint), + addCancelRequestMiddleware(), + ) + }, + ) + if err == nil { + t.Fatal("Expected an error, got none") + } else if !errors.Is(err, errCancelOperation) { + t.Fatalf("Unexpected error: %s", err) + } return endpoint } diff --git a/internal/service/globalaccelerator/service_package.go b/internal/service/globalaccelerator/service_package.go index 4126e265623..f6ebfa80016 100644 --- a/internal/service/globalaccelerator/service_package.go +++ b/internal/service/globalaccelerator/service_package.go @@ -6,21 +6,21 @@ package globalaccelerator import ( "context" - aws_sdkv1 "github.com/aws/aws-sdk-go/aws" - endpoints_sdkv1 "github.com/aws/aws-sdk-go/aws/endpoints" - session_sdkv1 "github.com/aws/aws-sdk-go/aws/session" - globalaccelerator_sdkv1 "github.com/aws/aws-sdk-go/service/globalaccelerator" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + "github.com/hashicorp/terraform-provider-aws/names" ) -// NewConn returns a new AWS SDK for Go v1 client for this service package's AWS API. -func (p *servicePackage) NewConn(ctx context.Context, m map[string]any) (*globalaccelerator_sdkv1.GlobalAccelerator, error) { - sess := m["session"].(*session_sdkv1.Session) - config := &aws_sdkv1.Config{Endpoint: aws_sdkv1.String(m["endpoint"].(string))} +// NewConn returns a new AWS SDK for Go v2 client for this service package's AWS API. +func (p *servicePackage) NewClient(ctx context.Context, config map[string]any) (*globalaccelerator.Client, error) { + cfg := *(config["aws_sdkv2_config"].(*aws.Config)) - // Force "global" services to correct Regions. - if m["partition"].(string) == endpoints_sdkv1.AwsPartitionID { - config.Region = aws_sdkv1.String(endpoints_sdkv1.UsWest2RegionID) - } - - return globalaccelerator_sdkv1.New(sess.Copy(config)), nil + return globalaccelerator.NewFromConfig(cfg, func(o *globalaccelerator.Options) { + if endpoint := config["endpoint"].(string); endpoint != "" { + o.BaseEndpoint = aws.String(endpoint) + } else if config["partition"].(string) == names.StandardPartitionID { + // Global Accelerator endpoint is only available in AWS Commercial us-west-2 Region. + o.Region = names.USWest2RegionID + } + }), nil } diff --git a/internal/service/globalaccelerator/service_package_gen.go b/internal/service/globalaccelerator/service_package_gen.go index ee841fd98c6..f94220966ad 100644 --- a/internal/service/globalaccelerator/service_package_gen.go +++ b/internal/service/globalaccelerator/service_package_gen.go @@ -15,20 +15,30 @@ type servicePackage struct{} func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.ServicePackageFrameworkDataSource { return []*types.ServicePackageFrameworkDataSource{ { - Factory: newDataSourceAccelerator, + Factory: newAcceleratorDataSource, + Name: "Accelerator", }, } } func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.ServicePackageFrameworkResource { - return []*types.ServicePackageFrameworkResource{} + return []*types.ServicePackageFrameworkResource{ + { + Factory: newCrossAccountAttachmentResource, + Name: "Cross-account Attachment", + Tags: &types.ServicePackageResourceTags{ + IdentifierAttribute: "id", + }, + }, + } } func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePackageSDKDataSource { return []*types.ServicePackageSDKDataSource{ { - Factory: DataSourceCustomRoutingAccelerator, + Factory: dataSourceCustomRoutingAccelerator, TypeName: "aws_globalaccelerator_custom_routing_accelerator", + Name: "Custom Routing Accelerator", }, } } @@ -36,7 +46,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePackageSDKResource { return []*types.ServicePackageSDKResource{ { - Factory: ResourceAccelerator, + Factory: resourceAccelerator, TypeName: "aws_globalaccelerator_accelerator", Name: "Accelerator", Tags: &types.ServicePackageResourceTags{ @@ -44,7 +54,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceCustomRoutingAccelerator, + Factory: resourceCustomRoutingAccelerator, TypeName: "aws_globalaccelerator_custom_routing_accelerator", Name: "Custom Routing Accelerator", Tags: &types.ServicePackageResourceTags{ @@ -52,20 +62,24 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceCustomRoutingEndpointGroup, + Factory: resourceCustomRoutingEndpointGroup, TypeName: "aws_globalaccelerator_custom_routing_endpoint_group", + Name: "Custom Routing Endpoint Group", }, { - Factory: ResourceCustomRoutingListener, + Factory: resourceCustomRoutingListener, TypeName: "aws_globalaccelerator_custom_routing_listener", + Name: "Custom Routing Listener", }, { - Factory: ResourceEndpointGroup, + Factory: resourceEndpointGroup, TypeName: "aws_globalaccelerator_endpoint_group", + Name: "Endpoint Group", }, { - Factory: ResourceListener, + Factory: resourceListener, TypeName: "aws_globalaccelerator_listener", + Name: "Listener", }, } } diff --git a/internal/service/globalaccelerator/sweep.go b/internal/service/globalaccelerator/sweep.go index 5a82f7406b0..a340596bb43 100644 --- a/internal/service/globalaccelerator/sweep.go +++ b/internal/service/globalaccelerator/sweep.go @@ -7,12 +7,11 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - multierror "github.com/hashicorp/go-multierror" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/sweep" - "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv1" + "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2" ) func RegisterSweepers() { @@ -65,33 +64,30 @@ func sweepAccelerators(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListAcceleratorsInput{} sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Accelerator sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { - r := ResourceAccelerator() + r := resourceAccelerator() d := r.Data(nil) - d.SetId(aws.StringValue(v.AcceleratorArn)) + d.SetId(aws.ToString(v.AcceleratorArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global Accelerator Accelerator sweep for %s: %s", region, err) - return nil - } - - if err != nil { - return fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err) } err = sweep.SweepOrchestrator(ctx, sweepResources) @@ -109,14 +105,21 @@ func sweepEndpointGroups(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListAcceleratorsInput{} - var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Endpoint Group sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { @@ -124,9 +127,12 @@ func sweepEndpointGroups(region string) error { AcceleratorArn: v.AcceleratorArn, } - err := conn.ListListenersPagesWithContext(ctx, input, func(page *globalaccelerator.ListListenersOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListListenersPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.Listeners { @@ -134,62 +140,34 @@ func sweepEndpointGroups(region string) error { ListenerArn: v.ListenerArn, } - err := conn.ListEndpointGroupsPagesWithContext(ctx, input, func(page *globalaccelerator.ListEndpointGroupsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListEndpointGroupsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.EndpointGroups { - r := ResourceEndpointGroup() + r := resourceEndpointGroup() d := r.Data(nil) - d.SetId(aws.StringValue(v.EndpointGroupArn)) + d.SetId(aws.ToString(v.EndpointGroupArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Endpoint Groups (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Listeners (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global Accelerator Endpoint Group sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err)) } err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Global Accelerator Endpoint Groups (%s): %w", region, err)) + return fmt.Errorf("error sweeping Global Accelerator Endpoint Groups (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepListeners(region string) error { @@ -198,14 +176,21 @@ func sweepListeners(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListAcceleratorsInput{} - var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Endpoint Group sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { @@ -213,50 +198,32 @@ func sweepListeners(region string) error { AcceleratorArn: v.AcceleratorArn, } - err := conn.ListListenersPagesWithContext(ctx, input, func(page *globalaccelerator.ListListenersOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListListenersPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.Listeners { - r := ResourceListener() + r := resourceListener() d := r.Data(nil) - d.SetId(aws.StringValue(v.ListenerArn)) + d.SetId(aws.ToString(v.ListenerArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Listeners (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global Accelerator Listener sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Accelerators (%s): %w", region, err)) } err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Global Accelerator Listeners (%s): %w", region, err)) + return fmt.Errorf("error sweeping Global Accelerator Listeners (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepCustomRoutingAccelerators(region string) error { @@ -265,33 +232,30 @@ func sweepCustomRoutingAccelerators(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListCustomRoutingAcceleratorsInput{} sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListCustomRoutingAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Custom Routing Accelerator sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { - r := ResourceCustomRoutingAccelerator() + r := resourceCustomRoutingAccelerator() d := r.Data(nil) - d.SetId(aws.StringValue(v.AcceleratorArn)) + d.SetId(aws.ToString(v.AcceleratorArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global Accelerator Custom Routing Accelerator sweep for %s: %s", region, err) - return nil - } - - if err != nil { - return fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err) } err = sweep.SweepOrchestrator(ctx, sweepResources) @@ -309,14 +273,21 @@ func sweepCustomRoutingEndpointGroups(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListCustomRoutingAcceleratorsInput{} - var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListCustomRoutingAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Custom Routing Accelerator sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { @@ -324,9 +295,12 @@ func sweepCustomRoutingEndpointGroups(region string) error { AcceleratorArn: v.AcceleratorArn, } - err := conn.ListCustomRoutingListenersPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingListenersOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingListenersPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.Listeners { @@ -334,62 +308,34 @@ func sweepCustomRoutingEndpointGroups(region string) error { ListenerArn: v.ListenerArn, } - err := conn.ListCustomRoutingEndpointGroupsPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingEndpointGroupsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingEndpointGroupsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.EndpointGroups { - r := ResourceCustomRoutingEndpointGroup() + r := resourceCustomRoutingEndpointGroup() d := r.Data(nil) - d.SetId(aws.StringValue(v.EndpointGroupArn)) + d.SetId(aws.ToString(v.EndpointGroupArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Custom Routing Endpoint Groups (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Custom Routing Listeners (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global AcceleratorCustom Routing Endpoint Group sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err)) } err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Global Accelerator Custom Routing Endpoint Groups (%s): %w", region, err)) + return fmt.Errorf("error sweeping Global Accelerator Custom Routing Endpoint Groups (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepCustomRoutingListeners(region string) error { @@ -398,14 +344,21 @@ func sweepCustomRoutingListeners(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.GlobalAcceleratorConn(ctx) + conn := client.GlobalAcceleratorClient(ctx) input := &globalaccelerator.ListCustomRoutingAcceleratorsInput{} - var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = conn.ListCustomRoutingAcceleratorsPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingAcceleratorsOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingAcceleratorsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + log.Printf("[WARN] Skipping Global Accelerator Custom Routing Accelerator sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err) } for _, v := range page.Accelerators { @@ -413,48 +366,30 @@ func sweepCustomRoutingListeners(region string) error { AcceleratorArn: v.AcceleratorArn, } - err := conn.ListCustomRoutingListenersPagesWithContext(ctx, input, func(page *globalaccelerator.ListCustomRoutingListenersOutput, lastPage bool) bool { - if page == nil { - return !lastPage + pages := globalaccelerator.NewListCustomRoutingListenersPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + continue } for _, v := range page.Listeners { - r := ResourceCustomRoutingListener() + r := resourceCustomRoutingListener() d := r.Data(nil) - d.SetId(aws.StringValue(v.ListenerArn)) + d.SetId(aws.ToString(v.ListenerArn)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - continue - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Custom Routing Listeners (%s): %w", region, err)) } } - - return !lastPage - }) - - if awsv1.SkipSweepError(err) { - log.Printf("[WARN] Skipping Global Accelerator Custom Routing Listener sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors - } - - if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Global Accelerator Custom Routing Accelerators (%s): %w", region, err)) } err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Global Accelerator Custom Routing Listeners (%s): %w", region, err)) + return fmt.Errorf("error sweeping Global Accelerator Custom Routing Listeners (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } diff --git a/internal/service/globalaccelerator/tags_gen.go b/internal/service/globalaccelerator/tags_gen.go index e9974334e9e..49aeeefe362 100644 --- a/internal/service/globalaccelerator/tags_gen.go +++ b/internal/service/globalaccelerator/tags_gen.go @@ -5,9 +5,9 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/globalaccelerator" - "github.com/aws/aws-sdk-go/service/globalaccelerator/globalacceleratoriface" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/globalaccelerator" + awstypes "github.com/aws/aws-sdk-go-v2/service/globalaccelerator/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/logging" @@ -19,12 +19,12 @@ import ( // listTags lists globalaccelerator service tags. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func listTags(ctx context.Context, conn globalacceleratoriface.GlobalAcceleratorAPI, identifier string) (tftags.KeyValueTags, error) { +func listTags(ctx context.Context, conn *globalaccelerator.Client, identifier string, optFns ...func(*globalaccelerator.Options)) (tftags.KeyValueTags, error) { input := &globalaccelerator.ListTagsForResourceInput{ ResourceArn: aws.String(identifier), } - output, err := conn.ListTagsForResourceWithContext(ctx, input) + output, err := conn.ListTagsForResource(ctx, input, optFns...) if err != nil { return tftags.New(ctx, nil), err @@ -36,7 +36,7 @@ func listTags(ctx context.Context, conn globalacceleratoriface.GlobalAccelerator // ListTags lists globalaccelerator service tags and set them in Context. // It is called from outside this package. func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier string) error { - tags, err := listTags(ctx, meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx), identifier) + tags, err := listTags(ctx, meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx), identifier) if err != nil { return err @@ -52,11 +52,11 @@ func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier stri // []*SERVICE.Tag handling // Tags returns globalaccelerator service tags. -func Tags(tags tftags.KeyValueTags) []*globalaccelerator.Tag { - result := make([]*globalaccelerator.Tag, 0, len(tags)) +func Tags(tags tftags.KeyValueTags) []awstypes.Tag { + result := make([]awstypes.Tag, 0, len(tags)) for k, v := range tags.Map() { - tag := &globalaccelerator.Tag{ + tag := awstypes.Tag{ Key: aws.String(k), Value: aws.String(v), } @@ -68,11 +68,11 @@ func Tags(tags tftags.KeyValueTags) []*globalaccelerator.Tag { } // KeyValueTags creates tftags.KeyValueTags from globalaccelerator service tags. -func KeyValueTags(ctx context.Context, tags []*globalaccelerator.Tag) tftags.KeyValueTags { +func KeyValueTags(ctx context.Context, tags []awstypes.Tag) tftags.KeyValueTags { m := make(map[string]*string, len(tags)) for _, tag := range tags { - m[aws.StringValue(tag.Key)] = tag.Value + m[aws.ToString(tag.Key)] = tag.Value } return tftags.New(ctx, m) @@ -80,7 +80,7 @@ func KeyValueTags(ctx context.Context, tags []*globalaccelerator.Tag) tftags.Key // getTagsIn returns globalaccelerator service tags from Context. // nil is returned if there are no input tags. -func getTagsIn(ctx context.Context) []*globalaccelerator.Tag { +func getTagsIn(ctx context.Context) []awstypes.Tag { if inContext, ok := tftags.FromContext(ctx); ok { if tags := Tags(inContext.TagsIn.UnwrapOrDefault()); len(tags) > 0 { return tags @@ -91,7 +91,7 @@ func getTagsIn(ctx context.Context) []*globalaccelerator.Tag { } // setTagsOut sets globalaccelerator service tags in Context. -func setTagsOut(ctx context.Context, tags []*globalaccelerator.Tag) { +func setTagsOut(ctx context.Context, tags []awstypes.Tag) { if inContext, ok := tftags.FromContext(ctx); ok { inContext.TagsOut = option.Some(KeyValueTags(ctx, tags)) } @@ -100,7 +100,7 @@ func setTagsOut(ctx context.Context, tags []*globalaccelerator.Tag) { // updateTags updates globalaccelerator service tags. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func updateTags(ctx context.Context, conn globalacceleratoriface.GlobalAcceleratorAPI, identifier string, oldTagsMap, newTagsMap any) error { +func updateTags(ctx context.Context, conn *globalaccelerator.Client, identifier string, oldTagsMap, newTagsMap any, optFns ...func(*globalaccelerator.Options)) error { oldTags := tftags.New(ctx, oldTagsMap) newTags := tftags.New(ctx, newTagsMap) @@ -111,10 +111,10 @@ func updateTags(ctx context.Context, conn globalacceleratoriface.GlobalAccelerat if len(removedTags) > 0 { input := &globalaccelerator.UntagResourceInput{ ResourceArn: aws.String(identifier), - TagKeys: aws.StringSlice(removedTags.Keys()), + TagKeys: removedTags.Keys(), } - _, err := conn.UntagResourceWithContext(ctx, input) + _, err := conn.UntagResource(ctx, input, optFns...) if err != nil { return fmt.Errorf("untagging resource (%s): %w", identifier, err) @@ -129,7 +129,7 @@ func updateTags(ctx context.Context, conn globalacceleratoriface.GlobalAccelerat Tags: Tags(updatedTags), } - _, err := conn.TagResourceWithContext(ctx, input) + _, err := conn.TagResource(ctx, input, optFns...) if err != nil { return fmt.Errorf("tagging resource (%s): %w", identifier, err) @@ -142,5 +142,5 @@ func updateTags(ctx context.Context, conn globalacceleratoriface.GlobalAccelerat // UpdateTags updates globalaccelerator service tags. // It is called from outside this package. func (p *servicePackage) UpdateTags(ctx context.Context, meta any, identifier string, oldTags, newTags any) error { - return updateTags(ctx, meta.(*conns.AWSClient).GlobalAcceleratorConn(ctx), identifier, oldTags, newTags) + return updateTags(ctx, meta.(*conns.AWSClient).GlobalAcceleratorClient(ctx), identifier, oldTags, newTags) } diff --git a/internal/service/ssm/document.go b/internal/service/ssm/document.go index 425a0bfa4cb..8efb788b360 100644 --- a/internal/service/ssm/document.go +++ b/internal/service/ssm/document.go @@ -28,6 +28,7 @@ import ( "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" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -403,7 +404,7 @@ func resourceDocumentUpdate(ctx context.Context, d *schema.ResourceData, meta in conn := meta.(*conns.AWSClient).SSMConn(ctx) if d.HasChange("permissions") { - var oldAccountIDs, newAccountIDs flex.Set[string] + var oldAccountIDs, newAccountIDs itypes.Set[string] o, n := d.GetChange("permissions") if v := o.(map[string]interface{}); len(v) > 0 { diff --git a/internal/framework/flex/flex.go b/internal/types/set.go similarity index 96% rename from internal/framework/flex/flex.go rename to internal/types/set.go index c15a5e76b93..c4bb73865e8 100644 --- a/internal/framework/flex/flex.go +++ b/internal/types/set.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -package flex +package types type Set[T comparable] []T diff --git a/internal/framework/flex/flex_test.go b/internal/types/set_test.go similarity index 50% rename from internal/framework/flex/flex_test.go rename to internal/types/set_test.go index c2143bf8efc..7da36787a66 100644 --- a/internal/framework/flex/flex_test.go +++ b/internal/types/set_test.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -package flex +package types import ( "testing" @@ -9,7 +9,61 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestSet_Difference_strings(t *testing.T) { +func TestSetDifference_ints(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + set1 Set[int] + set2 Set[int] + expected Set[int] + }{ + { + name: "nil sets", + expected: nil, + }, + { + name: "empty sets", + set1: Set[int]{}, + set2: Set[int]{}, + expected: nil, + }, + { + name: "no overlap", + set1: Set[int]{1, 3, 5, 7}, + set2: Set[int]{2, 4, 6, 8}, + expected: Set[int]{1, 3, 5, 7}, + }, + { + name: "no overlap swapped", + set1: Set[int]{2, 4, 6, 8}, + set2: Set[int]{1, 3, 5, 7}, + expected: Set[int]{2, 4, 6, 8}, + }, + { + name: "overlap", + set1: Set[int]{1, 2, 3, 4, 5, 7}, + set2: Set[int]{1, 2, 4, 6, 8}, + expected: Set[int]{3, 5, 7}, + }, + } + + for _, testCase := range testCases { + testCase := testCase + + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + got := testCase.set1.Difference(testCase.set2) + + if diff := cmp.Diff(got, testCase.expected); diff != "" { + t.Errorf("unexpected diff (+wanted, -got): %s", diff) + } + }) + } +} + +func TestSetDifference_strings(t *testing.T) { t.Parallel() type testCase struct { diff --git a/names/data/names_data.csv b/names/data/names_data.csv index a3efdbe3344..3ef7158d385 100644 --- a/names/data/names_data.csv +++ b/names/data/names_data.csv @@ -171,7 +171,7 @@ frauddetector,frauddetector,frauddetector,frauddetector,,frauddetector,,,FraudDe ,,,,,,,,,,,,,,,,,FreeRTOS,,x,,,,,,,,,No SDK support fsx,fsx,fsx,fsx,,fsx,,,FSx,FSx,,1,,,aws_fsx_,,fsx_,FSx,Amazon,,,,,,,FSx,DescribeFileSystems,, gamelift,gamelift,gamelift,gamelift,,gamelift,,,GameLift,GameLift,,1,,,aws_gamelift_,,gamelift_,GameLift,Amazon,,,,,,,GameLift,ListGameServerGroups,, -globalaccelerator,globalaccelerator,globalaccelerator,globalaccelerator,,globalaccelerator,,,GlobalAccelerator,GlobalAccelerator,x,1,,,aws_globalaccelerator_,,globalaccelerator_,Global Accelerator,AWS,,,,,,,Global Accelerator,ListAccelerators,, +globalaccelerator,globalaccelerator,globalaccelerator,globalaccelerator,,globalaccelerator,,,GlobalAccelerator,GlobalAccelerator,x,,2,,aws_globalaccelerator_,,globalaccelerator_,Global Accelerator,AWS,,,,,,,Global Accelerator,ListAccelerators,, glue,glue,glue,glue,,glue,,,Glue,Glue,,1,,,aws_glue_,,glue_,Glue,AWS,,,,,,,Glue,ListRegistries,, databrew,databrew,gluedatabrew,databrew,,databrew,,gluedatabrew,DataBrew,GlueDataBrew,,1,,,aws_databrew_,,databrew_,Glue DataBrew,AWS,,x,,,,,DataBrew,,, groundstation,groundstation,groundstation,groundstation,,groundstation,,,GroundStation,GroundStation,,,2,,aws_groundstation_,,groundstation_,Ground Station,AWS,,,,,,,GroundStation,ListConfigs,, diff --git a/website/docs/r/globalaccelerator_cross_account_attachment.html.markdown b/website/docs/r/globalaccelerator_cross_account_attachment.html.markdown new file mode 100644 index 00000000000..7a7edd54f8c --- /dev/null +++ b/website/docs/r/globalaccelerator_cross_account_attachment.html.markdown @@ -0,0 +1,84 @@ +--- +subcategory: "Global Accelerator" +layout: "aws" +page_title: "AWS: aws_globalaccelerator_cross_account_attachment" +description: |- + Terraform resource for managing an AWS Global Accelerator Cross Account Attachment. +--- + +# Resource: aws_globalaccelerator_cross_account_attachment + +Terraform resource for managing an AWS Global Accelerator Cross Account Attachment. + +## Example Usage + +### Basic Usage + +```terraform +resource "aws_globalaccelerator_cross_account_attachment" "example" { + name = "example-cross-account-attachment" +} +``` + +### Usage with Optional Arguments + +```terraform +resource "aws_globalaccelerator_cross_account_attachment" "example" { + name = "example-cross-account-attachment" + principals = ["123456789012"] + + resource { + endpoint_id = "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188" + region = "us-west-2" + } +} +``` + +## Argument Reference + +The following arguments are required: + +* `name` - (Required) Name of the Cross Account Attachment. + +The following arguments are optional: + +* `principals` - (Optional) List of AWS account IDs that are allowed to associate resources with the accelerator. +* `resource` - (Optional) List of resources to be associated with the accelerator. + * `endpoint_id` - (Optional) The endpoint ID for the endpoint that is specified as a AWS resource. + * `region` - (Optional) The AWS Region where a shared endpoint resource is located. +* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. + +## Attribute Reference + +This resource exports the following attributes in addition to the arguments above: + +* `arn` - ARN of the Cross Account Attachment. +* `id` - ID of the Cross Account Attachment. +* `created_time` - Creation Time when the Cross Account Attachment. +* `last_modified_time` - Last modified time of the Cross Account Attachment. +* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block). + +## Timeouts + +[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts): + +* `create` - (Default `30m`) +* `update` - (Default `30m`) +* `delete` - (Default `30m`) + +## Import + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import Global Accelerator Cross Account Attachment using the `example_id_arg`. For example: + +```terraform +import { + to = aws_globalaccelerator_cross_account_attachment.example + id = "arn:aws:globalaccelerator::012345678910:attachment/01234567-abcd-8910-efgh-123456789012" +} +``` + +Using `terraform import`, import Global Accelerator Cross Account Attachment using the `example_id_arg`. For example: + +```console +% terraform import aws_globalaccelerator_cross_account_attachment.example arn:aws:globalaccelerator::012345678910:attachment/01234567-abcd-8910-efgh-123456789012 +```