From cc940efafeb054fe48368aba9ce0a99dba09a9af Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Wed, 1 May 2024 00:01:27 +0100 Subject: [PATCH 01/59] directconnect: Migrate to AWS SDK v2 --- go.mod | 1 + go.sum | 2 + internal/conns/awsclient_gen.go | 6 +- internal/service/directconnect/acc_test.go | 25 +-- internal/service/directconnect/bgp_peer.go | 73 ++++----- .../service/directconnect/bgp_peer_test.go | 15 +- internal/service/directconnect/connection.go | 31 ++-- .../directconnect/connection_association.go | 29 ++-- .../connection_association_test.go | 4 +- .../directconnect/connection_confirmation.go | 12 +- .../connection_confirmation_test.go | 9 +- .../directconnect/connection_data_source.go | 19 +-- .../service/directconnect/connection_test.go | 28 ++-- internal/service/directconnect/find.go | 143 ++++++++---------- internal/service/directconnect/gateway.go | 27 ++-- .../directconnect/gateway_association.go | 39 ++--- .../gateway_association_migrate.go | 6 +- .../gateway_association_proposal.go | 41 +++-- .../gateway_association_proposal_test.go | 44 +++--- .../directconnect/gateway_association_test.go | 50 +++--- .../directconnect/gateway_data_source.go | 17 ++- .../service/directconnect/gateway_test.go | 16 +- internal/service/directconnect/generate.go | 4 +- .../directconnect/hosted_connection.go | 20 +-- .../directconnect/hosted_connection_test.go | 4 +- .../hosted_private_virtual_interface.go | 57 +++---- ...sted_private_virtual_interface_accepter.go | 37 ++--- .../hosted_private_virtual_interface_test.go | 16 +- .../hosted_public_virtual_interface.go | 56 +++---- ...osted_public_virtual_interface_accepter.go | 40 +++-- .../hosted_public_virtual_interface_test.go | 16 +- .../hosted_transit_virtual_interface.go | 55 +++---- ...sted_transit_virtual_interface_accepter.go | 35 ++--- .../hosted_transit_virtual_interface_test.go | 16 +- internal/service/directconnect/lag.go | 41 ++--- internal/service/directconnect/lag_test.go | 18 +-- .../service/directconnect/list_pages_gen.go | 25 ++- .../directconnect/location_data_source.go | 9 +- .../directconnect/locations_data_source.go | 7 +- internal/service/directconnect/macsec_key.go | 22 ++- .../private_virtual_interface.go | 56 +++---- .../private_virtual_interface_test.go | 28 ++-- .../directconnect/public_virtual_interface.go | 55 +++---- .../public_virtual_interface_test.go | 16 +- .../router_configuration_data_source.go | 25 +-- .../router_configuration_data_source_test.go | 3 +- .../service_endpoints_gen_test.go | 40 +++-- .../directconnect/service_package_gen.go | 17 ++- internal/service/directconnect/status.go | 23 ++- internal/service/directconnect/sweep.go | 96 ++++++------ internal/service/directconnect/tags_gen.go | 40 ++--- .../transit_virtual_interface.go | 56 +++---- .../transit_virtual_interface_test.go | 24 +-- internal/service/directconnect/vif.go | 63 ++++---- internal/service/directconnect/wait.go | 78 +++++----- names/data/names_data.csv | 2 +- names/names.go | 1 + 57 files changed, 829 insertions(+), 909 deletions(-) diff --git a/go.mod b/go.mod index 2093b8ed8d7..625e3ae0246 100644 --- a/go.mod +++ b/go.mod @@ -71,6 +71,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/datazone v1.8.0 github.com/aws/aws-sdk-go-v2/service/dax v1.19.4 github.com/aws/aws-sdk-go-v2/service/devopsguru v1.30.4 + github.com/aws/aws-sdk-go-v2/service/directconnect v1.24.4 github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.4 github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.9.3 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1 diff --git a/go.sum b/go.sum index 0608c633287..aef8720a02d 100644 --- a/go.sum +++ b/go.sum @@ -162,6 +162,8 @@ github.com/aws/aws-sdk-go-v2/service/dax v1.19.4 h1:S3mvtYjRVVsg1R4EuV1LWZUiD72t github.com/aws/aws-sdk-go-v2/service/dax v1.19.4/go.mod h1:ZfNHbSICNHSqX4l5pJ6APeyWdgXgQg3PbuSFS2e5mCo= github.com/aws/aws-sdk-go-v2/service/devopsguru v1.30.4 h1:qnMBNiyAPWwI9XSbtfOPr9rLZMdLFD2GAcZujMqw7Ow= github.com/aws/aws-sdk-go-v2/service/devopsguru v1.30.4/go.mod h1:+ezG+QXnBXCWFp1rCQrxyebQMM5lBAxziL4iuswpxqo= +github.com/aws/aws-sdk-go-v2/service/directconnect v1.24.4 h1:9g0G4b/HG8Yw9YQQMUotq2QjvOYxX8kHxnAO6Yq7FuI= +github.com/aws/aws-sdk-go-v2/service/directconnect v1.24.4/go.mod h1:+Y1uRuhFUQpU4/cV2zgdkk6SlE/oiZEDRP8ilynvDZk= github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.4 h1:XBgx3sdaA0SoPXsZSNSUL14H0UnYnTSVArieaYNv0EI= github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.4/go.mod h1:Lm/qj7nCC0zEFoAdjbun8xLkflPFNbbspQVZgQQiOz8= github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.9.3 h1:HihsSNvSKUYna0rE7OAcNnsfNjita5nFz4OfC+lX1jc= diff --git a/internal/conns/awsclient_gen.go b/internal/conns/awsclient_gen.go index 3dac23527e2..f0b60c7d756 100644 --- a/internal/conns/awsclient_gen.go +++ b/internal/conns/awsclient_gen.go @@ -63,6 +63,7 @@ import ( datazone_sdkv2 "github.com/aws/aws-sdk-go-v2/service/datazone" dax_sdkv2 "github.com/aws/aws-sdk-go-v2/service/dax" devopsguru_sdkv2 "github.com/aws/aws-sdk-go-v2/service/devopsguru" + directconnect_sdkv2 "github.com/aws/aws-sdk-go-v2/service/directconnect" directoryservice_sdkv2 "github.com/aws/aws-sdk-go-v2/service/directoryservice" docdbelastic_sdkv2 "github.com/aws/aws-sdk-go-v2/service/docdbelastic" dynamodb_sdkv2 "github.com/aws/aws-sdk-go-v2/service/dynamodb" @@ -180,7 +181,6 @@ import ( datapipeline_sdkv1 "github.com/aws/aws-sdk-go/service/datapipeline" detective_sdkv1 "github.com/aws/aws-sdk-go/service/detective" devicefarm_sdkv1 "github.com/aws/aws-sdk-go/service/devicefarm" - directconnect_sdkv1 "github.com/aws/aws-sdk-go/service/directconnect" directoryservice_sdkv1 "github.com/aws/aws-sdk-go/service/directoryservice" dlm_sdkv1 "github.com/aws/aws-sdk-go/service/dlm" docdb_sdkv1 "github.com/aws/aws-sdk-go/service/docdb" @@ -562,8 +562,8 @@ func (c *AWSClient) DeviceFarmConn(ctx context.Context) *devicefarm_sdkv1.Device return errs.Must(conn[*devicefarm_sdkv1.DeviceFarm](ctx, c, names.DeviceFarm, make(map[string]any))) } -func (c *AWSClient) DirectConnectConn(ctx context.Context) *directconnect_sdkv1.DirectConnect { - return errs.Must(conn[*directconnect_sdkv1.DirectConnect](ctx, c, names.DirectConnect, make(map[string]any))) +func (c *AWSClient) DirectConnectClient(ctx context.Context) *directconnect_sdkv2.Client { + return errs.Must(client[*directconnect_sdkv2.Client](ctx, c, names.DirectConnect, make(map[string]any))) } func (c *AWSClient) DocDBConn(ctx context.Context) *docdb_sdkv1.DocDB { diff --git a/internal/service/directconnect/acc_test.go b/internal/service/directconnect/acc_test.go index 0886a423d3b..b966eab6668 100644 --- a/internal/service/directconnect/acc_test.go +++ b/internal/service/directconnect/acc_test.go @@ -7,18 +7,19 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "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" + "github.com/hashicorp/terraform-provider-aws/internal/errs" ) -func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) rs, ok := s.RootModule().Resources[name] if !ok { @@ -28,7 +29,7 @@ func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *d return fmt.Errorf("No ID is set") } - resp, err := conn.DescribeVirtualInterfacesWithContext(ctx, &directconnect.DescribeVirtualInterfacesInput{ + resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ VirtualInterfaceId: aws.String(rs.Primary.ID), }) if err != nil { @@ -36,8 +37,8 @@ func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *d } for _, v := range resp.VirtualInterfaces { - if aws.StringValue(v.VirtualInterfaceId) == rs.Primary.ID { - *vif = *v + if aws.ToString(v.VirtualInterfaceId) == rs.Primary.ID { + *vif = v return nil } @@ -48,7 +49,7 @@ func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *d } func testAccCheckVirtualInterfaceDestroy(ctx context.Context, s *terraform.State, t string) error { // nosemgrep:ci.semgrep.acctest.naming.destroy-check-signature - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != t { @@ -58,10 +59,10 @@ func testAccCheckVirtualInterfaceDestroy(ctx context.Context, s *terraform.State return fmt.Errorf("No ID is set") } - resp, err := conn.DescribeVirtualInterfacesWithContext(ctx, &directconnect.DescribeVirtualInterfacesInput{ + resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ VirtualInterfaceId: aws.String(rs.Primary.ID), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "does not exist") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { continue } if err != nil { @@ -69,7 +70,7 @@ func testAccCheckVirtualInterfaceDestroy(ctx context.Context, s *terraform.State } for _, v := range resp.VirtualInterfaces { - if aws.StringValue(v.VirtualInterfaceId) == rs.Primary.ID && aws.StringValue(v.VirtualInterfaceState) != directconnect.VirtualInterfaceStateDeleted { + if aws.ToString(v.VirtualInterfaceId) == rs.Primary.ID && v.VirtualInterfaceState != awstypes.VirtualInterfaceStateDeleted { return fmt.Errorf("[DESTROY ERROR] Direct Connect virtual interface (%s) not deleted", rs.Primary.ID) } } diff --git a/internal/service/directconnect/bgp_peer.go b/internal/service/directconnect/bgp_peer.go index 4d48c9cb8bd..722ef54d16d 100644 --- a/internal/service/directconnect/bgp_peer.go +++ b/internal/service/directconnect/bgp_peer.go @@ -9,14 +9,15 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" ) @@ -29,10 +30,10 @@ func ResourceBGPPeer() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{directconnect.AddressFamilyIpv4, directconnect.AddressFamilyIpv6}, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "bgp_asn": { Type: schema.TypeInt, @@ -85,17 +86,17 @@ func ResourceBGPPeer() *schema.Resource { func resourceBGPPeerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vifId := d.Get("virtual_interface_id").(string) addrFamily := d.Get("address_family").(string) - asn := int64(d.Get("bgp_asn").(int)) + asn := int32(d.Get("bgp_asn").(int)) req := &directconnect.CreateBGPPeerInput{ VirtualInterfaceId: aws.String(vifId), - NewBGPPeer: &directconnect.NewBGPPeer{ - AddressFamily: aws.String(addrFamily), - Asn: aws.Int64(asn), + NewBGPPeer: &awstypes.NewBGPPeer{ + AddressFamily: awstypes.AddressFamily(addrFamily), + Asn: asn, }, } if v, ok := d.GetOk("amazon_address"); ok { @@ -109,7 +110,7 @@ func resourceBGPPeerCreate(ctx context.Context, d *schema.ResourceData, meta int } log.Printf("[DEBUG] Creating Direct Connect BGP peer: %#v", req) - _, err := conn.CreateBGPPeerWithContext(ctx, req) + _, err := conn.CreateBGPPeer(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect BGP peer: %s", err) } @@ -117,13 +118,8 @@ func resourceBGPPeerCreate(ctx context.Context, d *schema.ResourceData, meta int d.SetId(fmt.Sprintf("%s-%s-%d", vifId, addrFamily, asn)) stateConf := &retry.StateChangeConf{ - Pending: []string{ - directconnect.BGPPeerStatePending, - }, - Target: []string{ - directconnect.BGPPeerStateAvailable, - directconnect.BGPPeerStateVerifying, - }, + Pending: enum.Slice(awstypes.BGPPeerStatePending), + Target: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateVerifying), Refresh: bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn), Timeout: d.Timeout(schema.TimeoutDelete), Delay: 10 * time.Second, @@ -139,23 +135,23 @@ func resourceBGPPeerCreate(ctx context.Context, d *schema.ResourceData, meta int func resourceBGPPeerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vifId := d.Get("virtual_interface_id").(string) addrFamily := d.Get("address_family").(string) - asn := int64(d.Get("bgp_asn").(int)) + asn := int32(d.Get("bgp_asn").(int)) bgpPeerRaw, state, err := bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn)() if err != nil { return sdkdiag.AppendErrorf(diags, "reading Direct Connect BGP peer: %s", err) } - if state == directconnect.BGPPeerStateDeleted { + if state == string(awstypes.BGPPeerStateDeleted) { log.Printf("[WARN] Direct Connect BGP peer (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - bgpPeer := bgpPeerRaw.(*directconnect.BGPPeer) + bgpPeer := bgpPeerRaw.(*awstypes.BGPPeer) d.Set("amazon_address", bgpPeer.AmazonAddress) d.Set("bgp_auth_key", bgpPeer.AuthKey) d.Set("customer_address", bgpPeer.CustomerAddress) @@ -168,15 +164,15 @@ func resourceBGPPeerRead(ctx context.Context, d *schema.ResourceData, meta inter func resourceBGPPeerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vifId := d.Get("virtual_interface_id").(string) addrFamily := d.Get("address_family").(string) - asn := int64(d.Get("bgp_asn").(int)) + asn := int32(d.Get("bgp_asn").(int)) log.Printf("[DEBUG] Deleting Direct Connect BGP peer: %s", d.Id()) - _, err := conn.DeleteBGPPeerWithContext(ctx, &directconnect.DeleteBGPPeerInput{ - Asn: aws.Int64(asn), + _, err := conn.DeleteBGPPeer(ctx, &directconnect.DeleteBGPPeerInput{ + Asn: asn, CustomerAddress: aws.String(d.Get("customer_address").(string)), VirtualInterfaceId: aws.String(vifId), }) @@ -189,15 +185,8 @@ func resourceBGPPeerDelete(ctx context.Context, d *schema.ResourceData, meta int } stateConf := &retry.StateChangeConf{ - Pending: []string{ - directconnect.BGPPeerStateAvailable, - directconnect.BGPPeerStateDeleting, - directconnect.BGPPeerStatePending, - directconnect.BGPPeerStateVerifying, - }, - Target: []string{ - directconnect.BGPPeerStateDeleted, - }, + Pending: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateDeleting, awstypes.BGPPeerStatePending, awstypes.BGPPeerStateVerifying), + Target: enum.Slice(awstypes.BGPPeerStateDeleted), Refresh: bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn), Timeout: d.Timeout(schema.TimeoutDelete), Delay: 10 * time.Second, @@ -211,22 +200,22 @@ func resourceBGPPeerDelete(ctx context.Context, d *schema.ResourceData, meta int return diags } -func bgpPeerStateRefresh(ctx context.Context, conn *directconnect.DirectConnect, vifId, addrFamily string, asn int64) retry.StateRefreshFunc { +func bgpPeerStateRefresh(ctx context.Context, conn *directconnect.Client, vifId, addrFamily string, asn int32) retry.StateRefreshFunc { return func() (interface{}, string, error) { vif, err := virtualInterfaceRead(ctx, vifId, conn) if err != nil { return nil, "", err } if vif == nil { - return "", directconnect.BGPPeerStateDeleted, nil + return "", string(awstypes.BGPPeerStateDeleted), nil } for _, bgpPeer := range vif.BgpPeers { - if aws.StringValue(bgpPeer.AddressFamily) == addrFamily && aws.Int64Value(bgpPeer.Asn) == asn { - return bgpPeer, aws.StringValue(bgpPeer.BgpPeerState), nil + if string(bgpPeer.AddressFamily) == addrFamily && bgpPeer.Asn == asn { + return bgpPeer, string(bgpPeer.BgpPeerState), nil } } - return "", directconnect.BGPPeerStateDeleted, nil + return "", string(awstypes.BGPPeerStateDeleted), nil } } diff --git a/internal/service/directconnect/bgp_peer_test.go b/internal/service/directconnect/bgp_peer_test.go index 90384a05d80..ccf36d136cf 100644 --- a/internal/service/directconnect/bgp_peer_test.go +++ b/internal/service/directconnect/bgp_peer_test.go @@ -10,8 +10,9 @@ import ( "strconv" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -48,7 +49,7 @@ func TestAccDirectConnectBGPPeer_basic(t *testing.T) { func testAccCheckBGPPeerDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_bgp_peer" { @@ -58,14 +59,14 @@ func testAccCheckBGPPeerDestroy(ctx context.Context) resource.TestCheckFunc { VirtualInterfaceId: aws.String(rs.Primary.Attributes["virtual_interface_id"]), } - resp, err := conn.DescribeVirtualInterfacesWithContext(ctx, input) + resp, err := conn.DescribeVirtualInterfaces(ctx, input) if err != nil { return err } for _, peer := range resp.VirtualInterfaces[0].BgpPeers { - if aws.StringValue(peer.AddressFamily) == rs.Primary.Attributes["address_family"] && - strconv.Itoa(int(aws.Int64Value(peer.Asn))) == rs.Primary.Attributes["bgp_asn"] && - aws.StringValue(peer.BgpPeerState) != directconnect.BGPPeerStateDeleted { + if string(peer.AddressFamily) == rs.Primary.Attributes["address_family"] && + strconv.Itoa(int(peer.Asn)) == rs.Primary.Attributes["bgp_asn"] && + peer.BgpPeerState != awstypes.BGPPeerStateDeleted { return fmt.Errorf("[DESTROY ERROR] Dx BGP peer (%s) not deleted", rs.Primary.ID) } } diff --git a/internal/service/directconnect/connection.go b/internal/service/directconnect/connection.go index a8f8693a164..c6afd4a0ff6 100644 --- a/internal/service/directconnect/connection.go +++ b/internal/service/directconnect/connection.go @@ -9,14 +9,15 @@ import ( "log" "strconv" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -238,7 +239,7 @@ func ResourceConnection() *schema.Resource { func resourceConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) name := d.Get("name").(string) input := &directconnect.CreateConnectionInput{ @@ -253,20 +254,20 @@ func resourceConnectionCreate(ctx context.Context, d *schema.ResourceData, meta input.ProviderName = aws.String(v.(string)) } - output, err := conn.CreateConnectionWithContext(ctx, input) + output, err := conn.CreateConnection(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Connection (%s): %s", name, err) } - d.SetId(aws.StringValue(output.ConnectionId)) + d.SetId(aws.ToString(output.ConnectionId)) return append(diags, resourceConnectionRead(ctx, d, meta)...) } func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) connection, err := FindConnectionByID(ctx, conn, d.Id()) @@ -282,9 +283,9 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta in arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, - Region: aws.StringValue(connection.Region), + Region: aws.ToString(connection.Region), Service: "directconnect", - AccountID: aws.StringValue(connection.OwnerAccount), + AccountID: aws.ToString(connection.OwnerAccount), Resource: fmt.Sprintf("dxcon/%s", d.Id()), }.String() d.Set("arn", arn) @@ -313,7 +314,7 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta in func resourceConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.HasChange("encryption_mode") { input := &directconnect.UpdateConnectionInput{ @@ -321,7 +322,7 @@ func resourceConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta EncryptionMode: aws.String(d.Get("encryption_mode").(string)), } - _, err := conn.UpdateConnectionWithContext(ctx, input) + _, err := conn.UpdateConnection(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Direct Connect Connection (%s): %s", d.Id(), err) @@ -337,7 +338,7 @@ func resourceConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta func resourceConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if _, ok := d.GetOk("skip_destroy"); ok { return diags @@ -350,13 +351,13 @@ func resourceConnectionDelete(ctx context.Context, d *schema.ResourceData, meta return diags } -func deleteConnection(ctx context.Context, conn *directconnect.DirectConnect, connectionID string, waiter func(context.Context, *directconnect.DirectConnect, string) (*directconnect.Connection, error)) error { +func deleteConnection(ctx context.Context, conn *directconnect.Client, connectionID string, waiter func(context.Context, *directconnect.Client, string) (*awstypes.Connection, error)) error { log.Printf("[DEBUG] Deleting Direct Connect Connection: %s", connectionID) - _, err := conn.DeleteConnectionWithContext(ctx, &directconnect.DeleteConnectionInput{ + _, err := conn.DeleteConnection(ctx, &directconnect.DeleteConnectionInput{ ConnectionId: aws.String(connectionID), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Connection with ID") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { return nil } diff --git a/internal/service/directconnect/connection_association.go b/internal/service/directconnect/connection_association.go index 384a6b79f4c..089bd602b00 100644 --- a/internal/service/directconnect/connection_association.go +++ b/internal/service/directconnect/connection_association.go @@ -8,12 +8,13 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" + "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" ) @@ -42,7 +43,7 @@ func ResourceConnectionAssociation() *schema.Resource { func resourceConnectionAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) connectionID := d.Get("connection_id").(string) lagID := d.Get("lag_id").(string) @@ -51,21 +52,21 @@ func resourceConnectionAssociationCreate(ctx context.Context, d *schema.Resource LagId: aws.String(lagID), } - log.Printf("[DEBUG] Creating Direct Connect Connection LAG Association: %s", input) - output, err := conn.AssociateConnectionWithLagWithContext(ctx, input) + log.Printf("[DEBUG] Creating Direct Connect Connection LAG Association: %#v", input) + output, err := conn.AssociateConnectionWithLag(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Connection (%s) LAG (%s) Association: %s", connectionID, lagID, err) } - d.SetId(aws.StringValue(output.ConnectionId)) + d.SetId(aws.ToString(output.ConnectionId)) return diags } func resourceConnectionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) lagID := d.Get("lag_id").(string) err := FindConnectionAssociationExists(ctx, conn, d.Id(), lagID) @@ -85,7 +86,7 @@ func resourceConnectionAssociationRead(ctx context.Context, d *schema.ResourceDa func resourceConnectionAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if err := deleteConnectionLAGAssociation(ctx, conn, d.Id(), d.Get("lag_id").(string)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -93,7 +94,7 @@ func resourceConnectionAssociationDelete(ctx context.Context, d *schema.Resource return diags } -func deleteConnectionLAGAssociation(ctx context.Context, conn *directconnect.DirectConnect, connectionID, lagID string) error { +func deleteConnectionLAGAssociation(ctx context.Context, conn *directconnect.Client, connectionID, lagID string) error { input := &directconnect.DisassociateConnectionFromLagInput{ ConnectionId: aws.String(connectionID), LagId: aws.String(lagID), @@ -101,15 +102,15 @@ func deleteConnectionLAGAssociation(ctx context.Context, conn *directconnect.Dir _, err := tfresource.RetryWhen(ctx, connectionDisassociatedTimeout, func() (interface{}, error) { - return conn.DisassociateConnectionFromLagWithContext(ctx, input) + return conn.DisassociateConnectionFromLag(ctx, input) }, func(err error) (bool, error) { - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Connection does not exist") || - tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Lag does not exist") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Connection does not exist") || + errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Lag does not exist") { return false, nil } - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "is in a transitioning state") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "is in a transitioning state") { return true, err } diff --git a/internal/service/directconnect/connection_association_test.go b/internal/service/directconnect/connection_association_test.go index e8d95ad5075..5f67c404023 100644 --- a/internal/service/directconnect/connection_association_test.go +++ b/internal/service/directconnect/connection_association_test.go @@ -85,7 +85,7 @@ func TestAccDirectConnectConnectionAssociation_multiple(t *testing.T) { func testAccCheckConnectionAssociationDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_connection_association" { @@ -111,7 +111,7 @@ func testAccCheckConnectionAssociationDestroy(ctx context.Context) resource.Test func testAccCheckConnectionAssociationExists(ctx context.Context, name string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) rs, ok := s.RootModule().Resources[name] if !ok { diff --git a/internal/service/directconnect/connection_confirmation.go b/internal/service/directconnect/connection_confirmation.go index 7dd9f4e8fb2..7eebe23f9bc 100644 --- a/internal/service/directconnect/connection_confirmation.go +++ b/internal/service/directconnect/connection_confirmation.go @@ -7,8 +7,8 @@ import ( "context" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" "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" @@ -35,15 +35,15 @@ func ResourceConnectionConfirmation() *schema.Resource { func resourceConnectionConfirmationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) connectionID := d.Get("connection_id").(string) input := &directconnect.ConfirmConnectionInput{ ConnectionId: aws.String(connectionID), } - log.Printf("[DEBUG] Confirming Direct Connect Connection: %s", input) - _, err := conn.ConfirmConnectionWithContext(ctx, input) + log.Printf("[DEBUG] Confirming Direct Connect Connection: %#v", input) + _, err := conn.ConfirmConnection(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "confirming Direct Connection Connection (%s): %s", connectionID, err) @@ -60,7 +60,7 @@ func resourceConnectionConfirmationCreate(ctx context.Context, d *schema.Resourc func resourceConnectionConfirmationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) _, err := FindConnectionByID(ctx, conn, d.Id()) diff --git a/internal/service/directconnect/connection_confirmation_test.go b/internal/service/directconnect/connection_confirmation_test.go index a3c89c1a21b..89de35bc89f 100644 --- a/internal/service/directconnect/connection_confirmation_test.go +++ b/internal/service/directconnect/connection_confirmation_test.go @@ -9,8 +9,7 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -64,7 +63,7 @@ func testAccCheckConnectionConfirmationExists(ctx context.Context, name string, } provider := providerFunc() - conn := provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) connection, err := tfdirectconnect.FindConnectionByID(ctx, conn, rs.Primary.ID) @@ -72,8 +71,8 @@ func testAccCheckConnectionConfirmationExists(ctx context.Context, name string, return err } - if state := aws.StringValue(connection.ConnectionState); state != directconnect.ConnectionStateAvailable { - return fmt.Errorf("Direct Connect Connection %s in unexpected state: %s", rs.Primary.ID, state) + if connection.ConnectionState != awstypes.ConnectionStateAvailable { + return fmt.Errorf("Direct Connect Connection %s in unexpected state: %s", rs.Primary.ID, string(connection.ConnectionState)) } return nil diff --git a/internal/service/directconnect/connection_data_source.go b/internal/service/directconnect/connection_data_source.go index fe7333b5395..d1f0f24289f 100644 --- a/internal/service/directconnect/connection_data_source.go +++ b/internal/service/directconnect/connection_data_source.go @@ -7,9 +7,10 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "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" @@ -66,22 +67,22 @@ func DataSourceConnection() *schema.Resource { func dataSourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - var connections []*directconnect.Connection + var connections []awstypes.Connection input := &directconnect.DescribeConnectionsInput{} name := d.Get("name").(string) // DescribeConnections is not paginated. - output, err := conn.DescribeConnectionsWithContext(ctx, input) + output, err := conn.DescribeConnections(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Direct Connect Connections: %s", err) } for _, connection := range output.Connections { - if aws.StringValue(connection.ConnectionName) == name { + if aws.ToString(connection.ConnectionName) == name { connections = append(connections, connection) } } @@ -96,13 +97,13 @@ func dataSourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta connection := connections[0] - d.SetId(aws.StringValue(connection.ConnectionId)) + d.SetId(aws.ToString(connection.ConnectionId)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, - Region: aws.StringValue(connection.Region), + Region: aws.ToString(connection.Region), Service: "directconnect", - AccountID: aws.StringValue(connection.OwnerAccount), + AccountID: aws.ToString(connection.OwnerAccount), Resource: fmt.Sprintf("dxcon/%s", d.Id()), }.String() d.Set("arn", arn) diff --git a/internal/service/directconnect/connection_test.go b/internal/service/directconnect/connection_test.go index 5530fef6dd2..eb974520699 100644 --- a/internal/service/directconnect/connection_test.go +++ b/internal/service/directconnect/connection_test.go @@ -10,7 +10,7 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -23,7 +23,7 @@ import ( func TestAccDirectConnectConnection_basic(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -61,7 +61,7 @@ func TestAccDirectConnectConnection_basic(t *testing.T) { func TestAccDirectConnectConnection_disappears(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -97,7 +97,7 @@ func TestAccDirectConnectConnection_encryptionMode(t *testing.T) { t.Skipf("Environment variable %s is not set", dxName) } - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" ckn := testAccDirecConnectMacSecGenerateHex() cak := testAccDirecConnectMacSecGenerateHex() @@ -143,7 +143,7 @@ func TestAccDirectConnectConnection_encryptionMode(t *testing.T) { func TestAccDirectConnectConnection_macsecRequested(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -179,7 +179,7 @@ func TestAccDirectConnectConnection_macsecRequested(t *testing.T) { func TestAccDirectConnectConnection_providerName(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -215,7 +215,7 @@ func TestAccDirectConnectConnection_providerName(t *testing.T) { func TestAccDirectConnectConnection_skipDestroy(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -238,7 +238,7 @@ func TestAccDirectConnectConnection_skipDestroy(t *testing.T) { func TestAccDirectConnectConnection_tags(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -290,7 +290,7 @@ func TestAccDirectConnectConnection_tags(t *testing.T) { // https://github.com/hashicorp/terraform-provider-aws/issues/31732. func TestAccDirectConnectConnection_vlanIDMigration501(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -327,7 +327,7 @@ func TestAccDirectConnectConnection_vlanIDMigration501(t *testing.T) { func TestAccDirectConnectConnection_vlanIDMigration510(t *testing.T) { ctx := acctest.Context(t) - var connection directconnect.Connection + var connection awstypes.Connection resourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -361,7 +361,7 @@ func TestAccDirectConnectConnection_vlanIDMigration510(t *testing.T) { func testAccCheckConnectionDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_connection" { @@ -385,9 +385,9 @@ func testAccCheckConnectionDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckConnectionExists(ctx context.Context, name string, v *directconnect.Connection) resource.TestCheckFunc { +func testAccCheckConnectionExists(ctx context.Context, name string, v *awstypes.Connection) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) rs, ok := s.RootModule().Resources[name] if !ok { @@ -412,7 +412,7 @@ func testAccCheckConnectionExists(ctx context.Context, name string, v *directcon func testAccCheckConnectionNoDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_connection" { diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index a9bd6ce7a18..c2a8acd4aa3 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -6,21 +6,22 @@ package directconnect import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindConnectionByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) { +func FindConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { input := &directconnect.DescribeConnectionsInput{ ConnectionId: aws.String(id), } - output, err := conn.DescribeConnectionsWithContext(ctx, input) + output, err := conn.DescribeConnections(ctx, input) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Connection with ID") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -31,19 +32,15 @@ func FindConnectionByID(ctx context.Context, conn *directconnect.DirectConnect, return nil, err } - if output == nil || len(output.Connections) == 0 || output.Connections[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + connection, err := tfresource.AssertSingleValueResult(output.Connections) - if count := len(output.Connections); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - connection := output.Connections[0] - - if state := aws.StringValue(connection.ConnectionState); state == directconnect.ConnectionStateDeleted || state == directconnect.ConnectionStateRejected { + if connection.ConnectionState == awstypes.ConnectionStateDeleted || connection.ConnectionState == awstypes.ConnectionStateRejected { return nil, &retry.NotFoundError{ - Message: state, + Message: string(connection.ConnectionState), LastRequest: input, } } @@ -51,44 +48,40 @@ func FindConnectionByID(ctx context.Context, conn *directconnect.DirectConnect, return connection, nil } -func FindConnectionAssociationExists(ctx context.Context, conn *directconnect.DirectConnect, connectionID, lagID string) error { +func FindConnectionAssociationExists(ctx context.Context, conn *directconnect.Client, connectionID, lagID string) error { connection, err := FindConnectionByID(ctx, conn, connectionID) if err != nil { return err } - if lagID != aws.StringValue(connection.LagId) { + if lagID != aws.ToString(connection.LagId) { return &retry.NotFoundError{} } return nil } -func FindGatewayByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Gateway, error) { +func FindGatewayByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGateway, error) { input := &directconnect.DescribeDirectConnectGatewaysInput{ DirectConnectGatewayId: aws.String(id), } - output, err := conn.DescribeDirectConnectGatewaysWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGateways(ctx, input) if err != nil { return nil, err } - if output == nil || len(output.DirectConnectGateways) == 0 || output.DirectConnectGateways[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + gateway, err := tfresource.AssertSingleValueResult(output.DirectConnectGateways) - if count := len(output.DirectConnectGateways); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - gateway := output.DirectConnectGateways[0] - - if state := aws.StringValue(gateway.DirectConnectGatewayState); state == directconnect.GatewayStateDeleted { + if gateway.DirectConnectGatewayState == awstypes.DirectConnectGatewayStateDeleted { return nil, &retry.NotFoundError{ - Message: state, + Message: string(gateway.DirectConnectGatewayState), LastRequest: input, } } @@ -96,7 +89,7 @@ func FindGatewayByID(ctx context.Context, conn *directconnect.DirectConnect, id return gateway, nil } -func FindGatewayAssociationByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.GatewayAssociation, error) { +func FindGatewayAssociationByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociation, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ AssociationId: aws.String(id), } @@ -104,7 +97,7 @@ func FindGatewayAssociationByID(ctx context.Context, conn *directconnect.DirectC return FindGatewayAssociation(ctx, conn, input) } -func FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context, conn *directconnect.DirectConnect, directConnectGatewayID, associatedGatewayID string) (*directconnect.GatewayAssociation, error) { +func FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, associatedGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ AssociatedGatewayId: aws.String(associatedGatewayID), DirectConnectGatewayId: aws.String(directConnectGatewayID), @@ -113,7 +106,7 @@ func FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context return FindGatewayAssociation(ctx, conn, input) } -func FindGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, conn *directconnect.DirectConnect, directConnectGatewayID, virtualGatewayID string) (*directconnect.GatewayAssociation, error) { +func FindGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, virtualGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ DirectConnectGatewayId: aws.String(directConnectGatewayID), VirtualGatewayId: aws.String(virtualGatewayID), @@ -122,26 +115,22 @@ func FindGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, c return FindGatewayAssociation(ctx, conn, input) } -func FindGatewayAssociation(ctx context.Context, conn *directconnect.DirectConnect, input *directconnect.DescribeDirectConnectGatewayAssociationsInput) (*directconnect.GatewayAssociation, error) { - output, err := conn.DescribeDirectConnectGatewayAssociationsWithContext(ctx, input) +func FindGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput) (*awstypes.DirectConnectGatewayAssociation, error) { + output, err := conn.DescribeDirectConnectGatewayAssociations(ctx, input) if err != nil { return nil, err } - if output == nil || len(output.DirectConnectGatewayAssociations) == 0 || output.DirectConnectGatewayAssociations[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + association, err := tfresource.AssertSingleValueResult(output.DirectConnectGatewayAssociations) - if count := len(output.DirectConnectGatewayAssociations); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - association := output.DirectConnectGatewayAssociations[0] - - if state := aws.StringValue(association.AssociationState); state == directconnect.GatewayAssociationStateDisassociated { + if association.AssociationState == awstypes.DirectConnectGatewayAssociationStateDisassociated { return nil, &retry.NotFoundError{ - Message: state, + Message: string(association.AssociationState), LastRequest: input, } } @@ -156,30 +145,26 @@ func FindGatewayAssociation(ctx context.Context, conn *directconnect.DirectConne return association, nil } -func FindGatewayAssociationProposalByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.GatewayAssociationProposal, error) { +func FindGatewayAssociationProposalByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociationProposal, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{ ProposalId: aws.String(id), } - output, err := conn.DescribeDirectConnectGatewayAssociationProposalsWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGatewayAssociationProposals(ctx, input) if err != nil { return nil, err } - if output == nil || len(output.DirectConnectGatewayAssociationProposals) == 0 || output.DirectConnectGatewayAssociationProposals[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + proposal, err := tfresource.AssertSingleValueResult(output.DirectConnectGatewayAssociationProposals) - if count := len(output.DirectConnectGatewayAssociationProposals); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - proposal := output.DirectConnectGatewayAssociationProposals[0] - - if state := aws.StringValue(proposal.ProposalState); state == directconnect.GatewayAssociationProposalStateDeleted { + if proposal.ProposalState == awstypes.DirectConnectGatewayAssociationProposalStateDeleted { return nil, &retry.NotFoundError{ - Message: state, + Message: string(proposal.ProposalState), LastRequest: input, } } @@ -194,14 +179,14 @@ func FindGatewayAssociationProposalByID(ctx context.Context, conn *directconnect return proposal, nil } -func FindHostedConnectionByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) { +func FindHostedConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { input := &directconnect.DescribeHostedConnectionsInput{ ConnectionId: aws.String(id), } - output, err := conn.DescribeHostedConnectionsWithContext(ctx, input) + output, err := conn.DescribeHostedConnections(ctx, input) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Connection with ID") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -212,19 +197,15 @@ func FindHostedConnectionByID(ctx context.Context, conn *directconnect.DirectCon return nil, err } - if output == nil || len(output.Connections) == 0 || output.Connections[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + connection, err := tfresource.AssertSingleValueResult(output.Connections) - if count := len(output.Connections); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - connection := output.Connections[0] - - if state := aws.StringValue(connection.ConnectionState); state == directconnect.ConnectionStateDeleted || state == directconnect.ConnectionStateRejected { + if connection.ConnectionState == awstypes.ConnectionStateDeleted || connection.ConnectionState == awstypes.ConnectionStateRejected { return nil, &retry.NotFoundError{ - Message: state, + Message: string(connection.ConnectionState), LastRequest: input, } } @@ -232,14 +213,14 @@ func FindHostedConnectionByID(ctx context.Context, conn *directconnect.DirectCon return connection, nil } -func FindLagByID(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Lag, error) { +func FindLagByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { input := &directconnect.DescribeLagsInput{ LagId: aws.String(id), } - output, err := conn.DescribeLagsWithContext(ctx, input) + output, err := conn.DescribeLags(ctx, input) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Lag with ID") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Lag with ID") { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -250,19 +231,15 @@ func FindLagByID(ctx context.Context, conn *directconnect.DirectConnect, id stri return nil, err } - if output == nil || len(output.Lags) == 0 || output.Lags[0] == nil { - return nil, tfresource.NewEmptyResultError(input) - } + lag, err := tfresource.AssertSingleValueResult(output.Lags) - if count := len(output.Lags); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) + if err != nil { + return nil, err } - lag := output.Lags[0] - - if state := aws.StringValue(lag.LagState); state == directconnect.LagStateDeleted { + if lag.LagState == awstypes.LagStateDeleted { return nil, &retry.NotFoundError{ - Message: state, + Message: string(lag.LagState), LastRequest: input, } } @@ -270,26 +247,26 @@ func FindLagByID(ctx context.Context, conn *directconnect.DirectConnect, id stri return lag, nil } -func FindLocationByCode(ctx context.Context, conn *directconnect.DirectConnect, code string) (*directconnect.Location, error) { +func FindLocationByCode(ctx context.Context, conn *directconnect.Client, code string) (awstypes.Location, error) { input := &directconnect.DescribeLocationsInput{} locations, err := FindLocations(ctx, conn, input) if err != nil { - return nil, err + return awstypes.Location{}, err } for _, location := range locations { - if aws.StringValue(location.LocationCode) == code { + if aws.ToString(location.LocationCode) == code { return location, nil } } - return nil, tfresource.NewEmptyResultError(input) + return awstypes.Location{}, tfresource.NewEmptyResultError(input) } -func FindLocations(ctx context.Context, conn *directconnect.DirectConnect, input *directconnect.DescribeLocationsInput) ([]*directconnect.Location, error) { - output, err := conn.DescribeLocationsWithContext(ctx, input) +func FindLocations(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLocationsInput) ([]awstypes.Location, error) { + output, err := conn.DescribeLocations(ctx, input) if err != nil { return nil, err diff --git a/internal/service/directconnect/gateway.go b/internal/service/directconnect/gateway.go index f8ccdcf5702..6c05fa48cb6 100644 --- a/internal/service/directconnect/gateway.go +++ b/internal/service/directconnect/gateway.go @@ -9,12 +9,13 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" + "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" @@ -58,7 +59,7 @@ func ResourceGateway() *schema.Resource { func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) name := d.Get("name").(string) input := &directconnect.CreateDirectConnectGatewayInput{ @@ -70,13 +71,13 @@ func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta int input.AmazonSideAsn = aws.Int64(v) } - output, err := conn.CreateDirectConnectGatewayWithContext(ctx, input) + output, err := conn.CreateDirectConnectGateway(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Gateway (%s): %s", name, err) } - d.SetId(aws.StringValue(output.DirectConnectGateway.DirectConnectGatewayId)) + d.SetId(aws.ToString(output.DirectConnectGateway.DirectConnectGatewayId)) if _, err := waitGatewayCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway (%s) create: %s", d.Id(), err) @@ -87,7 +88,7 @@ func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta int func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) output, err := FindGatewayByID(ctx, conn, d.Id()) @@ -101,7 +102,7 @@ func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta inter return sdkdiag.AppendErrorf(diags, "reading Direct Connect Gateway (%s): %s", d.Id(), err) } - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(output.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(output.AmazonSideAsn), 10)) d.Set("name", output.DirectConnectGatewayName) d.Set("owner_account_id", output.OwnerAccount) @@ -110,7 +111,7 @@ func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta inter func resourceGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.HasChange("name") { input := &directconnect.UpdateDirectConnectGatewayInput{ @@ -118,7 +119,7 @@ func resourceGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta int NewDirectConnectGatewayName: aws.String(d.Get("name").(string)), } - _, err := conn.UpdateDirectConnectGatewayWithContext(ctx, input) + _, err := conn.UpdateDirectConnectGateway(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Direct Connect Gateway (%s): %s", d.Id(), err) @@ -130,14 +131,14 @@ func resourceGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta int func resourceGatewayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Printf("[DEBUG] Deleting Direct Connect Gateway: %s", d.Id()) - _, err := conn.DeleteDirectConnectGatewayWithContext(ctx, &directconnect.DeleteDirectConnectGatewayInput{ + _, err := conn.DeleteDirectConnectGateway(ctx, &directconnect.DeleteDirectConnectGatewayInput{ DirectConnectGatewayId: aws.String(d.Id()), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "does not exist") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { return diags } diff --git a/internal/service/directconnect/gateway_association.go b/internal/service/directconnect/gateway_association.go index 5f2b9012615..1daa29382f8 100644 --- a/internal/service/directconnect/gateway_association.go +++ b/internal/service/directconnect/gateway_association.go @@ -10,12 +10,13 @@ import ( "strings" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" + "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" @@ -117,7 +118,7 @@ func ResourceGatewayAssociation() *schema.Resource { func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) var associationID string directConnectGatewayID := d.Get("dx_gateway_id").(string) @@ -134,16 +135,16 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat input.OverrideAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Accepting Direct Connect Gateway Association Proposal: %s", input) - output, err := conn.AcceptDirectConnectGatewayAssociationProposalWithContext(ctx, input) + log.Printf("[DEBUG] Accepting Direct Connect Gateway Association Proposal: %#v", input) + output, err := conn.AcceptDirectConnectGatewayAssociationProposal(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "accepting Direct Connect Gateway Association Proposal (%s): %s", proposalID, err) } // For historical reasons the resource ID isn't set to the association ID returned from the API. - associationID = aws.StringValue(output.DirectConnectGatewayAssociation.AssociationId) - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, aws.StringValue(output.DirectConnectGatewayAssociation.AssociatedGateway.Id))) + associationID = aws.ToString(output.DirectConnectGatewayAssociation.AssociationId) + d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, aws.ToString(output.DirectConnectGatewayAssociation.AssociatedGateway.Id))) } else { associatedGatewayID := d.Get("associated_gateway_id").(string) input := &directconnect.CreateDirectConnectGatewayAssociationInput{ @@ -155,15 +156,15 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat input.AddAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect Gateway Association: %s", input) - output, err := conn.CreateDirectConnectGatewayAssociationWithContext(ctx, input) + log.Printf("[DEBUG] Creating Direct Connect Gateway Association: %#v", input) + output, err := conn.CreateDirectConnectGatewayAssociation(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Gateway Association (%s/%s): %s", directConnectGatewayID, associatedGatewayID, err) } // For historical reasons the resource ID isn't set to the association ID returned from the API. - associationID = aws.StringValue(output.DirectConnectGatewayAssociation.AssociationId) + associationID = aws.ToString(output.DirectConnectGatewayAssociation.AssociationId) d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID)) } @@ -178,7 +179,7 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat func resourceGatewayAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) associationID := d.Get("dx_gateway_association_id").(string) @@ -210,7 +211,7 @@ func resourceGatewayAssociationRead(ctx context.Context, d *schema.ResourceData, func resourceGatewayAssociationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) associationID := d.Get("dx_gateway_association_id").(string) input := &directconnect.UpdateDirectConnectGatewayAssociationInput{ @@ -228,8 +229,8 @@ func resourceGatewayAssociationUpdate(ctx context.Context, d *schema.ResourceDat input.RemoveAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(del.List()) } - log.Printf("[DEBUG] Updating Direct Connect Gateway Association: %s", input) - _, err := conn.UpdateDirectConnectGatewayAssociationWithContext(ctx, input) + log.Printf("[DEBUG] Updating Direct Connect Gateway Association: %#v", input) + _, err := conn.UpdateDirectConnectGatewayAssociation(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Direct Connect Gateway Association (%s): %s", d.Id(), err) @@ -244,16 +245,16 @@ func resourceGatewayAssociationUpdate(ctx context.Context, d *schema.ResourceDat func resourceGatewayAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) associationID := d.Get("dx_gateway_association_id").(string) log.Printf("[DEBUG] Deleting Direct Connect Gateway Association: %s", d.Id()) - _, err := conn.DeleteDirectConnectGatewayAssociationWithContext(ctx, &directconnect.DeleteDirectConnectGatewayAssociationInput{ + _, err := conn.DeleteDirectConnectGatewayAssociation(ctx, &directconnect.DeleteDirectConnectGatewayAssociationInput{ AssociationId: aws.String(associationID), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "does not exist") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { return diags } @@ -269,7 +270,7 @@ func resourceGatewayAssociationDelete(ctx context.Context, d *schema.ResourceDat } func resourceGatewayAssociationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) parts := strings.Split(d.Id(), "/") diff --git a/internal/service/directconnect/gateway_association_migrate.go b/internal/service/directconnect/gateway_association_migrate.go index 6b655bb2816..b2cbfb1ae81 100644 --- a/internal/service/directconnect/gateway_association_migrate.go +++ b/internal/service/directconnect/gateway_association_migrate.go @@ -7,7 +7,7 @@ import ( "context" "log" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -79,7 +79,7 @@ func resourceGatewayAssociationResourceV0() *schema.Resource { } func GatewayAssociationStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Println("[INFO] Found Direct Connect Gateway Association state v0; migrating to v1") @@ -91,7 +91,7 @@ func GatewayAssociationStateUpgradeV0(ctx context.Context, rawState map[string]i return nil, err } - rawState["dx_gateway_association_id"] = aws.StringValue(output.AssociationId) + rawState["dx_gateway_association_id"] = aws.ToString(output.AssociationId) } return rawState, nil diff --git a/internal/service/directconnect/gateway_association_proposal.go b/internal/service/directconnect/gateway_association_proposal.go index f64345ececd..9d52632ba58 100644 --- a/internal/service/directconnect/gateway_association_proposal.go +++ b/internal/service/directconnect/gateway_association_proposal.go @@ -9,13 +9,14 @@ import ( "log" "strings" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -36,7 +37,7 @@ func ResourceGatewayAssociationProposal() *schema.Resource { // Accepting the proposal with overridden prefixes changes the returned RequestedAllowedPrefixesToDirectConnectGateway value (allowed_prefixes attribute). // We only want to force a new resource if this value changes and the current proposal state is "requested". customdiff.ForceNewIf("allowed_prefixes", func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Printf("[DEBUG] CustomizeDiff for Direct Connect Gateway Association Proposal (%s) allowed_prefixes", d.Id()) @@ -52,7 +53,7 @@ func ResourceGatewayAssociationProposal() *schema.Resource { return false } - return aws.StringValue(output.ProposalState) == directconnect.GatewayAssociationProposalStateRequested + return output.ProposalState == awstypes.DirectConnectGatewayAssociationProposalStateRequested }), ), @@ -98,7 +99,7 @@ func ResourceGatewayAssociationProposal() *schema.Resource { func resourceGatewayAssociationProposalCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) directConnectGatewayID := d.Get("dx_gateway_id").(string) associatedGatewayID := d.Get("associated_gateway_id").(string) @@ -112,21 +113,21 @@ func resourceGatewayAssociationProposalCreate(ctx context.Context, d *schema.Res input.AddAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect Gateway Association Proposal: %s", input) - output, err := conn.CreateDirectConnectGatewayAssociationProposalWithContext(ctx, input) + log.Printf("[DEBUG] Creating Direct Connect Gateway Association Proposal: %#v", input) + output, err := conn.CreateDirectConnectGatewayAssociationProposal(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Gateway Association Proposal (%s/%s): %s", directConnectGatewayID, associatedGatewayID, err) } - d.SetId(aws.StringValue(output.DirectConnectGatewayAssociationProposal.ProposalId)) + d.SetId(aws.ToString(output.DirectConnectGatewayAssociationProposal.ProposalId)) return append(diags, resourceGatewayAssociationProposalRead(ctx, d, meta)...) } func resourceGatewayAssociationProposalRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) // First attempt to find by proposal ID. output, err := FindGatewayAssociationProposalByID(ctx, conn, d.Id()) @@ -181,14 +182,14 @@ func resourceGatewayAssociationProposalRead(ctx context.Context, d *schema.Resou func resourceGatewayAssociationProposalDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Printf("[DEBUG] Deleting Direct Connect Gateway Association Proposal: %s", d.Id()) - _, err := conn.DeleteDirectConnectGatewayAssociationProposalWithContext(ctx, &directconnect.DeleteDirectConnectGatewayAssociationProposalInput{ + _, err := conn.DeleteDirectConnectGatewayAssociationProposal(ctx, &directconnect.DeleteDirectConnectGatewayAssociationProposalInput{ ProposalId: aws.String(d.Id()), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "is not found") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "is not found") { return diags } @@ -199,12 +200,12 @@ func resourceGatewayAssociationProposalDelete(ctx context.Context, d *schema.Res return diags } -func expandRouteFilterPrefixes(tfList []interface{}) []*directconnect.RouteFilterPrefix { +func expandRouteFilterPrefixes(tfList []interface{}) []awstypes.RouteFilterPrefix { if len(tfList) == 0 { return nil } - var apiObjects []*directconnect.RouteFilterPrefix + var apiObjects []awstypes.RouteFilterPrefix for _, tfStringRaw := range tfList { tfString, ok := tfStringRaw.(string) @@ -213,7 +214,7 @@ func expandRouteFilterPrefixes(tfList []interface{}) []*directconnect.RouteFilte continue } - apiObject := &directconnect.RouteFilterPrefix{ + apiObject := awstypes.RouteFilterPrefix{ Cidr: aws.String(tfString), } @@ -223,7 +224,7 @@ func expandRouteFilterPrefixes(tfList []interface{}) []*directconnect.RouteFilte return apiObjects } -func flattenRouteFilterPrefixes(apiObjects []*directconnect.RouteFilterPrefix) []interface{} { +func flattenRouteFilterPrefixes(apiObjects []awstypes.RouteFilterPrefix) []interface{} { if len(apiObjects) == 0 { return nil } @@ -231,11 +232,7 @@ func flattenRouteFilterPrefixes(apiObjects []*directconnect.RouteFilterPrefix) [ var tfList []interface{} for _, apiObject := range apiObjects { - if apiObject == nil { - continue - } - - tfList = append(tfList, aws.StringValue(apiObject.Cidr)) + tfList = append(tfList, aws.ToString(apiObject.Cidr)) } return tfList diff --git a/internal/service/directconnect/gateway_association_proposal_test.go b/internal/service/directconnect/gateway_association_proposal_test.go index 796b8492744..f41636f9716 100644 --- a/internal/service/directconnect/gateway_association_proposal_test.go +++ b/internal/service/directconnect/gateway_association_proposal_test.go @@ -9,8 +9,8 @@ import ( "strings" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -23,7 +23,7 @@ import ( func TestAccDirectConnectGatewayAssociationProposal_basicVPNGateway(t *testing.T) { ctx := acctest.Context(t) - var proposal directconnect.GatewayAssociationProposal + var proposal awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -59,7 +59,7 @@ func TestAccDirectConnectGatewayAssociationProposal_basicVPNGateway(t *testing.T func TestAccDirectConnectGatewayAssociationProposal_basicTransitGateway(t *testing.T) { ctx := acctest.Context(t) - var proposal directconnect.GatewayAssociationProposal + var proposal awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -97,7 +97,7 @@ func TestAccDirectConnectGatewayAssociationProposal_basicTransitGateway(t *testi func TestAccDirectConnectGatewayAssociationProposal_disappears(t *testing.T) { ctx := acctest.Context(t) - var proposal directconnect.GatewayAssociationProposal + var proposal awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -122,7 +122,7 @@ func TestAccDirectConnectGatewayAssociationProposal_disappears(t *testing.T) { func TestAccDirectConnectGatewayAssociationProposal_endOfLifeVPN(t *testing.T) { ctx := acctest.Context(t) - var proposal directconnect.GatewayAssociationProposal + var proposal awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -145,9 +145,9 @@ func TestAccDirectConnectGatewayAssociationProposal_endOfLifeVPN(t *testing.T) { ResourceName: resourceName, ImportStateIdFunc: func(s *terraform.State) (string, error) { return strings.Join([]string{ - aws.StringValue(proposal.ProposalId), - aws.StringValue(proposal.DirectConnectGatewayId), - aws.StringValue(proposal.AssociatedGateway.Id), + aws.ToString(proposal.ProposalId), + aws.ToString(proposal.DirectConnectGatewayId), + aws.ToString(proposal.AssociatedGateway.Id), }, "/"), nil }, ImportState: true, @@ -159,7 +159,7 @@ func TestAccDirectConnectGatewayAssociationProposal_endOfLifeVPN(t *testing.T) { func TestAccDirectConnectGatewayAssociationProposal_endOfLifeTgw(t *testing.T) { ctx := acctest.Context(t) - var proposal directconnect.GatewayAssociationProposal + var proposal awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -182,9 +182,9 @@ func TestAccDirectConnectGatewayAssociationProposal_endOfLifeTgw(t *testing.T) { ResourceName: resourceName, ImportStateIdFunc: func(s *terraform.State) (string, error) { return strings.Join([]string{ - aws.StringValue(proposal.ProposalId), - aws.StringValue(proposal.DirectConnectGatewayId), - aws.StringValue(proposal.AssociatedGateway.Id), + aws.ToString(proposal.ProposalId), + aws.ToString(proposal.DirectConnectGatewayId), + aws.ToString(proposal.AssociatedGateway.Id), }, "/"), nil }, ImportState: true, @@ -196,7 +196,7 @@ func TestAccDirectConnectGatewayAssociationProposal_endOfLifeTgw(t *testing.T) { func TestAccDirectConnectGatewayAssociationProposal_allowedPrefixes(t *testing.T) { ctx := acctest.Context(t) - var proposal1, proposal2 directconnect.GatewayAssociationProposal + var proposal1, proposal2 awstypes.DirectConnectGatewayAssociationProposal rBgpAsn := sdkacctest.RandIntRange(64512, 65534) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_dx_gateway_association_proposal.test" @@ -234,7 +234,7 @@ func TestAccDirectConnectGatewayAssociationProposal_allowedPrefixes(t *testing.T func testAccCheckGatewayAssociationProposalDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_gateway_association_proposal" { @@ -258,7 +258,7 @@ func testAccCheckGatewayAssociationProposalDestroy(ctx context.Context) resource } } -func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceName string, gatewayAssociationProposal *directconnect.GatewayAssociationProposal) resource.TestCheckFunc { +func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceName string, gatewayAssociationProposal *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] if !ok { @@ -269,7 +269,7 @@ func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceN return fmt.Errorf("No ID is set") } - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) output, err := tfdirectconnect.FindGatewayAssociationProposalByID(ctx, conn, rs.Primary.ID) @@ -283,9 +283,9 @@ func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceN } } -func testAccCheckGatewayAssociationProposalRecreated(old, new *directconnect.GatewayAssociationProposal) resource.TestCheckFunc { +func testAccCheckGatewayAssociationProposalRecreated(old, new *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { - if old, new := aws.StringValue(old.ProposalId), aws.StringValue(new.ProposalId); old == new { + if old, new := aws.ToString(old.ProposalId), aws.ToString(new.ProposalId); old == new { return fmt.Errorf("Direct Connect Gateway Association Proposal (%s) not recreated", old) } @@ -300,7 +300,7 @@ func testAccCheckGatewayAssociationProposalAccepted(ctx context.Context, resourc return fmt.Errorf("Not found: %s", resourceName) } - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) output, err := tfdirectconnect.FindGatewayAssociationProposalByID(ctx, conn, rs.Primary.ID) @@ -308,8 +308,8 @@ func testAccCheckGatewayAssociationProposalAccepted(ctx context.Context, resourc return err } - if state := aws.StringValue(output.ProposalState); state != directconnect.GatewayAssociationProposalStateAccepted { - return fmt.Errorf("Direct Connect Gateway Association Proposal (%s) not accepted (%s)", rs.Primary.ID, state) + if output.ProposalState != awstypes.DirectConnectGatewayAssociationProposalStateAccepted { + return fmt.Errorf("Direct Connect Gateway Association Proposal (%s) not accepted (%s)", rs.Primary.ID, string(output.ProposalState)) } return nil diff --git a/internal/service/directconnect/gateway_association_test.go b/internal/service/directconnect/gateway_association_test.go index 39f95b64f67..faab6004407 100644 --- a/internal/service/directconnect/gateway_association_test.go +++ b/internal/service/directconnect/gateway_association_test.go @@ -8,8 +8,8 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -26,8 +26,8 @@ func TestAccDirectConnectGatewayAssociation_v0StateUpgrade(t *testing.T) { resourceName := "aws_dx_gateway_association.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -53,8 +53,8 @@ func TestAccDirectConnectGatewayAssociation_basicVPNGatewaySingleAccount(t *test resourceNameVgw := "aws_vpn_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -93,8 +93,8 @@ func TestAccDirectConnectGatewayAssociation_basicVPNGatewayCrossAccount(t *testi resourceNameVgw := "aws_vpn_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAlternateAccount(t) }, @@ -128,8 +128,8 @@ func TestAccDirectConnectGatewayAssociation_basicTransitGatewaySingleAccount(t * resourceNameTgw := "aws_ec2_transit_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -169,8 +169,8 @@ func TestAccDirectConnectGatewayAssociation_basicTransitGatewayCrossAccount(t *t resourceNameTgw := "aws_ec2_transit_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAlternateAccount(t) }, @@ -204,8 +204,8 @@ func TestAccDirectConnectGatewayAssociation_multiVPNGatewaysSingleAccount(t *tes resourceName2 := "aws_dx_gateway_association.test.1" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -237,8 +237,8 @@ func TestAccDirectConnectGatewayAssociation_allowedPrefixesVPNGatewaySingleAccou resourceNameVgw := "aws_vpn_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -283,8 +283,8 @@ func TestAccDirectConnectGatewayAssociation_allowedPrefixesVPNGatewayCrossAccoun resourceNameVgw := "aws_vpn_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga directconnect.GatewayAssociation - var gap directconnect.GatewayAssociationProposal + var ga awstypes.DirectConnectGatewayAssociation + var gap awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAlternateAccount(t) }, @@ -326,8 +326,8 @@ func TestAccDirectConnectGatewayAssociation_recreateProposal(t *testing.T) { resourceName := "aws_dx_gateway_association.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) - var ga1, ga2 directconnect.GatewayAssociation - var gap1, gap2 directconnect.GatewayAssociationProposal + var ga1, ga2 awstypes.DirectConnectGatewayAssociation + var gap1, gap2 awstypes.DirectConnectGatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAlternateAccount(t) }, @@ -366,7 +366,7 @@ func testAccGatewayAssociationImportStateIdFunc(resourceName string) resource.Im func testAccCheckGatewayAssociationDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_gateway_association" { @@ -389,7 +389,7 @@ func testAccCheckGatewayAssociationDestroy(ctx context.Context) resource.TestChe } } -func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga *directconnect.GatewayAssociation, gap *directconnect.GatewayAssociationProposal) resource.TestCheckFunc { +func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga *awstypes.DirectConnectGatewayAssociation, gap *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -400,7 +400,7 @@ func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga * return fmt.Errorf("No Direct Connect Gateway Association ID is set") } - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) output, err := tfdirectconnect.FindGatewayAssociationByID(ctx, conn, rs.Primary.Attributes["dx_gateway_association_id"]) @@ -424,9 +424,9 @@ func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga * } } -func testAccCheckGatewayAssociationNotRecreated(old, new *directconnect.GatewayAssociation) resource.TestCheckFunc { +func testAccCheckGatewayAssociationNotRecreated(old, new *awstypes.DirectConnectGatewayAssociation) resource.TestCheckFunc { return func(s *terraform.State) error { - if old, new := aws.StringValue(old.AssociationId), aws.StringValue(new.AssociationId); old != new { + if old, new := aws.ToString(old.AssociationId), aws.ToString(new.AssociationId); old != new { return fmt.Errorf("Direct Connect Gateway Association (%s) recreated (%s)", old, new) } diff --git a/internal/service/directconnect/gateway_data_source.go b/internal/service/directconnect/gateway_data_source.go index 0765282ae5f..a5c4ec8ebb8 100644 --- a/internal/service/directconnect/gateway_data_source.go +++ b/internal/service/directconnect/gateway_data_source.go @@ -7,8 +7,9 @@ import ( "context" "strconv" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -39,19 +40,19 @@ func DataSourceGateway() *schema.Resource { func dataSourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) name := d.Get("name").(string) - gateways := make([]*directconnect.Gateway, 0) + gateways := make([]awstypes.DirectConnectGateway, 0) // DescribeDirectConnectGatewaysInput does not have a name parameter for filtering input := &directconnect.DescribeDirectConnectGatewaysInput{} for { - output, err := conn.DescribeDirectConnectGatewaysWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGateways(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Direct Connect Gateway: %s", err) } for _, gateway := range output.DirectConnectGateways { - if aws.StringValue(gateway.DirectConnectGatewayName) == name { + if aws.ToString(gateway.DirectConnectGatewayName) == name { gateways = append(gateways, gateway) } } @@ -71,8 +72,8 @@ func dataSourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta int gateway := gateways[0] - d.SetId(aws.StringValue(gateway.DirectConnectGatewayId)) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(gateway.AmazonSideAsn), 10)) + d.SetId(aws.ToString(gateway.DirectConnectGatewayId)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(gateway.AmazonSideAsn), 10)) d.Set("owner_account_id", gateway.OwnerAccount) return diags diff --git a/internal/service/directconnect/gateway_test.go b/internal/service/directconnect/gateway_test.go index 204427f409d..15246b83e20 100644 --- a/internal/service/directconnect/gateway_test.go +++ b/internal/service/directconnect/gateway_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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 TestAccDirectConnectGateway_basic(t *testing.T) { ctx := acctest.Context(t) - var v directconnect.Gateway + var v awstypes.DirectConnectGateway rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) resourceName := "aws_dx_gateway.test" @@ -50,7 +50,7 @@ func TestAccDirectConnectGateway_basic(t *testing.T) { func TestAccDirectConnectGateway_disappears(t *testing.T) { ctx := acctest.Context(t) - var v directconnect.Gateway + var v awstypes.DirectConnectGateway rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) resourceName := "aws_dx_gateway.test" @@ -75,7 +75,7 @@ func TestAccDirectConnectGateway_disappears(t *testing.T) { func TestAccDirectConnectGateway_complex(t *testing.T) { ctx := acctest.Context(t) - var v directconnect.Gateway + var v awstypes.DirectConnectGateway rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) resourceName := "aws_dx_gateway.test" @@ -104,7 +104,7 @@ func TestAccDirectConnectGateway_complex(t *testing.T) { func TestAccDirectConnectGateway_update(t *testing.T) { ctx := acctest.Context(t) - var v directconnect.Gateway + var v awstypes.DirectConnectGateway rName1 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rBgpAsn := sdkacctest.RandIntRange(64512, 65534) @@ -136,7 +136,7 @@ func TestAccDirectConnectGateway_update(t *testing.T) { func testAccCheckGatewayDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_gateway" { @@ -159,7 +159,7 @@ func testAccCheckGatewayDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckGatewayExists(ctx context.Context, name string, v *directconnect.Gateway) resource.TestCheckFunc { +func testAccCheckGatewayExists(ctx context.Context, name string, v *awstypes.DirectConnectGateway) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -170,7 +170,7 @@ func testAccCheckGatewayExists(ctx context.Context, name string, v *directconnec return fmt.Errorf("No ID is set") } - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) output, err := tfdirectconnect.FindGatewayByID(ctx, conn, rs.Primary.ID) diff --git a/internal/service/directconnect/generate.go b/internal/service/directconnect/generate.go index 13a0f277c61..d6180ff44f6 100644 --- a/internal/service/directconnect/generate.go +++ b/internal/service/directconnect/generate.go @@ -1,8 +1,8 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:generate go run ../../generate/listpages/main.go -ListOps=DescribeDirectConnectGateways,DescribeDirectConnectGatewayAssociations,DescribeDirectConnectGatewayAssociationProposals -//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=DescribeTags -ListTagsInIDElem=ResourceArns -ListTagsInIDNeedSlice=yes -ListTagsOutTagsElem=ResourceTags[0].Tags -ServiceTagsSlice -UpdateTags -CreateTags +//go:generate go run ../../generate/listpages/main.go -AWSSDKVersion=2 -ListOps=DescribeDirectConnectGateways,DescribeDirectConnectGatewayAssociations,DescribeDirectConnectGatewayAssociationProposals +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsOp=DescribeTags -ListTagsInIDElem=ResourceArns -ListTagsInIDNeedValueSlice=yes -ListTagsOutTagsElem=ResourceTags[0].Tags -ServiceTagsSlice -UpdateTags -CreateTags //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/directconnect/hosted_connection.go b/internal/service/directconnect/hosted_connection.go index c0601520e31..f11be99bebb 100644 --- a/internal/service/directconnect/hosted_connection.go +++ b/internal/service/directconnect/hosted_connection.go @@ -8,8 +8,8 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -101,7 +101,7 @@ func ResourceHostedConnection() *schema.Resource { func resourceHostedConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) name := d.Get("name").(string) input := &directconnect.AllocateHostedConnectionInput{ @@ -109,24 +109,24 @@ func resourceHostedConnectionCreate(ctx context.Context, d *schema.ResourceData, ConnectionId: aws.String(d.Get("connection_id").(string)), ConnectionName: aws.String(name), OwnerAccount: aws.String(d.Get("owner_account_id").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), } - log.Printf("[DEBUG] Creating Direct Connect Hosted Connection: %s", input) - output, err := conn.AllocateHostedConnectionWithContext(ctx, input) + log.Printf("[DEBUG] Creating Direct Connect Hosted Connection: %#v", input) + output, err := conn.AllocateHostedConnection(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect Hosted Connection (%s): %s", name, err) } - d.SetId(aws.StringValue(output.ConnectionId)) + d.SetId(aws.ToString(output.ConnectionId)) return append(diags, resourceHostedConnectionRead(ctx, d, meta)...) } func resourceHostedConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) connection, err := FindHostedConnectionByID(ctx, conn, d.Id()) @@ -147,7 +147,7 @@ func resourceHostedConnectionRead(ctx context.Context, d *schema.ResourceData, m d.Set("has_logical_redundancy", connection.HasLogicalRedundancy) d.Set("jumbo_frame_capable", connection.JumboFrameCapable) d.Set("lag_id", connection.LagId) - d.Set("loa_issue_time", aws.TimeValue(connection.LoaIssueTime).Format(time.RFC3339)) + d.Set("loa_issue_time", aws.ToTime(connection.LoaIssueTime).Format(time.RFC3339)) d.Set("location", connection.Location) d.Set("name", connection.ConnectionName) d.Set("owner_account_id", connection.OwnerAccount) @@ -162,7 +162,7 @@ func resourceHostedConnectionRead(ctx context.Context, d *schema.ResourceData, m func resourceHostedConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if err := deleteConnection(ctx, conn, d.Id(), waitHostedConnectionDeleted); err != nil { return sdkdiag.AppendFromErr(diags, err) diff --git a/internal/service/directconnect/hosted_connection_test.go b/internal/service/directconnect/hosted_connection_test.go index 72e07f79b1b..182c4a7f9af 100644 --- a/internal/service/directconnect/hosted_connection_test.go +++ b/internal/service/directconnect/hosted_connection_test.go @@ -73,7 +73,7 @@ func testAccCheckHostedConnectionEnv() (*testAccDxHostedConnectionEnv, error) { func testAccCheckHostedConnectionDestroy(ctx context.Context, providerFunc func() *schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { provider := providerFunc() - conn := provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_hosted_connection" { @@ -99,7 +99,7 @@ func testAccCheckHostedConnectionDestroy(ctx context.Context, providerFunc func( func testAccCheckHostedConnectionExists(ctx context.Context, name string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) rs, ok := s.RootModule().Resources[name] if !ok { diff --git a/internal/service/directconnect/hosted_private_virtual_interface.go b/internal/service/directconnect/hosted_private_virtual_interface.go index 50af122d5f2..7d66f982218 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface.go +++ b/internal/service/directconnect/hosted_private_virtual_interface.go @@ -10,13 +10,15 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) @@ -33,13 +35,10 @@ func ResourceHostedPrivateVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -120,16 +119,16 @@ func ResourceHostedPrivateVirtualInterface() *schema.Resource { func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocatePrivateVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), - NewPrivateVirtualInterfaceAllocation: &directconnect.NewPrivateVirtualInterfaceAllocation{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), - Mtu: aws.Int64(int64(d.Get("mtu").(int))), + NewPrivateVirtualInterfaceAllocation: &awstypes.NewPrivateVirtualInterfaceAllocation{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), + Mtu: aws.Int32(int32(d.Get("mtu").(int))), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, OwnerAccount: aws.String(d.Get("owner_account_id").(string)), } @@ -143,16 +142,16 @@ func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema. req.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) } if v, ok := d.GetOk("mtu"); ok { - req.NewPrivateVirtualInterfaceAllocation.Mtu = aws.Int64(int64(v.(int))) + req.NewPrivateVirtualInterfaceAllocation.Mtu = aws.Int32(int32(v.(int))) } - log.Printf("[DEBUG] Creating Direct Connect hosted private virtual interface: %s", req) - resp, err := conn.AllocatePrivateVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Creating Direct Connect hosted private virtual interface: %#v", req) + resp, err := conn.AllocatePrivateVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect hosted private virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterfaceId)) if err := hostedPrivateVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -163,7 +162,7 @@ func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema. func resourceHostedPrivateVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -177,7 +176,7 @@ func resourceHostedPrivateVirtualInterfaceRead(ctx context.Context, d *schema.Re d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -205,7 +204,7 @@ func resourceHostedPrivateVirtualInterfaceDelete(ctx context.Context, d *schema. } func resourceHostedPrivateVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -215,23 +214,17 @@ func resourceHostedPrivateVirtualInterfaceImport(ctx context.Context, d *schema. return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "private" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } return []*schema.ResourceData{d}, nil } -func hostedPrivateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedPrivateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/hosted_private_virtual_interface_accepter.go b/internal/service/directconnect/hosted_private_virtual_interface_accepter.go index 3744491ffd9..8e6df20aed3 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_private_virtual_interface_accepter.go @@ -9,12 +9,14 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "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" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -70,7 +72,7 @@ func ResourceHostedPrivateVirtualInterfaceAccepter() *schema.Resource { func resourceHostedPrivateVirtualInterfaceAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id") dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id") @@ -89,8 +91,8 @@ func resourceHostedPrivateVirtualInterfaceAccepterCreate(ctx context.Context, d req.VirtualGatewayId = aws.String(vgwIdRaw.(string)) } - log.Printf("[DEBUG] Accepting Direct Connect hosted private virtual interface: %s", req) - _, err := conn.ConfirmPrivateVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Accepting Direct Connect hosted private virtual interface: %#v", req) + _, err := conn.ConfirmPrivateVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted private virtual interface: %s", err) } @@ -118,7 +120,7 @@ func resourceHostedPrivateVirtualInterfaceAccepterCreate(ctx context.Context, d func resourceHostedPrivateVirtualInterfaceAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -129,10 +131,9 @@ func resourceHostedPrivateVirtualInterfaceAccepterRead(ctx context.Context, d *s d.SetId("") return diags } - vifState := aws.StringValue(vif.VirtualInterfaceState) - if vifState != directconnect.VirtualInterfaceStateAvailable && - vifState != directconnect.VirtualInterfaceStateDown { - log.Printf("[WARN] Direct Connect hosted private virtual interface (%s) is '%s', removing from state", vifState, d.Id()) + if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && + vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown { + log.Printf("[WARN] Direct Connect hosted private virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) d.SetId("") return diags } @@ -162,7 +163,7 @@ func resourceHostedPrivateVirtualInterfaceAccepterDelete(ctx context.Context, d } func resourceHostedPrivateVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -172,7 +173,7 @@ func resourceHostedPrivateVirtualInterfaceAccepterImport(ctx context.Context, d return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "private" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } @@ -188,16 +189,10 @@ func resourceHostedPrivateVirtualInterfaceAccepterImport(ctx context.Context, d return []*schema.ResourceData{d}, nil } -func hostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/hosted_private_virtual_interface_test.go b/internal/service/directconnect/hosted_private_virtual_interface_test.go index a1073f76033..70317ac6e27 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_private_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -28,7 +28,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_private_virtual_interface.test" accepterResourceName := "aws_dx_hosted_private_virtual_interface_accepter.test" vpnGatewayResourceName := "aws_vpn_gateway.test" @@ -51,7 +51,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -87,7 +87,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_private_virtual_interface.test" accepterResourceName := "aws_dx_hosted_private_virtual_interface_accepter.test" vpnGatewayResourceName := "aws_vpn_gateway.test" @@ -110,7 +110,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -136,7 +136,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -166,7 +166,7 @@ func testAccCheckHostedPrivateVirtualInterfaceDestroy(ctx context.Context) resou } } -func testAccCheckHostedPrivateVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckHostedPrivateVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/hosted_public_virtual_interface.go b/internal/service/directconnect/hosted_public_virtual_interface.go index 4f679676055..6eadfe71f99 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface.go +++ b/internal/service/directconnect/hosted_public_virtual_interface.go @@ -10,13 +10,15 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) @@ -34,13 +36,10 @@ func ResourceHostedPublicVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -117,15 +116,15 @@ func ResourceHostedPublicVirtualInterface() *schema.Resource { func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocatePublicVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), - NewPublicVirtualInterfaceAllocation: &directconnect.NewPublicVirtualInterfaceAllocation{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), + NewPublicVirtualInterfaceAllocation: &awstypes.NewPublicVirtualInterfaceAllocation{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, OwnerAccount: aws.String(d.Get("owner_account_id").(string)), } @@ -142,13 +141,13 @@ func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.R req.NewPublicVirtualInterfaceAllocation.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Allocating Direct Connect hosted public virtual interface: %s", req) - resp, err := conn.AllocatePublicVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Allocating Direct Connect hosted public virtual interface: %#v", req) + resp, err := conn.AllocatePublicVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "allocating Direct Connect hosted public virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterfaceId)) if err := hostedPublicVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -159,7 +158,7 @@ func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.R func resourceHostedPublicVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -173,7 +172,7 @@ func resourceHostedPublicVirtualInterfaceRead(ctx context.Context, d *schema.Res d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -202,7 +201,7 @@ func resourceHostedPublicVirtualInterfaceDelete(ctx context.Context, d *schema.R } func resourceHostedPublicVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -212,7 +211,7 @@ func resourceHostedPublicVirtualInterfaceImport(ctx context.Context, d *schema.R return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "public" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } @@ -222,7 +221,7 @@ func resourceHostedPublicVirtualInterfaceImport(ctx context.Context, d *schema.R func resourceHostedPublicVirtualInterfaceCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { if diff.Id() == "" { // New resource. - if addressFamily := diff.Get("address_family").(string); addressFamily == directconnect.AddressFamilyIpv4 { + if addressFamily := diff.Get("address_family").(string); addressFamily == string(awstypes.AddressFamilyIPv4) { if _, ok := diff.GetOk("customer_address"); !ok { return fmt.Errorf("'customer_address' must be set when 'address_family' is '%s'", addressFamily) } @@ -235,17 +234,10 @@ func resourceHostedPublicVirtualInterfaceCustomizeDiff(_ context.Context, diff * return nil } -func hostedPublicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedPublicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStateDown, - directconnect.VirtualInterfaceStateVerifying, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) } diff --git a/internal/service/directconnect/hosted_public_virtual_interface_accepter.go b/internal/service/directconnect/hosted_public_virtual_interface_accepter.go index b864da38d75..397dd3fbcca 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_public_virtual_interface_accepter.go @@ -9,12 +9,14 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "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" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -58,15 +60,15 @@ func ResourceHostedPublicVirtualInterfaceAccepter() *schema.Resource { func resourceHostedPublicVirtualInterfaceAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vifId := d.Get("virtual_interface_id").(string) req := &directconnect.ConfirmPublicVirtualInterfaceInput{ VirtualInterfaceId: aws.String(vifId), } - log.Printf("[DEBUG] Accepting Direct Connect hosted public virtual interface: %s", req) - _, err := conn.ConfirmPublicVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Accepting Direct Connect hosted public virtual interface: %#v", req) + _, err := conn.ConfirmPublicVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted public virtual interface: %s", err) } @@ -94,7 +96,7 @@ func resourceHostedPublicVirtualInterfaceAccepterCreate(ctx context.Context, d * func resourceHostedPublicVirtualInterfaceAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -105,11 +107,10 @@ func resourceHostedPublicVirtualInterfaceAccepterRead(ctx context.Context, d *sc d.SetId("") return diags } - vifState := aws.StringValue(vif.VirtualInterfaceState) - if vifState != directconnect.VirtualInterfaceStateAvailable && - vifState != directconnect.VirtualInterfaceStateDown && - vifState != directconnect.VirtualInterfaceStateVerifying { - log.Printf("[WARN] Direct Connect hosted public virtual interface (%s) is '%s', removing from state", vifState, d.Id()) + if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && + vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown && + vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateVerifying { + log.Printf("[WARN] Direct Connect hosted public virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) d.SetId("") return diags } @@ -137,7 +138,7 @@ func resourceHostedPublicVirtualInterfaceAccepterDelete(ctx context.Context, d * } func resourceHostedPublicVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -147,7 +148,7 @@ func resourceHostedPublicVirtualInterfaceAccepterImport(ctx context.Context, d * return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "public" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } @@ -163,17 +164,10 @@ func resourceHostedPublicVirtualInterfaceAccepterImport(ctx context.Context, d * return []*schema.ResourceData{d}, nil } -func hostedPublicVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedPublicVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - directconnect.VirtualInterfaceStateVerifying, - }) + enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) } diff --git a/internal/service/directconnect/hosted_public_virtual_interface_test.go b/internal/service/directconnect/hosted_public_virtual_interface_test.go index 37c07a145d6..ac2d20aa5cc 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_public_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -28,7 +28,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_public_virtual_interface.test" accepterResourceName := "aws_dx_hosted_public_virtual_interface_accepter.test" rName := fmt.Sprintf("tf-testacc-public-vif-%s", sdkacctest.RandString(10)) @@ -53,7 +53,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -89,7 +89,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_public_virtual_interface.test" accepterResourceName := "aws_dx_hosted_public_virtual_interface_accepter.test" rName := fmt.Sprintf("tf-testacc-public-vif-%s", sdkacctest.RandString(10)) @@ -114,7 +114,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -141,7 +141,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -171,7 +171,7 @@ func testAccCheckHostedPublicVirtualInterfaceDestroy(ctx context.Context) resour } } -func testAccCheckHostedPublicVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckHostedPublicVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/hosted_transit_virtual_interface.go b/internal/service/directconnect/hosted_transit_virtual_interface.go index 8dce83bbf10..5ec52561824 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface.go @@ -10,13 +10,15 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) @@ -33,13 +35,10 @@ func ResourceHostedTransitVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -120,17 +119,17 @@ func ResourceHostedTransitVirtualInterface() *schema.Resource { func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocateTransitVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), OwnerAccount: aws.String(d.Get("owner_account_id").(string)), - NewTransitVirtualInterfaceAllocation: &directconnect.NewTransitVirtualInterfaceAllocation{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), - Mtu: aws.Int64(int64(d.Get("mtu").(int))), + NewTransitVirtualInterfaceAllocation: &awstypes.NewTransitVirtualInterfaceAllocation{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), + Mtu: aws.Int32(int32(d.Get("mtu").(int))), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, } if v, ok := d.GetOk("amazon_address"); ok { @@ -143,13 +142,13 @@ func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema. req.NewTransitVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect hosted transit virtual interface: %s", req) - resp, err := conn.AllocateTransitVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Creating Direct Connect hosted transit virtual interface: %#v", req) + resp, err := conn.AllocateTransitVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect hosted transit virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterface.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterface.VirtualInterfaceId)) if err := hostedTransitVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -160,7 +159,7 @@ func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema. func resourceHostedTransitVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -174,7 +173,7 @@ func resourceHostedTransitVirtualInterfaceRead(ctx context.Context, d *schema.Re d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -202,7 +201,7 @@ func resourceHostedTransitVirtualInterfaceDelete(ctx context.Context, d *schema. } func resourceHostedTransitVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -212,23 +211,17 @@ func resourceHostedTransitVirtualInterfaceImport(ctx context.Context, d *schema. return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "transit" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } return []*schema.ResourceData{d}, nil } -func hostedTransitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedTransitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go b/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go index dc0184894b4..ef3c37326df 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go @@ -9,12 +9,14 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "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" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -63,7 +65,7 @@ func ResourceHostedTransitVirtualInterfaceAccepter() *schema.Resource { func resourceHostedTransitVirtualInterfaceAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vifId := d.Get("virtual_interface_id").(string) req := &directconnect.ConfirmTransitVirtualInterfaceInput{ @@ -71,8 +73,8 @@ func resourceHostedTransitVirtualInterfaceAccepterCreate(ctx context.Context, d VirtualInterfaceId: aws.String(vifId), } - log.Printf("[DEBUG] Accepting Direct Connect hosted transit virtual interface: %s", req) - _, err := conn.ConfirmTransitVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Accepting Direct Connect hosted transit virtual interface: %#v", req) + _, err := conn.ConfirmTransitVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted transit virtual interface (%s): %s", vifId, err) } @@ -100,7 +102,7 @@ func resourceHostedTransitVirtualInterfaceAccepterCreate(ctx context.Context, d func resourceHostedTransitVirtualInterfaceAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -111,9 +113,8 @@ func resourceHostedTransitVirtualInterfaceAccepterRead(ctx context.Context, d *s d.SetId("") return diags } - vifState := aws.StringValue(vif.VirtualInterfaceState) - if vifState != directconnect.VirtualInterfaceStateAvailable && vifState != directconnect.VirtualInterfaceStateDown { - log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", vifState, d.Id()) + if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown { + log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) d.SetId("") return diags } @@ -142,7 +143,7 @@ func resourceHostedTransitVirtualInterfaceAccepterDelete(ctx context.Context, d } func resourceHostedTransitVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -152,7 +153,7 @@ func resourceHostedTransitVirtualInterfaceAccepterImport(ctx context.Context, d return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "transit" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } @@ -168,16 +169,10 @@ func resourceHostedTransitVirtualInterfaceAccepterImport(ctx context.Context, d return []*schema.ResourceData{d}, nil } -func hostedTransitVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func hostedTransitVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/hosted_transit_virtual_interface_test.go b/internal/service/directconnect/hosted_transit_virtual_interface_test.go index 1ad1e135c62..3a27bd15a91 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -39,7 +39,7 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_transit_virtual_interface.test" accepterResourceName := "aws_dx_hosted_transit_virtual_interface_accepter.test" dxGatewayResourceName := "aws_dx_gateway.test" @@ -63,7 +63,7 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -99,7 +99,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_transit_virtual_interface.test" accepterResourceName := "aws_dx_hosted_transit_virtual_interface_accepter.test" dxGatewayResourceName := "aws_dx_gateway.test" @@ -123,7 +123,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -149,7 +149,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -173,7 +173,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { }) } -func testAccCheckHostedTransitVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckHostedTransitVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/lag.go b/internal/service/directconnect/lag.go index 3fe7f9a3585..6eb31549da5 100644 --- a/internal/service/directconnect/lag.go +++ b/internal/service/directconnect/lag.go @@ -8,13 +8,14 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "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" + "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -92,7 +93,7 @@ func ResourceLag() *schema.Resource { func resourceLagCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) name := d.Get("name").(string) input := &directconnect.CreateLagInput{ @@ -106,27 +107,27 @@ func resourceLagCreate(ctx context.Context, d *schema.ResourceData, meta interfa if v, ok := d.GetOk("connection_id"); ok { connectionIDSpecified = true input.ConnectionId = aws.String(v.(string)) - input.NumberOfConnections = aws.Int64(1) + input.NumberOfConnections = int32(1) } else { - input.NumberOfConnections = aws.Int64(1) + input.NumberOfConnections = int32(1) } if v, ok := d.GetOk("provider_name"); ok { input.ProviderName = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect LAG: %s", input) - output, err := conn.CreateLagWithContext(ctx, input) + log.Printf("[DEBUG] Creating Direct Connect LAG: %#v", input) + output, err := conn.CreateLag(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect LAG (%s): %s", name, err) } - d.SetId(aws.StringValue(output.LagId)) + d.SetId(aws.ToString(output.LagId)) // Delete unmanaged connection. if !connectionIDSpecified { - if err := deleteConnection(ctx, conn, aws.StringValue(output.Connections[0].ConnectionId), waitConnectionDeleted); err != nil { + if err := deleteConnection(ctx, conn, aws.ToString(output.Connections[0].ConnectionId), waitConnectionDeleted); err != nil { return sdkdiag.AppendFromErr(diags, err) } } @@ -136,7 +137,7 @@ func resourceLagCreate(ctx context.Context, d *schema.ResourceData, meta interfa func resourceLagRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) lag, err := FindLagByID(ctx, conn, d.Id()) @@ -152,9 +153,9 @@ func resourceLagRead(ctx context.Context, d *schema.ResourceData, meta interface arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, - Region: aws.StringValue(lag.Region), + Region: aws.ToString(lag.Region), Service: "directconnect", - AccountID: aws.StringValue(lag.OwnerAccount), + AccountID: aws.ToString(lag.OwnerAccount), Resource: fmt.Sprintf("dxlag/%s", d.Id()), }.String() d.Set("arn", arn) @@ -171,7 +172,7 @@ func resourceLagRead(ctx context.Context, d *schema.ResourceData, meta interface func resourceLagUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.HasChange("name") { input := &directconnect.UpdateLagInput{ @@ -179,8 +180,8 @@ func resourceLagUpdate(ctx context.Context, d *schema.ResourceData, meta interfa LagName: aws.String(d.Get("name").(string)), } - log.Printf("[DEBUG] Updating Direct Connect LAG: %s", input) - _, err := conn.UpdateLagWithContext(ctx, input) + log.Printf("[DEBUG] Updating Direct Connect LAG: %#v", input) + _, err := conn.UpdateLag(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Direct Connect LAG (%s): %s", d.Id(), err) @@ -192,7 +193,7 @@ func resourceLagUpdate(ctx context.Context, d *schema.ResourceData, meta interfa func resourceLagDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.Get("force_destroy").(bool) { lag, err := FindLagByID(ctx, conn, d.Id()) @@ -202,7 +203,7 @@ func resourceLagDelete(ctx context.Context, d *schema.ResourceData, meta interfa } for _, connection := range lag.Connections { - if err := deleteConnection(ctx, conn, aws.StringValue(connection.ConnectionId), waitConnectionDeleted); err != nil { + if err := deleteConnection(ctx, conn, aws.ToString(connection.ConnectionId), waitConnectionDeleted); err != nil { return sdkdiag.AppendFromErr(diags, err) } } @@ -213,11 +214,11 @@ func resourceLagDelete(ctx context.Context, d *schema.ResourceData, meta interfa } log.Printf("[DEBUG] Deleting Direct Connect LAG: %s", d.Id()) - _, err := conn.DeleteLagWithContext(ctx, &directconnect.DeleteLagInput{ + _, err := conn.DeleteLag(ctx, &directconnect.DeleteLagInput{ LagId: aws.String(d.Id()), }) - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "Could not find Lag with ID") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Lag with ID") { return diags } diff --git a/internal/service/directconnect/lag_test.go b/internal/service/directconnect/lag_test.go index 5bb6ab5cb45..c5ec16830d9 100644 --- a/internal/service/directconnect/lag_test.go +++ b/internal/service/directconnect/lag_test.go @@ -9,7 +9,7 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -22,7 +22,7 @@ import ( func TestAccDirectConnectLag_basic(t *testing.T) { ctx := acctest.Context(t) - var lag directconnect.Lag + var lag awstypes.Lag resourceName := "aws_dx_lag.test" rName1 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -79,7 +79,7 @@ func TestAccDirectConnectLag_basic(t *testing.T) { func TestAccDirectConnectLag_disappears(t *testing.T) { ctx := acctest.Context(t) - var lag directconnect.Lag + var lag awstypes.Lag resourceName := "aws_dx_lag.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -103,7 +103,7 @@ func TestAccDirectConnectLag_disappears(t *testing.T) { func TestAccDirectConnectLag_connectionID(t *testing.T) { ctx := acctest.Context(t) - var lag directconnect.Lag + var lag awstypes.Lag resourceName := "aws_dx_lag.test" connectionResourceName := "aws_dx_connection.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -143,7 +143,7 @@ func TestAccDirectConnectLag_connectionID(t *testing.T) { func TestAccDirectConnectLag_providerName(t *testing.T) { ctx := acctest.Context(t) - var lag directconnect.Lag + var lag awstypes.Lag resourceName := "aws_dx_lag.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -182,7 +182,7 @@ func TestAccDirectConnectLag_providerName(t *testing.T) { func TestAccDirectConnectLag_tags(t *testing.T) { ctx := acctest.Context(t) - var lag directconnect.Lag + var lag awstypes.Lag resourceName := "aws_dx_lag.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -232,7 +232,7 @@ func TestAccDirectConnectLag_tags(t *testing.T) { func testAccCheckLagDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_dx_lag" { @@ -256,9 +256,9 @@ func testAccCheckLagDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckLagExists(ctx context.Context, name string, v *directconnect.Lag) resource.TestCheckFunc { +func testAccCheckLagExists(ctx context.Context, name string, v *awstypes.Lag) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) rs, ok := s.RootModule().Resources[name] if !ok { diff --git a/internal/service/directconnect/list_pages_gen.go b/internal/service/directconnect/list_pages_gen.go index bfe7fbc49fd..a6ea2a72ee5 100644 --- a/internal/service/directconnect/list_pages_gen.go +++ b/internal/service/directconnect/list_pages_gen.go @@ -1,23 +1,22 @@ -// Code generated by "internal/generate/listpages/main.go -ListOps=DescribeDirectConnectGateways,DescribeDirectConnectGatewayAssociations,DescribeDirectConnectGatewayAssociationProposals"; DO NOT EDIT. +// Code generated by "internal/generate/listpages/main.go -AWSSDKVersion=2 -ListOps=DescribeDirectConnectGateways,DescribeDirectConnectGatewayAssociations,DescribeDirectConnectGatewayAssociationProposals"; DO NOT EDIT. package directconnect import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "github.com/aws/aws-sdk-go/service/directconnect/directconnectiface" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" ) -func describeGatewayAssociationProposalsPages(ctx context.Context, conn directconnectiface.DirectConnectAPI, input *directconnect.DescribeDirectConnectGatewayAssociationProposalsInput, fn func(*directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, bool) bool) error { +func describeDirectConnectGatewayAssociationProposalsPages(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationProposalsInput, fn func(*directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, bool) bool) error { for { - output, err := conn.DescribeDirectConnectGatewayAssociationProposalsWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGatewayAssociationProposals(ctx, input) if err != nil { return err } - lastPage := aws.StringValue(output.NextToken) == "" + lastPage := aws.ToString(output.NextToken) == "" if !fn(output, lastPage) || lastPage { break } @@ -26,14 +25,14 @@ func describeGatewayAssociationProposalsPages(ctx context.Context, conn directco } return nil } -func describeGatewayAssociationsPages(ctx context.Context, conn directconnectiface.DirectConnectAPI, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, fn func(*directconnect.DescribeDirectConnectGatewayAssociationsOutput, bool) bool) error { +func describeDirectConnectGatewayAssociationsPages(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, fn func(*directconnect.DescribeDirectConnectGatewayAssociationsOutput, bool) bool) error { for { - output, err := conn.DescribeDirectConnectGatewayAssociationsWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGatewayAssociations(ctx, input) if err != nil { return err } - lastPage := aws.StringValue(output.NextToken) == "" + lastPage := aws.ToString(output.NextToken) == "" if !fn(output, lastPage) || lastPage { break } @@ -42,14 +41,14 @@ func describeGatewayAssociationsPages(ctx context.Context, conn directconnectifa } return nil } -func describeGatewaysPages(ctx context.Context, conn directconnectiface.DirectConnectAPI, input *directconnect.DescribeDirectConnectGatewaysInput, fn func(*directconnect.DescribeDirectConnectGatewaysOutput, bool) bool) error { +func describeDirectConnectGatewaysPages(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewaysInput, fn func(*directconnect.DescribeDirectConnectGatewaysOutput, bool) bool) error { for { - output, err := conn.DescribeDirectConnectGatewaysWithContext(ctx, input) + output, err := conn.DescribeDirectConnectGateways(ctx, input) if err != nil { return err } - lastPage := aws.StringValue(output.NextToken) == "" + lastPage := aws.ToString(output.NextToken) == "" if !fn(output, lastPage) || lastPage { break } diff --git a/internal/service/directconnect/location_data_source.go b/internal/service/directconnect/location_data_source.go index d865335e036..e31099b3303 100644 --- a/internal/service/directconnect/location_data_source.go +++ b/internal/service/directconnect/location_data_source.go @@ -6,7 +6,6 @@ package directconnect import ( "context" - "github.com/aws/aws-sdk-go/aws" "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" @@ -53,7 +52,7 @@ func DataSourceLocation() *schema.Resource { func dataSourceLocationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) locationCode := d.Get("location_code").(string) location, err := FindLocationByCode(ctx, conn, locationCode) @@ -67,9 +66,9 @@ func dataSourceLocationRead(ctx context.Context, d *schema.ResourceData, meta in } d.SetId(locationCode) - d.Set("available_macsec_port_speeds", aws.StringValueSlice(location.AvailableMacSecPortSpeeds)) - d.Set("available_port_speeds", aws.StringValueSlice(location.AvailablePortSpeeds)) - d.Set("available_providers", aws.StringValueSlice(location.AvailableProviders)) + d.Set("available_macsec_port_speeds", location.AvailableMacSecPortSpeeds) + d.Set("available_port_speeds", location.AvailablePortSpeeds) + d.Set("available_providers", location.AvailableProviders) d.Set("location_code", location.LocationCode) d.Set("location_name", location.LocationName) diff --git a/internal/service/directconnect/locations_data_source.go b/internal/service/directconnect/locations_data_source.go index 78bfc6437c6..a3b7586f943 100644 --- a/internal/service/directconnect/locations_data_source.go +++ b/internal/service/directconnect/locations_data_source.go @@ -6,8 +6,7 @@ package directconnect import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/service/directconnect" "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" @@ -31,7 +30,7 @@ func DataSourceLocations() *schema.Resource { func dataSourceLocationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) locations, err := FindLocations(ctx, conn, &directconnect.DescribeLocationsInput{}) @@ -46,7 +45,7 @@ func dataSourceLocationsRead(ctx context.Context, d *schema.ResourceData, meta i } d.SetId(meta.(*conns.AWSClient).Region) - d.Set("location_codes", aws.StringValueSlice(locationCodes)) + d.Set("location_codes", locationCodes) return diags } diff --git a/internal/service/directconnect/macsec_key.go b/internal/service/directconnect/macsec_key.go index 84f1c39a70f..13e0204e143 100644 --- a/internal/service/directconnect/macsec_key.go +++ b/internal/service/directconnect/macsec_key.go @@ -10,8 +10,8 @@ import ( "strings" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -74,7 +74,7 @@ func ResourceMacSecKeyAssociation() *schema.Resource { func resourceMacSecKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) input := &directconnect.AssociateMacSecKeyInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), @@ -90,7 +90,7 @@ func resourceMacSecKeyCreate(ctx context.Context, d *schema.ResourceData, meta i } log.Printf("[DEBUG] Creating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) - output, err := conn.AssociateMacSecKeyWithContext(ctx, input) + output, err := conn.AssociateMacSecKey(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating MACSec secret key on Direct Connect Connection (%s): %s", *input.ConnectionId, err) @@ -99,7 +99,7 @@ func resourceMacSecKeyCreate(ctx context.Context, d *schema.ResourceData, meta i secret_arn := MacSecKeyParseSecretARN(output) // Create a composite ID based on connection ID and secret ARN - d.SetId(fmt.Sprintf("%s_%s", secret_arn, aws.StringValue(output.ConnectionId))) + d.SetId(fmt.Sprintf("%s_%s", secret_arn, aws.ToString(output.ConnectionId))) d.Set("secret_arn", secret_arn) @@ -108,7 +108,7 @@ func resourceMacSecKeyCreate(ctx context.Context, d *schema.ResourceData, meta i func resourceMacSecKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) secretArn, connId, err := MacSecKeyParseID(d.Id()) if err != nil { @@ -125,7 +125,7 @@ func resourceMacSecKeyRead(ctx context.Context, d *schema.ResourceData, meta int } for _, key := range connection.MacSecKeys { - if aws.StringValue(key.SecretARN) == aws.StringValue(&secretArn) { + if aws.ToString(key.SecretARN) == aws.ToString(&secretArn) { d.Set("ckn", key.Ckn) d.Set("connection_id", connId) d.Set("secret_arn", key.SecretARN) @@ -139,7 +139,7 @@ func resourceMacSecKeyRead(ctx context.Context, d *schema.ResourceData, meta int func resourceMacSecKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) input := &directconnect.DisassociateMacSecKeyInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), @@ -147,7 +147,7 @@ func resourceMacSecKeyDelete(ctx context.Context, d *schema.ResourceData, meta i } log.Printf("[DEBUG] Disassociating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) - _, err := conn.DisassociateMacSecKeyWithContext(ctx, input) + _, err := conn.DisassociateMacSecKey(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "Unable to disassociate MACSec secret key on Direct Connect Connection (%s): %s", *input.ConnectionId, err) @@ -161,9 +161,7 @@ func MacSecKeyParseSecretARN(output *directconnect.AssociateMacSecKeyOutput) str var result string for _, key := range output.MacSecKeys { - if key != nil { - result = aws.StringValue(key.SecretARN) - } + result = aws.ToString(key.SecretARN) } return result diff --git a/internal/service/directconnect/private_virtual_interface.go b/internal/service/directconnect/private_virtual_interface.go index 35389d199e5..9ab956f7db3 100644 --- a/internal/service/directconnect/private_virtual_interface.go +++ b/internal/service/directconnect/private_virtual_interface.go @@ -10,13 +10,15 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -37,13 +39,10 @@ func ResourcePrivateVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -138,7 +137,7 @@ func ResourcePrivateVirtualInterface() *schema.Resource { func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id") dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id") @@ -148,14 +147,14 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour req := &directconnect.CreatePrivateVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), - NewPrivateVirtualInterface: &directconnect.NewPrivateVirtualInterface{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), + NewPrivateVirtualInterface: &awstypes.NewPrivateVirtualInterface{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), - Mtu: aws.Int64(int64(d.Get("mtu").(int))), + Mtu: aws.Int32(int32(d.Get("mtu").(int))), Tags: getTagsIn(ctx), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, } if vgwOk && vgwIdRaw.(string) != "" { @@ -174,13 +173,13 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour req.NewPrivateVirtualInterface.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect private virtual interface: %s", req) - resp, err := conn.CreatePrivateVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Creating Direct Connect private virtual interface: %#v", req) + resp, err := conn.CreatePrivateVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect private virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterfaceId)) if err := privateVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -191,7 +190,7 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour func resourcePrivateVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -205,7 +204,7 @@ func resourcePrivateVirtualInterfaceRead(ctx context.Context, d *schema.Resource d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -238,7 +237,7 @@ func resourcePrivateVirtualInterfaceUpdate(ctx context.Context, d *schema.Resour return diags } - if err := privateVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectConn(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { + if err := privateVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -250,7 +249,7 @@ func resourcePrivateVirtualInterfaceDelete(ctx context.Context, d *schema.Resour } func resourcePrivateVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -260,22 +259,17 @@ func resourcePrivateVirtualInterfaceImport(ctx context.Context, d *schema.Resour return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "private" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } return []*schema.ResourceData{d}, nil } -func privateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func privateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/private_virtual_interface_test.go b/internal/service/directconnect/private_virtual_interface_test.go index 221abce2cba..5a308cf7f1b 100644 --- a/internal/service/directconnect/private_virtual_interface_test.go +++ b/internal/service/directconnect/private_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -28,7 +28,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" vpnGatewayResourceName := "aws_vpn_gateway.test" rName := fmt.Sprintf("tf-testacc-private-vif-%s", sdkacctest.RandString(9)) @@ -48,7 +48,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -69,7 +69,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -101,7 +101,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" vpnGatewayResourceName := "aws_vpn_gateway.test" rName := fmt.Sprintf("tf-testacc-private-vif-%s", sdkacctest.RandString(9)) @@ -121,7 +121,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -145,7 +145,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -180,7 +180,7 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" dxGatewayResourceName := "aws_dx_gateway.test" rName := fmt.Sprintf("tf-testacc-private-vif-%s", sdkacctest.RandString(9)) @@ -201,7 +201,7 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -233,7 +233,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" dxGatewayResourceName := "aws_dx_gateway.test" rName := fmt.Sprintf("tf-testacc-private-vif-%s", sdkacctest.RandString(9)) @@ -254,7 +254,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -276,7 +276,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -307,7 +307,7 @@ func testAccCheckPrivateVirtualInterfaceDestroy(ctx context.Context) resource.Te } } -func testAccCheckPrivateVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckPrivateVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/public_virtual_interface.go b/internal/service/directconnect/public_virtual_interface.go index d1580d7de5f..5397ea57c89 100644 --- a/internal/service/directconnect/public_virtual_interface.go +++ b/internal/service/directconnect/public_virtual_interface.go @@ -10,14 +10,16 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "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/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -42,13 +44,10 @@ func ResourcePublicVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -121,16 +120,16 @@ func ResourcePublicVirtualInterface() *schema.Resource { func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.CreatePublicVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), - NewPublicVirtualInterface: &directconnect.NewPublicVirtualInterface{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), + NewPublicVirtualInterface: &awstypes.NewPublicVirtualInterface{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), Tags: getTagsIn(ctx), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, } if v, ok := d.GetOk("amazon_address"); ok { @@ -146,13 +145,13 @@ func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.Resourc req.NewPublicVirtualInterface.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect public virtual interface: %s", req) - resp, err := conn.CreatePublicVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Creating Direct Connect public virtual interface: %#v", req) + resp, err := conn.CreatePublicVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect public virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterfaceId)) if err := publicVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -163,7 +162,7 @@ func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.Resourc func resourcePublicVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -177,7 +176,7 @@ func resourcePublicVirtualInterfaceRead(ctx context.Context, d *schema.ResourceD d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -216,7 +215,7 @@ func resourcePublicVirtualInterfaceDelete(ctx context.Context, d *schema.Resourc } func resourcePublicVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -226,7 +225,7 @@ func resourcePublicVirtualInterfaceImport(ctx context.Context, d *schema.Resourc return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "public" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } @@ -236,7 +235,7 @@ func resourcePublicVirtualInterfaceImport(ctx context.Context, d *schema.Resourc func resourcePublicVirtualInterfaceCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { if diff.Id() == "" { // New resource. - if addressFamily := diff.Get("address_family").(string); addressFamily == directconnect.AddressFamilyIpv4 { + if addressFamily := diff.Get("address_family").(string); addressFamily == string(awstypes.AddressFamilyIPv4) { if _, ok := diff.GetOk("customer_address"); !ok { return fmt.Errorf("'customer_address' must be set when 'address_family' is '%s'", addressFamily) } @@ -249,16 +248,10 @@ func resourcePublicVirtualInterfaceCustomizeDiff(_ context.Context, diff *schema return nil } -func publicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func publicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - directconnect.VirtualInterfaceStateVerifying, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) } diff --git a/internal/service/directconnect/public_virtual_interface_test.go b/internal/service/directconnect/public_virtual_interface_test.go index a70e3612c97..63c5feb95b8 100644 --- a/internal/service/directconnect/public_virtual_interface_test.go +++ b/internal/service/directconnect/public_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -28,7 +28,7 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_public_virtual_interface.test" rName := fmt.Sprintf("tf-testacc-public-vif-%s", sdkacctest.RandString(10)) // DirectConnectClientException: Amazon Address is not allowed to contain a private IP @@ -53,7 +53,7 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -85,7 +85,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_public_virtual_interface.test" rName := fmt.Sprintf("tf-testacc-public-vif-%s", sdkacctest.RandString(10)) amazonAddress := "175.45.176.3/28" @@ -106,7 +106,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -130,7 +130,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -163,7 +163,7 @@ func testAccCheckPublicVirtualInterfaceDestroy(ctx context.Context) resource.Tes } } -func testAccCheckPublicVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckPublicVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/router_configuration_data_source.go b/internal/service/directconnect/router_configuration_data_source.go index 4a776ae330d..3af336103e4 100644 --- a/internal/service/directconnect/router_configuration_data_source.go +++ b/internal/service/directconnect/router_configuration_data_source.go @@ -7,8 +7,9 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -83,7 +84,7 @@ const ( func dataSourceRouterConfigurationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) routerTypeIdentifier := d.Get("router_type_identifier").(string) virtualInterfaceId := d.Get("virtual_interface_id").(string) @@ -107,13 +108,13 @@ func dataSourceRouterConfigurationRead(ctx context.Context, d *schema.ResourceDa return diags } -func findRouterConfigurationByTypeAndVif(ctx context.Context, conn *directconnect.DirectConnect, routerTypeIdentifier string, virtualInterfaceId string) (*directconnect.DescribeRouterConfigurationOutput, error) { +func findRouterConfigurationByTypeAndVif(ctx context.Context, conn *directconnect.Client, routerTypeIdentifier string, virtualInterfaceId string) (*directconnect.DescribeRouterConfigurationOutput, error) { input := &directconnect.DescribeRouterConfigurationInput{ RouterTypeIdentifier: aws.String(routerTypeIdentifier), VirtualInterfaceId: aws.String(virtualInterfaceId), } - output, err := conn.DescribeRouterConfigurationWithContext(ctx, input) + output, err := conn.DescribeRouterConfiguration(ctx, input) if err != nil { return nil, err @@ -126,31 +127,31 @@ func findRouterConfigurationByTypeAndVif(ctx context.Context, conn *directconnec return output, nil } -func flattenRouter(apiObject *directconnect.RouterType) []interface{} { +func flattenRouter(apiObject *awstypes.RouterType) []interface{} { tfMap := map[string]interface{}{} if v := apiObject.Platform; v != nil { - tfMap["platform"] = aws.StringValue(v) + tfMap["platform"] = aws.ToString(v) } if v := apiObject.RouterTypeIdentifier; v != nil { - tfMap["router_type_identifier"] = aws.StringValue(v) + tfMap["router_type_identifier"] = aws.ToString(v) } if v := apiObject.Software; v != nil { - tfMap["software"] = aws.StringValue(v) + tfMap["software"] = aws.ToString(v) } if v := apiObject.Vendor; v != nil { - tfMap["vendor"] = aws.StringValue(v) + tfMap["vendor"] = aws.ToString(v) } if v := apiObject.XsltTemplateName; v != nil { - tfMap["xslt_template_name"] = aws.StringValue(v) + tfMap["xslt_template_name"] = aws.ToString(v) } if v := apiObject.XsltTemplateNameForMacSec; v != nil { - tfMap["xslt_template_name_for_mac_sec"] = aws.StringValue(v) + tfMap["xslt_template_name_for_mac_sec"] = aws.ToString(v) } return []interface{}{tfMap} diff --git a/internal/service/directconnect/router_configuration_data_source_test.go b/internal/service/directconnect/router_configuration_data_source_test.go index e7294b5b549..09c3ee2c69d 100644 --- a/internal/service/directconnect/router_configuration_data_source_test.go +++ b/internal/service/directconnect/router_configuration_data_source_test.go @@ -8,7 +8,6 @@ import ( "os" "testing" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" "github.com/hashicorp/terraform-provider-aws/names" @@ -28,7 +27,7 @@ func TestAccDirectConnectRouterConfigurationDataSource_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) - acctest.PreCheckPartitionHasService(t, directconnect.EndpointsID) + acctest.PreCheckPartitionHasService(t, names.DirectConnectEndpointID) }, ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, diff --git a/internal/service/directconnect/service_endpoints_gen_test.go b/internal/service/directconnect/service_endpoints_gen_test.go index 21350619e5f..384b17455d0 100644 --- a/internal/service/directconnect/service_endpoints_gen_test.go +++ b/internal/service/directconnect/service_endpoints_gen_test.go @@ -4,17 +4,17 @@ package directconnect_test import ( "context" + "errors" "fmt" "maps" - "net/url" "os" "path/filepath" "reflect" "strings" "testing" - "github.com/aws/aws-sdk-go/aws/endpoints" - directconnect_sdkv1 "github.com/aws/aws-sdk-go/service/directconnect" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + directconnect_sdkv2 "github.com/aws/aws-sdk-go-v2/service/directconnect" "github.com/aws/smithy-go/middleware" smithyhttp "github.com/aws/smithy-go/transport/http" "github.com/google/go-cmp/cmp" @@ -212,32 +212,42 @@ func TestEndpointConfiguration(t *testing.T) { //nolint:paralleltest // uses t.S } func defaultEndpoint(region string) string { - r := endpoints.DefaultResolver() + r := directconnect_sdkv2.NewDefaultEndpointResolverV2() - ep, err := r.EndpointFor(directconnect_sdkv1.EndpointsID, region) + ep, err := r.ResolveEndpoint(context.Background(), directconnect_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.DirectConnectConn(ctx) - - req, _ := client.DescribeConnectionsRequest(&directconnect_sdkv1.DescribeConnectionsInput{}) + var endpoint string - req.HTTPRequest.URL.Path = "/" + client := meta.DirectConnectClient(ctx) - endpoint := req.HTTPRequest.URL.String() + _, err := client.DescribeConnections(ctx, &directconnect_sdkv2.DescribeConnectionsInput{}, + func(opts *directconnect_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/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index df3792aa21c..dc01200bc1d 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -5,9 +5,8 @@ package directconnect import ( "context" - aws_sdkv1 "github.com/aws/aws-sdk-go/aws" - session_sdkv1 "github.com/aws/aws-sdk-go/aws/session" - directconnect_sdkv1 "github.com/aws/aws-sdk-go/service/directconnect" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + directconnect_sdkv2 "github.com/aws/aws-sdk-go-v2/service/directconnect" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/types" "github.com/hashicorp/terraform-provider-aws/names" @@ -165,11 +164,15 @@ func (p *servicePackage) ServicePackageName() string { return names.DirectConnect } -// NewConn returns a new AWS SDK for Go v1 client for this service package's AWS API. -func (p *servicePackage) NewConn(ctx context.Context, config map[string]any) (*directconnect_sdkv1.DirectConnect, error) { - sess := config["session"].(*session_sdkv1.Session) +// NewClient 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) (*directconnect_sdkv2.Client, error) { + cfg := *(config["aws_sdkv2_config"].(*aws_sdkv2.Config)) - return directconnect_sdkv1.New(sess.Copy(&aws_sdkv1.Config{Endpoint: aws_sdkv1.String(config["endpoint"].(string))})), nil + return directconnect_sdkv2.NewFromConfig(cfg, func(o *directconnect_sdkv2.Options) { + if endpoint := config["endpoint"].(string); endpoint != "" { + o.BaseEndpoint = aws_sdkv2.String(endpoint) + } + }), nil } func ServicePackage(ctx context.Context) conns.ServicePackage { diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go index 45c3e9b06f4..12a5cae7045 100644 --- a/internal/service/directconnect/status.go +++ b/internal/service/directconnect/status.go @@ -6,13 +6,12 @@ package directconnect import ( "context" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func statusConnectionState(ctx context.Context, conn *directconnect.DirectConnect, id string) retry.StateRefreshFunc { +func statusConnectionState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindConnectionByID(ctx, conn, id) @@ -24,11 +23,11 @@ func statusConnectionState(ctx context.Context, conn *directconnect.DirectConnec return nil, "", err } - return output, aws.StringValue(output.ConnectionState), nil + return output, string(output.ConnectionState), nil } } -func statusGatewayState(ctx context.Context, conn *directconnect.DirectConnect, id string) retry.StateRefreshFunc { +func statusGatewayState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindGatewayByID(ctx, conn, id) @@ -40,11 +39,11 @@ func statusGatewayState(ctx context.Context, conn *directconnect.DirectConnect, return nil, "", err } - return output, aws.StringValue(output.DirectConnectGatewayState), nil + return output, string(output.DirectConnectGatewayState), nil } } -func statusGatewayAssociationState(ctx context.Context, conn *directconnect.DirectConnect, id string) retry.StateRefreshFunc { +func statusGatewayAssociationState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindGatewayAssociationByID(ctx, conn, id) @@ -56,11 +55,11 @@ func statusGatewayAssociationState(ctx context.Context, conn *directconnect.Dire return nil, "", err } - return output, aws.StringValue(output.AssociationState), nil + return output, string(output.AssociationState), nil } } -func statusHostedConnectionState(ctx context.Context, conn *directconnect.DirectConnect, id string) retry.StateRefreshFunc { +func statusHostedConnectionState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindHostedConnectionByID(ctx, conn, id) @@ -72,11 +71,11 @@ func statusHostedConnectionState(ctx context.Context, conn *directconnect.Direct return nil, "", err } - return output, aws.StringValue(output.ConnectionState), nil + return output, string(output.ConnectionState), nil } } -func statusLagState(ctx context.Context, conn *directconnect.DirectConnect, id string) retry.StateRefreshFunc { +func statusLagState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindLagByID(ctx, conn, id) @@ -88,6 +87,6 @@ func statusLagState(ctx context.Context, conn *directconnect.DirectConnect, id s return nil, "", err } - return output, aws.StringValue(output.LagState), nil + return output, string(output.LagState), nil } } diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index d9c884a4384..d57b458e8c7 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -7,14 +7,16 @@ import ( "fmt" "log" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go-v2/service/secretsmanager" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/go-multierror" "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() { @@ -64,7 +66,7 @@ func sweepConnections(region string) error { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectConn(ctx) + conn := client.DirectConnectClient(ctx) sweepResources := make([]sweep.Sweepable, 0) var sweeperErrs *multierror.Error @@ -72,7 +74,7 @@ func sweepConnections(region string) error { input := &directconnect.DescribeConnectionsInput{} // DescribeConnections has no pagination support - output, err := conn.DescribeConnectionsWithContext(ctx, input) + output, err := conn.DescribeConnections(ctx, input) if awsv1.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect Connection sweep for %s: %s", region, err) @@ -92,11 +94,7 @@ func sweepConnections(region string) error { } for _, connection := range output.Connections { - if connection == nil { - continue - } - - id := aws.StringValue(connection.ConnectionId) + id := aws.ToString(connection.ConnectionId) r := ResourceConnection() d := r.Data(nil) @@ -118,25 +116,25 @@ func sweepGatewayAssociationProposals(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectConn(ctx) + conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{} sweepResources := make([]sweep.Sweepable, 0) - err = describeGatewayAssociationProposalsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, lastPage bool) bool { + err = describeDirectConnectGatewayAssociationProposalsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, lastPage bool) bool { if page == nil { return !lastPage } for _, proposal := range page.DirectConnectGatewayAssociationProposals { - proposalID := aws.StringValue(proposal.ProposalId) + proposalID := aws.ToString(proposal.ProposalId) - if proposalRegion := aws.StringValue(proposal.AssociatedGateway.Region); proposalRegion != region { + if proposalRegion := aws.ToString(proposal.AssociatedGateway.Region); proposalRegion != region { log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal (%s) in different home region: %s", proposalID, proposalRegion) continue } - if state := aws.StringValue(proposal.ProposalState); state != directconnect.GatewayAssociationProposalStateAccepted { - log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal (%s) in non-accepted (%s) state", proposalID, state) + if proposal.ProposalState != awstypes.DirectConnectGatewayAssociationProposalStateAccepted { + log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal (%s) in non-accepted (%s) state", proposalID, string(proposal.ProposalState)) continue } @@ -150,7 +148,7 @@ func sweepGatewayAssociationProposals(region string) error { return !lastPage }) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway Association Proposal sweep for %s: %w", region, err)) return nil } @@ -174,38 +172,38 @@ func sweepGatewayAssociations(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectConn(ctx) + conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewaysInput{} var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = describeGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { + err = describeDirectConnectGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { if page == nil { return !lastPage } for _, gateway := range page.DirectConnectGateways { - directConnectGatewayID := aws.StringValue(gateway.DirectConnectGatewayId) + directConnectGatewayID := aws.ToString(gateway.DirectConnectGatewayId) input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ DirectConnectGatewayId: aws.String(directConnectGatewayID), } - err := describeGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { + err := describeDirectConnectGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { if page == nil { return !lastPage } for _, association := range page.DirectConnectGatewayAssociations { - gatewayID := aws.StringValue(association.AssociatedGateway.Id) + gatewayID := aws.ToString(association.AssociatedGateway.Id) - if aws.StringValue(association.AssociatedGateway.Region) != region { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s) in different home region: %s", directConnectGatewayID, gatewayID, aws.StringValue(association.AssociatedGateway.Region)) + if aws.ToString(association.AssociatedGateway.Region) != region { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s) in different home region: %s", directConnectGatewayID, gatewayID, aws.ToString(association.AssociatedGateway.Region)) continue } - if state := aws.StringValue(association.AssociationState); state != directconnect.GatewayAssociationStateAssociated { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, state, gatewayID) + if association.AssociationState != awstypes.DirectConnectGatewayAssociationStateAssociated { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, string(association.AssociationState), gatewayID) continue } @@ -228,7 +226,7 @@ func sweepGatewayAssociations(region string) error { return !lastPage }) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway Association sweep for %s: %w", region, err)) return sweeperErrs // In case we have completed some pages, but had errors } @@ -250,26 +248,26 @@ func sweepGatewayAssociations(region string) error { } for _, transitGateway := range page.TransitGateways { - if aws.StringValue(transitGateway.State) == ec2.TransitGatewayStateDeleted { + if aws.ToString(transitGateway.State) == ec2.TransitGatewayStateDeleted { continue } - transitGatewayID := aws.StringValue(transitGateway.TransitGatewayId) + transitGatewayID := aws.ToString(transitGateway.TransitGatewayId) input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ AssociatedGatewayId: aws.String(transitGatewayID), } - err := describeGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { + err := describeDirectConnectGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { if page == nil { return !lastPage } for _, association := range page.DirectConnectGatewayAssociations { - directConnectGatewayID := aws.StringValue(association.DirectConnectGatewayId) + directConnectGatewayID := aws.ToString(association.DirectConnectGatewayId) - if state := aws.StringValue(association.AssociationState); state != directconnect.GatewayAssociationStateAssociated { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, state, transitGatewayID) + if association.AssociationState != awstypes.DirectConnectGatewayAssociationStateAssociated { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, string(association.AssociationState), transitGatewayID) continue } @@ -311,21 +309,21 @@ func sweepGateways(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectConn(ctx) + conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewaysInput{} var sweeperErrs *multierror.Error sweepResources := make([]sweep.Sweepable, 0) - err = describeGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { + err = describeDirectConnectGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { if page == nil { return !lastPage } for _, gateway := range page.DirectConnectGateways { - directConnectGatewayID := aws.StringValue(gateway.DirectConnectGatewayId) + directConnectGatewayID := aws.ToString(gateway.DirectConnectGatewayId) - if state := aws.StringValue(gateway.DirectConnectGatewayState); state != directconnect.GatewayStateAvailable { - log.Printf("[INFO] Skipping Direct Connect Gateway in non-available (%s) state: %s", state, directConnectGatewayID) + if gateway.DirectConnectGatewayState != awstypes.DirectConnectGatewayStateAvailable { + log.Printf("[INFO] Skipping Direct Connect Gateway in non-available (%s) state: %s", string(gateway.DirectConnectGatewayState), directConnectGatewayID) continue } @@ -335,7 +333,7 @@ func sweepGateways(region string) error { var associations bool - err := describeGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { + err := describeDirectConnectGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { if page == nil { return !lastPage } @@ -372,7 +370,7 @@ func sweepGateways(region string) error { return !lastPage }) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway sweep for %s: %w", region, err)) return sweeperErrs // In case we have completed some pages, but had errors } @@ -397,7 +395,7 @@ func sweepLags(region string) error { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectConn(ctx) + conn := client.DirectConnectClient(ctx) sweepResources := make([]sweep.Sweepable, 0) var sweeperErrs *multierror.Error @@ -405,9 +403,9 @@ func sweepLags(region string) error { input := &directconnect.DescribeLagsInput{} // DescribeLags has no pagination support - output, err := conn.DescribeLagsWithContext(ctx, input) + output, err := conn.DescribeLags(ctx, input) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect LAG sweep for %s: %s", region, err) return sweeperErrs.ErrorOrNil() } @@ -425,11 +423,7 @@ func sweepLags(region string) error { } for _, lag := range output.Lags { - if lag == nil { - continue - } - - id := aws.StringValue(lag.LagId) + id := aws.ToString(lag.LagId) r := ResourceLag() d := r.Data(nil) @@ -452,7 +446,7 @@ func sweepMacSecKeys(region string) error { return fmt.Errorf("error getting client: %w", err) } - dxConn := client.DirectConnectConn(ctx) + dxConn := client.DirectConnectClient(ctx) // Clean up leaked Secrets Manager resources created by Direct Connect. // Direct Connect does not remove the corresponding Secrets Manager @@ -463,9 +457,9 @@ func sweepMacSecKeys(region string) error { dxInput := &directconnect.DescribeConnectionsInput{} var sweeperErrs *multierror.Error - output, err := dxConn.DescribeConnectionsWithContext(ctx, dxInput) + output, err := dxConn.DescribeConnections(ctx, dxInput) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect MACsec Keys sweep for %s: %s", region, err) return sweeperErrs.ErrorOrNil() } @@ -488,7 +482,7 @@ func sweepMacSecKeys(region string) error { } for _, key := range connection.MacSecKeys { - arn := aws.StringValue(key.SecretARN) + arn := aws.ToString(key.SecretARN) input := &secretsmanager.DeleteSecretInput{ SecretId: aws.String(arn), diff --git a/internal/service/directconnect/tags_gen.go b/internal/service/directconnect/tags_gen.go index 36eff3c345e..766bbb48f36 100644 --- a/internal/service/directconnect/tags_gen.go +++ b/internal/service/directconnect/tags_gen.go @@ -5,9 +5,9 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "github.com/aws/aws-sdk-go/service/directconnect/directconnectiface" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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 directconnect 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 directconnectiface.DirectConnectAPI, identifier string) (tftags.KeyValueTags, error) { +func listTags(ctx context.Context, conn *directconnect.Client, identifier string, optFns ...func(*directconnect.Options)) (tftags.KeyValueTags, error) { input := &directconnect.DescribeTagsInput{ - ResourceArns: aws.StringSlice([]string{identifier}), + ResourceArns: []string{identifier}, } - output, err := conn.DescribeTagsWithContext(ctx, input) + output, err := conn.DescribeTags(ctx, input, optFns...) if err != nil { return tftags.New(ctx, nil), err @@ -36,7 +36,7 @@ func listTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, ide // ListTags lists directconnect 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).DirectConnectConn(ctx), identifier) + tags, err := listTags(ctx, meta.(*conns.AWSClient).DirectConnectClient(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 directconnect service tags. -func Tags(tags tftags.KeyValueTags) []*directconnect.Tag { - result := make([]*directconnect.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 := &directconnect.Tag{ + tag := awstypes.Tag{ Key: aws.String(k), Value: aws.String(v), } @@ -68,11 +68,11 @@ func Tags(tags tftags.KeyValueTags) []*directconnect.Tag { } // KeyValueTags creates tftags.KeyValueTags from directconnect service tags. -func KeyValueTags(ctx context.Context, tags []*directconnect.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 []*directconnect.Tag) tftags.KeyValu // getTagsIn returns directconnect service tags from Context. // nil is returned if there are no input tags. -func getTagsIn(ctx context.Context) []*directconnect.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,14 +91,14 @@ func getTagsIn(ctx context.Context) []*directconnect.Tag { } // setTagsOut sets directconnect service tags in Context. -func setTagsOut(ctx context.Context, tags []*directconnect.Tag) { +func setTagsOut(ctx context.Context, tags []awstypes.Tag) { if inContext, ok := tftags.FromContext(ctx); ok { inContext.TagsOut = option.Some(KeyValueTags(ctx, tags)) } } // createTags creates directconnect service tags for new resources. -func createTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, identifier string, tags []*directconnect.Tag) error { +func createTags(ctx context.Context, conn *directconnect.Client, identifier string, tags []awstypes.Tag) error { if len(tags) == 0 { return nil } @@ -109,7 +109,7 @@ func createTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, i // updateTags updates directconnect 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 directconnectiface.DirectConnectAPI, identifier string, oldTagsMap, newTagsMap any) error { +func updateTags(ctx context.Context, conn *directconnect.Client, identifier string, oldTagsMap, newTagsMap any, optFns ...func(*directconnect.Options)) error { oldTags := tftags.New(ctx, oldTagsMap) newTags := tftags.New(ctx, newTagsMap) @@ -120,10 +120,10 @@ func updateTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, i if len(removedTags) > 0 { input := &directconnect.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) @@ -138,7 +138,7 @@ func updateTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, i 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) @@ -151,5 +151,5 @@ func updateTags(ctx context.Context, conn directconnectiface.DirectConnectAPI, i // UpdateTags updates directconnect 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).DirectConnectConn(ctx), identifier, oldTags, newTags) + return updateTags(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), identifier, oldTags, newTags) } diff --git a/internal/service/directconnect/transit_virtual_interface.go b/internal/service/directconnect/transit_virtual_interface.go index 314bcbe26ed..58d7b383935 100644 --- a/internal/service/directconnect/transit_virtual_interface.go +++ b/internal/service/directconnect/transit_virtual_interface.go @@ -10,13 +10,15 @@ import ( "strconv" "time" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/directconnect" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -37,13 +39,10 @@ func ResourceTransitVirtualInterface() *schema.Resource { Schema: map[string]*schema.Schema{ "address_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - directconnect.AddressFamilyIpv4, - directconnect.AddressFamilyIpv6, - }, false), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, "amazon_address": { Type: schema.TypeString, @@ -131,19 +130,19 @@ func ResourceTransitVirtualInterface() *schema.Resource { func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.CreateTransitVirtualInterfaceInput{ ConnectionId: aws.String(d.Get("connection_id").(string)), - NewTransitVirtualInterface: &directconnect.NewTransitVirtualInterface{ - AddressFamily: aws.String(d.Get("address_family").(string)), - Asn: aws.Int64(int64(d.Get("bgp_asn").(int))), + NewTransitVirtualInterface: &awstypes.NewTransitVirtualInterface{ + AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), + Asn: int32(d.Get("bgp_asn").(int)), DirectConnectGatewayId: aws.String(d.Get("dx_gateway_id").(string)), EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), - Mtu: aws.Int64(int64(d.Get("mtu").(int))), + Mtu: aws.Int32(int32(d.Get("mtu").(int))), Tags: getTagsIn(ctx), VirtualInterfaceName: aws.String(d.Get("name").(string)), - Vlan: aws.Int64(int64(d.Get("vlan").(int))), + Vlan: int32(d.Get("vlan").(int)), }, } if v, ok := d.GetOk("amazon_address"); ok { @@ -156,13 +155,13 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour req.NewTransitVirtualInterface.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect transit virtual interface: %s", req) - resp, err := conn.CreateTransitVirtualInterfaceWithContext(ctx, req) + log.Printf("[DEBUG] Creating Direct Connect transit virtual interface: %#v", req) + resp, err := conn.CreateTransitVirtualInterface(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Direct Connect transit virtual interface: %s", err) } - d.SetId(aws.StringValue(resp.VirtualInterface.VirtualInterfaceId)) + d.SetId(aws.ToString(resp.VirtualInterface.VirtualInterfaceId)) if err := transitVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) @@ -173,7 +172,7 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour func resourceTransitVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -187,7 +186,7 @@ func resourceTransitVirtualInterfaceRead(ctx context.Context, d *schema.Resource d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -219,7 +218,7 @@ func resourceTransitVirtualInterfaceUpdate(ctx context.Context, d *schema.Resour return diags } - if err := transitVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectConn(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { + if err := transitVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -231,7 +230,7 @@ func resourceTransitVirtualInterfaceDelete(ctx context.Context, d *schema.Resour } func resourceTransitVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) vif, err := virtualInterfaceRead(ctx, d.Id(), conn) if err != nil { @@ -241,22 +240,17 @@ func resourceTransitVirtualInterfaceImport(ctx context.Context, d *schema.Resour return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) } - if vifType := aws.StringValue(vif.VirtualInterfaceType); vifType != "transit" { + if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) } return []*schema.ResourceData{d}, nil } -func transitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error { +func transitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { return virtualInterfaceWaitUntilAvailable(ctx, conn, vifId, timeout, - []string{ - directconnect.VirtualInterfaceStatePending, - }, - []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateDown, - }) + enum.Slice(awstypes.VirtualInterfaceStatePending), + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) } diff --git a/internal/service/directconnect/transit_virtual_interface_test.go b/internal/service/directconnect/transit_virtual_interface_test.go index cb66a9a5b53..fc8535a6249 100644 --- a/internal/service/directconnect/transit_virtual_interface_test.go +++ b/internal/service/directconnect/transit_virtual_interface_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" @@ -40,7 +40,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" dxGatewayResourceName := "aws_dx_gateway.test" rName := fmt.Sprintf("tf-testacc-transit-vif-%s", sdkacctest.RandString(9)) @@ -61,7 +61,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -82,7 +82,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -114,7 +114,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" dxGatewayResourceName := "aws_dx_gateway.test" rName := fmt.Sprintf("tf-testacc-transit-vif-%s", sdkacctest.RandString(9)) @@ -135,7 +135,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -159,7 +159,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -194,7 +194,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { t.Skipf("Environment variable %s is not set", key) } - var vif directconnect.VirtualInterface + var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" dxGatewayResourceName := "aws_dx_gateway.test" rName := fmt.Sprintf("tf-testacc-transit-vif-%s", sdkacctest.RandString(9)) @@ -215,7 +215,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -237,7 +237,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.StringValue(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), @@ -262,7 +262,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { }) } -func testAccCheckTransitVirtualInterfaceExists(ctx context.Context, name string, vif *directconnect.VirtualInterface) resource.TestCheckFunc { +func testAccCheckTransitVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { return testAccCheckVirtualInterfaceExists(ctx, name, vif) } diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index 6c1648a64d1..afde99b2d09 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -9,39 +9,41 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" - "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/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" ) -func virtualInterfaceRead(ctx context.Context, id string, conn *directconnect.DirectConnect) (*directconnect.VirtualInterface, error) { +func virtualInterfaceRead(ctx context.Context, id string, conn *directconnect.Client) (*awstypes.VirtualInterface, error) { resp, state, err := virtualInterfaceStateRefresh(ctx, conn, id)() if err != nil { return nil, fmt.Errorf("reading Direct Connect virtual interface (%s): %s", id, err) } - if state == directconnect.VirtualInterfaceStateDeleted { + if state == string(awstypes.VirtualInterfaceStateDeleted) { return nil, nil } - return resp.(*directconnect.VirtualInterface), nil + return resp.(*awstypes.VirtualInterface), nil } func virtualInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.HasChange("mtu") { req := &directconnect.UpdateVirtualInterfaceAttributesInput{ - Mtu: aws.Int64(int64(d.Get("mtu").(int))), + Mtu: aws.Int32(int32(d.Get("mtu").(int))), VirtualInterfaceId: aws.String(d.Id()), } - log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %s", req) - _, err := conn.UpdateVirtualInterfaceAttributesWithContext(ctx, req) + log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %#v", req) + _, err := conn.UpdateVirtualInterfaceAttributes(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "modifying Direct Connect virtual interface (%s) attributes: %s", d.Id(), err) } @@ -51,8 +53,8 @@ func virtualInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta in EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), VirtualInterfaceId: aws.String(d.Id()), } - log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %s", req) - _, err := conn.UpdateVirtualInterfaceAttributesWithContext(ctx, req) + log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %#v", req) + _, err := conn.UpdateVirtualInterfaceAttributes(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "modifying Direct Connect virtual interface (%s) attributes: %s", d.Id(), err) } @@ -63,32 +65,29 @@ func virtualInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta in func virtualInterfaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectConn(ctx) + conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Printf("[DEBUG] Deleting Direct Connect virtual interface: %s", d.Id()) - _, err := conn.DeleteVirtualInterfaceWithContext(ctx, &directconnect.DeleteVirtualInterfaceInput{ + _, err := conn.DeleteVirtualInterface(ctx, &directconnect.DeleteVirtualInterfaceInput{ VirtualInterfaceId: aws.String(d.Id()), }) if err != nil { - if tfawserr.ErrMessageContains(err, directconnect.ErrCodeClientException, "does not exist") { + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { return diags } return sdkdiag.AppendErrorf(diags, "deleting Direct Connect virtual interface (%s): %s", d.Id(), err) } deleteStateConf := &retry.StateChangeConf{ - Pending: []string{ - directconnect.VirtualInterfaceStateAvailable, - directconnect.VirtualInterfaceStateConfirming, - directconnect.VirtualInterfaceStateDeleting, - directconnect.VirtualInterfaceStateDown, - directconnect.VirtualInterfaceStatePending, - directconnect.VirtualInterfaceStateRejected, - directconnect.VirtualInterfaceStateVerifying, - }, - Target: []string{ - directconnect.VirtualInterfaceStateDeleted, - }, + Pending: enum.Slice( + awstypes.VirtualInterfaceStateAvailable, + awstypes.VirtualInterfaceStateConfirming, + awstypes.VirtualInterfaceStateDeleting, + awstypes.VirtualInterfaceStateDown, + awstypes.VirtualInterfaceStatePending, + awstypes.VirtualInterfaceStateRejected, + awstypes.VirtualInterfaceStateVerifying), + Target: enum.Slice(awstypes.VirtualInterfaceStateDeleted), Refresh: virtualInterfaceStateRefresh(ctx, conn, d.Id()), Timeout: d.Timeout(schema.TimeoutDelete), Delay: 10 * time.Second, @@ -102,9 +101,9 @@ func virtualInterfaceDelete(ctx context.Context, d *schema.ResourceData, meta in return diags } -func virtualInterfaceStateRefresh(ctx context.Context, conn *directconnect.DirectConnect, vifId string) retry.StateRefreshFunc { +func virtualInterfaceStateRefresh(ctx context.Context, conn *directconnect.Client, vifId string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - resp, err := conn.DescribeVirtualInterfacesWithContext(ctx, &directconnect.DescribeVirtualInterfacesInput{ + resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ VirtualInterfaceId: aws.String(vifId), }) if err != nil { @@ -114,11 +113,11 @@ func virtualInterfaceStateRefresh(ctx context.Context, conn *directconnect.Direc n := len(resp.VirtualInterfaces) switch n { case 0: - return "", directconnect.VirtualInterfaceStateDeleted, nil + return "", string(awstypes.VirtualInterfaceStateDeleted), nil case 1: vif := resp.VirtualInterfaces[0] - return vif, aws.StringValue(vif.VirtualInterfaceState), nil + return vif, string(vif.VirtualInterfaceState), nil default: return nil, "", fmt.Errorf("Found %d Direct Connect virtual interfaces for %s, expected 1", n, vifId) @@ -126,7 +125,7 @@ func virtualInterfaceStateRefresh(ctx context.Context, conn *directconnect.Direc } } -func virtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.DirectConnect, vifId string, timeout time.Duration, pending, target []string) error { +func virtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration, pending, target []string) error { stateConf := &retry.StateChangeConf{ Pending: pending, Target: target, diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go index 98f6d84dc58..fbbb6787f21 100644 --- a/internal/service/directconnect/wait.go +++ b/internal/service/directconnect/wait.go @@ -8,9 +8,11 @@ import ( "errors" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) @@ -22,26 +24,26 @@ const ( lagDeletedTimeout = 10 * time.Minute ) -func waitConnectionConfirmed(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) { //nolint:unparam +func waitConnectionConfirmed(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { //nolint:unparam stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.ConnectionStatePending, directconnect.ConnectionStateOrdering, directconnect.ConnectionStateRequested}, - Target: []string{directconnect.ConnectionStateAvailable}, + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateRequested), + Target: enum.Slice(awstypes.ConnectionStateAvailable), Refresh: statusConnectionState(ctx, conn, id), Timeout: connectionConfirmedTimeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Connection); ok { + if output, ok := outputRaw.(*awstypes.Connection); ok { return output, err } return nil, err } -func waitConnectionDeleted(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) { +func waitConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.ConnectionStatePending, directconnect.ConnectionStateOrdering, directconnect.ConnectionStateAvailable, directconnect.ConnectionStateRequested, directconnect.ConnectionStateDeleting}, + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), Target: []string{}, Refresh: statusConnectionState(ctx, conn, id), Timeout: connectionDeletedTimeout, @@ -49,25 +51,25 @@ func waitConnectionDeleted(ctx context.Context, conn *directconnect.DirectConnec outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Connection); ok { + if output, ok := outputRaw.(*awstypes.Connection); ok { return output, err } return nil, err } -func waitGatewayCreated(ctx context.Context, conn *directconnect.DirectConnect, id string, timeout time.Duration) (*directconnect.Gateway, error) { +func waitGatewayCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.GatewayStatePending}, - Target: []string{directconnect.GatewayStateAvailable}, + Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending), + Target: enum.Slice(awstypes.DirectConnectGatewayStateAvailable), Refresh: statusGatewayState(ctx, conn, id), Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Gateway); ok { - tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateChangeError))) + if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) return output, err } @@ -75,9 +77,9 @@ func waitGatewayCreated(ctx context.Context, conn *directconnect.DirectConnect, return nil, err } -func waitGatewayDeleted(ctx context.Context, conn *directconnect.DirectConnect, id string, timeout time.Duration) (*directconnect.Gateway, error) { +func waitGatewayDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.GatewayStatePending, directconnect.GatewayStateAvailable, directconnect.GatewayStateDeleting}, + Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending, awstypes.DirectConnectGatewayStateAvailable, awstypes.DirectConnectGatewayStateDeleting), Target: []string{}, Refresh: statusGatewayState(ctx, conn, id), Timeout: timeout, @@ -85,8 +87,8 @@ func waitGatewayDeleted(ctx context.Context, conn *directconnect.DirectConnect, outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Gateway); ok { - tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateChangeError))) + if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) return output, err } @@ -94,18 +96,18 @@ func waitGatewayDeleted(ctx context.Context, conn *directconnect.DirectConnect, return nil, err } -func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.DirectConnect, id string, timeout time.Duration) (*directconnect.GatewayAssociation, error) { +func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.GatewayAssociationStateAssociating}, - Target: []string{directconnect.GatewayAssociationStateAssociated}, + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociating), + Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), Refresh: statusGatewayAssociationState(ctx, conn, id), Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.GatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateChangeError))) + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) return output, err } @@ -113,18 +115,18 @@ func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.Dire return nil, err } -func waitGatewayAssociationUpdated(ctx context.Context, conn *directconnect.DirectConnect, id string, timeout time.Duration) (*directconnect.GatewayAssociation, error) { +func waitGatewayAssociationUpdated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.GatewayAssociationStateUpdating}, - Target: []string{directconnect.GatewayAssociationStateAssociated}, + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateUpdating), + Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), Refresh: statusGatewayAssociationState(ctx, conn, id), Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.GatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateChangeError))) + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) return output, err } @@ -132,9 +134,9 @@ func waitGatewayAssociationUpdated(ctx context.Context, conn *directconnect.Dire return nil, err } -func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.DirectConnect, id string, timeout time.Duration) (*directconnect.GatewayAssociation, error) { +func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.GatewayAssociationStateDisassociating}, + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateDisassociating), Target: []string{}, Refresh: statusGatewayAssociationState(ctx, conn, id), Timeout: timeout, @@ -142,8 +144,8 @@ func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.Dire outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.GatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateChangeError))) + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) return output, err } @@ -151,9 +153,9 @@ func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.Dire return nil, err } -func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Connection, error) { +func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.ConnectionStatePending, directconnect.ConnectionStateOrdering, directconnect.ConnectionStateAvailable, directconnect.ConnectionStateRequested, directconnect.ConnectionStateDeleting}, + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), Target: []string{}, Refresh: statusHostedConnectionState(ctx, conn, id), Timeout: hostedConnectionDeletedTimeout, @@ -161,16 +163,16 @@ func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.Direct outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Connection); ok { + if output, ok := outputRaw.(*awstypes.Connection); ok { return output, err } return nil, err } -func waitLagDeleted(ctx context.Context, conn *directconnect.DirectConnect, id string) (*directconnect.Lag, error) { +func waitLagDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{directconnect.LagStateAvailable, directconnect.LagStateRequested, directconnect.LagStatePending, directconnect.LagStateDeleting}, + Pending: enum.Slice(awstypes.LagStateAvailable, awstypes.LagStateRequested, awstypes.LagStatePending, awstypes.LagStateDeleting), Target: []string{}, Refresh: statusLagState(ctx, conn, id), Timeout: lagDeletedTimeout, @@ -178,7 +180,7 @@ func waitLagDeleted(ctx context.Context, conn *directconnect.DirectConnect, id s outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*directconnect.Lag); ok { + if output, ok := outputRaw.(*awstypes.Lag); ok { return output, err } diff --git a/names/data/names_data.csv b/names/data/names_data.csv index c0ee031d37d..bcfff98db80 100644 --- a/names/data/names_data.csv +++ b/names/data/names_data.csv @@ -119,7 +119,7 @@ datazone,datazone,datazone,datazone,,datazone,,,DataZone,DataZone,,,2,,aws_dataz detective,detective,detective,detective,,detective,,,Detective,Detective,,1,,,aws_detective_,,detective_,Detective,Amazon,,,,,,,Detective,ListGraphs,, devicefarm,devicefarm,devicefarm,devicefarm,,devicefarm,,,DeviceFarm,DeviceFarm,,1,,,aws_devicefarm_,,devicefarm_,Device Farm,AWS,,,,,,,Device Farm,ListDeviceInstances,, devops-guru,devopsguru,devopsguru,devopsguru,,devopsguru,,,DevOpsGuru,DevOpsGuru,,,2,,aws_devopsguru_,,devopsguru_,DevOps Guru,Amazon,,,,,,,DevOps Guru,DescribeAccountHealth,, -directconnect,directconnect,directconnect,directconnect,,directconnect,,,DirectConnect,DirectConnect,,1,,aws_dx_,aws_directconnect_,,dx_,Direct Connect,AWS,,,,,,,Direct Connect,DescribeConnections,, +directconnect,directconnect,directconnect,directconnect,,directconnect,,,DirectConnect,DirectConnect,,,2,aws_dx_,aws_directconnect_,,dx_,Direct Connect,AWS,,,,,,,Direct Connect,DescribeConnections,, dlm,dlm,dlm,dlm,,dlm,,,DLM,DLM,,1,,,aws_dlm_,,dlm_,DLM (Data Lifecycle Manager),Amazon,,,,,,,DLM,GetLifecyclePolicies,, dms,dms,databasemigrationservice,databasemigrationservice,,dms,,databasemigration;databasemigrationservice,DMS,DatabaseMigrationService,,1,,,aws_dms_,,dms_,DMS (Database Migration),AWS,,,,,,,Database Migration Service,DescribeCertificates,, docdb,docdb,docdb,docdb,,docdb,,,DocDB,DocDB,,1,,,aws_docdb_,,docdb_,DocumentDB,Amazon,,,,,,,DocDB,DescribeDBClusters,, diff --git a/names/names.go b/names/names.go index ad25c7b9d49..91145b684e3 100644 --- a/names/names.go +++ b/names/names.go @@ -54,6 +54,7 @@ const ( ComprehendEndpointID = "comprehend" ConfigServiceEndpointID = "config" DevOpsGuruEndpointID = "devops-guru" + DirectConnectEndpointID = "directconnect" ECREndpointID = "api.ecr" EKSEndpointID = "eks" EMREndpointID = "elasticmapreduce" From a3116df0862dc3631405f2803863091f384d9411 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 1 Aug 2024 12:39:56 -0400 Subject: [PATCH 02/59] Run 'make fix-constants PKG=directconnect'. --- internal/service/directconnect/gateway.go | 14 +-- .../directconnect/gateway_data_source.go | 8 +- .../directconnect/hosted_connection.go | 32 +++---- .../hosted_private_virtual_interface.go | 22 ++--- .../hosted_private_virtual_interface_test.go | 54 ++++++------ .../hosted_public_virtual_interface.go | 22 ++--- .../hosted_public_virtual_interface_test.go | 42 ++++----- .../hosted_transit_virtual_interface.go | 22 ++--- .../hosted_transit_virtual_interface_test.go | 56 ++++++------ .../private_virtual_interface.go | 16 ++-- .../private_virtual_interface_test.go | 88 +++++++++---------- .../directconnect/public_virtual_interface.go | 16 ++-- .../public_virtual_interface_test.go | 30 +++---- .../transit_virtual_interface.go | 16 ++-- .../transit_virtual_interface_test.go | 78 ++++++++-------- 15 files changed, 258 insertions(+), 258 deletions(-) diff --git a/internal/service/directconnect/gateway.go b/internal/service/directconnect/gateway.go index 6c05fa48cb6..aac0aab1122 100644 --- a/internal/service/directconnect/gateway.go +++ b/internal/service/directconnect/gateway.go @@ -40,11 +40,11 @@ func ResourceGateway() *schema.Resource { ForceNew: true, ValidateFunc: verify.ValidAmazonSideASN, }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Computed: true, }, @@ -61,7 +61,7 @@ func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - name := d.Get("name").(string) + name := d.Get(names.AttrName).(string) input := &directconnect.CreateDirectConnectGatewayInput{ DirectConnectGatewayName: aws.String(name), } @@ -103,8 +103,8 @@ func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta inter } d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(output.AmazonSideAsn), 10)) - d.Set("name", output.DirectConnectGatewayName) - d.Set("owner_account_id", output.OwnerAccount) + d.Set(names.AttrName, output.DirectConnectGatewayName) + d.Set(names.AttrOwnerAccountID, output.OwnerAccount) return diags } @@ -113,10 +113,10 @@ func resourceGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - if d.HasChange("name") { + if d.HasChange(names.AttrName) { input := &directconnect.UpdateDirectConnectGatewayInput{ DirectConnectGatewayId: aws.String(d.Id()), - NewDirectConnectGatewayName: aws.String(d.Get("name").(string)), + NewDirectConnectGatewayName: aws.String(d.Get(names.AttrName).(string)), } _, err := conn.UpdateDirectConnectGateway(ctx, input) diff --git a/internal/service/directconnect/gateway_data_source.go b/internal/service/directconnect/gateway_data_source.go index a5c4ec8ebb8..27d5169c1d2 100644 --- a/internal/service/directconnect/gateway_data_source.go +++ b/internal/service/directconnect/gateway_data_source.go @@ -26,11 +26,11 @@ func DataSourceGateway() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Computed: true, }, @@ -41,7 +41,7 @@ func DataSourceGateway() *schema.Resource { func dataSourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - name := d.Get("name").(string) + name := d.Get(names.AttrName).(string) gateways := make([]awstypes.DirectConnectGateway, 0) // DescribeDirectConnectGatewaysInput does not have a name parameter for filtering @@ -74,7 +74,7 @@ func dataSourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta int d.SetId(aws.ToString(gateway.DirectConnectGatewayId)) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(gateway.AmazonSideAsn), 10)) - d.Set("owner_account_id", gateway.OwnerAccount) + d.Set(names.AttrOwnerAccountID, gateway.OwnerAccount) return diags } diff --git a/internal/service/directconnect/hosted_connection.go b/internal/service/directconnect/hosted_connection.go index f11be99bebb..2e4e3519d78 100644 --- a/internal/service/directconnect/hosted_connection.go +++ b/internal/service/directconnect/hosted_connection.go @@ -37,7 +37,7 @@ func ResourceHostedConnection() *schema.Resource { ForceNew: true, ValidateFunc: validConnectionBandWidth(), }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -58,16 +58,16 @@ func ResourceHostedConnection() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "location": { + names.AttrLocation: { Type: schema.TypeString, Computed: true, }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -77,15 +77,15 @@ func ResourceHostedConnection() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "provider_name": { + names.AttrProviderName: { Type: schema.TypeString, Computed: true, }, - "region": { + names.AttrRegion: { Type: schema.TypeString, Computed: true, }, - "state": { + names.AttrState: { Type: schema.TypeString, Computed: true, }, @@ -103,12 +103,12 @@ func resourceHostedConnectionCreate(ctx context.Context, d *schema.ResourceData, var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - name := d.Get("name").(string) + name := d.Get(names.AttrName).(string) input := &directconnect.AllocateHostedConnectionInput{ Bandwidth: aws.String(d.Get("bandwidth").(string)), - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), ConnectionName: aws.String(name), - OwnerAccount: aws.String(d.Get("owner_account_id").(string)), + OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), Vlan: int32(d.Get("vlan").(int)), } @@ -148,13 +148,13 @@ func resourceHostedConnectionRead(ctx context.Context, d *schema.ResourceData, m d.Set("jumbo_frame_capable", connection.JumboFrameCapable) d.Set("lag_id", connection.LagId) d.Set("loa_issue_time", aws.ToTime(connection.LoaIssueTime).Format(time.RFC3339)) - d.Set("location", connection.Location) - d.Set("name", connection.ConnectionName) - d.Set("owner_account_id", connection.OwnerAccount) + d.Set(names.AttrLocation, connection.Location) + d.Set(names.AttrName, connection.ConnectionName) + d.Set(names.AttrOwnerAccountID, connection.OwnerAccount) d.Set("partner_name", connection.PartnerName) - d.Set("provider_name", connection.ProviderName) - d.Set("region", connection.Region) - d.Set("state", connection.ConnectionState) + d.Set(names.AttrProviderName, connection.ProviderName) + d.Set(names.AttrRegion, connection.Region) + d.Set(names.AttrState, connection.ConnectionState) d.Set("vlan", connection.Vlan) return diags diff --git a/internal/service/directconnect/hosted_private_virtual_interface.go b/internal/service/directconnect/hosted_private_virtual_interface.go index 7d66f982218..714efffdf02 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface.go +++ b/internal/service/directconnect/hosted_private_virtual_interface.go @@ -46,7 +46,7 @@ func ResourceHostedPrivateVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -69,7 +69,7 @@ func ResourceHostedPrivateVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -91,12 +91,12 @@ func ResourceHostedPrivateVirtualInterface() *schema.Resource { ForceNew: true, ValidateFunc: validation.IntInSlice([]int{1500, 9001}), }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -122,15 +122,15 @@ func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema. conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocatePrivateVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPrivateVirtualInterfaceAllocation: &awstypes.NewPrivateVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), Mtu: aws.Int32(int32(d.Get("mtu").(int))), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, - OwnerAccount: aws.String(d.Get("owner_account_id").(string)), + OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), } if v, ok := d.GetOk("amazon_address"); ok { req.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) @@ -184,16 +184,16 @@ func resourceHostedPrivateVirtualInterfaceRead(ctx context.Context, d *schema.Re AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) - d.Set("connection_id", vif.ConnectionId) + d.Set(names.AttrConnectionID, vif.ConnectionId) d.Set("customer_address", vif.CustomerAddress) d.Set("jumbo_frame_capable", vif.JumboFrameCapable) d.Set("mtu", vif.Mtu) - d.Set("name", vif.VirtualInterfaceName) - d.Set("owner_account_id", vif.OwnerAccount) + d.Set(names.AttrName, vif.VirtualInterfaceName) + d.Set(names.AttrOwnerAccountID, vif.OwnerAccount) d.Set("vlan", vif.Vlan) return diags diff --git a/internal/service/directconnect/hosted_private_virtual_interface_test.go b/internal/service/directconnect/hosted_private_virtual_interface_test.go index 70317ac6e27..7c488d37f01 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_private_virtual_interface_test.go @@ -51,21 +51,21 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "0"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), - resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct0), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), + resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, // Test import. @@ -110,24 +110,24 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2a"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), - resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), + resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, { @@ -136,24 +136,24 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key3", "Value3"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), - resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), + resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, }, diff --git a/internal/service/directconnect/hosted_public_virtual_interface.go b/internal/service/directconnect/hosted_public_virtual_interface.go index 6eadfe71f99..37ce43b65d9 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface.go +++ b/internal/service/directconnect/hosted_public_virtual_interface.go @@ -51,7 +51,7 @@ func ResourceHostedPublicVirtualInterface() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -70,7 +70,7 @@ func ResourceHostedPublicVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -81,12 +81,12 @@ func ResourceHostedPublicVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -119,14 +119,14 @@ func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.R conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocatePublicVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPublicVirtualInterfaceAllocation: &awstypes.NewPublicVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, - OwnerAccount: aws.String(d.Get("owner_account_id").(string)), + OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), } if v, ok := d.GetOk("amazon_address"); ok { req.NewPublicVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) @@ -180,14 +180,14 @@ func resourceHostedPublicVirtualInterfaceRead(ctx context.Context, d *schema.Res AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) - d.Set("connection_id", vif.ConnectionId) + d.Set(names.AttrConnectionID, vif.ConnectionId) d.Set("customer_address", vif.CustomerAddress) - d.Set("name", vif.VirtualInterfaceName) - d.Set("owner_account_id", vif.OwnerAccount) + d.Set(names.AttrName, vif.VirtualInterfaceName) + d.Set(names.AttrOwnerAccountID, vif.OwnerAccount) if err := d.Set("route_filter_prefixes", flattenRouteFilterPrefixes(vif.RouteFilterPrefixes)); err != nil { return sdkdiag.AppendErrorf(diags, "setting route_filter_prefixes: %s", err) } diff --git a/internal/service/directconnect/hosted_public_virtual_interface_test.go b/internal/service/directconnect/hosted_public_virtual_interface_test.go index ac2d20aa5cc..89a7b259bb4 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_public_virtual_interface_test.go @@ -53,21 +53,21 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "0"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct0), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, // Test import. @@ -114,24 +114,24 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2a"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, { @@ -141,24 +141,24 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key3", "Value3"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, }, diff --git a/internal/service/directconnect/hosted_transit_virtual_interface.go b/internal/service/directconnect/hosted_transit_virtual_interface.go index 5ec52561824..d6c83810375 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface.go @@ -50,7 +50,7 @@ func ResourceHostedTransitVirtualInterface() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -69,7 +69,7 @@ func ResourceHostedTransitVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -91,12 +91,12 @@ func ResourceHostedTransitVirtualInterface() *schema.Resource { ForceNew: true, ValidateFunc: validation.IntInSlice([]int{1500, 8500}), }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, }, - "owner_account_id": { + names.AttrOwnerAccountID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -122,13 +122,13 @@ func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema. conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.AllocateTransitVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), - OwnerAccount: aws.String(d.Get("owner_account_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), + OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), NewTransitVirtualInterfaceAllocation: &awstypes.NewTransitVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), Mtu: aws.Int32(int32(d.Get("mtu").(int))), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, } @@ -181,16 +181,16 @@ func resourceHostedTransitVirtualInterfaceRead(ctx context.Context, d *schema.Re AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) - d.Set("connection_id", vif.ConnectionId) + d.Set(names.AttrConnectionID, vif.ConnectionId) d.Set("customer_address", vif.CustomerAddress) d.Set("jumbo_frame_capable", vif.JumboFrameCapable) d.Set("mtu", vif.Mtu) - d.Set("name", vif.VirtualInterfaceName) - d.Set("owner_account_id", vif.OwnerAccount) + d.Set(names.AttrName, vif.VirtualInterfaceName) + d.Set(names.AttrOwnerAccountID, vif.OwnerAccount) d.Set("vlan", vif.Vlan) return diags diff --git a/internal/service/directconnect/hosted_transit_virtual_interface_test.go b/internal/service/directconnect/hosted_transit_virtual_interface_test.go index 3a27bd15a91..98903477924 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface_test.go @@ -24,7 +24,7 @@ func TestAccDirectConnectHostedTransitVirtualInterface_serial(t *testing.T) { t.Parallel() testCases := map[string]func(t *testing.T){ - "basic": testAccHostedTransitVirtualInterface_basic, + acctest.CtBasic: testAccHostedTransitVirtualInterface_basic, "accepterTags": testAccHostedTransitVirtualInterface_accepterTags, } @@ -63,21 +63,21 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "0"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct0), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, // Test import. @@ -123,24 +123,24 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2a"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, { @@ -149,24 +149,24 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttrSet(resourceName, "owner_account_id"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttrSet(resourceName, names.AttrOwnerAccountID), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), // Accepter's attributes: - resource.TestCheckResourceAttrSet(accepterResourceName, "arn"), - resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(accepterResourceName, "tags.%", "3"), + resource.TestCheckResourceAttrSet(accepterResourceName, names.AttrARN), + resource.TestCheckResourceAttrPair(accepterResourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(accepterResourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(accepterResourceName, "tags.Name", rName), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(accepterResourceName, "tags.Key3", "Value3"), - resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, "id"), + resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, }, diff --git a/internal/service/directconnect/private_virtual_interface.go b/internal/service/directconnect/private_virtual_interface.go index 9ab956f7db3..12de5bbc966 100644 --- a/internal/service/directconnect/private_virtual_interface.go +++ b/internal/service/directconnect/private_virtual_interface.go @@ -54,7 +54,7 @@ func ResourcePrivateVirtualInterface() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -73,7 +73,7 @@ func ResourcePrivateVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -100,7 +100,7 @@ func ResourcePrivateVirtualInterface() *schema.Resource { Optional: true, ValidateFunc: validation.IntInSlice([]int{1500, 9001}), }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -146,14 +146,14 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour } req := &directconnect.CreatePrivateVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPrivateVirtualInterface: &awstypes.NewPrivateVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), Mtu: aws.Int32(int32(d.Get("mtu").(int))), Tags: getTagsIn(ctx), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, } @@ -212,16 +212,16 @@ func resourcePrivateVirtualInterfaceRead(ctx context.Context, d *schema.Resource AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) - d.Set("connection_id", vif.ConnectionId) + d.Set(names.AttrConnectionID, vif.ConnectionId) d.Set("customer_address", vif.CustomerAddress) d.Set("dx_gateway_id", vif.DirectConnectGatewayId) d.Set("jumbo_frame_capable", vif.JumboFrameCapable) d.Set("mtu", vif.Mtu) - d.Set("name", vif.VirtualInterfaceName) + d.Set(names.AttrName, vif.VirtualInterfaceName) d.Set("sitelink_enabled", vif.SiteLinkEnabled) d.Set("vlan", vif.Vlan) d.Set("vpn_gateway_id", vif.VirtualGatewayId) diff --git a/internal/service/directconnect/private_virtual_interface_test.go b/internal/service/directconnect/private_virtual_interface_test.go index 5a308cf7f1b..cbb6a965333 100644 --- a/internal/service/directconnect/private_virtual_interface_test.go +++ b/internal/service/directconnect/private_virtual_interface_test.go @@ -48,18 +48,18 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, { @@ -69,18 +69,18 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "9001"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, // Test import. @@ -121,21 +121,21 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2a"), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, { @@ -145,21 +145,21 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(resourceName, "tags.Key3", "Value3"), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, // Test import. @@ -201,17 +201,17 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, @@ -254,19 +254,19 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", acctest.CtTrue), ), }, { @@ -276,19 +276,19 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), - resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", acctest.CtFalse), ), }, // Test import. diff --git a/internal/service/directconnect/public_virtual_interface.go b/internal/service/directconnect/public_virtual_interface.go index 5397ea57c89..7249d9398ce 100644 --- a/internal/service/directconnect/public_virtual_interface.go +++ b/internal/service/directconnect/public_virtual_interface.go @@ -59,7 +59,7 @@ func ResourcePublicVirtualInterface() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -78,7 +78,7 @@ func ResourcePublicVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -89,7 +89,7 @@ func ResourcePublicVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -123,12 +123,12 @@ func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.Resourc conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.CreatePublicVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPublicVirtualInterface: &awstypes.NewPublicVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), Tags: getTagsIn(ctx), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, } @@ -184,13 +184,13 @@ func resourcePublicVirtualInterfaceRead(ctx context.Context, d *schema.ResourceD AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) d.Set("customer_address", vif.CustomerAddress) - d.Set("connection_id", vif.ConnectionId) - d.Set("name", vif.VirtualInterfaceName) + d.Set(names.AttrConnectionID, vif.ConnectionId) + d.Set(names.AttrName, vif.VirtualInterfaceName) if err := d.Set("route_filter_prefixes", flattenRouteFilterPrefixes(vif.RouteFilterPrefixes)); err != nil { return sdkdiag.AppendErrorf(diags, "setting route_filter_prefixes: %s", err) } diff --git a/internal/service/directconnect/public_virtual_interface_test.go b/internal/service/directconnect/public_virtual_interface_test.go index 63c5feb95b8..654684e0f99 100644 --- a/internal/service/directconnect/public_virtual_interface_test.go +++ b/internal/service/directconnect/public_virtual_interface_test.go @@ -53,17 +53,17 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, @@ -106,17 +106,17 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2a"), @@ -130,17 +130,17 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttr(resourceName, "amazon_address", amazonAddress), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", "2"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "210.52.109.0/24"), resource.TestCheckTypeSetElemAttr(resourceName, "route_filter_prefixes.*", "175.45.176.0/22"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(resourceName, "tags.Key3", "Value3"), diff --git a/internal/service/directconnect/transit_virtual_interface.go b/internal/service/directconnect/transit_virtual_interface.go index 58d7b383935..bd4ffeebea7 100644 --- a/internal/service/directconnect/transit_virtual_interface.go +++ b/internal/service/directconnect/transit_virtual_interface.go @@ -54,7 +54,7 @@ func ResourceTransitVirtualInterface() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "arn": { + names.AttrARN: { Type: schema.TypeString, Computed: true, }, @@ -73,7 +73,7 @@ func ResourceTransitVirtualInterface() *schema.Resource { Computed: true, ForceNew: true, }, - "connection_id": { + names.AttrConnectionID: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -99,7 +99,7 @@ func ResourceTransitVirtualInterface() *schema.Resource { Optional: true, ValidateFunc: validation.IntInSlice([]int{1500, 8500}), }, - "name": { + names.AttrName: { Type: schema.TypeString, Required: true, ForceNew: true, @@ -133,7 +133,7 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) req := &directconnect.CreateTransitVirtualInterfaceInput{ - ConnectionId: aws.String(d.Get("connection_id").(string)), + ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewTransitVirtualInterface: &awstypes.NewTransitVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), @@ -141,7 +141,7 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), Mtu: aws.Int32(int32(d.Get("mtu").(int))), Tags: getTagsIn(ctx), - VirtualInterfaceName: aws.String(d.Get("name").(string)), + VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, } @@ -194,16 +194,16 @@ func resourceTransitVirtualInterfaceRead(ctx context.Context, d *schema.Resource AccountID: meta.(*conns.AWSClient).AccountID, Resource: fmt.Sprintf("dxvif/%s", d.Id()), }.String() - d.Set("arn", arn) + d.Set(names.AttrARN, arn) d.Set("aws_device", vif.AwsDeviceV2) d.Set("bgp_asn", vif.Asn) d.Set("bgp_auth_key", vif.AuthKey) - d.Set("connection_id", vif.ConnectionId) + d.Set(names.AttrConnectionID, vif.ConnectionId) d.Set("customer_address", vif.CustomerAddress) d.Set("dx_gateway_id", vif.DirectConnectGatewayId) d.Set("jumbo_frame_capable", vif.JumboFrameCapable) d.Set("mtu", vif.Mtu) - d.Set("name", vif.VirtualInterfaceName) + d.Set(names.AttrName, vif.VirtualInterfaceName) d.Set("sitelink_enabled", vif.SiteLinkEnabled) d.Set("vlan", vif.Vlan) diff --git a/internal/service/directconnect/transit_virtual_interface_test.go b/internal/service/directconnect/transit_virtual_interface_test.go index fc8535a6249..181822eb54c 100644 --- a/internal/service/directconnect/transit_virtual_interface_test.go +++ b/internal/service/directconnect/transit_virtual_interface_test.go @@ -24,7 +24,7 @@ func TestAccDirectConnectTransitVirtualInterface_serial(t *testing.T) { t.Parallel() testCases := map[string]func(t *testing.T){ - "basic": testAccTransitVirtualInterface_basic, + acctest.CtBasic: testAccTransitVirtualInterface_basic, "tags": testAccTransitVirtualInterface_tags, "sitelink": testAccTransitVirtualInterface_siteLink, } @@ -61,17 +61,17 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, @@ -82,17 +82,17 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "8500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, @@ -135,17 +135,17 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key1", "Value1"), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2a"), @@ -159,17 +159,17 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct3), resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2b"), resource.TestCheckResourceAttr(resourceName, "tags.Key3", "Value3"), @@ -215,18 +215,18 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "8500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", acctest.CtTrue), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, @@ -237,18 +237,18 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_address"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), - acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), + acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(fmt.Sprintf("dxvif/%s", aws.ToString(vif.VirtualInterfaceId)))), resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, "connection_id", connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), - resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", "true"), + resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), + resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "8500"), - resource.TestCheckResourceAttr(resourceName, "name", rName), - resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", "false"), - resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), + resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", acctest.CtFalse), + resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, From b8fe757d490e328f8c7c36c132058e1b4de955ef Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 1 Aug 2024 17:13:58 -0400 Subject: [PATCH 03/59] Fix semgrep errors. --- .ci/semgrep/migrate/context.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.ci/semgrep/migrate/context.yml b/.ci/semgrep/migrate/context.yml index 8bdc830bc96..be3699fc7da 100644 --- a/.ci/semgrep/migrate/context.yml +++ b/.ci/semgrep/migrate/context.yml @@ -30,6 +30,7 @@ rules: - pattern-not: conn.Options() - pattern-not: codestarconnections_sdkv2.$API() - pattern-not: connectcases_sdkv2.$API() + - pattern-not: directconnect_sdkv2.$API() - pattern-not: kafkaconnect_sdkv2.$API() - pattern-not: mediaconnect_sdkv2.$API() - pattern-not: pcaconnectorad_sdkv2.$API() From dd9ced19808ad36fb163569c1f6e8628d2c05d9d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 1 Aug 2024 17:14:15 -0400 Subject: [PATCH 04/59] r/aws_dx_bgp_peer: Tidy up. --- internal/service/directconnect/bgp_peer.go | 207 +++++++++++------- .../service/directconnect/bgp_peer_test.go | 84 ++++--- .../service/directconnect/exports_test.go | 5 +- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/vif.go | 45 ++++ 5 files changed, 229 insertions(+), 115 deletions(-) diff --git a/internal/service/directconnect/bgp_peer.go b/internal/service/directconnect/bgp_peer.go index 722ef54d16d..8e226ad2769 100644 --- a/internal/service/directconnect/bgp_peer.go +++ b/internal/service/directconnect/bgp_peer.go @@ -12,17 +12,19 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -// @SDKResource("aws_dx_bgp_peer") -func ResourceBGPPeer() *schema.Resource { +// @SDKResource("aws_dx_bgp_peer", name="BGP Peer") +func resourceBGPPeer() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceBGPPeerCreate, ReadWithoutTimeout: resourceBGPPeerRead, @@ -35,20 +37,19 @@ func ResourceBGPPeer() *schema.Resource { ForceNew: true, ValidateDiagFunc: enum.Validate[awstypes.AddressFamily](), }, - "bgp_asn": { - Type: schema.TypeInt, - Required: true, - ForceNew: true, - }, - "virtual_interface_id": { + "amazon_address": { Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, }, - "amazon_address": { + "aws_device": { Type: schema.TypeString, - Optional: true, Computed: true, + }, + "bgp_asn": { + Type: schema.TypeInt, + Required: true, ForceNew: true, }, "bgp_auth_key": { @@ -57,23 +58,24 @@ func ResourceBGPPeer() *schema.Resource { Computed: true, ForceNew: true, }, - "customer_address": { + "bgp_peer_id": { Type: schema.TypeString, - Optional: true, Computed: true, - ForceNew: true, }, "bgp_status": { Type: schema.TypeString, Computed: true, }, - "bgp_peer_id": { + "customer_address": { Type: schema.TypeString, + Optional: true, Computed: true, + ForceNew: true, }, - "aws_device": { + "virtual_interface_id": { Type: schema.TypeString, - Computed: true, + Required: true, + ForceNew: true, }, }, @@ -88,46 +90,37 @@ func resourceBGPPeerCreate(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vifId := d.Get("virtual_interface_id").(string) - addrFamily := d.Get("address_family").(string) + vifID := d.Get("virtual_interface_id").(string) + addrFamily := awstypes.AddressFamily(d.Get("address_family").(string)) asn := int32(d.Get("bgp_asn").(int)) - - req := &directconnect.CreateBGPPeerInput{ - VirtualInterfaceId: aws.String(vifId), + input := &directconnect.CreateBGPPeerInput{ NewBGPPeer: &awstypes.NewBGPPeer{ - AddressFamily: awstypes.AddressFamily(addrFamily), + AddressFamily: addrFamily, Asn: asn, }, + VirtualInterfaceId: aws.String(vifID), } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewBGPPeer.AmazonAddress = aws.String(v.(string)) + input.NewBGPPeer.AmazonAddress = aws.String(v.(string)) } if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewBGPPeer.AuthKey = aws.String(v.(string)) + input.NewBGPPeer.AuthKey = aws.String(v.(string)) } if v, ok := d.GetOk("customer_address"); ok { - req.NewBGPPeer.CustomerAddress = aws.String(v.(string)) + input.NewBGPPeer.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect BGP peer: %#v", req) - _, err := conn.CreateBGPPeer(ctx, req) + _, err := conn.CreateBGPPeer(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect BGP peer: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect BGP Peer: %s", err) } - d.SetId(fmt.Sprintf("%s-%s-%d", vifId, addrFamily, asn)) + d.SetId(fmt.Sprintf("%s-%s-%d", vifID, addrFamily, asn)) - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.BGPPeerStatePending), - Target: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateVerifying), - Refresh: bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 10 * time.Second, - MinTimeout: 5 * time.Second, - } - _, err = stateConf.WaitForStateContext(ctx) - if err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect BGP peer (%s) to be available: %s", d.Id(), err) + if _, err = waitBGPPeerCreated(ctx, conn, vifID, addrFamily, asn, d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect BGP Peer (%s) create: %s", d.Id(), err) } return append(diags, resourceBGPPeerRead(ctx, d, meta)...) @@ -137,27 +130,27 @@ func resourceBGPPeerRead(ctx context.Context, d *schema.ResourceData, meta inter var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vifId := d.Get("virtual_interface_id").(string) - addrFamily := d.Get("address_family").(string) + vifID := d.Get("virtual_interface_id").(string) + addrFamily := awstypes.AddressFamily(d.Get("address_family").(string)) asn := int32(d.Get("bgp_asn").(int)) + bgpPeer, err := findBGPPeerByThreePartKey(ctx, conn, vifID, addrFamily, asn) - bgpPeerRaw, state, err := bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn)() - if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect BGP peer: %s", err) - } - if state == string(awstypes.BGPPeerStateDeleted) { - log.Printf("[WARN] Direct Connect BGP peer (%s) not found, removing from state", d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect BGP Peer (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - bgpPeer := bgpPeerRaw.(*awstypes.BGPPeer) + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect BGP Peer (%s): %s", d.Id(), err) + } + d.Set("amazon_address", bgpPeer.AmazonAddress) + d.Set("aws_device", bgpPeer.AwsDeviceV2) d.Set("bgp_auth_key", bgpPeer.AuthKey) - d.Set("customer_address", bgpPeer.CustomerAddress) - d.Set("bgp_status", bgpPeer.BgpStatus) d.Set("bgp_peer_id", bgpPeer.BgpPeerId) - d.Set("aws_device", bgpPeer.AwsDeviceV2) + d.Set("bgp_status", bgpPeer.BgpStatus) + d.Set("customer_address", bgpPeer.CustomerAddress) return diags } @@ -166,56 +159,106 @@ func resourceBGPPeerDelete(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vifId := d.Get("virtual_interface_id").(string) - addrFamily := d.Get("address_family").(string) + vifID := d.Get("virtual_interface_id").(string) + addrFamily := awstypes.AddressFamily(d.Get("address_family").(string)) asn := int32(d.Get("bgp_asn").(int)) log.Printf("[DEBUG] Deleting Direct Connect BGP peer: %s", d.Id()) _, err := conn.DeleteBGPPeer(ctx, &directconnect.DeleteBGPPeerInput{ Asn: asn, CustomerAddress: aws.String(d.Get("customer_address").(string)), - VirtualInterfaceId: aws.String(vifId), + VirtualInterfaceId: aws.String(vifID), }) + + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "The last BGP Peer on a Virtual Interface cannot be deleted") { + return diags + } + if err != nil { - // This is the error returned if the BGP peering has already gone. - if tfawserr.ErrMessageContains(err, "DirectConnectClientException", "The last BGP Peer on a Virtual Interface cannot be deleted") { - return diags - } return sdkdiag.AppendErrorf(diags, "deleting Direct Connect BGP Peer (%s): %s", d.Id(), err) } - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateDeleting, awstypes.BGPPeerStatePending, awstypes.BGPPeerStateVerifying), - Target: enum.Slice(awstypes.BGPPeerStateDeleted), - Refresh: bgpPeerStateRefresh(ctx, conn, vifId, addrFamily, asn), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 10 * time.Second, - MinTimeout: 5 * time.Second, + if _, err = waitBGPPeerDeleted(ctx, conn, vifID, addrFamily, asn, d.Timeout(schema.TimeoutDelete)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect BGP Peer (%s) delete: %s", d.Id(), err) } - _, err = stateConf.WaitForStateContext(ctx) + + return diags +} + +func findBGPPeerByThreePartKey(ctx context.Context, conn *directconnect.Client, vifID string, addrFamily awstypes.AddressFamily, asn int32) (*awstypes.BGPPeer, error) { + vif, err := findVirtualInterfaceByID(ctx, conn, vifID) + if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting Direct Connect BGP Peer (%s): waiting for completion: %s", d.Id(), err) + return nil, err } - return diags + output, err := tfresource.AssertSingleValueResult(tfslices.Filter(vif.BgpPeers, func(v awstypes.BGPPeer) bool { + return v.AddressFamily == addrFamily && v.Asn == asn + })) + + if err != nil { + return nil, err + } + + if state := output.BgpPeerState; state == awstypes.BGPPeerStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + } + } + + return output, nil } -func bgpPeerStateRefresh(ctx context.Context, conn *directconnect.Client, vifId, addrFamily string, asn int32) retry.StateRefreshFunc { +func statusBGPPeer(ctx context.Context, conn *directconnect.Client, vifID string, addrFamily awstypes.AddressFamily, asn int32) retry.StateRefreshFunc { return func() (interface{}, string, error) { - vif, err := virtualInterfaceRead(ctx, vifId, conn) + output, err := findBGPPeerByThreePartKey(ctx, conn, vifID, addrFamily, asn) + + if tfresource.NotFound(err) { + return nil, "", nil + } + if err != nil { return nil, "", err } - if vif == nil { - return "", string(awstypes.BGPPeerStateDeleted), nil - } - for _, bgpPeer := range vif.BgpPeers { - if string(bgpPeer.AddressFamily) == addrFamily && bgpPeer.Asn == asn { - return bgpPeer, string(bgpPeer.BgpPeerState), nil - } - } + return output, string(output.BgpPeerState), nil + } +} + +func waitBGPPeerCreated(ctx context.Context, conn *directconnect.Client, vifID string, addrFamily awstypes.AddressFamily, asn int32, timeout time.Duration) (*awstypes.BGPPeer, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.BGPPeerStatePending), + Target: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateVerifying), + Refresh: statusBGPPeer(ctx, conn, vifID, addrFamily, asn), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) - return "", string(awstypes.BGPPeerStateDeleted), nil + if output, ok := outputRaw.(*awstypes.BGPPeer); ok { + return output, err } + + return nil, err +} + +func waitBGPPeerDeleted(ctx context.Context, conn *directconnect.Client, vifID string, addrFamily awstypes.AddressFamily, asn int32, timeout time.Duration) (*awstypes.BGPPeer, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.BGPPeerStateAvailable, awstypes.BGPPeerStateDeleting, awstypes.BGPPeerStatePending, awstypes.BGPPeerStateVerifying), + Target: []string{}, + Refresh: statusBGPPeer(ctx, conn, vifID, addrFamily, asn), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.BGPPeer); ok { + return output, err + } + + return nil, err } diff --git a/internal/service/directconnect/bgp_peer_test.go b/internal/service/directconnect/bgp_peer_test.go index ccf36d136cf..bd765db9025 100644 --- a/internal/service/directconnect/bgp_peer_test.go +++ b/internal/service/directconnect/bgp_peer_test.go @@ -6,29 +6,48 @@ package directconnect_test import ( "context" "fmt" - "os" - "strconv" "testing" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" + "github.com/hashicorp/terraform-provider-aws/internal/flex" + tfdirectconnect "github.com/hashicorp/terraform-provider-aws/internal/service/directconnect" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccDirectConnectBGPPeer_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_VIRTUAL_INTERFACE_ID" - vifId := os.Getenv(key) - if vifId == "" { - t.Skipf("Environment variable %s is not set", key) - } + vifID := acctest.SkipIfEnvVarNotSet(t, "DX_VIRTUAL_INTERFACE_ID") + bgpAsn := sdkacctest.RandIntRange(64512, 65534) + resourceName := "aws_dx_bgp_peer.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckBGPPeerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccBGPPeerConfig_basic(vifID, bgpAsn), + Check: resource.ComposeTestCheckFunc( + testAccCheckBGPPeerExists(ctx, resourceName), + resource.TestCheckResourceAttr(resourceName, "address_family", "ipv6"), + ), + }, + }, + }) +} + +func TestAccDirectConnectBGPPeer_disappears(t *testing.T) { + ctx := acctest.Context(t) + vifID := acctest.SkipIfEnvVarNotSet(t, "DX_VIRTUAL_INTERFACE_ID") bgpAsn := sdkacctest.RandIntRange(64512, 65534) + resourceName := "aws_dx_bgp_peer.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -37,11 +56,12 @@ func TestAccDirectConnectBGPPeer_basic(t *testing.T) { CheckDestroy: testAccCheckBGPPeerDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccBGPPeerConfig_basic(vifId, bgpAsn), + Config: testAccBGPPeerConfig_basic(vifID, bgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckBGPPeerExists("aws_dx_bgp_peer.foo"), - resource.TestCheckResourceAttr("aws_dx_bgp_peer.foo", "address_family", "ipv6"), + testAccCheckBGPPeerExists(ctx, resourceName), + acctest.CheckResourceDisappears(ctx, acctest.Provider, tfdirectconnect.ResourceBGPPeer(), resourceName), ), + ExpectNonEmptyPlan: true, }, }, }) @@ -55,44 +75,46 @@ func testAccCheckBGPPeerDestroy(ctx context.Context) resource.TestCheckFunc { if rs.Type != "aws_dx_bgp_peer" { continue } - input := &directconnect.DescribeVirtualInterfacesInput{ - VirtualInterfaceId: aws.String(rs.Primary.Attributes["virtual_interface_id"]), + + _, err := tfdirectconnect.FindBGPPeerByThreePartKey(ctx, conn, rs.Primary.Attributes["virtual_interface_id"], awstypes.AddressFamily(rs.Primary.Attributes["address_family"]), flex.StringValueToInt32Value(rs.Primary.Attributes["bgp_asn"])) + + if tfresource.NotFound(err) { + continue } - resp, err := conn.DescribeVirtualInterfaces(ctx, input) if err != nil { return err } - for _, peer := range resp.VirtualInterfaces[0].BgpPeers { - if string(peer.AddressFamily) == rs.Primary.Attributes["address_family"] && - strconv.Itoa(int(peer.Asn)) == rs.Primary.Attributes["bgp_asn"] && - peer.BgpPeerState != awstypes.BGPPeerStateDeleted { - return fmt.Errorf("[DESTROY ERROR] Dx BGP peer (%s) not deleted", rs.Primary.ID) - } - } + + return fmt.Errorf("Direct Connect BGP Peer %s still exists", rs.Primary.ID) } + return nil } } -func testAccCheckBGPPeerExists(name string) resource.TestCheckFunc { +func testAccCheckBGPPeerExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - _, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - return nil + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) + + _, err := tfdirectconnect.FindBGPPeerByThreePartKey(ctx, conn, rs.Primary.Attributes["virtual_interface_id"], awstypes.AddressFamily(rs.Primary.Attributes["address_family"]), flex.StringValueToInt32Value(rs.Primary.Attributes["bgp_asn"])) + + return err } } -func testAccBGPPeerConfig_basic(vifId string, bgpAsn int) string { +func testAccBGPPeerConfig_basic(vifID string, bgpAsn int) string { return fmt.Sprintf(` -resource "aws_dx_bgp_peer" "foo" { - virtual_interface_id = "%s" +resource "aws_dx_bgp_peer" "test" { + virtual_interface_id = %[1]q address_family = "ipv6" - bgp_asn = %d + bgp_asn = %[2]d } -`, vifId, bgpAsn) +`, vifID, bgpAsn) } diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index c8eb889439a..61a1e8499d8 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,5 +5,8 @@ package directconnect // Exports for use in tests only. var ( - ValidConnectionBandWidth = validConnectionBandWidth + ResourceBGPPeer = resourceBGPPeer + + FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 4d0440e821e..a73732ce503 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -50,8 +50,9 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePackageSDKResource { return []*types.ServicePackageSDKResource{ { - Factory: ResourceBGPPeer, + Factory: resourceBGPPeer, TypeName: "aws_dx_bgp_peer", + Name: "BGP Peer", }, { Factory: ResourceConnection, diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index afde99b2d09..67cb708c987 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) func virtualInterfaceRead(ctx context.Context, id string, conn *directconnect.Client) (*awstypes.VirtualInterface, error) { @@ -140,3 +141,47 @@ func virtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect return nil } + +func findVirtualInterfaceByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.VirtualInterface, error) { + input := &directconnect.DescribeVirtualInterfacesInput{ + VirtualInterfaceId: aws.String(id), + } + output, err := findVirtualInterface(ctx, conn, input) + + if err != nil { + return nil, err + } + + if state := output.VirtualInterfaceState; state == awstypes.VirtualInterfaceStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findVirtualInterface(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput) (*awstypes.VirtualInterface, error) { + output, err := findVirtualInterfaces(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findVirtualInterfaces(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput) ([]awstypes.VirtualInterface, error) { + output, err := conn.DescribeVirtualInterfaces(ctx, input) + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.VirtualInterfaces, nil +} From 5a16390ead8ba9881a3fd39f3a58142d0ee06172 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 2 Aug 2024 14:50:14 -0400 Subject: [PATCH 05/59] 'directconnect/acc_test.go' -> 'directconnect/vif_test.go'. --- internal/service/directconnect/acc_test.go | 80 ------------------- .../service/directconnect/exports_test.go | 1 + internal/service/directconnect/vif_test.go | 62 ++++++++++++++ 3 files changed, 63 insertions(+), 80 deletions(-) delete mode 100644 internal/service/directconnect/acc_test.go create mode 100644 internal/service/directconnect/vif_test.go diff --git a/internal/service/directconnect/acc_test.go b/internal/service/directconnect/acc_test.go deleted file mode 100644 index b966eab6668..00000000000 --- a/internal/service/directconnect/acc_test.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect_test - -import ( - "context" - "fmt" - - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/directconnect" - awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "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" - "github.com/hashicorp/terraform-provider-aws/internal/errs" -) - -func testAccCheckVirtualInterfaceExists(ctx context.Context, name string, vif *awstypes.VirtualInterface) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - - rs, ok := s.RootModule().Resources[name] - if !ok { - return fmt.Errorf("Not found: %s", name) - } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ - VirtualInterfaceId: aws.String(rs.Primary.ID), - }) - if err != nil { - return err - } - - for _, v := range resp.VirtualInterfaces { - if aws.ToString(v.VirtualInterfaceId) == rs.Primary.ID { - *vif = v - - return nil - } - } - - return fmt.Errorf("Direct Connect virtual interface (%s) not found", rs.Primary.ID) - } -} - -func testAccCheckVirtualInterfaceDestroy(ctx context.Context, s *terraform.State, t string) error { // nosemgrep:ci.semgrep.acctest.naming.destroy-check-signature - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != t { - continue - } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ - VirtualInterfaceId: aws.String(rs.Primary.ID), - }) - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { - continue - } - if err != nil { - return err - } - - for _, v := range resp.VirtualInterfaces { - if aws.ToString(v.VirtualInterfaceId) == rs.Primary.ID && v.VirtualInterfaceState != awstypes.VirtualInterfaceStateDeleted { - return fmt.Errorf("[DESTROY ERROR] Direct Connect virtual interface (%s) not deleted", rs.Primary.ID) - } - } - } - - return nil -} diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 61a1e8499d8..602c76ae566 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -8,5 +8,6 @@ var ( ResourceBGPPeer = resourceBGPPeer FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + FindVirtualInterfaceByID = findVirtualInterfaceByID ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/vif_test.go b/internal/service/directconnect/vif_test.go new file mode 100644 index 00000000000..1f49e7137ea --- /dev/null +++ b/internal/service/directconnect/vif_test.go @@ -0,0 +1,62 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package directconnect_test + +import ( + "context" + "fmt" + + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" + "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" + tfdirectconnect "github.com/hashicorp/terraform-provider-aws/internal/service/directconnect" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func testAccCheckVirtualInterfaceExists(ctx context.Context, n string, v *awstypes.VirtualInterface) 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).DirectConnectClient(ctx) + + output, err := tfdirectconnect.FindVirtualInterfaceByID(ctx, conn, rs.Primary.ID) + + if err != nil { + return err + } + + *v = *output + + return nil + } +} + +func testAccCheckVirtualInterfaceDestroy(ctx context.Context, s *terraform.State, typ string) error { // nosemgrep:ci.semgrep.acctest.naming.destroy-check-signature + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != typ { + continue + } + + _, err := tfdirectconnect.FindVirtualInterfaceByID(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Direct Connect Virtual Interface (%s) %s still exists", typ, rs.Primary.ID) + } + + return nil +} From 0bdbc384905e97cb2f55263fbd9d0851874fcda2 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 2 Aug 2024 14:52:49 -0400 Subject: [PATCH 06/59] directconnect: Fix compilation errors (a3116df0862dc3631405f2803863091f384d9411). --- internal/service/directconnect/gateway.go | 1 + internal/service/directconnect/gateway_data_source.go | 1 + internal/service/directconnect/hosted_connection.go | 1 + .../service/directconnect/hosted_private_virtual_interface.go | 1 + .../service/directconnect/hosted_public_virtual_interface.go | 1 + .../service/directconnect/hosted_transit_virtual_interface.go | 1 + 6 files changed, 6 insertions(+) diff --git a/internal/service/directconnect/gateway.go b/internal/service/directconnect/gateway.go index aac0aab1122..570bf5c8fae 100644 --- a/internal/service/directconnect/gateway.go +++ b/internal/service/directconnect/gateway.go @@ -19,6 +19,7 @@ import ( "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" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_gateway") diff --git a/internal/service/directconnect/gateway_data_source.go b/internal/service/directconnect/gateway_data_source.go index 27d5169c1d2..461d359f9e8 100644 --- a/internal/service/directconnect/gateway_data_source.go +++ b/internal/service/directconnect/gateway_data_source.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKDataSource("aws_dx_gateway") diff --git a/internal/service/directconnect/hosted_connection.go b/internal/service/directconnect/hosted_connection.go index 2e4e3519d78..7ec1b34ed93 100644 --- a/internal/service/directconnect/hosted_connection.go +++ b/internal/service/directconnect/hosted_connection.go @@ -17,6 +17,7 @@ import ( "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" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_hosted_connection") diff --git a/internal/service/directconnect/hosted_private_virtual_interface.go b/internal/service/directconnect/hosted_private_virtual_interface.go index 714efffdf02..c89f086e475 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface.go +++ b/internal/service/directconnect/hosted_private_virtual_interface.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_hosted_private_virtual_interface") diff --git a/internal/service/directconnect/hosted_public_virtual_interface.go b/internal/service/directconnect/hosted_public_virtual_interface.go index 37ce43b65d9..ea4263ca19f 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface.go +++ b/internal/service/directconnect/hosted_public_virtual_interface.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_hosted_public_virtual_interface") diff --git a/internal/service/directconnect/hosted_transit_virtual_interface.go b/internal/service/directconnect/hosted_transit_virtual_interface.go index d6c83810375..609872d4d91 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/verify" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_hosted_transit_virtual_interface") From 3c2a05a3c9bff883b01a2ff0601ffb1f8d112172 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 2 Aug 2024 15:13:14 -0400 Subject: [PATCH 07/59] r/aws_dx_connection: Migrate to AWS SDK for Go v2. --- internal/service/directconnect/connection.go | 142 +++++++++++++++--- .../directconnect/connection_confirmation.go | 2 +- .../service/directconnect/connection_test.go | 35 ++--- .../service/directconnect/exports_test.go | 4 +- internal/service/directconnect/find.go | 36 +---- internal/service/directconnect/macsec_key.go | 2 +- .../directconnect/service_package_gen.go | 2 +- internal/service/directconnect/status.go | 16 -- internal/service/directconnect/sweep.go | 2 +- internal/service/directconnect/wait.go | 36 ----- 10 files changed, 138 insertions(+), 139 deletions(-) diff --git a/internal/service/directconnect/connection.go b/internal/service/directconnect/connection.go index 55c6ec685bb..7f57b3f8664 100644 --- a/internal/service/directconnect/connection.go +++ b/internal/service/directconnect/connection.go @@ -8,15 +8,18 @@ import ( "fmt" "log" "strconv" + "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/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/aws/aws-sdk-go/aws/arn" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "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" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" @@ -27,7 +30,7 @@ import ( // @SDKResource("aws_dx_connection", name="Connection") // @Tags(identifierAttribute="arn") -func ResourceConnection() *schema.Resource { +func resourceConnection() *schema.Resource { // Resource with v0 schema (provider v5.0.1). resourceV0 := &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -40,17 +43,14 @@ func ResourceConnection() *schema.Resource { Computed: true, }, "bandwidth": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validConnectionBandWidth(), + Type: schema.TypeString, + Required: true, + ForceNew: true, }, - // The MAC Security (MACsec) connection encryption mode. "encryption_mode": { - Type: schema.TypeString, - Computed: true, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{"no_encrypt", "should_encrypt", "must_encrypt"}, false), + Type: schema.TypeString, + Computed: true, + Optional: true, }, "has_logical_redundancy": { Type: schema.TypeString, @@ -65,12 +65,10 @@ func ResourceConnection() *schema.Resource { Required: true, ForceNew: true, }, - // Indicates whether the connection supports MAC Security (MACsec). "macsec_capable": { Type: schema.TypeBool, Computed: true, }, - // Enable or disable MAC Security (MACsec) on this connection. "request_macsec": { Type: schema.TypeBool, Optional: true, @@ -269,7 +267,7 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta in var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - connection, err := FindConnectionByID(ctx, conn, d.Id()) + connection, err := findConnectionByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Connection (%s) not found, removing from state", d.Id()) @@ -301,13 +299,10 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta in d.Set("partner_name", connection.PartnerName) d.Set("port_encryption_status", connection.PortEncryptionStatus) d.Set(names.AttrProviderName, connection.ProviderName) - d.Set("vlan_id", connection.Vlan) - - // d.Set("request_macsec", d.Get("request_macsec").(bool)) - if !d.IsNewResource() && !d.Get("request_macsec").(bool) { d.Set("request_macsec", aws.Bool(false)) } + d.Set("vlan_id", connection.Vlan) return diags } @@ -365,11 +360,116 @@ func deleteConnection(ctx context.Context, conn *directconnect.Client, connectio return fmt.Errorf("deleting Direct Connect Connection (%s): %w", connectionID, err) } - _, err = waiter(ctx, conn, connectionID) - - if err != nil { + if _, err := waiter(ctx, conn, connectionID); err != nil { return fmt.Errorf("waiting for Direct Connect Connection (%s): %w", connectionID, err) } return nil } + +func findConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { + input := &directconnect.DescribeConnectionsInput{ + ConnectionId: aws.String(id), + } + output, err := findConnection(ctx, conn, input) + + if err != nil { + return nil, err + } + + if state := output.ConnectionState; state == awstypes.ConnectionStateDeleted || state == awstypes.ConnectionStateRejected { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findConnection(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput) (*awstypes.Connection, error) { + output, err := findConnections(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findConnections(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput) ([]awstypes.Connection, error) { + output, err := conn.DescribeConnections(ctx, input) + + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.Connections, nil +} + +func statusConnection(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findConnectionByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.ConnectionState), nil + } +} + +func waitConnectionConfirmed(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { //nolint:unparam + const ( + timeout = 10 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateRequested), + Target: enum.Slice(awstypes.ConnectionStateAvailable), + Refresh: statusConnection(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Connection); ok { + return output, err + } + + return nil, err +} + +func waitConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { + const ( + timeout = 10 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), + Target: []string{}, + Refresh: statusConnection(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Connection); ok { + return output, err + } + + return nil, err +} diff --git a/internal/service/directconnect/connection_confirmation.go b/internal/service/directconnect/connection_confirmation.go index 0dd01cb35ce..b5cabfcf3c4 100644 --- a/internal/service/directconnect/connection_confirmation.go +++ b/internal/service/directconnect/connection_confirmation.go @@ -63,7 +63,7 @@ func resourceConnectionConfirmationRead(ctx context.Context, d *schema.ResourceD var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - _, err := FindConnectionByID(ctx, conn, d.Id()) + _, err := findConnectionByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Connection (%s) not found, removing from state", d.Id()) diff --git a/internal/service/directconnect/connection_test.go b/internal/service/directconnect/connection_test.go index 7c29bb01570..3bbda557ab6 100644 --- a/internal/service/directconnect/connection_test.go +++ b/internal/service/directconnect/connection_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "testing" "github.com/YakDriver/regexache" @@ -48,7 +47,6 @@ func TestAccDirectConnectConnection_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan_id", acctest.Ct0), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -85,17 +83,8 @@ func TestAccDirectConnectConnection_disappears(t *testing.T) { func TestAccDirectConnectConnection_encryptionMode(t *testing.T) { ctx := acctest.Context(t) - dxKey := "DX_CONNECTION_ID" - connectionId := os.Getenv(dxKey) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", dxKey) - } - - dxName := "DX_CONNECTION_NAME" - connectionName := os.Getenv(dxName) - if connectionName == "" { - t.Skipf("Environment variable %s is not set", dxName) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") + connectionName := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_NAME") var connection awstypes.Connection resourceName := "aws_dx_connection.test" @@ -112,7 +101,7 @@ func TestAccDirectConnectConnection_encryptionMode(t *testing.T) { Config: testAccConnectionConfig_encryptionModeShouldEncrypt(connectionName, ckn, cak), ResourceName: resourceName, ImportState: true, - ImportStateId: connectionId, + ImportStateId: connectionID, ImportStatePersist: true, }, { @@ -202,7 +191,6 @@ func TestAccDirectConnectConnection_providerName(t *testing.T) { resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -257,7 +245,6 @@ func TestAccDirectConnectConnection_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, acctest.CtTagsKey1, acctest.CtValue1), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -385,26 +372,22 @@ func testAccCheckConnectionDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckConnectionExists(ctx context.Context, name string, v *awstypes.Connection) resource.TestCheckFunc { +func testAccCheckConnectionExists(ctx context.Context, n string, v *awstypes.Connection) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No Direct Connect Connection ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - connection, err := tfdirectconnect.FindConnectionByID(ctx, conn, rs.Primary.ID) + output, err := tfdirectconnect.FindConnectionByID(ctx, conn, rs.Primary.ID) if err != nil { return err } - *v = *connection + *v = *output return nil } diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 602c76ae566..1afb1e694e1 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,9 +5,11 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + FindConnectionByID = findConnectionByID FindVirtualInterfaceByID = findVirtualInterfaceByID ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index c2a8acd4aa3..fa1fc6833ed 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,42 +14,8 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { - input := &directconnect.DescribeConnectionsInput{ - ConnectionId: aws.String(id), - } - - output, err := conn.DescribeConnections(ctx, input) - - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { - return nil, &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - connection, err := tfresource.AssertSingleValueResult(output.Connections) - - if err != nil { - return nil, err - } - - if connection.ConnectionState == awstypes.ConnectionStateDeleted || connection.ConnectionState == awstypes.ConnectionStateRejected { - return nil, &retry.NotFoundError{ - Message: string(connection.ConnectionState), - LastRequest: input, - } - } - - return connection, nil -} - func FindConnectionAssociationExists(ctx context.Context, conn *directconnect.Client, connectionID, lagID string) error { - connection, err := FindConnectionByID(ctx, conn, connectionID) + connection, err := findConnectionByID(ctx, conn, connectionID) if err != nil { return err diff --git a/internal/service/directconnect/macsec_key.go b/internal/service/directconnect/macsec_key.go index ac91836a7ca..61237f575ea 100644 --- a/internal/service/directconnect/macsec_key.go +++ b/internal/service/directconnect/macsec_key.go @@ -116,7 +116,7 @@ func resourceMacSecKeyRead(ctx context.Context, d *schema.ResourceData, meta int return sdkdiag.AppendErrorf(diags, "unexpected format of ID (%s), expected secretArn_connectionId", d.Id()) } - connection, err := FindConnectionByID(ctx, conn, connId) + connection, err := findConnectionByID(ctx, conn, connId) if err != nil { return sdkdiag.AppendErrorf(diags, "reading Direct Connect Connection (%s): %s", d.Id(), err) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index a73732ce503..fe82b2d1401 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -55,7 +55,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "BGP Peer", }, { - Factory: ResourceConnection, + Factory: resourceConnection, TypeName: "aws_dx_connection", Name: "Connection", Tags: &types.ServicePackageResourceTags{ diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go index 12a5cae7045..2b4f2088623 100644 --- a/internal/service/directconnect/status.go +++ b/internal/service/directconnect/status.go @@ -11,22 +11,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func statusConnectionState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindConnectionByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, string(output.ConnectionState), nil - } -} - func statusGatewayState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindGatewayByID(ctx, conn, id) diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index d3b175183b4..d90ea0f64b4 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -97,7 +97,7 @@ func sweepConnections(region string) error { for _, connection := range output.Connections { id := aws.ToString(connection.ConnectionId) - r := ResourceConnection() + r := resourceConnection() d := r.Data(nil) d.SetId(id) diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go index fbbb6787f21..592872e859b 100644 --- a/internal/service/directconnect/wait.go +++ b/internal/service/directconnect/wait.go @@ -17,47 +17,11 @@ import ( ) const ( - connectionConfirmedTimeout = 10 * time.Minute - connectionDeletedTimeout = 10 * time.Minute connectionDisassociatedTimeout = 1 * time.Minute hostedConnectionDeletedTimeout = 10 * time.Minute lagDeletedTimeout = 10 * time.Minute ) -func waitConnectionConfirmed(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { //nolint:unparam - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateRequested), - Target: enum.Slice(awstypes.ConnectionStateAvailable), - Refresh: statusConnectionState(ctx, conn, id), - Timeout: connectionConfirmedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.Connection); ok { - return output, err - } - - return nil, err -} - -func waitConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), - Target: []string{}, - Refresh: statusConnectionState(ctx, conn, id), - Timeout: connectionDeletedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.Connection); ok { - return output, err - } - - return nil, err -} - func waitGatewayCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending), From 85a2cbddca87d35fa608540d7da131f4aaf6483e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 2 Aug 2024 15:53:47 -0400 Subject: [PATCH 08/59] r/aws_dx_connection_association: Tidy up. --- .../directconnect/connection_association.go | 47 +++++++++++-------- .../connection_association_test.go | 16 +++---- .../service/directconnect/exports_test.go | 14 +++--- internal/service/directconnect/find.go | 14 ------ .../directconnect/service_package_gen.go | 3 +- 5 files changed, 43 insertions(+), 51 deletions(-) diff --git a/internal/service/directconnect/connection_association.go b/internal/service/directconnect/connection_association.go index 0b8e07e9339..68a0a69a241 100644 --- a/internal/service/directconnect/connection_association.go +++ b/internal/service/directconnect/connection_association.go @@ -12,6 +12,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs" @@ -20,8 +21,8 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_connection_association") -func ResourceConnectionAssociation() *schema.Resource { +// @SDKResource("aws_dx_connection_association", name="Connection LAG Association") +func resourceConnectionAssociation() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceConnectionAssociationCreate, ReadWithoutTimeout: resourceConnectionAssociationRead, @@ -53,7 +54,6 @@ func resourceConnectionAssociationCreate(ctx context.Context, d *schema.Resource LagId: aws.String(lagID), } - log.Printf("[DEBUG] Creating Direct Connect Connection LAG Association: %#v", input) output, err := conn.AssociateConnectionWithLag(ctx, input) if err != nil { @@ -62,7 +62,7 @@ func resourceConnectionAssociationCreate(ctx context.Context, d *schema.Resource d.SetId(aws.ToString(output.ConnectionId)) - return diags + return append(diags, resourceConnectionAssociationRead(ctx, d, meta)...) } func resourceConnectionAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { @@ -70,7 +70,7 @@ func resourceConnectionAssociationRead(ctx context.Context, d *schema.ResourceDa conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) lagID := d.Get("lag_id").(string) - err := FindConnectionAssociationExists(ctx, conn, d.Id(), lagID) + err := findConnectionLAGAssociation(ctx, conn, d.Id(), lagID) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Connection (%s) LAG (%s) Association not found, removing from state", d.Id(), lagID) @@ -92,6 +92,7 @@ func resourceConnectionAssociationDelete(ctx context.Context, d *schema.Resource if err := deleteConnectionLAGAssociation(ctx, conn, d.Id(), d.Get("lag_id").(string)); err != nil { return sdkdiag.AppendFromErr(diags, err) } + return diags } @@ -101,27 +102,33 @@ func deleteConnectionLAGAssociation(ctx context.Context, conn *directconnect.Cli LagId: aws.String(lagID), } - _, err := tfresource.RetryWhen(ctx, connectionDisassociatedTimeout, + _, err := tfresource.RetryWhenIsAErrorMessageContains[*awstypes.DirectConnectClientException](ctx, connectionDisassociatedTimeout, func() (interface{}, error) { return conn.DisassociateConnectionFromLag(ctx, input) - }, - func(err error) (bool, error) { - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Connection does not exist") || - errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Lag does not exist") { - return false, nil - } - - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "is in a transitioning state") { - return true, err - } + }, "is in a transitioning state") - return false, err - }, - ) + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Connection does not exist") || + errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Lag does not exist") { + return nil + } if err != nil { return fmt.Errorf("deleting Direct Connect Connection (%s) LAG (%s) Association: %w", connectionID, lagID, err) } - return err + return nil +} + +func findConnectionLAGAssociation(ctx context.Context, conn *directconnect.Client, connectionID, lagID string) error { + connection, err := findConnectionByID(ctx, conn, connectionID) + + if err != nil { + return err + } + + if lagID != aws.ToString(connection.LagId) { + return &retry.NotFoundError{} + } + + return nil } diff --git a/internal/service/directconnect/connection_association_test.go b/internal/service/directconnect/connection_association_test.go index 5f67c404023..9738eac4a75 100644 --- a/internal/service/directconnect/connection_association_test.go +++ b/internal/service/directconnect/connection_association_test.go @@ -92,7 +92,7 @@ func testAccCheckConnectionAssociationDestroy(ctx context.Context) resource.Test continue } - err := tfdirectconnect.FindConnectionAssociationExists(ctx, conn, rs.Primary.ID, rs.Primary.Attributes["lag_id"]) + err := tfdirectconnect.FindConnectionLAGAssociation(ctx, conn, rs.Primary.ID, rs.Primary.Attributes["lag_id"]) if tfresource.NotFound(err) { continue @@ -109,20 +109,16 @@ func testAccCheckConnectionAssociationDestroy(ctx context.Context) resource.Test } } -func testAccCheckConnectionAssociationExists(ctx context.Context, name string) resource.TestCheckFunc { +func testAccCheckConnectionAssociationExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - return tfdirectconnect.FindConnectionAssociationExists(ctx, conn, rs.Primary.ID, rs.Primary.Attributes["lag_id"]) + return tfdirectconnect.FindConnectionLAGAssociation(ctx, conn, rs.Primary.ID, rs.Primary.Attributes["lag_id"]) } } diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 1afb1e694e1..fe52631271a 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,11 +5,13 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer - ResourceConnection = resourceConnection + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection + ResourceConnectionAssociation = resourceConnectionAssociation - FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey - FindConnectionByID = findConnectionByID - FindVirtualInterfaceByID = findVirtualInterfaceByID - ValidConnectionBandWidth = validConnectionBandWidth + FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + FindConnectionByID = findConnectionByID + FindConnectionLAGAssociation = findConnectionLAGAssociation + FindVirtualInterfaceByID = findVirtualInterfaceByID + ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index fa1fc6833ed..8234b9aab2f 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,20 +14,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindConnectionAssociationExists(ctx context.Context, conn *directconnect.Client, connectionID, lagID string) error { - connection, err := findConnectionByID(ctx, conn, connectionID) - - if err != nil { - return err - } - - if lagID != aws.ToString(connection.LagId) { - return &retry.NotFoundError{} - } - - return nil -} - func FindGatewayByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGateway, error) { input := &directconnect.DescribeDirectConnectGatewaysInput{ DirectConnectGatewayId: aws.String(id), diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index fe82b2d1401..bb70687fd9e 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -63,8 +63,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceConnectionAssociation, + Factory: resourceConnectionAssociation, TypeName: "aws_dx_connection_association", + Name: "Connection LAG Association", }, { Factory: ResourceConnectionConfirmation, From 60af88d6d8dee8e077d0449e0ef9f059a2e11a4b Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 16:39:40 -0400 Subject: [PATCH 09/59] d/aws_dx_connection: Tidy up. --- internal/service/directconnect/connection.go | 11 ++++--- .../directconnect/connection_data_source.go | 32 +++++-------------- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/vif.go | 11 ++++--- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/internal/service/directconnect/connection.go b/internal/service/directconnect/connection.go index 7f57b3f8664..a535038ab09 100644 --- a/internal/service/directconnect/connection.go +++ b/internal/service/directconnect/connection.go @@ -22,6 +22,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -371,7 +372,7 @@ func findConnectionByID(ctx context.Context, conn *directconnect.Client, id stri input := &directconnect.DescribeConnectionsInput{ ConnectionId: aws.String(id), } - output, err := findConnection(ctx, conn, input) + output, err := findConnection(ctx, conn, input, tfslices.PredicateTrue[*awstypes.Connection]()) if err != nil { return nil, err @@ -387,8 +388,8 @@ func findConnectionByID(ctx context.Context, conn *directconnect.Client, id stri return output, nil } -func findConnection(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput) (*awstypes.Connection, error) { - output, err := findConnections(ctx, conn, input) +func findConnection(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput, filter tfslices.Predicate[*awstypes.Connection]) (*awstypes.Connection, error) { + output, err := findConnections(ctx, conn, input, filter) if err != nil { return nil, err @@ -397,7 +398,7 @@ func findConnection(ctx context.Context, conn *directconnect.Client, input *dire return tfresource.AssertSingleValueResult(output) } -func findConnections(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput) ([]awstypes.Connection, error) { +func findConnections(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeConnectionsInput, filter tfslices.Predicate[*awstypes.Connection]) ([]awstypes.Connection, error) { output, err := conn.DescribeConnections(ctx, input) if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { @@ -415,7 +416,7 @@ func findConnections(ctx context.Context, conn *directconnect.Client, input *dir return nil, tfresource.NewEmptyResultError(input) } - return output.Connections, nil + return tfslices.Filter(output.Connections, tfslices.PredicateValue(filter)), nil } func statusConnection(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { diff --git a/internal/service/directconnect/connection_data_source.go b/internal/service/directconnect/connection_data_source.go index 7353ef7b21c..54ffd79ff06 100644 --- a/internal/service/directconnect/connection_data_source.go +++ b/internal/service/directconnect/connection_data_source.go @@ -16,11 +16,12 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_dx_connection") -func DataSourceConnection() *schema.Resource { +// @SDKDataSource("aws_dx_connection", name="Connection") +func dataSourceConnection() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceConnectionRead, @@ -71,35 +72,18 @@ func dataSourceConnectionRead(ctx context.Context, d *schema.ResourceData, meta conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - var connections []awstypes.Connection - input := &directconnect.DescribeConnectionsInput{} name := d.Get(names.AttrName).(string) + input := &directconnect.DescribeConnectionsInput{} - // DescribeConnections is not paginated. - output, err := conn.DescribeConnections(ctx, input) + connection, err := findConnection(ctx, conn, input, func(v *awstypes.Connection) bool { + return aws.ToString(v.ConnectionName) == name + }) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect Connections: %s", err) - } - - for _, connection := range output.Connections { - if aws.ToString(connection.ConnectionName) == name { - connections = append(connections, connection) - } + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("Direct Connect Connection", err)) } - switch count := len(connections); count { - case 0: - return sdkdiag.AppendErrorf(diags, "no matching Direct Connect Connection found") - case 1: - default: - return sdkdiag.AppendErrorf(diags, "%d Direct Connect Connections matched; use additional constraints to reduce matches to a single Direct Connect Connection", count) - } - - connection := connections[0] - d.SetId(aws.ToString(connection.ConnectionId)) - arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: aws.ToString(connection.Region), diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index bb70687fd9e..5a711de4282 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -25,8 +25,9 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.Servic func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePackageSDKDataSource { return []*types.ServicePackageSDKDataSource{ { - Factory: DataSourceConnection, + Factory: dataSourceConnection, TypeName: "aws_dx_connection", + Name: "Connection", }, { Factory: DataSourceGateway, diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index 67cb708c987..852619e69fe 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) @@ -146,7 +147,7 @@ func findVirtualInterfaceByID(ctx context.Context, conn *directconnect.Client, i input := &directconnect.DescribeVirtualInterfacesInput{ VirtualInterfaceId: aws.String(id), } - output, err := findVirtualInterface(ctx, conn, input) + output, err := findVirtualInterface(ctx, conn, input, tfslices.PredicateTrue[*awstypes.VirtualInterface]()) if err != nil { return nil, err @@ -162,8 +163,8 @@ func findVirtualInterfaceByID(ctx context.Context, conn *directconnect.Client, i return output, nil } -func findVirtualInterface(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput) (*awstypes.VirtualInterface, error) { - output, err := findVirtualInterfaces(ctx, conn, input) +func findVirtualInterface(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput, filter tfslices.Predicate[*awstypes.VirtualInterface]) (*awstypes.VirtualInterface, error) { + output, err := findVirtualInterfaces(ctx, conn, input, filter) if err != nil { return nil, err @@ -172,7 +173,7 @@ func findVirtualInterface(ctx context.Context, conn *directconnect.Client, input return tfresource.AssertSingleValueResult(output) } -func findVirtualInterfaces(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput) ([]awstypes.VirtualInterface, error) { +func findVirtualInterfaces(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeVirtualInterfacesInput, filter tfslices.Predicate[*awstypes.VirtualInterface]) ([]awstypes.VirtualInterface, error) { output, err := conn.DescribeVirtualInterfaces(ctx, input) if err != nil { @@ -183,5 +184,5 @@ func findVirtualInterfaces(ctx context.Context, conn *directconnect.Client, inpu return nil, tfresource.NewEmptyResultError(input) } - return output.VirtualInterfaces, nil + return tfslices.Filter(output.VirtualInterfaces, tfslices.PredicateValue(filter)), nil } From 945fcdf81488028073cb72c98f3cc921ba9fc5f3 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 16:43:09 -0400 Subject: [PATCH 10/59] r/aws_dx_connection_confirmation: Tidy up. --- internal/service/directconnect/connection.go | 20 ---------- .../directconnect/connection_confirmation.go | 37 ++++++++++++++----- .../service/directconnect/exports_test.go | 7 ++-- .../directconnect/service_package_gen.go | 3 +- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/internal/service/directconnect/connection.go b/internal/service/directconnect/connection.go index a535038ab09..9d039ec3d4b 100644 --- a/internal/service/directconnect/connection.go +++ b/internal/service/directconnect/connection.go @@ -435,26 +435,6 @@ func statusConnection(ctx context.Context, conn *directconnect.Client, id string } } -func waitConnectionConfirmed(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { //nolint:unparam - const ( - timeout = 10 * time.Minute - ) - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateRequested), - Target: enum.Slice(awstypes.ConnectionStateAvailable), - Refresh: statusConnection(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.Connection); ok { - return output, err - } - - return nil, err -} - func waitConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { const ( timeout = 10 * time.Minute diff --git a/internal/service/directconnect/connection_confirmation.go b/internal/service/directconnect/connection_confirmation.go index b5cabfcf3c4..fe9de3c39f4 100644 --- a/internal/service/directconnect/connection_confirmation.go +++ b/internal/service/directconnect/connection_confirmation.go @@ -6,23 +6,27 @@ package directconnect import ( "context" "log" + "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_connection_confirmation") -func ResourceConnectionConfirmation() *schema.Resource { +// @SDKResource("aws_dx_connection_confirmation", name="Connection Confirmation") +func resourceConnectionConfirmation() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceConnectionConfirmationCreate, ReadWithoutTimeout: resourceConnectionConfirmationRead, - DeleteWithoutTimeout: resourceConnectionConfirmationDelete, + DeleteWithoutTimeout: schema.NoopContext, Schema: map[string]*schema.Schema{ names.AttrConnectionID: { @@ -43,17 +47,16 @@ func resourceConnectionConfirmationCreate(ctx context.Context, d *schema.Resourc ConnectionId: aws.String(connectionID), } - log.Printf("[DEBUG] Confirming Direct Connect Connection: %#v", input) _, err := conn.ConfirmConnection(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "confirming Direct Connection Connection (%s): %s", connectionID, err) + return sdkdiag.AppendErrorf(diags, "confirming Direct Connect Connection (%s): %s", connectionID, err) } d.SetId(connectionID) if _, err := waitConnectionConfirmed(ctx, conn, d.Id()); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connection Connection (%s) confirm: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Connection (%s) confirm: %s", d.Id(), err) } return diags @@ -78,8 +81,22 @@ func resourceConnectionConfirmationRead(ctx context.Context, d *schema.ResourceD return diags } -func resourceConnectionConfirmationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics - log.Printf("[WARN] Will not delete Direct Connect connection. Terraform will remove this resource from the state file, however resources may remain.") - return diags +func waitConnectionConfirmed(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { //nolint:unparam + const ( + timeout = 10 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateRequested), + Target: enum.Slice(awstypes.ConnectionStateAvailable), + Refresh: statusConnection(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Connection); ok { + return output, err + } + + return nil, err } diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index fe52631271a..79083a59620 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,9 +5,10 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer - ResourceConnection = resourceConnection - ResourceConnectionAssociation = resourceConnectionAssociation + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection + ResourceConnectionAssociation = resourceConnectionAssociation + ResourceConnectionConfirmation = resourceConnectionConfirmation FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 5a711de4282..2f06e72d3b2 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -69,8 +69,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Connection LAG Association", }, { - Factory: ResourceConnectionConfirmation, + Factory: resourceConnectionConfirmation, TypeName: "aws_dx_connection_confirmation", + Name: "Connection Confirmation", }, { Factory: ResourceGateway, From e1e0e456eb8a3dc292754a4f53c4d3483a1ebcbf Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 16:56:56 -0400 Subject: [PATCH 11/59] r/aws_dx_gateway: Tidy up. --- .../service/directconnect/exports_test.go | 2 + internal/service/directconnect/find.go | 27 ---- internal/service/directconnect/gateway.go | 125 +++++++++++++++++- .../service/directconnect/gateway_test.go | 4 - .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/status.go | 16 --- internal/service/directconnect/sweep.go | 2 +- internal/service/directconnect/wait.go | 38 ------ 8 files changed, 123 insertions(+), 94 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 79083a59620..512b094a60b 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -9,10 +9,12 @@ var ( ResourceConnection = resourceConnection ResourceConnectionAssociation = resourceConnectionAssociation ResourceConnectionConfirmation = resourceConnectionConfirmation + ResourceGateway = resourceGateway FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID FindConnectionLAGAssociation = findConnectionLAGAssociation + FindGatewayByID = findGatewayByID FindVirtualInterfaceByID = findVirtualInterfaceByID ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index 8234b9aab2f..11dcc8aae2e 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,33 +14,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindGatewayByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGateway, error) { - input := &directconnect.DescribeDirectConnectGatewaysInput{ - DirectConnectGatewayId: aws.String(id), - } - - output, err := conn.DescribeDirectConnectGateways(ctx, input) - - if err != nil { - return nil, err - } - - gateway, err := tfresource.AssertSingleValueResult(output.DirectConnectGateways) - - if err != nil { - return nil, err - } - - if gateway.DirectConnectGatewayState == awstypes.DirectConnectGatewayStateDeleted { - return nil, &retry.NotFoundError{ - Message: string(gateway.DirectConnectGatewayState), - LastRequest: input, - } - } - - return gateway, nil -} - func FindGatewayAssociationByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociation, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ AssociationId: aws.String(id), diff --git a/internal/service/directconnect/gateway.go b/internal/service/directconnect/gateway.go index 570bf5c8fae..02404897d86 100644 --- a/internal/service/directconnect/gateway.go +++ b/internal/service/directconnect/gateway.go @@ -5,25 +5,29 @@ package directconnect import ( "context" + "errors" "log" - "strconv" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_gateway") -func ResourceGateway() *schema.Resource { +// @SDKResource("aws_dx_gateway", name="Gateway") +func resourceGateway() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceGatewayCreate, ReadWithoutTimeout: resourceGatewayRead, @@ -68,8 +72,7 @@ func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta int } if v, ok := d.Get("amazon_side_asn").(string); ok && v != "" { - v, _ := strconv.ParseInt(v, 10, 64) - input.AmazonSideAsn = aws.Int64(v) + input.AmazonSideAsn = flex.StringValueToInt64(v) } output, err := conn.CreateDirectConnectGateway(ctx, input) @@ -91,7 +94,7 @@ func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta inter var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - output, err := FindGatewayByID(ctx, conn, d.Id()) + output, err := findGatewayByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Gateway (%s) not found, removing from state", d.Id()) @@ -103,7 +106,7 @@ func resourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta inter return sdkdiag.AppendErrorf(diags, "reading Direct Connect Gateway (%s): %s", d.Id(), err) } - d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(output.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", flex.Int64ToStringValue(output.AmazonSideAsn)) d.Set(names.AttrName, output.DirectConnectGatewayName) d.Set(names.AttrOwnerAccountID, output.OwnerAccount) @@ -153,3 +156,111 @@ func resourceGatewayDelete(ctx context.Context, d *schema.ResourceData, meta int return diags } + +func findGatewayByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGateway, error) { + input := &directconnect.DescribeDirectConnectGatewaysInput{ + DirectConnectGatewayId: aws.String(id), + } + output, err := findGateway(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGateway]()) + + if err != nil { + return nil, err + } + + if state := output.DirectConnectGatewayState; state == awstypes.DirectConnectGatewayStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findGateway(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewaysInput, filter tfslices.Predicate[*awstypes.DirectConnectGateway]) (*awstypes.DirectConnectGateway, error) { + output, err := findGateways(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findGateways(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewaysInput, filter tfslices.Predicate[*awstypes.DirectConnectGateway]) ([]awstypes.DirectConnectGateway, error) { + var output []awstypes.DirectConnectGateway + + err := describeDirectConnectGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, v := range page.DirectConnectGateways { + if filter(&v) { + output = append(output, v) + } + } + + return !lastPage + }) + + if err != nil { + return nil, err + } + + return output, nil +} + +func statusGateway(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findGatewayByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.DirectConnectGatewayState), nil + } +} + +func waitGatewayCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending), + Target: enum.Slice(awstypes.DirectConnectGatewayStateAvailable), + Refresh: statusGateway(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) + + return output, err + } + + return nil, err +} + +func waitGatewayDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending, awstypes.DirectConnectGatewayStateAvailable, awstypes.DirectConnectGatewayStateDeleting), + Target: []string{}, + Refresh: statusGateway(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) + + return output, err + } + + return nil, err +} diff --git a/internal/service/directconnect/gateway_test.go b/internal/service/directconnect/gateway_test.go index 2b0fcc3eedd..d43d467ae84 100644 --- a/internal/service/directconnect/gateway_test.go +++ b/internal/service/directconnect/gateway_test.go @@ -166,10 +166,6 @@ func testAccCheckGatewayExists(ctx context.Context, name string, v *awstypes.Dir return fmt.Errorf("Not found: %s", name) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) output, err := tfdirectconnect.FindGatewayByID(ctx, conn, rs.Primary.ID) diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 2f06e72d3b2..7fe9061df87 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -74,8 +74,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Connection Confirmation", }, { - Factory: ResourceGateway, + Factory: resourceGateway, TypeName: "aws_dx_gateway", + Name: "Gateway", }, { Factory: ResourceGatewayAssociation, diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go index 2b4f2088623..2a2c76c3038 100644 --- a/internal/service/directconnect/status.go +++ b/internal/service/directconnect/status.go @@ -11,22 +11,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func statusGatewayState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindGatewayByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, string(output.DirectConnectGatewayState), nil - } -} - func statusGatewayAssociationState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindGatewayAssociationByID(ctx, conn, id) diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index d90ea0f64b4..7f11dc19156 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -358,7 +358,7 @@ func sweepGateways(region string) error { continue } - r := ResourceGateway() + r := resourceGateway() d := r.Data(nil) d.SetId(directConnectGatewayID) diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go index 592872e859b..ac10d471ef2 100644 --- a/internal/service/directconnect/wait.go +++ b/internal/service/directconnect/wait.go @@ -22,44 +22,6 @@ const ( lagDeletedTimeout = 10 * time.Minute ) -func waitGatewayCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending), - Target: enum.Slice(awstypes.DirectConnectGatewayStateAvailable), - Refresh: statusGatewayState(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { - tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) - - return output, err - } - - return nil, err -} - -func waitGatewayDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGateway, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.DirectConnectGatewayStatePending, awstypes.DirectConnectGatewayStateAvailable, awstypes.DirectConnectGatewayStateDeleting), - Target: []string{}, - Refresh: statusGatewayState(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.DirectConnectGateway); ok { - tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) - - return output, err - } - - return nil, err -} - func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociating), From 9c18db30f8971c51bf7f336ba382da4cd5eabe40 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 17:03:45 -0400 Subject: [PATCH 12/59] d/aws_dx_gateway: Tidy up. --- .../directconnect/gateway_data_source.go | 36 +++++-------------- .../directconnect/gateway_data_source_test.go | 19 +++------- .../directconnect/service_package_gen.go | 3 +- 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/internal/service/directconnect/gateway_data_source.go b/internal/service/directconnect/gateway_data_source.go index 461d359f9e8..3357253b127 100644 --- a/internal/service/directconnect/gateway_data_source.go +++ b/internal/service/directconnect/gateway_data_source.go @@ -14,11 +14,12 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_dx_gateway") -func DataSourceGateway() *schema.Resource { +// @SDKDataSource("aws_dx_gateway", name="Gateway") +func dataSourceGateway() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceGatewayRead, @@ -42,37 +43,18 @@ func DataSourceGateway() *schema.Resource { func dataSourceGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - name := d.Get(names.AttrName).(string) - gateways := make([]awstypes.DirectConnectGateway, 0) - // DescribeDirectConnectGatewaysInput does not have a name parameter for filtering + name := d.Get(names.AttrName).(string) input := &directconnect.DescribeDirectConnectGatewaysInput{} - for { - output, err := conn.DescribeDirectConnectGateways(ctx, input) - if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect Gateway: %s", err) - } - for _, gateway := range output.DirectConnectGateways { - if aws.ToString(gateway.DirectConnectGatewayName) == name { - gateways = append(gateways, gateway) - } - } - if output.NextToken == nil { - break - } - input.NextToken = output.NextToken - } - if len(gateways) == 0 { - return sdkdiag.AppendErrorf(diags, "Direct Connect Gateway not found for name: %s", name) - } + gateway, err := findGateway(ctx, conn, input, func(v *awstypes.DirectConnectGateway) bool { + return aws.ToString(v.DirectConnectGatewayName) == name + }) - if len(gateways) > 1 { - return sdkdiag.AppendErrorf(diags, "Multiple Direct Connect Gateways found for name: %s", name) + if err != nil { + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("Direct Connect Gateway", err)) } - gateway := gateways[0] - d.SetId(aws.ToString(gateway.DirectConnectGatewayId)) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(gateway.AmazonSideAsn), 10)) d.Set(names.AttrOwnerAccountID, gateway.OwnerAccount) diff --git a/internal/service/directconnect/gateway_data_source_test.go b/internal/service/directconnect/gateway_data_source_test.go index 9ace8ed0a6b..f2580ebeb76 100644 --- a/internal/service/directconnect/gateway_data_source_test.go +++ b/internal/service/directconnect/gateway_data_source_test.go @@ -7,7 +7,6 @@ import ( "fmt" "testing" - "github.com/YakDriver/regexache" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" @@ -25,10 +24,6 @@ func TestAccDirectConnectGatewayDataSource_basic(t *testing.T) { ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ - { - Config: testAccGatewayDataSourceConfig_nonExistent, - ExpectError: regexache.MustCompile(`Direct Connect Gateway not found`), - }, { Config: testAccGatewayDataSourceConfig_name(rName, sdkacctest.RandIntRange(64512, 65534)), Check: resource.ComposeTestCheckFunc( @@ -45,13 +40,13 @@ func TestAccDirectConnectGatewayDataSource_basic(t *testing.T) { func testAccGatewayDataSourceConfig_name(rName string, rBgpAsn int) string { return fmt.Sprintf(` resource "aws_dx_gateway" "wrong" { - amazon_side_asn = "%d" - name = "%s-wrong" + amazon_side_asn = %[1]d + name = "%[2]s-wrong" } resource "aws_dx_gateway" "test" { - amazon_side_asn = "%d" - name = "%s" + amazon_side_asn = %[3]d + name = %[4]q } data "aws_dx_gateway" "test" { @@ -59,9 +54,3 @@ data "aws_dx_gateway" "test" { } `, rBgpAsn+1, rName, rBgpAsn, rName) } - -const testAccGatewayDataSourceConfig_nonExistent = ` -data "aws_dx_gateway" "test" { - name = "tf-acc-test-does-not-exist" -} -` diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 7fe9061df87..5b3e721b30c 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -30,8 +30,9 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Connection", }, { - Factory: DataSourceGateway, + Factory: dataSourceGateway, TypeName: "aws_dx_gateway", + Name: "Gateway", }, { Factory: DataSourceLocation, From ec8e34a511f08272fbc64b44d6fc06bf140d3251 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 17:19:56 -0400 Subject: [PATCH 13/59] d/aws_dx_gateway_association: Tidy up. --- .../service/directconnect/exports_test.go | 15 +- internal/service/directconnect/find.go | 56 ----- .../directconnect/gateway_association.go | 196 +++++++++++++++--- .../gateway_association_migrate.go | 4 +- .../gateway_association_proposal.go | 2 +- internal/service/directconnect/id.go | 12 -- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/status.go | 16 -- internal/service/directconnect/sweep.go | 8 +- internal/service/directconnect/wait.go | 60 ------ 10 files changed, 187 insertions(+), 185 deletions(-) delete mode 100644 internal/service/directconnect/id.go diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 512b094a60b..91874ae17c6 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -10,11 +10,14 @@ var ( ResourceConnectionAssociation = resourceConnectionAssociation ResourceConnectionConfirmation = resourceConnectionConfirmation ResourceGateway = resourceGateway + ResourceGatewayAssociation = resourceGatewayAssociation - FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey - FindConnectionByID = findConnectionByID - FindConnectionLAGAssociation = findConnectionLAGAssociation - FindGatewayByID = findGatewayByID - FindVirtualInterfaceByID = findVirtualInterfaceByID - ValidConnectionBandWidth = validConnectionBandWidth + FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + FindConnectionByID = findConnectionByID + FindConnectionLAGAssociation = findConnectionLAGAssociation + FindGatewayAssociationByID = findGatewayAssociationByID + FindGatewayByID = findGatewayByID + FindVirtualInterfaceByID = findVirtualInterfaceByID + GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 + ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index 11dcc8aae2e..d0a865ddbd0 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,62 +14,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindGatewayAssociationByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociation, error) { - input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ - AssociationId: aws.String(id), - } - - return FindGatewayAssociation(ctx, conn, input) -} - -func FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, associatedGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { - input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ - AssociatedGatewayId: aws.String(associatedGatewayID), - DirectConnectGatewayId: aws.String(directConnectGatewayID), - } - - return FindGatewayAssociation(ctx, conn, input) -} - -func FindGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, virtualGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { - input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ - DirectConnectGatewayId: aws.String(directConnectGatewayID), - VirtualGatewayId: aws.String(virtualGatewayID), - } - - return FindGatewayAssociation(ctx, conn, input) -} - -func FindGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput) (*awstypes.DirectConnectGatewayAssociation, error) { - output, err := conn.DescribeDirectConnectGatewayAssociations(ctx, input) - - if err != nil { - return nil, err - } - - association, err := tfresource.AssertSingleValueResult(output.DirectConnectGatewayAssociations) - - if err != nil { - return nil, err - } - - if association.AssociationState == awstypes.DirectConnectGatewayAssociationStateDisassociated { - return nil, &retry.NotFoundError{ - Message: string(association.AssociationState), - LastRequest: input, - } - } - - if association.AssociatedGateway == nil { - return nil, &retry.NotFoundError{ - Message: "Empty AssociatedGateway", - LastRequest: input, - } - } - - return association, nil -} - func FindGatewayAssociationProposalByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociationProposal, error) { input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{ ProposalId: aws.String(id), diff --git a/internal/service/directconnect/gateway_association.go b/internal/service/directconnect/gateway_association.go index 1daa29382f8..787fa165048 100644 --- a/internal/service/directconnect/gateway_association.go +++ b/internal/service/directconnect/gateway_association.go @@ -5,6 +5,7 @@ package directconnect import ( "context" + "errors" "fmt" "log" "strings" @@ -14,16 +15,19 @@ import ( "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -// @SDKResource("aws_dx_gateway_association") -func ResourceGatewayAssociation() *schema.Resource { +// @SDKResource("aws_dx_gateway_association", name="Gateway Association") +func resourceGatewayAssociation() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceGatewayAssociationCreate, ReadWithoutTimeout: resourceGatewayAssociationRead, @@ -38,7 +42,7 @@ func ResourceGatewayAssociation() *schema.Resource { StateUpgraders: []schema.StateUpgrader{ { Type: resourceGatewayAssociationResourceV0().CoreConfigSchema().ImpliedType(), - Upgrade: GatewayAssociationStateUpgradeV0, + Upgrade: gatewayAssociationStateUpgradeV0, Version: 0, }, }, @@ -50,7 +54,6 @@ func ResourceGatewayAssociation() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "associated_gateway_id": { Type: schema.TypeString, Optional: true, @@ -59,7 +62,6 @@ func ResourceGatewayAssociation() *schema.Resource { ConflictsWith: []string{"associated_gateway_owner_account_id", "proposal_id"}, AtLeastOneOf: []string{"associated_gateway_id", "associated_gateway_owner_account_id", "proposal_id"}, }, - "associated_gateway_owner_account_id": { Type: schema.TypeString, Optional: true, @@ -70,35 +72,29 @@ func ResourceGatewayAssociation() *schema.Resource { RequiredWith: []string{"proposal_id"}, AtLeastOneOf: []string{"associated_gateway_id", "associated_gateway_owner_account_id", "proposal_id"}, }, - "associated_gateway_type": { Type: schema.TypeString, Computed: true, }, - "dx_gateway_association_id": { Type: schema.TypeString, Computed: true, }, - "dx_gateway_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "dx_gateway_owner_account_id": { Type: schema.TypeString, Computed: true, }, - "proposal_id": { Type: schema.TypeString, Optional: true, ConflictsWith: []string{"associated_gateway_id", "vpn_gateway_id"}, AtLeastOneOf: []string{"associated_gateway_id", "associated_gateway_owner_account_id", "proposal_id"}, }, - "vpn_gateway_id": { Type: schema.TypeString, Optional: true, @@ -135,7 +131,6 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat input.OverrideAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Accepting Direct Connect Gateway Association Proposal: %#v", input) output, err := conn.AcceptDirectConnectGatewayAssociationProposal(ctx, input) if err != nil { @@ -144,7 +139,7 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat // For historical reasons the resource ID isn't set to the association ID returned from the API. associationID = aws.ToString(output.DirectConnectGatewayAssociation.AssociationId) - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, aws.ToString(output.DirectConnectGatewayAssociation.AssociatedGateway.Id))) + d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, aws.ToString(output.DirectConnectGatewayAssociation.AssociatedGateway.Id))) } else { associatedGatewayID := d.Get("associated_gateway_id").(string) input := &directconnect.CreateDirectConnectGatewayAssociationInput{ @@ -156,7 +151,6 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat input.AddAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect Gateway Association: %#v", input) output, err := conn.CreateDirectConnectGatewayAssociation(ctx, input) if err != nil { @@ -165,13 +159,13 @@ func resourceGatewayAssociationCreate(ctx context.Context, d *schema.ResourceDat // For historical reasons the resource ID isn't set to the association ID returned from the API. associationID = aws.ToString(output.DirectConnectGatewayAssociation.AssociationId) - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID)) + d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID)) } d.Set("dx_gateway_association_id", associationID) if _, err := waitGatewayAssociationCreated(ctx, conn, associationID, d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) to create: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) create: %s", d.Id(), err) } return append(diags, resourceGatewayAssociationRead(ctx, d, meta)...) @@ -183,7 +177,7 @@ func resourceGatewayAssociationRead(ctx context.Context, d *schema.ResourceData, associationID := d.Get("dx_gateway_association_id").(string) - output, err := FindGatewayAssociationByID(ctx, conn, associationID) + output, err := findGatewayAssociationByID(ctx, conn, associationID) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Gateway Association (%s) not found, removing from state", d.Id()) @@ -198,7 +192,6 @@ func resourceGatewayAssociationRead(ctx context.Context, d *schema.ResourceData, if err := d.Set("allowed_prefixes", flattenRouteFilterPrefixes(output.AllowedPrefixesToDirectConnectGateway)); err != nil { return sdkdiag.AppendErrorf(diags, "setting allowed_prefixes: %s", err) } - d.Set("associated_gateway_id", output.AssociatedGateway.Id) d.Set("associated_gateway_owner_account_id", output.AssociatedGateway.OwnerAccount) d.Set("associated_gateway_type", output.AssociatedGateway.Type) @@ -218,18 +211,17 @@ func resourceGatewayAssociationUpdate(ctx context.Context, d *schema.ResourceDat AssociationId: aws.String(associationID), } - oraw, nraw := d.GetChange("allowed_prefixes") - o, n := oraw.(*schema.Set), nraw.(*schema.Set) + o, n := d.GetChange("allowed_prefixes") + os, ns := o.(*schema.Set), n.(*schema.Set) - if add := n.Difference(o); add.Len() > 0 { + if add := ns.Difference(os); add.Len() > 0 { input.AddAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(add.List()) } - if del := o.Difference(n); del.Len() > 0 { + if del := os.Difference(ns); del.Len() > 0 { input.RemoveAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(del.List()) } - log.Printf("[DEBUG] Updating Direct Connect Gateway Association: %#v", input) _, err := conn.UpdateDirectConnectGatewayAssociation(ctx, input) if err != nil { @@ -237,7 +229,7 @@ func resourceGatewayAssociationUpdate(ctx context.Context, d *schema.ResourceDat } if _, err := waitGatewayAssociationUpdated(ctx, conn, associationID, d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) to update: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) update: %s", d.Id(), err) } return append(diags, resourceGatewayAssociationRead(ctx, d, meta)...) @@ -263,7 +255,7 @@ func resourceGatewayAssociationDelete(ctx context.Context, d *schema.ResourceDat } if _, err := waitGatewayAssociationDeleted(ctx, conn, associationID, d.Timeout(schema.TimeoutDelete)); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) to delete: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Gateway Association (%s) delete: %s", d.Id(), err) } return diags @@ -281,15 +273,165 @@ func resourceGatewayAssociationImport(ctx context.Context, d *schema.ResourceDat directConnectGatewayID := parts[0] associatedGatewayID := parts[1] - output, err := FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx, conn, directConnectGatewayID, associatedGatewayID) + output, err := findGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx, conn, directConnectGatewayID, associatedGatewayID) if err != nil { return nil, err } - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID)) + d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID)) d.Set("dx_gateway_id", output.DirectConnectGatewayId) d.Set("dx_gateway_association_id", output.AssociationId) return []*schema.ResourceData{d}, nil } + +func gatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID string) string { + return fmt.Sprintf("ga-%s%s", directConnectGatewayID, associatedGatewayID) +} + +func findGatewayAssociationByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociation, error) { + input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ + AssociationId: aws.String(id), + } + + output, err := findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) + + if err != nil { + return nil, err + } + + if state := output.AssociationState; state == awstypes.DirectConnectGatewayAssociationStateDisassociated { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, associatedGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { + input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ + AssociatedGatewayId: aws.String(associatedGatewayID), + DirectConnectGatewayId: aws.String(directConnectGatewayID), + } + + return findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) +} + +func findGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, virtualGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { + input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ + DirectConnectGatewayId: aws.String(directConnectGatewayID), + VirtualGatewayId: aws.String(virtualGatewayID), + } + + return findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) +} + +func findGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociation]) (*awstypes.DirectConnectGatewayAssociation, error) { + output, err := findGatewayAssociations(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findGatewayAssociations(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociation]) ([]awstypes.DirectConnectGatewayAssociation, error) { + var output []awstypes.DirectConnectGatewayAssociation + + err := describeDirectConnectGatewayAssociationsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, v := range page.DirectConnectGatewayAssociations { + if filter(&v) { + output = append(output, v) + } + } + + return !lastPage + }) + + if err != nil { + return nil, err + } + + return output, nil +} + +func statusGatewayAssociation(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findGatewayAssociationByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.AssociationState), nil + } +} + +func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociating), + Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), + Refresh: statusGatewayAssociation(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) + + return output, err + } + + return nil, err +} + +func waitGatewayAssociationUpdated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateUpdating), + Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), + Refresh: statusGatewayAssociation(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) + + return output, err + } + + return nil, err +} + +func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateDisassociating), + Target: []string{}, + Refresh: statusGatewayAssociation(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) + + return output, err + } + + return nil, err +} diff --git a/internal/service/directconnect/gateway_association_migrate.go b/internal/service/directconnect/gateway_association_migrate.go index b2cbfb1ae81..407d528e654 100644 --- a/internal/service/directconnect/gateway_association_migrate.go +++ b/internal/service/directconnect/gateway_association_migrate.go @@ -78,14 +78,14 @@ func resourceGatewayAssociationResourceV0() *schema.Resource { } } -func GatewayAssociationStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { +func gatewayAssociationStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) log.Println("[INFO] Found Direct Connect Gateway Association state v0; migrating to v1") // dx_gateway_association_id was introduced in v2.8.0. Handle the case where it's not yet present. if v, ok := rawState["dx_gateway_association_id"]; !ok || v == nil { - output, err := FindGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx, conn, rawState["dx_gateway_id"].(string), rawState["vpn_gateway_id"].(string)) + output, err := findGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx, conn, rawState["dx_gateway_id"].(string), rawState["vpn_gateway_id"].(string)) if err != nil { return nil, err diff --git a/internal/service/directconnect/gateway_association_proposal.go b/internal/service/directconnect/gateway_association_proposal.go index 9d52632ba58..e12de41f576 100644 --- a/internal/service/directconnect/gateway_association_proposal.go +++ b/internal/service/directconnect/gateway_association_proposal.go @@ -137,7 +137,7 @@ func resourceGatewayAssociationProposalRead(ctx context.Context, d *schema.Resou directConnectGatewayID := d.Get("dx_gateway_id").(string) associatedGatewayID := d.Get("associated_gateway_id").(string) - output, err := FindGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx, conn, directConnectGatewayID, associatedGatewayID) + output, err := findGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx, conn, directConnectGatewayID, associatedGatewayID) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Gateway Association Proposal (%s) not found, removing from state", d.Id()) diff --git a/internal/service/directconnect/id.go b/internal/service/directconnect/id.go deleted file mode 100644 index 9bef8c3a844..00000000000 --- a/internal/service/directconnect/id.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect - -import ( - "fmt" -) - -func GatewayAssociationCreateResourceID(directConnectGatewayID, associatedGatewayID string) string { - return fmt.Sprintf("ga-%s%s", directConnectGatewayID, associatedGatewayID) -} diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 5b3e721b30c..ad21f56dd79 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -80,8 +80,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Gateway", }, { - Factory: ResourceGatewayAssociation, + Factory: resourceGatewayAssociation, TypeName: "aws_dx_gateway_association", + Name: "Gateway Association", }, { Factory: ResourceGatewayAssociationProposal, diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go index 2a2c76c3038..7f8f998a365 100644 --- a/internal/service/directconnect/status.go +++ b/internal/service/directconnect/status.go @@ -11,22 +11,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func statusGatewayAssociationState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindGatewayAssociationByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, string(output.AssociationState), nil - } -} - func statusHostedConnectionState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindHostedConnectionByID(ctx, conn, id) diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index 7f11dc19156..4f6c7a965c6 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -208,9 +208,9 @@ func sweepGatewayAssociations(region string) error { continue } - r := ResourceGatewayAssociation() + r := resourceGatewayAssociation() d := r.Data(nil) - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, gatewayID)) + d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, gatewayID)) d.Set("dx_gateway_association_id", association.AssociationId) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) @@ -275,9 +275,9 @@ func sweepGatewayAssociations(region string) error { continue } - r := ResourceGatewayAssociation() + r := resourceGatewayAssociation() d := r.Data(nil) - d.SetId(GatewayAssociationCreateResourceID(directConnectGatewayID, transitGatewayID)) + d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, transitGatewayID)) d.Set("dx_gateway_association_id", association.AssociationId) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go index ac10d471ef2..7777b56575e 100644 --- a/internal/service/directconnect/wait.go +++ b/internal/service/directconnect/wait.go @@ -5,15 +5,12 @@ package directconnect import ( "context" - "errors" "time" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-provider-aws/internal/enum" - "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) const ( @@ -22,63 +19,6 @@ const ( lagDeletedTimeout = 10 * time.Minute ) -func waitGatewayAssociationCreated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociating), - Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), - Refresh: statusGatewayAssociationState(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) - - return output, err - } - - return nil, err -} - -func waitGatewayAssociationUpdated(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateUpdating), - Target: enum.Slice(awstypes.DirectConnectGatewayAssociationStateAssociated), - Refresh: statusGatewayAssociationState(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) - - return output, err - } - - return nil, err -} - -func waitGatewayAssociationDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.DirectConnectGatewayAssociation, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.DirectConnectGatewayAssociationStateDisassociating), - Target: []string{}, - Refresh: statusGatewayAssociationState(ctx, conn, id), - Timeout: timeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.DirectConnectGatewayAssociation); ok { - tfresource.SetLastError(err, errors.New(aws.ToString(output.StateChangeError))) - - return output, err - } - - return nil, err -} - func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), From 9036aff6c56a2de3d293e1b0e08656971b794ede Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 4 Aug 2024 17:38:05 -0400 Subject: [PATCH 14/59] r/aws_dx_gateway_association_proposal: Tidy up. --- .../service/directconnect/exports_test.go | 30 ++-- internal/service/directconnect/find.go | 34 ----- .../directconnect/gateway_association.go | 36 ++--- .../gateway_association_proposal.go | 128 +++++++++++++----- .../gateway_association_proposal_test.go | 12 +- .../directconnect/gateway_association_test.go | 16 +-- .../service/directconnect/gateway_test.go | 6 +- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/sweep.go | 2 +- 9 files changed, 141 insertions(+), 126 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 91874ae17c6..19b40ebbd23 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,19 +5,21 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer - ResourceConnection = resourceConnection - ResourceConnectionAssociation = resourceConnectionAssociation - ResourceConnectionConfirmation = resourceConnectionConfirmation - ResourceGateway = resourceGateway - ResourceGatewayAssociation = resourceGatewayAssociation + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection + ResourceConnectionAssociation = resourceConnectionAssociation + ResourceConnectionConfirmation = resourceConnectionConfirmation + ResourceGateway = resourceGateway + ResourceGatewayAssociation = resourceGatewayAssociation + ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal - FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey - FindConnectionByID = findConnectionByID - FindConnectionLAGAssociation = findConnectionLAGAssociation - FindGatewayAssociationByID = findGatewayAssociationByID - FindGatewayByID = findGatewayByID - FindVirtualInterfaceByID = findVirtualInterfaceByID - GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 - ValidConnectionBandWidth = validConnectionBandWidth + FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey + FindConnectionByID = findConnectionByID + FindConnectionLAGAssociation = findConnectionLAGAssociation + FindGatewayAssociationByID = findGatewayAssociationByID + FindGatewayAssociationProposalByID = findGatewayAssociationProposalByID + FindGatewayByID = findGatewayByID + FindVirtualInterfaceByID = findVirtualInterfaceByID + GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 + ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index d0a865ddbd0..037b4863405 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,40 +14,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindGatewayAssociationProposalByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociationProposal, error) { - input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{ - ProposalId: aws.String(id), - } - - output, err := conn.DescribeDirectConnectGatewayAssociationProposals(ctx, input) - - if err != nil { - return nil, err - } - - proposal, err := tfresource.AssertSingleValueResult(output.DirectConnectGatewayAssociationProposals) - - if err != nil { - return nil, err - } - - if proposal.ProposalState == awstypes.DirectConnectGatewayAssociationProposalStateDeleted { - return nil, &retry.NotFoundError{ - Message: string(proposal.ProposalState), - LastRequest: input, - } - } - - if proposal.AssociatedGateway == nil { - return nil, &retry.NotFoundError{ - Message: "Empty AssociatedGateway", - LastRequest: input, - } - } - - return proposal, nil -} - func FindHostedConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { input := &directconnect.DescribeHostedConnectionsInput{ ConnectionId: aws.String(id), diff --git a/internal/service/directconnect/gateway_association.go b/internal/service/directconnect/gateway_association.go index 787fa165048..e477d51957d 100644 --- a/internal/service/directconnect/gateway_association.go +++ b/internal/service/directconnect/gateway_association.go @@ -295,20 +295,7 @@ func findGatewayAssociationByID(ctx context.Context, conn *directconnect.Client, AssociationId: aws.String(id), } - output, err := findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) - - if err != nil { - return nil, err - } - - if state := output.AssociationState; state == awstypes.DirectConnectGatewayAssociationStateDisassociated { - return nil, &retry.NotFoundError{ - Message: string(state), - LastRequest: input, - } - } - - return output, nil + return findNonDisassociatedGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) } func findGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, associatedGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { @@ -317,7 +304,7 @@ func findGatewayAssociationByGatewayIDAndAssociatedGatewayID(ctx context.Context DirectConnectGatewayId: aws.String(directConnectGatewayID), } - return findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) + return findNonDisassociatedGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) } func findGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, conn *directconnect.Client, directConnectGatewayID, virtualGatewayID string) (*awstypes.DirectConnectGatewayAssociation, error) { @@ -326,7 +313,24 @@ func findGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, c VirtualGatewayId: aws.String(virtualGatewayID), } - return findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) + return findNonDisassociatedGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) +} + +func findNonDisassociatedGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociation]) (*awstypes.DirectConnectGatewayAssociation, error) { + output, err := findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) + + if err != nil { + return nil, err + } + + if state := output.AssociationState; state == awstypes.DirectConnectGatewayAssociationStateDisassociated { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil } func findGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociation]) (*awstypes.DirectConnectGatewayAssociation, error) { diff --git a/internal/service/directconnect/gateway_association_proposal.go b/internal/service/directconnect/gateway_association_proposal.go index e12de41f576..4b3ba458514 100644 --- a/internal/service/directconnect/gateway_association_proposal.go +++ b/internal/service/directconnect/gateway_association_proposal.go @@ -14,16 +14,18 @@ import ( awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -// @SDKResource("aws_dx_gateway_association_proposal") -func ResourceGatewayAssociationProposal() *schema.Resource { +// @SDKResource("aws_dx_gateway_association_proposal", name="Gateway Association Proposal") +func resourceGatewayAssociationProposal() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceGatewayAssociationProposalCreate, ReadWithoutTimeout: resourceGatewayAssociationProposalRead, @@ -41,7 +43,7 @@ func ResourceGatewayAssociationProposal() *schema.Resource { log.Printf("[DEBUG] CustomizeDiff for Direct Connect Gateway Association Proposal (%s) allowed_prefixes", d.Id()) - output, err := FindGatewayAssociationProposalByID(ctx, conn, d.Id()) + output, err := findGatewayAssociationProposalByID(ctx, conn, d.Id()) if tfresource.NotFound(err) { // Proposal may be end-of-life and removed by AWS. @@ -64,29 +66,24 @@ func ResourceGatewayAssociationProposal() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "associated_gateway_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "associated_gateway_owner_account_id": { Type: schema.TypeString, Computed: true, }, - "associated_gateway_type": { Type: schema.TypeString, Computed: true, }, - "dx_gateway_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "dx_gateway_owner_account_id": { Type: schema.TypeString, Required: true, @@ -113,7 +110,6 @@ func resourceGatewayAssociationProposalCreate(ctx context.Context, d *schema.Res input.AddAllowedPrefixesToDirectConnectGateway = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect Gateway Association Proposal: %#v", input) output, err := conn.CreateDirectConnectGatewayAssociationProposal(ctx, input) if err != nil { @@ -130,7 +126,7 @@ func resourceGatewayAssociationProposalRead(ctx context.Context, d *schema.Resou conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) // First attempt to find by proposal ID. - output, err := FindGatewayAssociationProposalByID(ctx, conn, d.Id()) + output, err := findGatewayAssociationProposalByID(ctx, conn, d.Id()) if tfresource.NotFound(err) { // Attempt to find an existing association. @@ -200,6 +196,91 @@ func resourceGatewayAssociationProposalDelete(ctx context.Context, d *schema.Res return diags } +func resourceGatewayAssociationProposalImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + switch parts := strings.Split(strings.ToLower(d.Id()), "/"); len(parts) { + case 1: + break + + case 3: + proposalID := parts[0] + directConnectGatewayID := parts[1] + associatedGatewayID := parts[2] + + if proposalID == "" || directConnectGatewayID == "" || associatedGatewayID == "" { + return nil, fmt.Errorf("Incorrect resource ID format: %q. PROPOSALID, DXGATEWAYID and ASSOCIATEDGATEWAYID must not be empty strings", d.Id()) + } + + // Use pseudo-proposal ID and actual DirectConnectGatewayId and AssociatedGatewayId. + d.SetId(proposalID) + d.Set("associated_gateway_id", associatedGatewayID) + d.Set("dx_gateway_id", directConnectGatewayID) + + default: + return nil, fmt.Errorf("Incorrect resource ID format: %q. Expected PROPOSALID or PROPOSALID/DXGATEWAYID/ASSOCIATEDGATEWAYID", d.Id()) + } + + return []*schema.ResourceData{d}, nil +} + +func findGatewayAssociationProposalByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.DirectConnectGatewayAssociationProposal, error) { + input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{ + ProposalId: aws.String(id), + } + + output, err := findGatewayAssociationProposal(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociationProposal]()) + + if err != nil { + return nil, err + } + + if state := output.ProposalState; state == awstypes.DirectConnectGatewayAssociationProposalStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + if output.AssociatedGateway == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output, nil +} + +func findGatewayAssociationProposal(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationProposalsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociationProposal]) (*awstypes.DirectConnectGatewayAssociationProposal, error) { + output, err := findGatewayAssociationProposals(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findGatewayAssociationProposals(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationProposalsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociationProposal]) ([]awstypes.DirectConnectGatewayAssociationProposal, error) { + var output []awstypes.DirectConnectGatewayAssociationProposal + + err := describeDirectConnectGatewayAssociationProposalsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, v := range page.DirectConnectGatewayAssociationProposals { + if filter(&v) { + output = append(output, v) + } + } + + return !lastPage + }) + + if err != nil { + return nil, err + } + + return output, nil +} + func expandRouteFilterPrefixes(tfList []interface{}) []awstypes.RouteFilterPrefix { if len(tfList) == 0 { return nil @@ -209,7 +290,6 @@ func expandRouteFilterPrefixes(tfList []interface{}) []awstypes.RouteFilterPrefi for _, tfStringRaw := range tfList { tfString, ok := tfStringRaw.(string) - if !ok { continue } @@ -237,29 +317,3 @@ func flattenRouteFilterPrefixes(apiObjects []awstypes.RouteFilterPrefix) []inter return tfList } - -func resourceGatewayAssociationProposalImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - switch parts := strings.Split(strings.ToLower(d.Id()), "/"); len(parts) { - case 1: - break - - case 3: - proposalID := parts[0] - directConnectGatewayID := parts[1] - associatedGatewayID := parts[2] - - if proposalID == "" || directConnectGatewayID == "" || associatedGatewayID == "" { - return nil, fmt.Errorf("Incorrect resource ID format: %q. PROPOSALID, DXGATEWAYID and ASSOCIATEDGATEWAYID must not be empty strings", d.Id()) - } - - // Use pseudo-proposal ID and actual DirectConnectGatewayId and AssociatedGatewayId. - d.SetId(proposalID) - d.Set("associated_gateway_id", associatedGatewayID) - d.Set("dx_gateway_id", directConnectGatewayID) - - default: - return nil, fmt.Errorf("Incorrect resource ID format: %q. Expected PROPOSALID or PROPOSALID/DXGATEWAYID/ASSOCIATEDGATEWAYID", d.Id()) - } - - return []*schema.ResourceData{d}, nil -} diff --git a/internal/service/directconnect/gateway_association_proposal_test.go b/internal/service/directconnect/gateway_association_proposal_test.go index 136667da411..659e3076315 100644 --- a/internal/service/directconnect/gateway_association_proposal_test.go +++ b/internal/service/directconnect/gateway_association_proposal_test.go @@ -258,15 +258,11 @@ func testAccCheckGatewayAssociationProposalDestroy(ctx context.Context) resource } } -func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceName string, gatewayAssociationProposal *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { +func testAccCheckGatewayAssociationProposalExists(ctx context.Context, n string, v *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[resourceName] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) @@ -277,7 +273,7 @@ func testAccCheckGatewayAssociationProposalExists(ctx context.Context, resourceN return err } - *gatewayAssociationProposal = *output + *v = *output return nil } diff --git a/internal/service/directconnect/gateway_association_test.go b/internal/service/directconnect/gateway_association_test.go index ddf5cd58e0a..da210a5171d 100644 --- a/internal/service/directconnect/gateway_association_test.go +++ b/internal/service/directconnect/gateway_association_test.go @@ -389,15 +389,11 @@ func testAccCheckGatewayAssociationDestroy(ctx context.Context) resource.TestChe } } -func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga *awstypes.DirectConnectGatewayAssociation, gap *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { +func testAccCheckGatewayAssociationExists(ctx context.Context, n string, v *awstypes.DirectConnectGatewayAssociation, gap *awstypes.DirectConnectGatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) - } - - if rs.Primary.Attributes["dx_gateway_association_id"] == "" { - return fmt.Errorf("No Direct Connect Gateway Association ID is set") + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) @@ -418,7 +414,7 @@ func testAccCheckGatewayAssociationExists(ctx context.Context, name string, ga * *gap = *output } - *ga = *output + *v = *output return nil } @@ -442,10 +438,6 @@ func testAccCheckGatewayAssociationStateUpgradeV0(ctx context.Context, name stri return fmt.Errorf("Not found: %s", name) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - rawState := map[string]interface{}{ "dx_gateway_id": rs.Primary.Attributes["dx_gateway_id"], "vpn_gateway_id": rs.Primary.Attributes["associated_gateway_id"], // vpn_gateway_id was removed in 3.0, but older state still has it diff --git a/internal/service/directconnect/gateway_test.go b/internal/service/directconnect/gateway_test.go index d43d467ae84..58d7c8fb14d 100644 --- a/internal/service/directconnect/gateway_test.go +++ b/internal/service/directconnect/gateway_test.go @@ -159,11 +159,11 @@ func testAccCheckGatewayDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckGatewayExists(ctx context.Context, name string, v *awstypes.DirectConnectGateway) resource.TestCheckFunc { +func testAccCheckGatewayExists(ctx context.Context, n string, v *awstypes.DirectConnectGateway) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index ad21f56dd79..4c4617fbc67 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -85,8 +85,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Gateway Association", }, { - Factory: ResourceGatewayAssociationProposal, + Factory: resourceGatewayAssociationProposal, TypeName: "aws_dx_gateway_association_proposal", + Name: "Gateway Association Proposal", }, { Factory: ResourceHostedConnection, diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index 4f6c7a965c6..ad0633f8d7c 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -139,7 +139,7 @@ func sweepGatewayAssociationProposals(region string) error { continue } - r := ResourceGatewayAssociationProposal() + r := resourceGatewayAssociationProposal() d := r.Data(nil) d.SetId(proposalID) From 5e7c5a9ff889c9609cda356b6c0a996ced2fc31a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 5 Aug 2024 08:21:03 -0400 Subject: [PATCH 15/59] r/aws_dx_hosted_connection: Tidy up. --- .../directconnect/connection_association.go | 6 +- .../connection_confirmation_test.go | 30 +++--- .../service/directconnect/exports_test.go | 2 + internal/service/directconnect/find.go | 34 ------- .../directconnect/hosted_connection.go | 99 ++++++++++++++++++- .../directconnect/hosted_connection_test.go | 58 +++-------- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/status.go | 16 --- internal/service/directconnect/wait.go | 21 +--- 9 files changed, 130 insertions(+), 139 deletions(-) diff --git a/internal/service/directconnect/connection_association.go b/internal/service/directconnect/connection_association.go index 68a0a69a241..47ebbbba36e 100644 --- a/internal/service/directconnect/connection_association.go +++ b/internal/service/directconnect/connection_association.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" @@ -102,7 +103,10 @@ func deleteConnectionLAGAssociation(ctx context.Context, conn *directconnect.Cli LagId: aws.String(lagID), } - _, err := tfresource.RetryWhenIsAErrorMessageContains[*awstypes.DirectConnectClientException](ctx, connectionDisassociatedTimeout, + const ( + timeout = 1 * time.Minute + ) + _, err := tfresource.RetryWhenIsAErrorMessageContains[*awstypes.DirectConnectClientException](ctx, timeout, func() (interface{}, error) { return conn.DisassociateConnectionFromLag(ctx, input) }, "is in a transitioning state") diff --git a/internal/service/directconnect/connection_confirmation_test.go b/internal/service/directconnect/connection_confirmation_test.go index 89de35bc89f..65280d0d928 100644 --- a/internal/service/directconnect/connection_confirmation_test.go +++ b/internal/service/directconnect/connection_confirmation_test.go @@ -5,7 +5,6 @@ package directconnect_test import ( "context" - "errors" "fmt" "testing" @@ -22,13 +21,10 @@ import ( func TestAccDirectConnectConnectionConfirmation_basic(t *testing.T) { ctx := acctest.Context(t) - env, err := testAccCheckHostedConnectionEnv() - if err != nil { - acctest.Skip(t, err.Error()) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "TEST_AWS_DX_CONNECTION_ID") + ownerAccountID := acctest.SkipIfEnvVarNotSet(t, "TEST_AWS_DX_OWNER_ACCOUNT_ID") var providers []*schema.Provider - connectionName := fmt.Sprintf("tf-dx-%s", sdkacctest.RandString(5)) resourceName := "aws_dx_connection_confirmation.test" providerFunc := testAccConnectionConfirmationProvider(&providers, 0) @@ -44,22 +40,18 @@ func TestAccDirectConnectConnectionConfirmation_basic(t *testing.T) { CheckDestroy: testAccCheckHostedConnectionDestroy(ctx, altProviderFunc), Steps: []resource.TestStep{ { - Config: testAccConnectionConfirmationConfig_basic(connectionName, env.ConnectionId, env.OwnerAccountId), + Config: testAccConnectionConfirmationConfig_basic(connectionName, connectionID, ownerAccountID), Check: testAccCheckConnectionConfirmationExists(ctx, resourceName, providerFunc), }, }, }) } -func testAccCheckConnectionConfirmationExists(ctx context.Context, name string, providerFunc func() *schema.Provider) resource.TestCheckFunc { +func testAccCheckConnectionConfirmationExists(ctx context.Context, n string, providerFunc func() *schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) - } - - if rs.Primary.ID == "" { - return errors.New("No Direct Connect Connection ID is set") + return fmt.Errorf("Not found: %s", n) } provider := providerFunc() @@ -79,16 +71,16 @@ func testAccCheckConnectionConfirmationExists(ctx context.Context, name string, } } -func testAccConnectionConfirmationConfig_basic(name, connectionId, ownerAccountId string) string { +func testAccConnectionConfirmationConfig_basic(name, connectionID, ownerAccountID string) string { return acctest.ConfigCompose( acctest.ConfigAlternateAccountProvider(), fmt.Sprintf(` resource "aws_dx_hosted_connection" "connection" { provider = "awsalternate" - name = "%s" - connection_id = "%s" - owner_account_id = "%s" + name = %[1]q + connection_id = %[2]q + owner_account_id = %[3]q bandwidth = "100Mbps" vlan = 4092 } @@ -96,7 +88,7 @@ resource "aws_dx_hosted_connection" "connection" { resource "aws_dx_connection_confirmation" "test" { connection_id = aws_dx_hosted_connection.connection.id } -`, name, connectionId, ownerAccountId)) +`, name, connectionID, ownerAccountID)) } func testAccConnectionConfirmationProvider(providers *[]*schema.Provider, index int) func() *schema.Provider { diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 19b40ebbd23..e30ca98854a 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -12,6 +12,7 @@ var ( ResourceGateway = resourceGateway ResourceGatewayAssociation = resourceGatewayAssociation ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal + ResourceHostedConnection = resourceHostedConnection FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID @@ -19,6 +20,7 @@ var ( FindGatewayAssociationByID = findGatewayAssociationByID FindGatewayAssociationProposalByID = findGatewayAssociationProposalByID FindGatewayByID = findGatewayByID + FindHostedConnectionByID = findHostedConnectionByID FindVirtualInterfaceByID = findVirtualInterfaceByID GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 ValidConnectionBandWidth = validConnectionBandWidth diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index 037b4863405..bed0ef786e4 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -14,40 +14,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindHostedConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { - input := &directconnect.DescribeHostedConnectionsInput{ - ConnectionId: aws.String(id), - } - - output, err := conn.DescribeHostedConnections(ctx, input) - - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { - return nil, &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - connection, err := tfresource.AssertSingleValueResult(output.Connections) - - if err != nil { - return nil, err - } - - if connection.ConnectionState == awstypes.ConnectionStateDeleted || connection.ConnectionState == awstypes.ConnectionStateRejected { - return nil, &retry.NotFoundError{ - Message: string(connection.ConnectionState), - LastRequest: input, - } - } - - return connection, nil -} - func FindLagByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { input := &directconnect.DescribeLagsInput{ LagId: aws.String(id), diff --git a/internal/service/directconnect/hosted_connection.go b/internal/service/directconnect/hosted_connection.go index 7ec1b34ed93..e79ad56191c 100644 --- a/internal/service/directconnect/hosted_connection.go +++ b/internal/service/directconnect/hosted_connection.go @@ -10,18 +10,23 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_connection") -func ResourceHostedConnection() *schema.Resource { +// @SDKResource("aws_dx_hosted_connection", name="Hosted Connection") +func resourceHostedConnection() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedConnectionCreate, ReadWithoutTimeout: resourceHostedConnectionRead, @@ -113,7 +118,6 @@ func resourceHostedConnectionCreate(ctx context.Context, d *schema.ResourceData, Vlan: int32(d.Get("vlan").(int)), } - log.Printf("[DEBUG] Creating Direct Connect Hosted Connection: %#v", input) output, err := conn.AllocateHostedConnection(ctx, input) if err != nil { @@ -129,7 +133,7 @@ func resourceHostedConnectionRead(ctx context.Context, d *schema.ResourceData, m var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - connection, err := FindHostedConnectionByID(ctx, conn, d.Id()) + connection, err := findHostedConnectionByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect Hosted Connection (%s) not found, removing from state", d.Id()) @@ -171,3 +175,90 @@ func resourceHostedConnectionDelete(ctx context.Context, d *schema.ResourceData, return diags } + +func findHostedConnectionByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { + input := &directconnect.DescribeHostedConnectionsInput{ + ConnectionId: aws.String(id), + } + output, err := findHostedConnection(ctx, conn, input, tfslices.PredicateTrue[*awstypes.Connection]()) + + if err != nil { + return nil, err + } + + if state := output.ConnectionState; state == awstypes.ConnectionStateDeleted || state == awstypes.ConnectionStateRejected { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findHostedConnection(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeHostedConnectionsInput, filter tfslices.Predicate[*awstypes.Connection]) (*awstypes.Connection, error) { + output, err := findHostedConnections(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findHostedConnections(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeHostedConnectionsInput, filter tfslices.Predicate[*awstypes.Connection]) ([]awstypes.Connection, error) { + output, err := conn.DescribeHostedConnections(ctx, input) + + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Connection with ID") { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return tfslices.Filter(output.Connections, tfslices.PredicateValue(filter)), nil +} + +func statusHostedConnection(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findHostedConnectionByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.ConnectionState), nil + } +} + +func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { + const ( + timeout = 10 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), + Target: []string{}, + Refresh: statusHostedConnection(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Connection); ok { + return output, err + } + + return nil, err +} diff --git a/internal/service/directconnect/hosted_connection_test.go b/internal/service/directconnect/hosted_connection_test.go index 7fa3232ea19..32e0d2edff5 100644 --- a/internal/service/directconnect/hosted_connection_test.go +++ b/internal/service/directconnect/hosted_connection_test.go @@ -5,9 +5,7 @@ package directconnect_test import ( "context" - "errors" "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -21,17 +19,10 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -type testAccDxHostedConnectionEnv struct { - ConnectionId string - OwnerAccountId string -} - func TestAccDirectConnectHostedConnection_basic(t *testing.T) { ctx := acctest.Context(t) - env, err := testAccCheckHostedConnectionEnv() - if err != nil { - acctest.Skip(t, err.Error()) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "TEST_AWS_DX_CONNECTION_ID") + ownerAccountID := acctest.SkipIfEnvVarNotSet(t, "TEST_AWS_DX_OWNER_ACCOUNT_ID") connectionName := fmt.Sprintf("tf-dx-%s", sdkacctest.RandString(5)) resourceName := "aws_dx_hosted_connection.test" @@ -40,15 +31,15 @@ func TestAccDirectConnectHostedConnection_basic(t *testing.T) { PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: testAccCheckHostedConnectionDestroy(ctx, testAccHostedConnectionProvider), + CheckDestroy: testAccCheckHostedConnectionDestroy(ctx, func() *schema.Provider { return acctest.Provider }), Steps: []resource.TestStep{ { - Config: testAccHostedConnectionConfig_basic(connectionName, env.ConnectionId, env.OwnerAccountId), + Config: testAccHostedConnectionConfig_basic(connectionName, connectionID, ownerAccountID), Check: resource.ComposeTestCheckFunc( testAccCheckHostedConnectionExists(ctx, resourceName), resource.TestCheckResourceAttr(resourceName, names.AttrName, connectionName), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, env.ConnectionId), - resource.TestCheckResourceAttr(resourceName, names.AttrOwnerAccountID, env.OwnerAccountId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), + resource.TestCheckResourceAttr(resourceName, names.AttrOwnerAccountID, ownerAccountID), resource.TestCheckResourceAttr(resourceName, "bandwidth", "100Mbps"), resource.TestCheckResourceAttr(resourceName, "vlan", "4094"), ), @@ -57,19 +48,6 @@ func TestAccDirectConnectHostedConnection_basic(t *testing.T) { }) } -func testAccCheckHostedConnectionEnv() (*testAccDxHostedConnectionEnv, error) { - result := &testAccDxHostedConnectionEnv{ - ConnectionId: os.Getenv("TEST_AWS_DX_CONNECTION_ID"), - OwnerAccountId: os.Getenv("TEST_AWS_DX_OWNER_ACCOUNT_ID"), - } - - if result.ConnectionId == "" || result.OwnerAccountId == "" { - return nil, errors.New("TEST_AWS_DX_CONNECTION_ID and TEST_AWS_DX_OWNER_ACCOUNT_ID must be set for tests involving hosted connections") - } - - return result, nil -} - func testAccCheckHostedConnectionDestroy(ctx context.Context, providerFunc func() *schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { provider := providerFunc() @@ -97,17 +75,13 @@ func testAccCheckHostedConnectionDestroy(ctx context.Context, providerFunc func( } } -func testAccCheckHostedConnectionExists(ctx context.Context, name string) resource.TestCheckFunc { +func testAccCheckHostedConnectionExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) - } - - if rs.Primary.ID == "" { - return errors.New("No Direct Connect Hosted Connection ID is set") + return fmt.Errorf("Not found: %s", n) } _, err := tfdirectconnect.FindHostedConnectionByID(ctx, conn, rs.Primary.ID) @@ -116,18 +90,14 @@ func testAccCheckHostedConnectionExists(ctx context.Context, name string) resour } } -func testAccHostedConnectionConfig_basic(name, connectionId, ownerAccountId string) string { +func testAccHostedConnectionConfig_basic(name, connectionID, ownerAccountID string) string { return fmt.Sprintf(` resource "aws_dx_hosted_connection" "test" { - name = "%s" - connection_id = "%s" - owner_account_id = "%s" + name = %[1]q + connection_id = %[2]q + owner_account_id = %[3]q bandwidth = "100Mbps" vlan = 4094 } -`, name, connectionId, ownerAccountId) -} - -func testAccHostedConnectionProvider() *schema.Provider { - return acctest.Provider +`, name, connectionID, ownerAccountID) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 4c4617fbc67..552bd19c289 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -90,8 +90,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Gateway Association Proposal", }, { - Factory: ResourceHostedConnection, + Factory: resourceHostedConnection, TypeName: "aws_dx_hosted_connection", + Name: "Hosted Connection", }, { Factory: ResourceHostedPrivateVirtualInterface, diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go index 7f8f998a365..9aa4c870b0d 100644 --- a/internal/service/directconnect/status.go +++ b/internal/service/directconnect/status.go @@ -11,22 +11,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func statusHostedConnectionState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindHostedConnectionByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, string(output.ConnectionState), nil - } -} - func statusLagState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindLagByID(ctx, conn, id) diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go index 7777b56575e..86973fb997f 100644 --- a/internal/service/directconnect/wait.go +++ b/internal/service/directconnect/wait.go @@ -14,28 +14,9 @@ import ( ) const ( - connectionDisassociatedTimeout = 1 * time.Minute - hostedConnectionDeletedTimeout = 10 * time.Minute - lagDeletedTimeout = 10 * time.Minute + lagDeletedTimeout = 10 * time.Minute ) -func waitHostedConnectionDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Connection, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.ConnectionStatePending, awstypes.ConnectionStateOrdering, awstypes.ConnectionStateAvailable, awstypes.ConnectionStateRequested, awstypes.ConnectionStateDeleting), - Target: []string{}, - Refresh: statusHostedConnectionState(ctx, conn, id), - Timeout: hostedConnectionDeletedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.Connection); ok { - return output, err - } - - return nil, err -} - func waitLagDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.LagStateAvailable, awstypes.LagStateRequested, awstypes.LagStatePending, awstypes.LagStateDeleting), From d4a3f1071055e7cd027415626d20ff283017f0a5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 6 Aug 2024 10:58:33 -0400 Subject: [PATCH 16/59] r/aws_dx_hosted_private_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 17 ++--- .../hosted_private_virtual_interface.go | 69 +++++++++++-------- .../hosted_private_virtual_interface_test.go | 46 +++++-------- .../directconnect/service_package_gen.go | 3 +- internal/service/directconnect/vif.go | 35 ++++++++++ 5 files changed, 103 insertions(+), 67 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index e30ca98854a..08fad9a25e6 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,14 +5,15 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer - ResourceConnection = resourceConnection - ResourceConnectionAssociation = resourceConnectionAssociation - ResourceConnectionConfirmation = resourceConnectionConfirmation - ResourceGateway = resourceGateway - ResourceGatewayAssociation = resourceGatewayAssociation - ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal - ResourceHostedConnection = resourceHostedConnection + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection + ResourceConnectionAssociation = resourceConnectionAssociation + ResourceConnectionConfirmation = resourceConnectionConfirmation + ResourceGateway = resourceGateway + ResourceGatewayAssociation = resourceGatewayAssociation + ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal + ResourceHostedConnection = resourceHostedConnection + ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/hosted_private_virtual_interface.go b/internal/service/directconnect/hosted_private_virtual_interface.go index c89f086e475..434f918f3ad 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface.go +++ b/internal/service/directconnect/hosted_private_virtual_interface.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "strconv" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -20,16 +19,19 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/enum" "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" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_private_virtual_interface") -func ResourceHostedPrivateVirtualInterface() *schema.Resource { +// @SDKResource("aws_dx_hosted_private_virtual_interface", name="Hosted Private Virtual Interface") +func resourceHostedPrivateVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedPrivateVirtualInterfaceCreate, ReadWithoutTimeout: resourceHostedPrivateVirtualInterfaceRead, DeleteWithoutTimeout: resourceHostedPrivateVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedPrivateVirtualInterfaceImport, }, @@ -122,7 +124,7 @@ func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema. var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - req := &directconnect.AllocatePrivateVirtualInterfaceInput{ + input := &directconnect.AllocatePrivateVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPrivateVirtualInterfaceAllocation: &awstypes.NewPrivateVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), @@ -133,29 +135,33 @@ func resourceHostedPrivateVirtualInterfaceCreate(ctx context.Context, d *schema. }, OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) + input.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewPrivateVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) + input.NewPrivateVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) + input.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("mtu"); ok { - req.NewPrivateVirtualInterfaceAllocation.Mtu = aws.Int32(int32(v.(int))) + input.NewPrivateVirtualInterfaceAllocation.Mtu = aws.Int32(int32(v.(int))) } - log.Printf("[DEBUG] Creating Direct Connect hosted private virtual interface: %#v", req) - resp, err := conn.AllocatePrivateVirtualInterface(ctx, req) + output, err := conn.AllocatePrivateVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect hosted private virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Hosted Private Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterfaceId)) - if err := hostedPrivateVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitHostedPrivateVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Hosted Private Virtual Interface (%s) create: %s", d.Id(), err) } return append(diags, resourceHostedPrivateVirtualInterfaceRead(ctx, d, meta)...) @@ -165,19 +171,21 @@ func resourceHostedPrivateVirtualInterfaceRead(ctx context.Context, d *schema.Re var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect hosted private virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Private Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Private Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", flex.Int64ToStringValue(vif.AmazonSideAsn)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -207,13 +215,11 @@ func resourceHostedPrivateVirtualInterfaceDelete(ctx context.Context, d *schema. func resourceHostedPrivateVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -222,10 +228,13 @@ func resourceHostedPrivateVirtualInterfaceImport(ctx context.Context, d *schema. return []*schema.ResourceData{d}, nil } -func hostedPrivateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitHostedPrivateVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/hosted_private_virtual_interface_test.go b/internal/service/directconnect/hosted_private_virtual_interface_test.go index 7c488d37f01..3415949b58c 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_private_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -22,11 +21,7 @@ import ( func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_private_virtual_interface.test" @@ -46,8 +41,8 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckHostedPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedPrivateVirtualInterfaceConfig_basic(connectionId, rName, bgpAsn, vlan), - Check: resource.ComposeTestCheckFunc( + Config: testAccHostedPrivateVirtualInterfaceConfig_basic(connectionID, rName, bgpAsn, vlan), + Check: resource.ComposeAggregateTestCheckFunc( testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), resource.TestCheckResourceAttrSet(resourceName, "amazon_side_asn"), @@ -55,7 +50,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -68,9 +63,8 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrPair(accepterResourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, - // Test import. { - Config: testAccHostedPrivateVirtualInterfaceConfig_basic(connectionId, rName, bgpAsn, vlan), + Config: testAccHostedPrivateVirtualInterfaceConfig_basic(connectionID, rName, bgpAsn, vlan), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -81,11 +75,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_basic(t *testing.T) { func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_private_virtual_interface.test" @@ -105,7 +95,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T CheckDestroy: testAccCheckHostedPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedPrivateVirtualInterfaceConfig_accepterTags(connectionId, rName, bgpAsn, vlan), + Config: testAccHostedPrivateVirtualInterfaceConfig_accepterTags(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -114,7 +104,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -131,7 +121,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T ), }, { - Config: testAccHostedPrivateVirtualInterfaceConfig_accepterTagsUpdated(connectionId, rName, bgpAsn, vlan), + Config: testAccHostedPrivateVirtualInterfaceConfig_accepterTagsUpdated(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -140,7 +130,7 @@ func TestAccDirectConnectHostedPrivateVirtualInterface_accepterTags(t *testing.T resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -171,7 +161,7 @@ func testAccCheckHostedPrivateVirtualInterfaceExists(ctx context.Context, name s } func testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName string, bgpAsn, vlan int) string { - return acctest.ConfigAlternateAccountProvider() + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAlternateAccountProvider(), fmt.Sprintf(` # Creator resource "aws_dx_hosted_private_virtual_interface" "test" { address_family = "ipv4" @@ -198,22 +188,22 @@ resource "aws_vpn_gateway" "test" { Name = %[2]q } } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccHostedPrivateVirtualInterfaceConfig_basic(cid, rName string, bgpAsn, vlan int) string { - return testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan) + ` + return acctest.ConfigCompose(testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan), ` resource "aws_dx_hosted_private_virtual_interface_accepter" "test" { provider = "awsalternate" virtual_interface_id = aws_dx_hosted_private_virtual_interface.test.id vpn_gateway_id = aws_vpn_gateway.test.id } -` +`) } func testAccHostedPrivateVirtualInterfaceConfig_accepterTags(cid, rName string, bgpAsn, vlan int) string { - return testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_private_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -226,11 +216,11 @@ resource "aws_dx_hosted_private_virtual_interface_accepter" "test" { Key2 = "Value2a" } } -`, rName) +`, rName)) } func testAccHostedPrivateVirtualInterfaceConfig_accepterTagsUpdated(cid, rName string, bgpAsn, vlan int) string { - return testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedPrivateVirtualInterfaceConfig_base(cid, rName, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_private_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -243,5 +233,5 @@ resource "aws_dx_hosted_private_virtual_interface_accepter" "test" { Key3 = "Value3" } } -`, rName) +`, rName)) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 552bd19c289..248f8c23d45 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -95,8 +95,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Hosted Connection", }, { - Factory: ResourceHostedPrivateVirtualInterface, + Factory: resourceHostedPrivateVirtualInterface, TypeName: "aws_dx_hosted_private_virtual_interface", + Name: "Hosted Private Virtual Interface", }, { Factory: ResourceHostedPrivateVirtualInterfaceAccepter, diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index 852619e69fe..0ae5b607281 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -186,3 +186,38 @@ func findVirtualInterfaces(ctx context.Context, conn *directconnect.Client, inpu return tfslices.Filter(output.VirtualInterfaces, tfslices.PredicateValue(filter)), nil } + +func statusVirtualInterface(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findVirtualInterfaceByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.VirtualInterfaceState), nil + } +} + +func waitVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, pending, target []string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + stateConf := &retry.StateChangeConf{ + Pending: pending, + Target: target, + Refresh: statusVirtualInterface(ctx, conn, id), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.VirtualInterface); ok { + return output, err + } + + return nil, err +} From 6f3277fe20a905193258adb24fa0fb1bf1322156 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 6 Aug 2024 11:29:02 -0400 Subject: [PATCH 17/59] r/aws_dx_hosted_private_virtual_interface_accepter: Tidy up. --- .../service/directconnect/exports_test.go | 19 ++-- ...sted_private_virtual_interface_accepter.go | 103 +++++++++--------- .../directconnect/service_package_gen.go | 4 +- internal/service/directconnect/vif.go | 78 +++++++------ 4 files changed, 108 insertions(+), 96 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 08fad9a25e6..c3de4d9a4a8 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -5,15 +5,16 @@ package directconnect // Exports for use in tests only. var ( - ResourceBGPPeer = resourceBGPPeer - ResourceConnection = resourceConnection - ResourceConnectionAssociation = resourceConnectionAssociation - ResourceConnectionConfirmation = resourceConnectionConfirmation - ResourceGateway = resourceGateway - ResourceGatewayAssociation = resourceGatewayAssociation - ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal - ResourceHostedConnection = resourceHostedConnection - ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface + ResourceBGPPeer = resourceBGPPeer + ResourceConnection = resourceConnection + ResourceConnectionAssociation = resourceConnectionAssociation + ResourceConnectionConfirmation = resourceConnectionConfirmation + ResourceGateway = resourceGateway + ResourceGatewayAssociation = resourceGatewayAssociation + ResourceGatewayAssociationProposal = resourceGatewayAssociationProposal + ResourceHostedConnection = resourceHostedConnection + ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface + ResourceHostedPrivateVirtualInterfaceAccepter = resourceHostedPrivateVirtualInterfaceAccepter FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/hosted_private_virtual_interface_accepter.go b/internal/service/directconnect/hosted_private_virtual_interface_accepter.go index 2ff435299e3..9ca1521deb6 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_private_virtual_interface_accepter.go @@ -19,18 +19,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_private_virtual_interface_accepter", name="Hosted Private Virtual Interface") +// @SDKResource("aws_dx_hosted_private_virtual_interface_accepter", name="Hosted Private Virtual Interface Accepter") // @Tags(identifierAttribute="arn") -func ResourceHostedPrivateVirtualInterfaceAccepter() *schema.Resource { +func resourceHostedPrivateVirtualInterfaceAccepter() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedPrivateVirtualInterfaceAccepterCreate, ReadWithoutTimeout: resourceHostedPrivateVirtualInterfaceAccepterRead, UpdateWithoutTimeout: resourceHostedPrivateVirtualInterfaceAccepterUpdate, - DeleteWithoutTimeout: resourceHostedPrivateVirtualInterfaceAccepterDelete, + DeleteWithoutTimeout: schema.NoopContext, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedPrivateVirtualInterfaceAccepterImport, }, @@ -41,10 +43,10 @@ func ResourceHostedPrivateVirtualInterfaceAccepter() *schema.Resource { Computed: true, }, "dx_gateway_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ConflictsWith: []string{"vpn_gateway_id"}, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ExactlyOneOf: []string{"dx_gateway_id", "vpn_gateway_id"}, }, names.AttrTags: tftags.TagsSchema(), names.AttrTagsAll: tftags.TagsSchemaComputed(), @@ -54,10 +56,10 @@ func ResourceHostedPrivateVirtualInterfaceAccepter() *schema.Resource { ForceNew: true, }, "vpn_gateway_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ConflictsWith: []string{"dx_gateway_id"}, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ExactlyOneOf: []string{"dx_gateway_id", "vpn_gateway_id"}, }, }, @@ -74,30 +76,26 @@ func resourceHostedPrivateVirtualInterfaceAccepterCreate(ctx context.Context, d var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id") - dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id") - if vgwOk == dxgwOk { - return sdkdiag.AppendErrorf(diags, "One of ['vpn_gateway_id', 'dx_gateway_id'] must be set to create a Direct Connect private virtual interface accepter") + vifID := d.Get("virtual_interface_id").(string) + input := &directconnect.ConfirmPrivateVirtualInterfaceInput{ + VirtualInterfaceId: aws.String(vifID), } - vifId := d.Get("virtual_interface_id").(string) - req := &directconnect.ConfirmPrivateVirtualInterfaceInput{ - VirtualInterfaceId: aws.String(vifId), - } - if dxgwOk && dxgwIdRaw.(string) != "" { - req.DirectConnectGatewayId = aws.String(dxgwIdRaw.(string)) + if v, ok := d.GetOk("dx_gateway_id"); ok { + input.DirectConnectGatewayId = aws.String(v.(string)) } - if vgwOk && vgwIdRaw.(string) != "" { - req.VirtualGatewayId = aws.String(vgwIdRaw.(string)) + + if v, ok := d.GetOk("vpn_gateway_id"); ok { + input.VirtualGatewayId = aws.String(v.(string)) } - log.Printf("[DEBUG] Accepting Direct Connect hosted private virtual interface: %#v", req) - _, err := conn.ConfirmPrivateVirtualInterface(ctx, req) + _, err := conn.ConfirmPrivateVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted private virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "accepting Direct Connect Hosted Private Virtual Interface (%s): %s", vifID, err) } - d.SetId(vifId) + d.SetId(vifID) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -107,12 +105,12 @@ func resourceHostedPrivateVirtualInterfaceAccepterCreate(ctx context.Context, d }.String() d.Set(names.AttrARN, arn) - if err := hostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitHostedPrivateVirtualInterfaceAccepterAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Hosted Private Virtual Interface Accepter (%s) create: %s", d.Id(), err) } if err := createTags(ctx, conn, arn, getTagsIn(ctx)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting Direct Connect hosted private virtual interface (%s) tags: %s", arn, err) + return sdkdiag.AppendErrorf(diags, "setting Direct Connect Hosted Private Virtual Interface (%s) tags: %s", arn, err) } return append(diags, resourceHostedPrivateVirtualInterfaceAccepterUpdate(ctx, d, meta)...) @@ -122,18 +120,20 @@ func resourceHostedPrivateVirtualInterfaceAccepterRead(ctx context.Context, d *s var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect hosted private virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Private Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && - vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown { - log.Printf("[WARN] Direct Connect hosted private virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Private Virtual Interface (%s): %s", d.Id(), err) + } + + if state := vif.VirtualInterfaceState; state != awstypes.VirtualInterfaceStateAvailable && state != awstypes.VirtualInterfaceStateDown { + log.Printf("[WARN] Direct Connect Hosted Private Virtual Interface (%s) is '%s', removing from state", d.Id(), state) d.SetId("") return diags } @@ -156,22 +156,14 @@ func resourceHostedPrivateVirtualInterfaceAccepterUpdate(ctx context.Context, d return append(diags, resourceHostedPrivateVirtualInterfaceAccepterRead(ctx, d, meta)...) } -func resourceHostedPrivateVirtualInterfaceAccepterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics - log.Printf("[WARN] Will not delete Direct Connect virtual interface. Terraform will remove this resource from the state file, however resources may remain.") - return diags -} - func resourceHostedPrivateVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -189,10 +181,13 @@ func resourceHostedPrivateVirtualInterfaceAccepterImport(ctx context.Context, d return []*schema.ResourceData{d}, nil } -func hostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitHostedPrivateVirtualInterfaceAccepterAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 248f8c23d45..ed3b5a3a7bf 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -100,9 +100,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Hosted Private Virtual Interface", }, { - Factory: ResourceHostedPrivateVirtualInterfaceAccepter, + Factory: resourceHostedPrivateVirtualInterfaceAccepter, TypeName: "aws_dx_hosted_private_virtual_interface_accepter", - Name: "Hosted Private Virtual Interface", + Name: "Hosted Private Virtual Interface Accepter", Tags: &types.ServicePackageResourceTags{ IdentifierAttribute: names.AttrARN, }, diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index 0ae5b607281..93bc831dbfd 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -40,25 +40,28 @@ func virtualInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta in conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.HasChange("mtu") { - req := &directconnect.UpdateVirtualInterfaceAttributesInput{ + input := &directconnect.UpdateVirtualInterfaceAttributesInput{ Mtu: aws.Int32(int32(d.Get("mtu").(int))), VirtualInterfaceId: aws.String(d.Id()), } - log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %#v", req) - _, err := conn.UpdateVirtualInterfaceAttributes(ctx, req) + + _, err := conn.UpdateVirtualInterfaceAttributes(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "modifying Direct Connect virtual interface (%s) attributes: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "updating Direct Connect Virtual Interface (%s) Mtu attribute: %s", d.Id(), err) } } + if d.HasChange("sitelink_enabled") { - req := &directconnect.UpdateVirtualInterfaceAttributesInput{ + input := &directconnect.UpdateVirtualInterfaceAttributesInput{ EnableSiteLink: aws.Bool(d.Get("sitelink_enabled").(bool)), VirtualInterfaceId: aws.String(d.Id()), } - log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %#v", req) - _, err := conn.UpdateVirtualInterfaceAttributes(ctx, req) + + _, err := conn.UpdateVirtualInterfaceAttributes(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "modifying Direct Connect virtual interface (%s) attributes: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "modifying Direct Connect Virtual Interface (%s) EnableSiteLink attribute: %s", d.Id(), err) } } @@ -69,35 +72,21 @@ func virtualInterfaceDelete(ctx context.Context, d *schema.ResourceData, meta in var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - log.Printf("[DEBUG] Deleting Direct Connect virtual interface: %s", d.Id()) + log.Printf("[DEBUG] Deleting Direct Connect Virtual Interface: %s", d.Id()) _, err := conn.DeleteVirtualInterface(ctx, &directconnect.DeleteVirtualInterfaceInput{ VirtualInterfaceId: aws.String(d.Id()), }) - if err != nil { - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { - return diags - } - return sdkdiag.AppendErrorf(diags, "deleting Direct Connect virtual interface (%s): %s", d.Id(), err) - } - deleteStateConf := &retry.StateChangeConf{ - Pending: enum.Slice( - awstypes.VirtualInterfaceStateAvailable, - awstypes.VirtualInterfaceStateConfirming, - awstypes.VirtualInterfaceStateDeleting, - awstypes.VirtualInterfaceStateDown, - awstypes.VirtualInterfaceStatePending, - awstypes.VirtualInterfaceStateRejected, - awstypes.VirtualInterfaceStateVerifying), - Target: enum.Slice(awstypes.VirtualInterfaceStateDeleted), - Refresh: virtualInterfaceStateRefresh(ctx, conn, d.Id()), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 10 * time.Second, - MinTimeout: 5 * time.Second, + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "does not exist") { + return diags } - _, err = deleteStateConf.WaitForStateContext(ctx) + if err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect virtual interface (%s) to be deleted: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "deleting Direct Connect Virtual Interface (%s): %s", d.Id(), err) + } + + if _, err := waitVirtualInterfaceDeleted(ctx, conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Virtual Interface (%s) delete: %s", d.Id(), err) } return diags @@ -221,3 +210,30 @@ func waitVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Clie return nil, err } + +func waitVirtualInterfaceDeleted(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice( + awstypes.VirtualInterfaceStateAvailable, + awstypes.VirtualInterfaceStateConfirming, + awstypes.VirtualInterfaceStateDeleting, + awstypes.VirtualInterfaceStateDown, + awstypes.VirtualInterfaceStatePending, + awstypes.VirtualInterfaceStateRejected, + awstypes.VirtualInterfaceStateVerifying, + ), + Target: []string{}, + Refresh: statusVirtualInterface(ctx, conn, id), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.VirtualInterface); ok { + return output, err + } + + return nil, err +} From e4f2bba2b215996bbb19353dd731c1bdd2357524 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 6 Aug 2024 11:40:31 -0400 Subject: [PATCH 18/59] r/aws_dx_hosted_public_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 1 + .../hosted_public_virtual_interface.go | 64 +++++++++++-------- .../hosted_public_virtual_interface_test.go | 44 +++++-------- .../directconnect/service_package_gen.go | 3 +- 4 files changed, 57 insertions(+), 55 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index c3de4d9a4a8..a398c0b38d3 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -15,6 +15,7 @@ var ( ResourceHostedConnection = resourceHostedConnection ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface ResourceHostedPrivateVirtualInterfaceAccepter = resourceHostedPrivateVirtualInterfaceAccepter + ResourceHostedPublicVirtualInterface = resourceHostedPublicVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/hosted_public_virtual_interface.go b/internal/service/directconnect/hosted_public_virtual_interface.go index ea4263ca19f..f03144f8d84 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface.go +++ b/internal/service/directconnect/hosted_public_virtual_interface.go @@ -20,19 +20,22 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/enum" "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" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_public_virtual_interface") -func ResourceHostedPublicVirtualInterface() *schema.Resource { +// @SDKResource("aws_dx_hosted_public_virtual_interface", name="Hosted Public Virtual Interface") +func resourceHostedPublicVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedPublicVirtualInterfaceCreate, ReadWithoutTimeout: resourceHostedPublicVirtualInterfaceRead, DeleteWithoutTimeout: resourceHostedPublicVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedPublicVirtualInterfaceImport, }, + CustomizeDiff: resourceHostedPublicVirtualInterfaceCustomizeDiff, Schema: map[string]*schema.Schema{ @@ -119,7 +122,7 @@ func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.R var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - req := &directconnect.AllocatePublicVirtualInterfaceInput{ + input := &directconnect.AllocatePublicVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPublicVirtualInterfaceAllocation: &awstypes.NewPublicVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), @@ -129,28 +132,32 @@ func resourceHostedPublicVirtualInterfaceCreate(ctx context.Context, d *schema.R }, OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewPublicVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) + input.NewPublicVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewPublicVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) + input.NewPublicVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewPublicVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) + input.NewPublicVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("route_filter_prefixes"); ok { - req.NewPublicVirtualInterfaceAllocation.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) + input.NewPublicVirtualInterfaceAllocation.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Allocating Direct Connect hosted public virtual interface: %#v", req) - resp, err := conn.AllocatePublicVirtualInterface(ctx, req) + output, err := conn.AllocatePublicVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "allocating Direct Connect hosted public virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Hosted Public Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterfaceId)) - if err := hostedPublicVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + if _, err := waitHostedPublicVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendFromErr(diags, err) } @@ -161,16 +168,18 @@ func resourceHostedPublicVirtualInterfaceRead(ctx context.Context, d *schema.Res var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Public Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Public Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) @@ -204,13 +213,11 @@ func resourceHostedPublicVirtualInterfaceDelete(ctx context.Context, d *schema.R func resourceHostedPublicVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -235,10 +242,13 @@ func resourceHostedPublicVirtualInterfaceCustomizeDiff(_ context.Context, diff * return nil } -func hostedPublicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitHostedPublicVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying), + timeout, + ) } diff --git a/internal/service/directconnect/hosted_public_virtual_interface_test.go b/internal/service/directconnect/hosted_public_virtual_interface_test.go index 89a7b259bb4..e52ad94602b 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_public_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -22,11 +21,7 @@ import ( func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_public_virtual_interface.test" @@ -47,7 +42,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckHostedPublicVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedPublicVirtualInterfaceConfig_basic(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccHostedPublicVirtualInterfaceConfig_basic(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -57,7 +52,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -70,9 +65,8 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, - // Test import. { - Config: testAccHostedPublicVirtualInterfaceConfig_basic(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccHostedPublicVirtualInterfaceConfig_basic(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -83,11 +77,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_basic(t *testing.T) { func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_public_virtual_interface.test" @@ -108,7 +98,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) CheckDestroy: testAccCheckHostedPublicVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedPublicVirtualInterfaceConfig_accepterTags(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccHostedPublicVirtualInterfaceConfig_accepterTags(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -118,7 +108,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -135,7 +125,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) ), }, { - Config: testAccHostedPublicVirtualInterfaceConfig_accepterTagsUpdated(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccHostedPublicVirtualInterfaceConfig_accepterTagsUpdated(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -145,7 +135,7 @@ func TestAccDirectConnectHostedPublicVirtualInterface_accepterTags(t *testing.T) resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -176,7 +166,7 @@ func testAccCheckHostedPublicVirtualInterfaceExists(ctx context.Context, name st } func testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr string, bgpAsn, vlan int) string { - return acctest.ConfigAlternateAccountProvider() + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAlternateAccountProvider(), fmt.Sprintf(` # Creator resource "aws_dx_hosted_public_virtual_interface" "test" { address_family = "ipv4" @@ -198,21 +188,21 @@ resource "aws_dx_hosted_public_virtual_interface" "test" { data "aws_caller_identity" "accepter" { provider = "awsalternate" } -`, cid, rName, amzAddr, custAddr, bgpAsn, vlan) +`, cid, rName, amzAddr, custAddr, bgpAsn, vlan)) } func testAccHostedPublicVirtualInterfaceConfig_basic(cid, rName, amzAddr, custAddr string, bgpAsn, vlan int) string { - return testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan) + ` + return acctest.ConfigCompose(testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan), ` resource "aws_dx_hosted_public_virtual_interface_accepter" "test" { provider = "awsalternate" virtual_interface_id = aws_dx_hosted_public_virtual_interface.test.id } -` +`) } func testAccHostedPublicVirtualInterfaceConfig_accepterTags(cid, rName, amzAddr, custAddr string, bgpAsn, vlan int) string { - return testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_public_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -224,11 +214,11 @@ resource "aws_dx_hosted_public_virtual_interface_accepter" "test" { Key2 = "Value2a" } } -`, rName) +`, rName)) } func testAccHostedPublicVirtualInterfaceConfig_accepterTagsUpdated(cid, rName, amzAddr, custAddr string, bgpAsn, vlan int) string { - return testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedPublicVirtualInterfaceConfig_base(cid, rName, amzAddr, custAddr, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_public_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -240,5 +230,5 @@ resource "aws_dx_hosted_public_virtual_interface_accepter" "test" { Key3 = "Value3" } } -`, rName) +`, rName)) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index ed3b5a3a7bf..060b59f640e 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -108,8 +108,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceHostedPublicVirtualInterface, + Factory: resourceHostedPublicVirtualInterface, TypeName: "aws_dx_hosted_public_virtual_interface", + Name: "Hosted Public Virtual Interface", }, { Factory: ResourceHostedPublicVirtualInterfaceAccepter, From 67e920d056246aedab48b6df11d8f460778f2883 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 11:14:58 -0400 Subject: [PATCH 19/59] r/aws_dx_hosted_transit_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 1 + .../hosted_transit_virtual_interface.go | 68 +++++++++++-------- .../hosted_transit_virtual_interface_test.go | 48 ++++++------- .../directconnect/service_package_gen.go | 3 +- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index a398c0b38d3..27a55b2184f 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -16,6 +16,7 @@ var ( ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface ResourceHostedPrivateVirtualInterfaceAccepter = resourceHostedPrivateVirtualInterfaceAccepter ResourceHostedPublicVirtualInterface = resourceHostedPublicVirtualInterface + ResourceHostedTransitVirtualInterface = resourceHostedTransitVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/hosted_transit_virtual_interface.go b/internal/service/directconnect/hosted_transit_virtual_interface.go index 609872d4d91..78bbe396cc0 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "strconv" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -20,16 +19,19 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/enum" "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" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_transit_virtual_interface") -func ResourceHostedTransitVirtualInterface() *schema.Resource { +// @SDKResource("aws_dx_hosted_transit_virtual_interface", name="Hosted Transit Virtual Interface") +func resourceHostedTransitVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedTransitVirtualInterfaceCreate, ReadWithoutTimeout: resourceHostedTransitVirtualInterfaceRead, DeleteWithoutTimeout: resourceHostedTransitVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedTransitVirtualInterfaceImport, }, @@ -122,9 +124,8 @@ func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema. var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - req := &directconnect.AllocateTransitVirtualInterfaceInput{ + input := &directconnect.AllocateTransitVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), - OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), NewTransitVirtualInterfaceAllocation: &awstypes.NewTransitVirtualInterfaceAllocation{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), Asn: int32(d.Get("bgp_asn").(int)), @@ -132,27 +133,31 @@ func resourceHostedTransitVirtualInterfaceCreate(ctx context.Context, d *schema. VirtualInterfaceName: aws.String(d.Get(names.AttrName).(string)), Vlan: int32(d.Get("vlan").(int)), }, + OwnerAccount: aws.String(d.Get(names.AttrOwnerAccountID).(string)), } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewTransitVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) + input.NewTransitVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewTransitVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) + input.NewTransitVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewTransitVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) + input.NewTransitVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect hosted transit virtual interface: %#v", req) - resp, err := conn.AllocateTransitVirtualInterface(ctx, req) + output, err := conn.AllocateTransitVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect hosted transit virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Hosted Transit Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterface.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterface.VirtualInterfaceId)) - if err := hostedTransitVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitHostedTransitVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Hosted Transit Virtual Interface (%s) create: %s", d.Id(), err) } return append(diags, resourceHostedTransitVirtualInterfaceRead(ctx, d, meta)...) @@ -162,19 +167,21 @@ func resourceHostedTransitVirtualInterfaceRead(ctx context.Context, d *schema.Re var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect hosted transit virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Transit Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Transit Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) - d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) + d.Set("amazon_side_asn", flex.Int64ToStringValue(vif.AmazonSideAsn)) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -204,13 +211,11 @@ func resourceHostedTransitVirtualInterfaceDelete(ctx context.Context, d *schema. func resourceHostedTransitVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -219,10 +224,13 @@ func resourceHostedTransitVirtualInterfaceImport(ctx context.Context, d *schema. return []*schema.ResourceData{d}, nil } -func hostedTransitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitHostedTransitVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/hosted_transit_virtual_interface_test.go b/internal/service/directconnect/hosted_transit_virtual_interface_test.go index 98903477924..53b05668e08 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface_test.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -24,8 +23,8 @@ func TestAccDirectConnectHostedTransitVirtualInterface_serial(t *testing.T) { t.Parallel() testCases := map[string]func(t *testing.T){ - acctest.CtBasic: testAccHostedTransitVirtualInterface_basic, - "accepterTags": testAccHostedTransitVirtualInterface_accepterTags, + acctest.CtBasic: testAccHostedTransitVirtualInterface_basic, + "accepterTags": testAccHostedTransitVirtualInterface_accepterTags, } acctest.RunSerialTests1Level(t, testCases, 0) @@ -33,11 +32,7 @@ func TestAccDirectConnectHostedTransitVirtualInterface_serial(t *testing.T) { func testAccHostedTransitVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_transit_virtual_interface.test" @@ -58,7 +53,7 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckHostedTransitVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedTransitVirtualInterfaceConfig_basic(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccHostedTransitVirtualInterfaceConfig_basic(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -67,7 +62,7 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -80,9 +75,8 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrPair(accepterResourceName, "virtual_interface_id", resourceName, names.AttrID), ), }, - // Test import. { - Config: testAccHostedTransitVirtualInterfaceConfig_basic(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccHostedTransitVirtualInterfaceConfig_basic(connectionID, rName, amzAsn, bgpAsn, vlan), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -93,11 +87,7 @@ func testAccHostedTransitVirtualInterface_basic(t *testing.T) { func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_hosted_transit_virtual_interface.test" @@ -118,7 +108,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { CheckDestroy: testAccCheckHostedTransitVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccHostedTransitVirtualInterfaceConfig_accepterTags(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccHostedTransitVirtualInterfaceConfig_accepterTags(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -127,7 +117,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -144,7 +134,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { ), }, { - Config: testAccHostedTransitVirtualInterfaceConfig_accepterTagsUpdated(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccHostedTransitVirtualInterfaceConfig_accepterTagsUpdated(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckHostedTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -153,7 +143,7 @@ func testAccHostedTransitVirtualInterface_accepterTags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), @@ -184,7 +174,7 @@ func testAccCheckHostedTransitVirtualInterfaceDestroy(ctx context.Context) resou } func testAccHostedTransitVirtualInterfaceConfig_base(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return acctest.ConfigAlternateAccountProvider() + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAlternateAccountProvider(), fmt.Sprintf(` # Creator resource "aws_dx_hosted_transit_virtual_interface" "test" { address_family = "ipv4" @@ -210,22 +200,22 @@ resource "aws_dx_gateway" "test" { amazon_side_asn = %[3]d name = %[2]q } -`, cid, rName, amzAsn, bgpAsn, vlan) +`, cid, rName, amzAsn, bgpAsn, vlan)) } func testAccHostedTransitVirtualInterfaceConfig_basic(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan) + ` + return acctest.ConfigCompose(testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan), ` resource "aws_dx_hosted_transit_virtual_interface_accepter" "test" { provider = "awsalternate" dx_gateway_id = aws_dx_gateway.test.id virtual_interface_id = aws_dx_hosted_transit_virtual_interface.test.id } -` +`) } func testAccHostedTransitVirtualInterfaceConfig_accepterTags(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_transit_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -238,11 +228,11 @@ resource "aws_dx_hosted_transit_virtual_interface_accepter" "test" { Key2 = "Value2a" } } -`, rName) +`, rName)) } func testAccHostedTransitVirtualInterfaceConfig_accepterTagsUpdated(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccHostedTransitVirtualInterfaceConfig_base(cid, rName, amzAsn, bgpAsn, vlan), fmt.Sprintf(` resource "aws_dx_hosted_transit_virtual_interface_accepter" "test" { provider = "awsalternate" @@ -255,5 +245,5 @@ resource "aws_dx_hosted_transit_virtual_interface_accepter" "test" { Key3 = "Value3" } } -`, rName) +`, rName)) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 060b59f640e..3ac81f1424c 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -121,8 +121,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceHostedTransitVirtualInterface, + Factory: resourceHostedTransitVirtualInterface, TypeName: "aws_dx_hosted_transit_virtual_interface", + Name: "Hosted Transit Virtual Interface", }, { Factory: ResourceHostedTransitVirtualInterfaceAccepter, From 6afffb8b622c0fa2ffa5ea5d4cc3bc80d7966e08 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 11:21:07 -0400 Subject: [PATCH 20/59] r/aws_dx_hosted_public_virtual_interface_accepter: Tidy up. --- .../service/directconnect/exports_test.go | 1 + ...osted_public_virtual_interface_accepter.go | 72 +++++++++---------- .../directconnect/service_package_gen.go | 4 +- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 27a55b2184f..6e666ab893a 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -16,6 +16,7 @@ var ( ResourceHostedPrivateVirtualInterface = resourceHostedPrivateVirtualInterface ResourceHostedPrivateVirtualInterfaceAccepter = resourceHostedPrivateVirtualInterfaceAccepter ResourceHostedPublicVirtualInterface = resourceHostedPublicVirtualInterface + ResourceHostedPublicVirtualInterfaceAccepter = resourceHostedPublicVirtualInterfaceAccepter ResourceHostedTransitVirtualInterface = resourceHostedTransitVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey diff --git a/internal/service/directconnect/hosted_public_virtual_interface_accepter.go b/internal/service/directconnect/hosted_public_virtual_interface_accepter.go index 99e309611eb..9210bf14633 100644 --- a/internal/service/directconnect/hosted_public_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_public_virtual_interface_accepter.go @@ -19,18 +19,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_public_virtual_interface_accepter", name="Hosted Public Virtual Interface") +// @SDKResource("aws_dx_hosted_public_virtual_interface_accepter", name="Hosted Public Virtual Interface Accepter") // @Tags(identifierAttribute="arn") -func ResourceHostedPublicVirtualInterfaceAccepter() *schema.Resource { +func resourceHostedPublicVirtualInterfaceAccepter() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedPublicVirtualInterfaceAccepterCreate, ReadWithoutTimeout: resourceHostedPublicVirtualInterfaceAccepterRead, UpdateWithoutTimeout: resourceHostedPublicVirtualInterfaceAccepterUpdate, - DeleteWithoutTimeout: resourceHostedPublicVirtualInterfaceAccepterDelete, + DeleteWithoutTimeout: schema.NoopContext, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedPublicVirtualInterfaceAccepterImport, }, @@ -62,18 +64,18 @@ func resourceHostedPublicVirtualInterfaceAccepterCreate(ctx context.Context, d * var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vifId := d.Get("virtual_interface_id").(string) - req := &directconnect.ConfirmPublicVirtualInterfaceInput{ - VirtualInterfaceId: aws.String(vifId), + vifID := d.Get("virtual_interface_id").(string) + input := &directconnect.ConfirmPublicVirtualInterfaceInput{ + VirtualInterfaceId: aws.String(vifID), } - log.Printf("[DEBUG] Accepting Direct Connect hosted public virtual interface: %#v", req) - _, err := conn.ConfirmPublicVirtualInterface(ctx, req) + _, err := conn.ConfirmPublicVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted public virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "accepting Direct Connect Hosted Public Virtual Interface (%s): %s", vifID, err) } - d.SetId(vifId) + d.SetId(vifID) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -83,12 +85,12 @@ func resourceHostedPublicVirtualInterfaceAccepterCreate(ctx context.Context, d * }.String() d.Set(names.AttrARN, arn) - if err := hostedPublicVirtualInterfaceAccepterWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitHostedPublicVirtualInterfaceAccepterAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Hosted Public Virtual Interface Accepter (%s) create: %s", d.Id(), err) } if err := createTags(ctx, conn, arn, getTagsIn(ctx)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting Direct Connect hosted public virtual interface (%s) tags: %s", arn, err) + return sdkdiag.AppendErrorf(diags, "setting Direct Connect Hosted Public Virtual Interface (%s) tags: %s", arn, err) } return append(diags, resourceHostedPublicVirtualInterfaceAccepterUpdate(ctx, d, meta)...) @@ -98,19 +100,20 @@ func resourceHostedPublicVirtualInterfaceAccepterRead(ctx context.Context, d *sc var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect hosted public virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Public Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && - vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown && - vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateVerifying { - log.Printf("[WARN] Direct Connect hosted public virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Public Virtual Interface (%s): %s", d.Id(), err) + } + + if state := vif.VirtualInterfaceState; state != awstypes.VirtualInterfaceStateAvailable && state != awstypes.VirtualInterfaceStateDown && state != awstypes.VirtualInterfaceStateVerifying { + log.Printf("[WARN] Direct Connect Hosted Public Virtual Interface (%s) is '%s', removing from state", d.Id(), state) d.SetId("") return diags } @@ -131,22 +134,14 @@ func resourceHostedPublicVirtualInterfaceAccepterUpdate(ctx context.Context, d * return append(diags, resourceHostedPublicVirtualInterfaceAccepterRead(ctx, d, meta)...) } -func resourceHostedPublicVirtualInterfaceAccepterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics - log.Printf("[WARN] Will not delete Direct Connect virtual interface. Terraform will remove this resource from the state file, however resources may remain.") - return diags -} - func resourceHostedPublicVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -164,10 +159,13 @@ func resourceHostedPublicVirtualInterfaceAccepterImport(ctx context.Context, d * return []*schema.ResourceData{d}, nil } -func hostedPublicVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, +func waitHostedPublicVirtualInterfaceAccepterAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, vifId, - timeout, enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying), + timeout, + ) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 3ac81f1424c..778532504c0 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -113,9 +113,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Hosted Public Virtual Interface", }, { - Factory: ResourceHostedPublicVirtualInterfaceAccepter, + Factory: resourceHostedPublicVirtualInterfaceAccepter, TypeName: "aws_dx_hosted_public_virtual_interface_accepter", - Name: "Hosted Public Virtual Interface", + Name: "Hosted Public Virtual Interface Accepter", Tags: &types.ServicePackageResourceTags{ IdentifierAttribute: names.AttrARN, }, From 9a0117015a5a021419405184690424e35dd72985 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 11:31:36 -0400 Subject: [PATCH 21/59] r/aws_dx_hosted_transit_virtual_interface_accepter: Tidy up. --- .../service/directconnect/exports_test.go | 1 + ...sted_transit_virtual_interface_accepter.go | 72 +++++++++---------- .../directconnect/service_package_gen.go | 4 +- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 6e666ab893a..e118e530be6 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -18,6 +18,7 @@ var ( ResourceHostedPublicVirtualInterface = resourceHostedPublicVirtualInterface ResourceHostedPublicVirtualInterfaceAccepter = resourceHostedPublicVirtualInterfaceAccepter ResourceHostedTransitVirtualInterface = resourceHostedTransitVirtualInterface + ResourceHostedTransitVirtualInterfaceAccepter = resourceHostedTransitVirtualInterfaceAccepter FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go b/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go index 87195be86cb..76e81e6cb7e 100644 --- a/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go +++ b/internal/service/directconnect/hosted_transit_virtual_interface_accepter.go @@ -19,18 +19,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_hosted_transit_virtual_interface_accepter", name="Hosted Transit Virtual Interface") +// @SDKResource("aws_dx_hosted_transit_virtual_interface_accepter", name="Hosted Transit Virtual Interface Accepter") // @Tags(identifierAttribute="arn") -func ResourceHostedTransitVirtualInterfaceAccepter() *schema.Resource { +func resourceHostedTransitVirtualInterfaceAccepter() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceHostedTransitVirtualInterfaceAccepterCreate, ReadWithoutTimeout: resourceHostedTransitVirtualInterfaceAccepterRead, UpdateWithoutTimeout: resourceHostedTransitVirtualInterfaceAccepterUpdate, - DeleteWithoutTimeout: resourceHostedTransitVirtualInterfaceAccepterDelete, + DeleteWithoutTimeout: schema.NoopContext, + Importer: &schema.ResourceImporter{ StateContext: resourceHostedTransitVirtualInterfaceAccepterImport, }, @@ -67,19 +69,19 @@ func resourceHostedTransitVirtualInterfaceAccepterCreate(ctx context.Context, d var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vifId := d.Get("virtual_interface_id").(string) - req := &directconnect.ConfirmTransitVirtualInterfaceInput{ + vifID := d.Get("virtual_interface_id").(string) + input := &directconnect.ConfirmTransitVirtualInterfaceInput{ DirectConnectGatewayId: aws.String(d.Get("dx_gateway_id").(string)), - VirtualInterfaceId: aws.String(vifId), + VirtualInterfaceId: aws.String(vifID), } - log.Printf("[DEBUG] Accepting Direct Connect hosted transit virtual interface: %#v", req) - _, err := conn.ConfirmTransitVirtualInterface(ctx, req) + _, err := conn.ConfirmTransitVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "accepting Direct Connect hosted transit virtual interface (%s): %s", vifId, err) + return sdkdiag.AppendErrorf(diags, "accepting Direct Connect Hosted Transit Virtual Interface (%s): %s", vifID, err) } - d.SetId(vifId) + d.SetId(vifID) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, Region: meta.(*conns.AWSClient).Region, @@ -89,12 +91,12 @@ func resourceHostedTransitVirtualInterfaceAccepterCreate(ctx context.Context, d }.String() d.Set(names.AttrARN, arn) - if err := hostedTransitVirtualInterfaceAccepterWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitHostedTransitVirtualInterfaceAccepterAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Hosted Transit Virtual Interface Accepter (%s) create: %s", d.Id(), err) } if err := createTags(ctx, conn, arn, getTagsIn(ctx)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting Direct Connect hosted transit virtual interface (%s) tags: %s", arn, err) + return sdkdiag.AppendErrorf(diags, "setting Direct Connect Hosted Transit Virtual Interface (%s) tags: %s", arn, err) } return append(diags, resourceHostedTransitVirtualInterfaceAccepterUpdate(ctx, d, meta)...) @@ -104,17 +106,20 @@ func resourceHostedTransitVirtualInterfaceAccepterRead(ctx context.Context, d *s var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect transit virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Hosted Transit Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - if vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateAvailable && vif.VirtualInterfaceState != awstypes.VirtualInterfaceStateDown { - log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", string(vif.VirtualInterfaceState), d.Id()) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Hosted Transit Virtual Interface (%s): %s", d.Id(), err) + } + + if state := vif.VirtualInterfaceState; state != awstypes.VirtualInterfaceStateAvailable && state != awstypes.VirtualInterfaceStateDown { + log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", d.Id(), state) d.SetId("") return diags } @@ -136,22 +141,14 @@ func resourceHostedTransitVirtualInterfaceAccepterUpdate(ctx context.Context, d return append(diags, resourceHostedTransitVirtualInterfaceAccepterRead(ctx, d, meta)...) } -func resourceHostedTransitVirtualInterfaceAccepterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics - log.Printf("[WARN] Will not delete Direct Connect virtual interface. Terraform will remove this resource from the state file, however resources may remain.") - return diags -} - func resourceHostedTransitVirtualInterfaceAccepterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -169,10 +166,13 @@ func resourceHostedTransitVirtualInterfaceAccepterImport(ctx context.Context, d return []*schema.ResourceData{d}, nil } -func hostedTransitVirtualInterfaceAccepterWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitHostedTransitVirtualInterfaceAccepterAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStateConfirming, awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 778532504c0..044d32b1091 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -126,9 +126,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Hosted Transit Virtual Interface", }, { - Factory: ResourceHostedTransitVirtualInterfaceAccepter, + Factory: resourceHostedTransitVirtualInterfaceAccepter, TypeName: "aws_dx_hosted_transit_virtual_interface_accepter", - Name: "Hosted Transit Virtual Interface", + Name: "Hosted Transit Virtual Interface Accepter", Tags: &types.ServicePackageResourceTags{ IdentifierAttribute: names.AttrARN, }, From 9175f57f02aa4fcc41ea7863cdfaf85aededf0bd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 11:45:43 -0400 Subject: [PATCH 22/59] r/aws_dx_lag: Tidy up. --- .../service/directconnect/exports_test.go | 2 + internal/service/directconnect/find.go | 36 ------ internal/service/directconnect/lag.go | 108 ++++++++++++++++-- internal/service/directconnect/lag_test.go | 8 +- .../directconnect/service_package_gen.go | 2 +- internal/service/directconnect/status.go | 28 ----- internal/service/directconnect/sweep.go | 2 +- internal/service/directconnect/wait.go | 35 ------ 8 files changed, 104 insertions(+), 117 deletions(-) delete mode 100644 internal/service/directconnect/status.go delete mode 100644 internal/service/directconnect/wait.go diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index e118e530be6..02586ff5cfa 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -19,6 +19,7 @@ var ( ResourceHostedPublicVirtualInterfaceAccepter = resourceHostedPublicVirtualInterfaceAccepter ResourceHostedTransitVirtualInterface = resourceHostedTransitVirtualInterface ResourceHostedTransitVirtualInterfaceAccepter = resourceHostedTransitVirtualInterfaceAccepter + ResourceLag = resourceLag FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID @@ -27,6 +28,7 @@ var ( FindGatewayAssociationProposalByID = findGatewayAssociationProposalByID FindGatewayByID = findGatewayByID FindHostedConnectionByID = findHostedConnectionByID + FindLagByID = findLagByID FindVirtualInterfaceByID = findVirtualInterfaceByID GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 ValidConnectionBandWidth = validConnectionBandWidth diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go index bed0ef786e4..c6b6c26908b 100644 --- a/internal/service/directconnect/find.go +++ b/internal/service/directconnect/find.go @@ -9,45 +9,9 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" - "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindLagByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { - input := &directconnect.DescribeLagsInput{ - LagId: aws.String(id), - } - - output, err := conn.DescribeLags(ctx, input) - - if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Lag with ID") { - return nil, &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - lag, err := tfresource.AssertSingleValueResult(output.Lags) - - if err != nil { - return nil, err - } - - if lag.LagState == awstypes.LagStateDeleted { - return nil, &retry.NotFoundError{ - Message: string(lag.LagState), - LastRequest: input, - } - } - - return lag, nil -} - func FindLocationByCode(ctx context.Context, conn *directconnect.Client, code string) (awstypes.Location, error) { input := &directconnect.DescribeLocationsInput{} diff --git a/internal/service/directconnect/lag.go b/internal/service/directconnect/lag.go index 275ebf723a9..36265c03a3f 100644 --- a/internal/service/directconnect/lag.go +++ b/internal/service/directconnect/lag.go @@ -7,16 +7,20 @@ import ( "context" "fmt" "log" + "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/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/aws/aws-sdk-go/aws/arn" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -25,12 +29,13 @@ import ( // @SDKResource("aws_dx_lag", name="LAG") // @Tags(identifierAttribute="arn") -func ResourceLag() *schema.Resource { +func resourceLag() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceLagCreate, ReadWithoutTimeout: resourceLagRead, UpdateWithoutTimeout: resourceLagUpdate, DeleteWithoutTimeout: resourceLagDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -116,7 +121,6 @@ func resourceLagCreate(ctx context.Context, d *schema.ResourceData, meta interfa input.ProviderName = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect LAG: %#v", input) output, err := conn.CreateLag(ctx, input) if err != nil { @@ -139,7 +143,7 @@ func resourceLagRead(ctx context.Context, d *schema.ResourceData, meta interface var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - lag, err := FindLagByID(ctx, conn, d.Id()) + lag, err := findLagByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Direct Connect LAG (%s) not found, removing from state", d.Id()) @@ -180,7 +184,6 @@ func resourceLagUpdate(ctx context.Context, d *schema.ResourceData, meta interfa LagName: aws.String(d.Get(names.AttrName).(string)), } - log.Printf("[DEBUG] Updating Direct Connect LAG: %#v", input) _, err := conn.UpdateLag(ctx, input) if err != nil { @@ -196,14 +199,14 @@ func resourceLagDelete(ctx context.Context, d *schema.ResourceData, meta interfa conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) if d.Get(names.AttrForceDestroy).(bool) { - lag, err := FindLagByID(ctx, conn, d.Id()) + lag, err := findLagByID(ctx, conn, d.Id()) if tfresource.NotFound(err) { return diags } if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting Direct Connect LAG (%s): listing connections: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "reading Direct Connect LAG (%s): %s", d.Id(), err) } for _, connection := range lag.Connections { @@ -230,11 +233,96 @@ func resourceLagDelete(ctx context.Context, d *schema.ResourceData, meta interfa return sdkdiag.AppendErrorf(diags, "deleting Direct Connect LAG (%s): %s", d.Id(), err) } - _, err = waitLagDeleted(ctx, conn, d.Id()) - - if err != nil { + if _, err := waitLagDeleted(ctx, conn, d.Id()); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect LAG (%s) delete: %s", d.Id(), err) } return diags } + +func findLagByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { + input := &directconnect.DescribeLagsInput{ + LagId: aws.String(id), + } + output, err := findLag(ctx, conn, input, tfslices.PredicateTrue[*awstypes.Lag]()) + + if err != nil { + return nil, err + } + + if state := output.LagState; state == awstypes.LagStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + +func findLag(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLagsInput, filter tfslices.Predicate[*awstypes.Lag]) (*awstypes.Lag, error) { + output, err := findLags(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLags(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLagsInput, filter tfslices.Predicate[*awstypes.Lag]) ([]awstypes.Lag, error) { + output, err := conn.DescribeLags(ctx, input) + + if errs.IsAErrorMessageContains[*awstypes.DirectConnectClientException](err, "Could not find Lag with ID") { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return tfslices.Filter(output.Lags, tfslices.PredicateValue(filter)), nil +} + +func statusLag(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findLagByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.LagState), nil + } +} + +func waitLagDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { + const ( + timeout = 10 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.LagStateAvailable, awstypes.LagStateRequested, awstypes.LagStatePending, awstypes.LagStateDeleting), + Target: []string{}, + Refresh: statusLag(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Lag); ok { + return output, err + } + + return nil, err +} diff --git a/internal/service/directconnect/lag_test.go b/internal/service/directconnect/lag_test.go index 7fa37a62c85..41711c5565a 100644 --- a/internal/service/directconnect/lag_test.go +++ b/internal/service/directconnect/lag_test.go @@ -265,17 +265,13 @@ func testAccCheckLagExists(ctx context.Context, name string, v *awstypes.Lag) re return fmt.Errorf("Not found: %s", name) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - lag, err := tfdirectconnect.FindLagByID(ctx, conn, rs.Primary.ID) + output, err := tfdirectconnect.FindLagByID(ctx, conn, rs.Primary.ID) if err != nil { return err } - *v = *lag + *v = *output return nil } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 044d32b1091..c6edcae8d70 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -134,7 +134,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceLag, + Factory: resourceLag, TypeName: "aws_dx_lag", Name: "LAG", Tags: &types.ServicePackageResourceTags{ diff --git a/internal/service/directconnect/status.go b/internal/service/directconnect/status.go deleted file mode 100644 index 9aa4c870b0d..00000000000 --- a/internal/service/directconnect/status.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect - -import ( - "context" - - "github.com/aws/aws-sdk-go-v2/service/directconnect" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" - "github.com/hashicorp/terraform-provider-aws/internal/tfresource" -) - -func statusLagState(ctx context.Context, conn *directconnect.Client, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindLagByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, string(output.LagState), nil - } -} diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index ad0633f8d7c..23b3fbea81d 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -423,7 +423,7 @@ func sweepLags(region string) error { for _, lag := range output.Lags { id := aws.ToString(lag.LagId) - r := ResourceLag() + r := resourceLag() d := r.Data(nil) d.SetId(id) diff --git a/internal/service/directconnect/wait.go b/internal/service/directconnect/wait.go deleted file mode 100644 index 86973fb997f..00000000000 --- a/internal/service/directconnect/wait.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect - -import ( - "context" - "time" - - "github.com/aws/aws-sdk-go-v2/service/directconnect" - awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" - "github.com/hashicorp/terraform-provider-aws/internal/enum" -) - -const ( - lagDeletedTimeout = 10 * time.Minute -) - -func waitLagDeleted(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.Lag, error) { - stateConf := &retry.StateChangeConf{ - Pending: enum.Slice(awstypes.LagStateAvailable, awstypes.LagStateRequested, awstypes.LagStatePending, awstypes.LagStateDeleting), - Target: []string{}, - Refresh: statusLagState(ctx, conn, id), - Timeout: lagDeletedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*awstypes.Lag); ok { - return output, err - } - - return nil, err -} From 784ae3d21f67361116dcbb8f3b9e01c7a6714b68 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 11:56:18 -0400 Subject: [PATCH 23/59] d/aws_dx_location(s): Tidy up. --- internal/service/directconnect/find.go | 45 ----------------- .../hosted_private_virtual_interface.go | 2 +- .../directconnect/location_data_source.go | 49 ++++++++++++++----- .../directconnect/locations_data_source.go | 22 ++++----- .../directconnect/service_package_gen.go | 6 ++- 5 files changed, 52 insertions(+), 72 deletions(-) delete mode 100644 internal/service/directconnect/find.go diff --git a/internal/service/directconnect/find.go b/internal/service/directconnect/find.go deleted file mode 100644 index c6b6c26908b..00000000000 --- a/internal/service/directconnect/find.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect - -import ( - "context" - - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/directconnect" - awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/hashicorp/terraform-provider-aws/internal/tfresource" -) - -func FindLocationByCode(ctx context.Context, conn *directconnect.Client, code string) (awstypes.Location, error) { - input := &directconnect.DescribeLocationsInput{} - - locations, err := FindLocations(ctx, conn, input) - - if err != nil { - return awstypes.Location{}, err - } - - for _, location := range locations { - if aws.ToString(location.LocationCode) == code { - return location, nil - } - } - - return awstypes.Location{}, tfresource.NewEmptyResultError(input) -} - -func FindLocations(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLocationsInput) ([]awstypes.Location, error) { - output, err := conn.DescribeLocations(ctx, input) - - if err != nil { - return nil, err - } - - if output == nil || len(output.Locations) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - - return output.Locations, nil -} diff --git a/internal/service/directconnect/hosted_private_virtual_interface.go b/internal/service/directconnect/hosted_private_virtual_interface.go index 434f918f3ad..68ff4098555 100644 --- a/internal/service/directconnect/hosted_private_virtual_interface.go +++ b/internal/service/directconnect/hosted_private_virtual_interface.go @@ -10,9 +10,9 @@ 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/directconnect" awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" - "github.com/aws/aws-sdk-go/aws/arn" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" diff --git a/internal/service/directconnect/location_data_source.go b/internal/service/directconnect/location_data_source.go index e31099b3303..a2fbfb7be0a 100644 --- a/internal/service/directconnect/location_data_source.go +++ b/internal/service/directconnect/location_data_source.go @@ -6,15 +6,19 @@ package directconnect import ( "context" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -// @SDKDataSource("aws_dx_location") -func DataSourceLocation() *schema.Resource { +// @SDKDataSource("aws_dx_location", name="Location") +func dataSourceLocation() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocationRead, @@ -24,24 +28,20 @@ func DataSourceLocation() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "available_port_speeds": { Type: schema.TypeList, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "available_providers": { Type: schema.TypeList, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "location_code": { Type: schema.TypeString, Required: true, }, - "location_name": { Type: schema.TypeString, Computed: true, @@ -53,16 +53,15 @@ func DataSourceLocation() *schema.Resource { func dataSourceLocationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - locationCode := d.Get("location_code").(string) - - location, err := FindLocationByCode(ctx, conn, locationCode) - if tfresource.NotFound(err) { - return sdkdiag.AppendErrorf(diags, "no Direct Connect location matched; change the search criteria and try again") - } + input := &directconnect.DescribeLocationsInput{} + locationCode := d.Get("location_code").(string) + location, err := findLocation(ctx, conn, input, func(v *awstypes.Location) bool { + return aws.ToString(v.LocationCode) == locationCode + }) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect location (%s): %s", locationCode, err) + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("Direct Connect Location", err)) } d.SetId(locationCode) @@ -74,3 +73,27 @@ func dataSourceLocationRead(ctx context.Context, d *schema.ResourceData, meta in return diags } + +func findLocation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLocationsInput, filter tfslices.Predicate[*awstypes.Location]) (*awstypes.Location, error) { + output, err := findLocations(ctx, conn, input, filter) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLocations(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeLocationsInput, filter tfslices.Predicate[*awstypes.Location]) ([]awstypes.Location, error) { + output, err := conn.DescribeLocations(ctx, input) + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return tfslices.Filter(output.Locations, tfslices.PredicateValue(filter)), nil +} diff --git a/internal/service/directconnect/locations_data_source.go b/internal/service/directconnect/locations_data_source.go index a3b7586f943..123baa2fd3b 100644 --- a/internal/service/directconnect/locations_data_source.go +++ b/internal/service/directconnect/locations_data_source.go @@ -6,15 +6,18 @@ package directconnect import ( "context" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/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" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" ) -// @SDKDataSource("aws_dx_locations") -func DataSourceLocations() *schema.Resource { +// @SDKDataSource("aws_dx_locations", name="Locations") +func dataSourceLocations() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocationsRead, @@ -32,20 +35,17 @@ func dataSourceLocationsRead(ctx context.Context, d *schema.ResourceData, meta i var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - locations, err := FindLocations(ctx, conn, &directconnect.DescribeLocationsInput{}) + input := &directconnect.DescribeLocationsInput{} + locations, err := findLocations(ctx, conn, input, tfslices.PredicateTrue[*awstypes.Location]()) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect locations: %s", err) - } - - var locationCodes []*string - - for _, location := range locations { - locationCodes = append(locationCodes, location.LocationCode) + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Locations: %s", err) } d.SetId(meta.(*conns.AWSClient).Region) - d.Set("location_codes", locationCodes) + d.Set("location_codes", tfslices.ApplyToAll(locations, func(v awstypes.Location) string { + return aws.ToString(v.LocationCode) + })) return diags } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index c6edcae8d70..4dc4421f6de 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -35,12 +35,14 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Gateway", }, { - Factory: DataSourceLocation, + Factory: dataSourceLocation, TypeName: "aws_dx_location", + Name: "Location", }, { - Factory: DataSourceLocations, + Factory: dataSourceLocations, TypeName: "aws_dx_locations", + Name: "Locations", }, { Factory: DataSourceRouterConfiguration, From ef41f2beaf337a205b40d3952aa110de84e985fd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 12:33:11 -0400 Subject: [PATCH 24/59] r/aws_dx_macsec_key_association: Tidy up. --- .changelog/37213.txt | 3 + .../service/directconnect/connection_test.go | 4 +- .../service/directconnect/exports_test.go | 2 + internal/service/directconnect/lag_test.go | 4 +- internal/service/directconnect/macsec_key.go | 142 ++++++++++-------- .../service/directconnect/macsec_key_test.go | 110 ++++++++------ .../directconnect/service_package_gen.go | 3 +- 7 files changed, 157 insertions(+), 111 deletions(-) create mode 100644 .changelog/37213.txt diff --git a/.changelog/37213.txt b/.changelog/37213.txt new file mode 100644 index 00000000000..d8893505576 --- /dev/null +++ b/.changelog/37213.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_dx_macsec_key_association: Add plan-time validation of `secret_arn` +``` \ No newline at end of file diff --git a/internal/service/directconnect/connection_test.go b/internal/service/directconnect/connection_test.go index 3bbda557ab6..1d8b04ff5f5 100644 --- a/internal/service/directconnect/connection_test.go +++ b/internal/service/directconnect/connection_test.go @@ -88,8 +88,8 @@ func TestAccDirectConnectConnection_encryptionMode(t *testing.T) { var connection awstypes.Connection resourceName := "aws_dx_connection.test" - ckn := testAccDirecConnectMacSecGenerateHex() - cak := testAccDirecConnectMacSecGenerateHex() + ckn := testAccMacSecGenerateHex() + cak := testAccMacSecGenerateHex() resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 02586ff5cfa..3c48504b66b 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -20,6 +20,7 @@ var ( ResourceHostedTransitVirtualInterface = resourceHostedTransitVirtualInterface ResourceHostedTransitVirtualInterfaceAccepter = resourceHostedTransitVirtualInterfaceAccepter ResourceLag = resourceLag + ResourceMacSecKeyAssociation = resourceMacSecKeyAssociation FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID @@ -29,6 +30,7 @@ var ( FindGatewayByID = findGatewayByID FindHostedConnectionByID = findHostedConnectionByID FindLagByID = findLagByID + FindMacSecKeyByTwoPartKey = findMacSecKeyByTwoPartKey FindVirtualInterfaceByID = findVirtualInterfaceByID GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 ValidConnectionBandWidth = validConnectionBandWidth diff --git a/internal/service/directconnect/lag_test.go b/internal/service/directconnect/lag_test.go index 41711c5565a..2247ab21e94 100644 --- a/internal/service/directconnect/lag_test.go +++ b/internal/service/directconnect/lag_test.go @@ -258,13 +258,13 @@ func testAccCheckLagDestroy(ctx context.Context) resource.TestCheckFunc { func testAccCheckLagExists(ctx context.Context, name string, v *awstypes.Lag) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) - rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) + output, err := tfdirectconnect.FindLagByID(ctx, conn, rs.Primary.ID) if err != nil { diff --git a/internal/service/directconnect/macsec_key.go b/internal/service/directconnect/macsec_key.go index 61237f575ea..753614436d1 100644 --- a/internal/service/directconnect/macsec_key.go +++ b/internal/service/directconnect/macsec_key.go @@ -12,42 +12,44 @@ import ( "github.com/YakDriver/regexache" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/directconnect" + awstypes "github.com/aws/aws-sdk-go-v2/service/directconnect/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "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/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_dx_macsec_key_association") -func ResourceMacSecKeyAssociation() *schema.Resource { +// @SDKResource("aws_dx_macsec_key_association", name="MACSec Key Association") +func resourceMacSecKeyAssociation() *schema.Resource { return &schema.Resource{ - // MacSecKey resource only supports create (Associate), read (Describe) and delete (Disassociate) - CreateWithoutTimeout: resourceMacSecKeyCreate, - ReadWithoutTimeout: resourceMacSecKeyRead, - DeleteWithoutTimeout: resourceMacSecKeyDelete, + CreateWithoutTimeout: resourceMacSecKeyAssociatioCreate, + ReadWithoutTimeout: resourceMacSecKeyAssociationRead, + DeleteWithoutTimeout: resourceMacSecKeyAssociationDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "cak": { - Type: schema.TypeString, - Optional: true, - // CAK requires CKN + Type: schema.TypeString, + Optional: true, + ForceNew: true, RequiredWith: []string{"ckn"}, ValidateFunc: validation.StringMatch(regexache.MustCompile(`[0-9A-Fa-f]{64}$`), "Must be 64-character hex code string"), - ForceNew: true, }, "ckn": { Type: schema.TypeString, - Computed: true, Optional: true, + ForceNew: true, + Computed: true, AtLeastOneOf: []string{"ckn", "secret_arn"}, ValidateFunc: validation.StringMatch(regexache.MustCompile(`[0-9A-Fa-f]{64}$`), "Must be 64-character hex code string"), - ForceNew: true, }, names.AttrConnectionID: { Type: schema.TypeString, @@ -58,8 +60,9 @@ func ResourceMacSecKeyAssociation() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - AtLeastOneOf: []string{"ckn", "secret_arn"}, ForceNew: true, + AtLeastOneOf: []string{"ckn", "secret_arn"}, + ValidateFunc: verify.ValidARN, }, "start_on": { Type: schema.TypeString, @@ -73,108 +76,119 @@ func ResourceMacSecKeyAssociation() *schema.Resource { } } -func resourceMacSecKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceMacSecKeyAssociatioCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) + connectionID := d.Get(names.AttrConnectionID).(string) input := &directconnect.AssociateMacSecKeyInput{ - ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), + ConnectionId: aws.String(connectionID), } - if d.Get("ckn").(string) != "" { + if v, ok := d.GetOk("ckn"); ok { input.Cak = aws.String(d.Get("cak").(string)) - input.Ckn = aws.String(d.Get("ckn").(string)) + input.Ckn = aws.String(v.(string)) } - if d.Get("secret_arn").(string) != "" { - input.SecretARN = aws.String(d.Get("secret_arn").(string)) + if v, ok := d.GetOk("secret_arn"); ok { + input.SecretARN = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) output, err := conn.AssociateMacSecKey(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "creating MACSec secret key on Direct Connect Connection (%s): %s", *input.ConnectionId, err) + return sdkdiag.AppendErrorf(diags, "creating MACSec Key Association with Direct Connect Connection (%s): %s", connectionID, err) } - secret_arn := MacSecKeyParseSecretARN(output) - - // Create a composite ID based on connection ID and secret ARN - d.SetId(fmt.Sprintf("%s_%s", secret_arn, aws.ToString(output.ConnectionId))) + var secretARN string + for _, key := range output.MacSecKeys { + secretARN = aws.ToString(key.SecretARN) + } - d.Set("secret_arn", secret_arn) + d.SetId(macSecKeyAssociationCreateResourceID(secretARN, connectionID)) - return append(diags, resourceMacSecKeyRead(ctx, d, meta)...) + return append(diags, resourceMacSecKeyAssociationRead(ctx, d, meta)...) } -func resourceMacSecKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceMacSecKeyAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - secretArn, connId, err := MacSecKeyParseID(d.Id()) + secretARN, connectionID, err := macSecKeyAssociationParseResourceID(d.Id()) if err != nil { - return sdkdiag.AppendErrorf(diags, "unexpected format of ID (%s), expected secretArn_connectionId", d.Id()) + return sdkdiag.AppendFromErr(diags, err) } - connection, err := findConnectionByID(ctx, conn, connId) - if err != nil { - return sdkdiag.AppendErrorf(diags, "reading Direct Connect Connection (%s): %s", d.Id(), err) - } + key, err := findMacSecKeyByTwoPartKey(ctx, conn, connectionID, secretARN) - if connection.MacSecKeys == nil { - return sdkdiag.AppendErrorf(diags, "no MACSec keys found on Direct Connect Connection (%s)", d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect MACSec Key Association (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - for _, key := range connection.MacSecKeys { - if aws.ToString(key.SecretARN) == aws.ToString(&secretArn) { - d.Set("ckn", key.Ckn) - d.Set(names.AttrConnectionID, connId) - d.Set("secret_arn", key.SecretARN) - d.Set("start_on", key.StartOn) - d.Set(names.AttrState, key.State) - } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect MACSec Key Association (%s): %s", d.Id(), err) } + d.Set("ckn", key.Ckn) + d.Set(names.AttrConnectionID, connectionID) + d.Set("secret_arn", key.SecretARN) + d.Set("start_on", key.StartOn) + d.Set(names.AttrState, key.State) + return diags } -func resourceMacSecKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceMacSecKeyAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - input := &directconnect.DisassociateMacSecKeyInput{ - ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), - SecretARN: aws.String(d.Get("secret_arn").(string)), + secretARN, connectionID, err := macSecKeyAssociationParseResourceID(d.Id()) + if err != nil { + return sdkdiag.AppendFromErr(diags, err) } - log.Printf("[DEBUG] Disassociating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) - _, err := conn.DisassociateMacSecKey(ctx, input) + log.Printf("[DEBUG] Deleting Direct Connect MACSec Key Association: %s", d.Id()) + _, err = conn.DisassociateMacSecKey(ctx, &directconnect.DisassociateMacSecKeyInput{ + ConnectionId: aws.String(connectionID), + SecretARN: aws.String(secretARN), + }) if err != nil { - return sdkdiag.AppendErrorf(diags, "Unable to disassociate MACSec secret key on Direct Connect Connection (%s): %s", *input.ConnectionId, err) + return sdkdiag.AppendErrorf(diags, "deleting MACSec Key Association (%s): %s", d.Id(), err) } return diags } -// MacSecKeyParseSecretARN parses the secret ARN returned from a CMK or secret_arn -func MacSecKeyParseSecretARN(output *directconnect.AssociateMacSecKeyOutput) string { - var result string +const macSecKeyAssociationResourceIDSeparator = "_" - for _, key := range output.MacSecKeys { - result = aws.ToString(key.SecretARN) +func macSecKeyAssociationCreateResourceID(secretARN, connectionID string) string { + parts := []string{secretARN, connectionID} + id := strings.Join(parts, macSecKeyAssociationResourceIDSeparator) + + return id +} + +func macSecKeyAssociationParseResourceID(id string) (string, string, error) { + parts := strings.SplitN(id, macSecKeyAssociationResourceIDSeparator, 2) + + if len(parts) == 2 && parts[0] != "" && parts[1] != "" { + return parts[0], parts[1], nil } - return result + return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected secretArn%[2]sconnectionId", id, macSecKeyAssociationResourceIDSeparator) } -// MacSecKeyParseID parses the resource ID and returns the secret ARN and connection ID -func MacSecKeyParseID(id string) (string, string, error) { - parts := strings.SplitN(id, "_", 2) +func findMacSecKeyByTwoPartKey(ctx context.Context, conn *directconnect.Client, connectionID, secretARN string) (*awstypes.MacSecKey, error) { + output, err := findConnectionByID(ctx, conn, connectionID) - if len(parts) != 2 || parts[0] == "" || parts[1] == "" { - return "", "", &retry.NotFoundError{} + if err != nil { + return nil, err } - return parts[0], parts[1], nil + return tfresource.AssertSingleValueResult(tfslices.Filter(output.MacSecKeys, func(v awstypes.MacSecKey) bool { + return aws.ToString(v.SecretARN) == secretARN + })) } diff --git a/internal/service/directconnect/macsec_key_test.go b/internal/service/directconnect/macsec_key_test.go index 7770bfa57ae..063d6f8a12b 100644 --- a/internal/service/directconnect/macsec_key_test.go +++ b/internal/service/directconnect/macsec_key_test.go @@ -4,40 +4,40 @@ package directconnect_test import ( + "context" "crypto/rand" "encoding/hex" "fmt" - "os" "testing" "github.com/YakDriver/regexache" "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" + tfdirectconnect "github.com/hashicorp/terraform-provider-aws/internal/service/directconnect" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccDirectConnectMacSecKey_withCkn(t *testing.T) { +func TestAccDirectConnectMacSecKeyAssociation_withCkn(t *testing.T) { ctx := acctest.Context(t) - // Requires an existing MACsec-capable DX connection set as environmental variable - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") resourceName := "aws_dx_macsec_key_association.test" - ckn := testAccDirecConnectMacSecGenerateHex() - cak := testAccDirecConnectMacSecGenerateHex() + ckn := testAccMacSecGenerateHex() + cak := testAccMacSecGenerateHex() resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: nil, + CheckDestroy: testAccCheckMacSecKeyAssociationDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccMacSecConfig_withCkn(ckn, cak, connectionId), + Config: testAccMacSecKeyAssociationConfig_withCkn(ckn, cak, connectionID), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + testAccCheckMacSecKeyAssociationExists(ctx, resourceName), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestMatchResourceAttr(resourceName, "ckn", regexache.MustCompile(ckn)), ), }, @@ -52,34 +52,24 @@ func TestAccDirectConnectMacSecKey_withCkn(t *testing.T) { }) } -func TestAccDirectConnectMacSecKey_withSecret(t *testing.T) { +func TestAccDirectConnectMacSecKeyAssociation_withSecret(t *testing.T) { ctx := acctest.Context(t) - // Requires an existing MACsec-capable DX connection set as environmental variable - dxKey := "DX_CONNECTION_ID" - connectionId := os.Getenv(dxKey) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", dxKey) - } - - secretKey := "SECRET_ARN" - secretArn := os.Getenv(secretKey) - if secretArn == "" { - t.Skipf("Environment variable %s is not set", secretKey) - } - + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") + secretARN := acctest.SkipIfEnvVarNotSet(t, "SECRET_ARN") resourceName := "aws_dx_macsec_key_association.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: nil, + CheckDestroy: testAccCheckMacSecKeyAssociationDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccMacSecConfig_withSecret(secretArn, connectionId), + Config: testAccMacSecKeyAssociationConfig_withSecret(secretARN, connectionID), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), - resource.TestCheckResourceAttr(resourceName, "secret_arn", secretArn), + testAccCheckMacSecKeyAssociationExists(ctx, resourceName), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), + resource.TestCheckResourceAttr(resourceName, "secret_arn", secretARN), ), }, { @@ -91,8 +81,7 @@ func TestAccDirectConnectMacSecKey_withSecret(t *testing.T) { }) } -// testAccDirecConnectMacSecGenerateKey generates a 64-character hex string to be used as CKN or CAK -func testAccDirecConnectMacSecGenerateHex() string { +func testAccMacSecGenerateHex() string { s := make([]byte, 32) if _, err := rand.Read(s); err != nil { return "" @@ -100,20 +89,59 @@ func testAccDirecConnectMacSecGenerateHex() string { return hex.EncodeToString(s) } -func testAccMacSecConfig_withCkn(ckn, cak, connectionId string) string { +func testAccCheckMacSecKeyAssociationDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_dx_macsec_key_association" { + continue + } + + _, err := tfdirectconnect.FindMacSecKeyByTwoPartKey(ctx, conn, rs.Primary.Attributes[names.AttrConnectionID], rs.Primary.Attributes["secret_arn"]) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Direct Connect MACSec Key Association %s still exists", rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckMacSecKeyAssociationExists(ctx context.Context, name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).DirectConnectClient(ctx) + + _, err := tfdirectconnect.FindMacSecKeyByTwoPartKey(ctx, conn, rs.Primary.Attributes[names.AttrConnectionID], rs.Primary.Attributes["secret_arn"]) + + return err + } +} + +func testAccMacSecKeyAssociationConfig_withCkn(ckn, cak, connectionID string) string { return fmt.Sprintf(` resource "aws_dx_macsec_key_association" "test" { connection_id = %[3]q ckn = %[1]q cak = %[2]q } - - -`, ckn, cak, connectionId) +`, ckn, cak, connectionID) } -// Can only be used with an EXISTING secrets created by previous association - cannot create secrets from scratch -func testAccMacSecConfig_withSecret(secretArn, connectionId string) string { +// Can only be used with an EXISTING secrets created by previous association - cannot create secrets from scratch. +func testAccMacSecKeyAssociationConfig_withSecret(secretARN, connectionID string) string { return fmt.Sprintf(` data "aws_secretsmanager_secret" "test" { arn = %[1]q @@ -123,7 +151,5 @@ resource "aws_dx_macsec_key_association" "test" { connection_id = %[2]q secret_arn = data.aws_secretsmanager_secret.test.arn } - - -`, secretArn, connectionId) +`, secretARN, connectionID) } diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 4dc4421f6de..3d7fd197c77 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -144,8 +144,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceMacSecKeyAssociation, + Factory: resourceMacSecKeyAssociation, TypeName: "aws_dx_macsec_key_association", + Name: "MACSec Key Association", }, { Factory: ResourcePrivateVirtualInterface, From baf78abc9a061d8cd41bffb01c797a79e878cd0c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 12:34:04 -0400 Subject: [PATCH 25/59] r/aws_dx_macsec_key_association: Correct source file names. --- .../directconnect/{macsec_key.go => macsec_key_association.go} | 0 .../{macsec_key_test.go => macsec_key_association_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename internal/service/directconnect/{macsec_key.go => macsec_key_association.go} (100%) rename internal/service/directconnect/{macsec_key_test.go => macsec_key_association_test.go} (100%) diff --git a/internal/service/directconnect/macsec_key.go b/internal/service/directconnect/macsec_key_association.go similarity index 100% rename from internal/service/directconnect/macsec_key.go rename to internal/service/directconnect/macsec_key_association.go diff --git a/internal/service/directconnect/macsec_key_test.go b/internal/service/directconnect/macsec_key_association_test.go similarity index 100% rename from internal/service/directconnect/macsec_key_test.go rename to internal/service/directconnect/macsec_key_association_test.go From 9acb51534c0cbaeefbd533ee97c7cd5c747b7794 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:16:00 -0400 Subject: [PATCH 26/59] r/aws_dx_private_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 1 + .../private_virtual_interface.go | 100 +++++++++--------- .../private_virtual_interface_test.go | 73 +++++-------- .../directconnect/service_package_gen.go | 2 +- 4 files changed, 80 insertions(+), 96 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 3c48504b66b..0a9892a9c27 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -21,6 +21,7 @@ var ( ResourceHostedTransitVirtualInterfaceAccepter = resourceHostedTransitVirtualInterfaceAccepter ResourceLag = resourceLag ResourceMacSecKeyAssociation = resourceMacSecKeyAssociation + ResourcePrivateVirtualInterface = resourcePrivateVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/private_virtual_interface.go b/internal/service/directconnect/private_virtual_interface.go index 12de5bbc966..8f60ee7a27b 100644 --- a/internal/service/directconnect/private_virtual_interface.go +++ b/internal/service/directconnect/private_virtual_interface.go @@ -21,18 +21,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_private_virtual_interface", name="Private Virtual Interface") // @Tags(identifierAttribute="arn") -func ResourcePrivateVirtualInterface() *schema.Resource { +func resourcePrivateVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourcePrivateVirtualInterfaceCreate, ReadWithoutTimeout: resourcePrivateVirtualInterfaceRead, UpdateWithoutTimeout: resourcePrivateVirtualInterfaceUpdate, DeleteWithoutTimeout: resourcePrivateVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourcePrivateVirtualInterfaceImport, }, @@ -85,10 +87,10 @@ func ResourcePrivateVirtualInterface() *schema.Resource { ForceNew: true, }, "dx_gateway_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ConflictsWith: []string{"vpn_gateway_id"}, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ExactlyOneOf: []string{"dx_gateway_id", "vpn_gateway_id"}, }, "jumbo_frame_capable": { Type: schema.TypeBool, @@ -118,10 +120,10 @@ func ResourcePrivateVirtualInterface() *schema.Resource { ValidateFunc: validation.IntBetween(1, 4094), }, "vpn_gateway_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ConflictsWith: []string{"dx_gateway_id"}, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ExactlyOneOf: []string{"dx_gateway_id", "vpn_gateway_id"}, }, }, @@ -139,13 +141,7 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id") - dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id") - if vgwOk == dxgwOk { - return sdkdiag.AppendErrorf(diags, "One of ['vpn_gateway_id', 'dx_gateway_id'] must be set to create a Direct Connect private virtual interface") - } - - req := &directconnect.CreatePrivateVirtualInterfaceInput{ + input := &directconnect.CreatePrivateVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPrivateVirtualInterface: &awstypes.NewPrivateVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), @@ -157,32 +153,37 @@ func resourcePrivateVirtualInterfaceCreate(ctx context.Context, d *schema.Resour Vlan: int32(d.Get("vlan").(int)), }, } - if vgwOk && vgwIdRaw.(string) != "" { - req.NewPrivateVirtualInterface.VirtualGatewayId = aws.String(vgwIdRaw.(string)) - } - if dxgwOk && dxgwIdRaw.(string) != "" { - req.NewPrivateVirtualInterface.DirectConnectGatewayId = aws.String(dxgwIdRaw.(string)) - } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewPrivateVirtualInterface.AmazonAddress = aws.String(v.(string)) + input.NewPrivateVirtualInterface.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewPrivateVirtualInterface.AuthKey = aws.String(v.(string)) + input.NewPrivateVirtualInterface.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewPrivateVirtualInterface.CustomerAddress = aws.String(v.(string)) + input.NewPrivateVirtualInterface.CustomerAddress = aws.String(v.(string)) + } + + if v, ok := d.GetOk("dx_gateway_id"); ok { + input.NewPrivateVirtualInterface.DirectConnectGatewayId = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect private virtual interface: %#v", req) - resp, err := conn.CreatePrivateVirtualInterface(ctx, req) + if v, ok := d.GetOk("vpn_gateway_id"); ok { + input.NewPrivateVirtualInterface.VirtualGatewayId = aws.String(v.(string)) + } + + output, err := conn.CreatePrivateVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect private virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Private Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterfaceId)) - if err := privateVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitPrivateVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Private Virtual Interface (%s) create: %s", d.Id(), err) } return append(diags, resourcePrivateVirtualInterfaceRead(ctx, d, meta)...) @@ -192,16 +193,18 @@ func resourcePrivateVirtualInterfaceRead(ctx context.Context, d *schema.Resource var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect private virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Private Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Private Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) @@ -237,8 +240,8 @@ func resourcePrivateVirtualInterfaceUpdate(ctx context.Context, d *schema.Resour return diags } - if err := privateVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitPrivateVirtualInterfaceAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Private Virtual Interface (%s) update: %s", d.Id(), err) } return append(diags, resourcePrivateVirtualInterfaceRead(ctx, d, meta)...) @@ -251,13 +254,11 @@ func resourcePrivateVirtualInterfaceDelete(ctx context.Context, d *schema.Resour func resourcePrivateVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "private" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -266,10 +267,13 @@ func resourcePrivateVirtualInterfaceImport(ctx context.Context, d *schema.Resour return []*schema.ResourceData{d}, nil } -func privateVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitPrivateVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/private_virtual_interface_test.go b/internal/service/directconnect/private_virtual_interface_test.go index cbb6a965333..8c057ed7dd3 100644 --- a/internal/service/directconnect/private_virtual_interface_test.go +++ b/internal/service/directconnect/private_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -22,11 +21,7 @@ import ( func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" @@ -42,7 +37,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPrivateVirtualInterfaceConfig_basic(connectionId, rName, bgpAsn, vlan), + Config: testAccPrivateVirtualInterfaceConfig_basic(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -52,7 +47,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), @@ -63,7 +58,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { ), }, { - Config: testAccPrivateVirtualInterfaceConfig_updated(connectionId, rName, bgpAsn, vlan), + Config: testAccPrivateVirtualInterfaceConfig_updated(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -73,7 +68,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "9001"), @@ -83,7 +78,6 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -95,11 +89,7 @@ func TestAccDirectConnectPrivateVirtualInterface_basic(t *testing.T) { func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" @@ -115,7 +105,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { CheckDestroy: testAccCheckPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPrivateVirtualInterfaceConfig_tags(connectionId, rName, bgpAsn, vlan), + Config: testAccPrivateVirtualInterfaceConfig_tags(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -125,7 +115,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), @@ -139,7 +129,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { ), }, { - Config: testAccPrivateVirtualInterfaceConfig_tagsUpdated(connectionId, rName, bgpAsn, vlan), + Config: testAccPrivateVirtualInterfaceConfig_tagsUpdated(connectionID, rName, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -149,7 +139,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), resource.TestCheckResourceAttr(resourceName, "mtu", "1500"), @@ -162,7 +152,6 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrPair(resourceName, "vpn_gateway_id", vpnGatewayResourceName, names.AttrID), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -174,11 +163,7 @@ func TestAccDirectConnectPrivateVirtualInterface_tags(t *testing.T) { func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" @@ -195,7 +180,7 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { CheckDestroy: testAccCheckPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPrivateVirtualInterfaceConfig_gateway(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccPrivateVirtualInterfaceConfig_gateway(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -205,7 +190,7 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -215,7 +200,6 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -227,11 +211,7 @@ func TestAccDirectConnectPrivateVirtualInterface_dxGateway(t *testing.T) { func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_private_virtual_interface.test" @@ -248,7 +228,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { CheckDestroy: testAccCheckPrivateVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPrivateVirtualInterfaceConfig_siteLinkBasic(connectionId, rName, amzAsn, bgpAsn, vlan, true), + Config: testAccPrivateVirtualInterfaceConfig_siteLinkBasic(connectionID, rName, amzAsn, bgpAsn, vlan, true), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -258,7 +238,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -270,7 +250,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { ), }, { - Config: testAccPrivateVirtualInterfaceConfig_siteLinkUpdated(connectionId, rName, amzAsn, bgpAsn, vlan, false), + Config: testAccPrivateVirtualInterfaceConfig_siteLinkUpdated(connectionID, rName, amzAsn, bgpAsn, vlan, false), Check: resource.ComposeTestCheckFunc( testAccCheckPrivateVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -280,7 +260,7 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -291,7 +271,6 @@ func TestAccDirectConnectPrivateVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "sitelink_enabled", acctest.CtFalse), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -321,7 +300,7 @@ resource "aws_vpn_gateway" "test" { } func testAccPrivateVirtualInterfaceConfig_basic(cid, rName string, bgpAsn, vlan int) string { - return testAccPrivateVirtualInterfaceConfig_vpnGateway(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPrivateVirtualInterfaceConfig_vpnGateway(rName), fmt.Sprintf(` resource "aws_dx_private_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -330,11 +309,11 @@ resource "aws_dx_private_virtual_interface" "test" { vlan = %[4]d vpn_gateway_id = aws_vpn_gateway.test.id } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccPrivateVirtualInterfaceConfig_updated(cid, rName string, bgpAsn, vlan int) string { - return testAccPrivateVirtualInterfaceConfig_vpnGateway(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPrivateVirtualInterfaceConfig_vpnGateway(rName), fmt.Sprintf(` resource "aws_dx_private_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -344,11 +323,11 @@ resource "aws_dx_private_virtual_interface" "test" { vlan = %[4]d vpn_gateway_id = aws_vpn_gateway.test.id } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccPrivateVirtualInterfaceConfig_tags(cid, rName string, bgpAsn, vlan int) string { - return testAccPrivateVirtualInterfaceConfig_vpnGateway(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPrivateVirtualInterfaceConfig_vpnGateway(rName), fmt.Sprintf(` resource "aws_dx_private_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -363,11 +342,11 @@ resource "aws_dx_private_virtual_interface" "test" { Key2 = "Value2a" } } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccPrivateVirtualInterfaceConfig_tagsUpdated(cid, rName string, bgpAsn, vlan int) string { - return testAccPrivateVirtualInterfaceConfig_vpnGateway(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPrivateVirtualInterfaceConfig_vpnGateway(rName), fmt.Sprintf(` resource "aws_dx_private_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -382,7 +361,7 @@ resource "aws_dx_private_virtual_interface" "test" { Key3 = "Value3" } } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccPrivateVirtualInterfaceConfig_gateway(cid, rName string, amzAsn, bgpAsn, vlan int) string { diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 3d7fd197c77..aa3e8a2c82f 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -149,7 +149,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "MACSec Key Association", }, { - Factory: ResourcePrivateVirtualInterface, + Factory: resourcePrivateVirtualInterface, TypeName: "aws_dx_private_virtual_interface", Name: "Private Virtual Interface", Tags: &types.ServicePackageResourceTags{ From 37ce093b7b64cd95fc2d717bf34793af380d004c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:22:44 -0400 Subject: [PATCH 27/59] r/aws_dx_public_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 1 + .../directconnect/public_virtual_interface.go | 64 +++++++++++-------- .../public_virtual_interface_test.go | 27 +++----- .../directconnect/service_package_gen.go | 2 +- 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 0a9892a9c27..43eeb457609 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -22,6 +22,7 @@ var ( ResourceLag = resourceLag ResourceMacSecKeyAssociation = resourceMacSecKeyAssociation ResourcePrivateVirtualInterface = resourcePrivateVirtualInterface + ResourcePublicVirtualInterface = resourcePublicVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/public_virtual_interface.go b/internal/service/directconnect/public_virtual_interface.go index 7249d9398ce..e1b30e3a385 100644 --- a/internal/service/directconnect/public_virtual_interface.go +++ b/internal/service/directconnect/public_virtual_interface.go @@ -22,21 +22,24 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_public_virtual_interface", name="Public Virtual Interface") // @Tags(identifierAttribute="arn") -func ResourcePublicVirtualInterface() *schema.Resource { +func resourcePublicVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourcePublicVirtualInterfaceCreate, ReadWithoutTimeout: resourcePublicVirtualInterfaceRead, UpdateWithoutTimeout: resourcePublicVirtualInterfaceUpdate, DeleteWithoutTimeout: resourcePublicVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourcePublicVirtualInterfaceImport, }, + CustomizeDiff: customdiff.Sequence( resourcePublicVirtualInterfaceCustomizeDiff, verify.SetTagsDiff, @@ -122,7 +125,7 @@ func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.Resourc var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - req := &directconnect.CreatePublicVirtualInterfaceInput{ + input := &directconnect.CreatePublicVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewPublicVirtualInterface: &awstypes.NewPublicVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), @@ -132,29 +135,33 @@ func resourcePublicVirtualInterfaceCreate(ctx context.Context, d *schema.Resourc Vlan: int32(d.Get("vlan").(int)), }, } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewPublicVirtualInterface.AmazonAddress = aws.String(v.(string)) + input.NewPublicVirtualInterface.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewPublicVirtualInterface.AuthKey = aws.String(v.(string)) + input.NewPublicVirtualInterface.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewPublicVirtualInterface.CustomerAddress = aws.String(v.(string)) + input.NewPublicVirtualInterface.CustomerAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("route_filter_prefixes"); ok { - req.NewPublicVirtualInterface.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) + input.NewPublicVirtualInterface.RouteFilterPrefixes = expandRouteFilterPrefixes(v.(*schema.Set).List()) } - log.Printf("[DEBUG] Creating Direct Connect public virtual interface: %#v", req) - resp, err := conn.CreatePublicVirtualInterface(ctx, req) + output, err := conn.CreatePublicVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect public virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Public Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterfaceId)) - if err := publicVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitPublicVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Public Virtual Interface (%s) create: %s", d.Id(), err) } return append(diags, resourcePublicVirtualInterfaceRead(ctx, d, meta)...) @@ -164,16 +171,18 @@ func resourcePublicVirtualInterfaceRead(ctx context.Context, d *schema.ResourceD var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Public Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Public Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) @@ -217,13 +226,11 @@ func resourcePublicVirtualInterfaceDelete(ctx context.Context, d *schema.Resourc func resourcePublicVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "public" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -248,10 +255,13 @@ func resourcePublicVirtualInterfaceCustomizeDiff(_ context.Context, diff *schema return nil } -func publicVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitPublicVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown, awstypes.VirtualInterfaceStateVerifying), + timeout, + ) } diff --git a/internal/service/directconnect/public_virtual_interface_test.go b/internal/service/directconnect/public_virtual_interface_test.go index 654684e0f99..f9539cbcb4c 100644 --- a/internal/service/directconnect/public_virtual_interface_test.go +++ b/internal/service/directconnect/public_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -22,11 +21,7 @@ import ( func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_public_virtual_interface.test" @@ -47,7 +42,7 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckPublicVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPublicVirtualInterfaceConfig_basic(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccPublicVirtualInterfaceConfig_basic(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -57,7 +52,7 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -67,7 +62,6 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -79,11 +73,7 @@ func TestAccDirectConnectPublicVirtualInterface_basic(t *testing.T) { func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_public_virtual_interface.test" @@ -100,7 +90,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { CheckDestroy: testAccCheckPublicVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccPublicVirtualInterfaceConfig_tags(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccPublicVirtualInterfaceConfig_tags(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -110,7 +100,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -124,7 +114,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { ), }, { - Config: testAccPublicVirtualInterfaceConfig_tagsUpdated(connectionId, rName, amazonAddress, customerAddress, bgpAsn, vlan), + Config: testAccPublicVirtualInterfaceConfig_tagsUpdated(connectionID, rName, amazonAddress, customerAddress, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckPublicVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -134,7 +124,7 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttr(resourceName, "customer_address", customerAddress), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceName, "route_filter_prefixes.#", acctest.Ct2), @@ -147,7 +137,6 @@ func TestAccDirectConnectPublicVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index aa3e8a2c82f..ab31ba6efd7 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -157,7 +157,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourcePublicVirtualInterface, + Factory: resourcePublicVirtualInterface, TypeName: "aws_dx_public_virtual_interface", Name: "Public Virtual Interface", Tags: &types.ServicePackageResourceTags{ From 3b57b7a17d1df67454934ddad0a9ae48fa701984 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:28:49 -0400 Subject: [PATCH 28/59] r/aws_dx_transit_virtual_interface: Tidy up. --- .../service/directconnect/exports_test.go | 1 + .../directconnect/service_package_gen.go | 2 +- .../transit_virtual_interface.go | 64 +++++++++------- .../transit_virtual_interface_test.go | 76 ++++++++----------- 4 files changed, 68 insertions(+), 75 deletions(-) diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 43eeb457609..304dba27682 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -23,6 +23,7 @@ var ( ResourceMacSecKeyAssociation = resourceMacSecKeyAssociation ResourcePrivateVirtualInterface = resourcePrivateVirtualInterface ResourcePublicVirtualInterface = resourcePublicVirtualInterface + ResourceTransitVirtualInterface = resourceTransitVirtualInterface FindBGPPeerByThreePartKey = findBGPPeerByThreePartKey FindConnectionByID = findConnectionByID diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index ab31ba6efd7..3ea89e87038 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -165,7 +165,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceTransitVirtualInterface, + Factory: resourceTransitVirtualInterface, TypeName: "aws_dx_transit_virtual_interface", Name: "Transit Virtual Interface", Tags: &types.ServicePackageResourceTags{ diff --git a/internal/service/directconnect/transit_virtual_interface.go b/internal/service/directconnect/transit_virtual_interface.go index bd4ffeebea7..c0821c3a9ce 100644 --- a/internal/service/directconnect/transit_virtual_interface.go +++ b/internal/service/directconnect/transit_virtual_interface.go @@ -21,18 +21,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_dx_transit_virtual_interface", name="Transit Virtual Interface") // @Tags(identifierAttribute="arn") -func ResourceTransitVirtualInterface() *schema.Resource { +func resourceTransitVirtualInterface() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceTransitVirtualInterfaceCreate, ReadWithoutTimeout: resourceTransitVirtualInterfaceRead, UpdateWithoutTimeout: resourceTransitVirtualInterfaceUpdate, DeleteWithoutTimeout: resourceTransitVirtualInterfaceDelete, + Importer: &schema.ResourceImporter{ StateContext: resourceTransitVirtualInterfaceImport, }, @@ -132,7 +134,7 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - req := &directconnect.CreateTransitVirtualInterfaceInput{ + input := &directconnect.CreateTransitVirtualInterfaceInput{ ConnectionId: aws.String(d.Get(names.AttrConnectionID).(string)), NewTransitVirtualInterface: &awstypes.NewTransitVirtualInterface{ AddressFamily: awstypes.AddressFamily(d.Get("address_family").(string)), @@ -145,26 +147,29 @@ func resourceTransitVirtualInterfaceCreate(ctx context.Context, d *schema.Resour Vlan: int32(d.Get("vlan").(int)), }, } + if v, ok := d.GetOk("amazon_address"); ok { - req.NewTransitVirtualInterface.AmazonAddress = aws.String(v.(string)) + input.NewTransitVirtualInterface.AmazonAddress = aws.String(v.(string)) } + if v, ok := d.GetOk("bgp_auth_key"); ok { - req.NewTransitVirtualInterface.AuthKey = aws.String(v.(string)) + input.NewTransitVirtualInterface.AuthKey = aws.String(v.(string)) } + if v, ok := d.GetOk("customer_address"); ok { - req.NewTransitVirtualInterface.CustomerAddress = aws.String(v.(string)) + input.NewTransitVirtualInterface.CustomerAddress = aws.String(v.(string)) } - log.Printf("[DEBUG] Creating Direct Connect transit virtual interface: %#v", req) - resp, err := conn.CreateTransitVirtualInterface(ctx, req) + output, err := conn.CreateTransitVirtualInterface(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating Direct Connect transit virtual interface: %s", err) + return sdkdiag.AppendErrorf(diags, "creating Direct Connect Transit Virtual Interface: %s", err) } - d.SetId(aws.ToString(resp.VirtualInterface.VirtualInterfaceId)) + d.SetId(aws.ToString(output.VirtualInterface.VirtualInterfaceId)) - if err := transitVirtualInterfaceWaitUntilAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitTransitVirtualInterfaceAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Transit Virtual Interface (%s) create: %s", d.Id(), err) } return append(diags, resourceTransitVirtualInterfaceRead(ctx, d, meta)...) @@ -174,16 +179,18 @@ func resourceTransitVirtualInterfaceRead(ctx context.Context, d *schema.Resource var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) - if err != nil { - return sdkdiag.AppendFromErr(diags, err) - } - if vif == nil { - log.Printf("[WARN] Direct Connect transit virtual interface (%s) not found, removing from state", d.Id()) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Direct Connect Transit Virtual Interface (%s) not found, removing from state", d.Id()) d.SetId("") return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Transit Virtual Interface (%s): %s", d.Id(), err) + } + d.Set("address_family", vif.AddressFamily) d.Set("amazon_address", vif.AmazonAddress) d.Set("amazon_side_asn", strconv.FormatInt(aws.ToInt64(vif.AmazonSideAsn), 10)) @@ -218,8 +225,8 @@ func resourceTransitVirtualInterfaceUpdate(ctx context.Context, d *schema.Resour return diags } - if err := transitVirtualInterfaceWaitUntilAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { - return sdkdiag.AppendFromErr(diags, err) + if _, err := waitTransitVirtualInterfaceAvailable(ctx, meta.(*conns.AWSClient).DirectConnectClient(ctx), d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for Direct Connect Transit Virtual Interface (%s) update: %s", d.Id(), err) } return append(diags, resourceTransitVirtualInterfaceRead(ctx, d, meta)...) @@ -232,13 +239,11 @@ func resourceTransitVirtualInterfaceDelete(ctx context.Context, d *schema.Resour func resourceTransitVirtualInterfaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) - vif, err := virtualInterfaceRead(ctx, d.Id(), conn) + vif, err := findVirtualInterfaceByID(ctx, conn, d.Id()) + if err != nil { return nil, err } - if vif == nil { - return nil, fmt.Errorf("virtual interface (%s) not found", d.Id()) - } if vifType := aws.ToString(vif.VirtualInterfaceType); vifType != "transit" { return nil, fmt.Errorf("virtual interface (%s) has incorrect type: %s", d.Id(), vifType) @@ -247,10 +252,13 @@ func resourceTransitVirtualInterfaceImport(ctx context.Context, d *schema.Resour return []*schema.ResourceData{d}, nil } -func transitVirtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration) error { - return virtualInterfaceWaitUntilAvailable(ctx, conn, - vifId, - timeout, +func waitTransitVirtualInterfaceAvailable(ctx context.Context, conn *directconnect.Client, id string, timeout time.Duration) (*awstypes.VirtualInterface, error) { + return waitVirtualInterfaceAvailable( + ctx, + conn, + id, enum.Slice(awstypes.VirtualInterfaceStatePending), - enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown)) + enum.Slice(awstypes.VirtualInterfaceStateAvailable, awstypes.VirtualInterfaceStateDown), + timeout, + ) } diff --git a/internal/service/directconnect/transit_virtual_interface_test.go b/internal/service/directconnect/transit_virtual_interface_test.go index 181822eb54c..0f78cc194b4 100644 --- a/internal/service/directconnect/transit_virtual_interface_test.go +++ b/internal/service/directconnect/transit_virtual_interface_test.go @@ -6,7 +6,6 @@ package directconnect_test import ( "context" "fmt" - "os" "strconv" "testing" @@ -24,9 +23,9 @@ func TestAccDirectConnectTransitVirtualInterface_serial(t *testing.T) { t.Parallel() testCases := map[string]func(t *testing.T){ - acctest.CtBasic: testAccTransitVirtualInterface_basic, - "tags": testAccTransitVirtualInterface_tags, - "sitelink": testAccTransitVirtualInterface_siteLink, + acctest.CtBasic: testAccTransitVirtualInterface_basic, + "tags": testAccTransitVirtualInterface_tags, + "sitelink": testAccTransitVirtualInterface_siteLink, } acctest.RunSerialTests1Level(t, testCases, 0) @@ -34,11 +33,7 @@ func TestAccDirectConnectTransitVirtualInterface_serial(t *testing.T) { func testAccTransitVirtualInterface_basic(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" @@ -55,7 +50,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { CheckDestroy: testAccCheckTransitVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccTransitVirtualInterfaceConfig_basic(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccTransitVirtualInterfaceConfig_basic(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -65,7 +60,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -76,7 +71,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { ), }, { - Config: testAccTransitVirtualInterfaceConfig_updated(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccTransitVirtualInterfaceConfig_updated(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -86,7 +81,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -96,7 +91,6 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -108,11 +102,7 @@ func testAccTransitVirtualInterface_basic(t *testing.T) { func testAccTransitVirtualInterface_tags(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" @@ -129,7 +119,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { CheckDestroy: testAccCheckTransitVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccTransitVirtualInterfaceConfig_tags(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccTransitVirtualInterfaceConfig_tags(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -139,7 +129,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -153,7 +143,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { ), }, { - Config: testAccTransitVirtualInterfaceConfig_tagsUpdated(connectionId, rName, amzAsn, bgpAsn, vlan), + Config: testAccTransitVirtualInterfaceConfig_tagsUpdated(connectionID, rName, amzAsn, bgpAsn, vlan), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -163,7 +153,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -176,7 +166,6 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -188,11 +177,7 @@ func testAccTransitVirtualInterface_tags(t *testing.T) { func testAccTransitVirtualInterface_siteLink(t *testing.T) { ctx := acctest.Context(t) - key := "DX_CONNECTION_ID" - connectionId := os.Getenv(key) - if connectionId == "" { - t.Skipf("Environment variable %s is not set", key) - } + connectionID := acctest.SkipIfEnvVarNotSet(t, "DX_CONNECTION_ID") var vif awstypes.VirtualInterface resourceName := "aws_dx_transit_virtual_interface.test" @@ -209,7 +194,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { CheckDestroy: testAccCheckTransitVirtualInterfaceDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccTransitVirtualInterfaceConfig_siteLinkBasic(connectionId, rName, amzAsn, bgpAsn, vlan, true), + Config: testAccTransitVirtualInterfaceConfig_siteLinkBasic(connectionID, rName, amzAsn, bgpAsn, vlan, true), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -219,7 +204,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -231,7 +216,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { ), }, { - Config: testAccTransitVirtualInterfaceConfig_siteLinkUpdated(connectionId, rName, amzAsn, bgpAsn, vlan, false), + Config: testAccTransitVirtualInterfaceConfig_siteLinkUpdated(connectionID, rName, amzAsn, bgpAsn, vlan, false), Check: resource.ComposeTestCheckFunc( testAccCheckTransitVirtualInterfaceExists(ctx, resourceName, &vif), resource.TestCheckResourceAttr(resourceName, "address_family", "ipv4"), @@ -241,7 +226,7 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "aws_device"), resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(bgpAsn)), resource.TestCheckResourceAttrSet(resourceName, "bgp_auth_key"), - resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionId), + resource.TestCheckResourceAttr(resourceName, names.AttrConnectionID, connectionID), resource.TestCheckResourceAttrSet(resourceName, "customer_address"), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", dxGatewayResourceName, names.AttrID), resource.TestCheckResourceAttr(resourceName, "jumbo_frame_capable", acctest.CtTrue), @@ -252,7 +237,6 @@ func testAccTransitVirtualInterface_siteLink(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vlan", strconv.Itoa(vlan)), ), }, - // Test import. { ResourceName: resourceName, ImportState: true, @@ -282,7 +266,7 @@ resource "aws_dx_gateway" "test" { } func testAccTransitVirtualInterfaceConfig_basic(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -291,11 +275,11 @@ resource "aws_dx_transit_virtual_interface" "test" { name = %[2]q vlan = %[4]d } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccTransitVirtualInterfaceConfig_updated(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -305,11 +289,11 @@ resource "aws_dx_transit_virtual_interface" "test" { name = %[2]q vlan = %[4]d } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccTransitVirtualInterfaceConfig_tags(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -324,11 +308,11 @@ resource "aws_dx_transit_virtual_interface" "test" { Key2 = "Value2a" } } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccTransitVirtualInterfaceConfig_tagsUpdated(cid, rName string, amzAsn, bgpAsn, vlan int) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -343,11 +327,11 @@ resource "aws_dx_transit_virtual_interface" "test" { Key3 = "Value3" } } -`, cid, rName, bgpAsn, vlan) +`, cid, rName, bgpAsn, vlan)) } func testAccTransitVirtualInterfaceConfig_siteLinkBasic(cid, rName string, amzAsn, bgpAsn, vlan int, sitelink_enabled bool) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -358,11 +342,11 @@ resource "aws_dx_transit_virtual_interface" "test" { sitelink_enabled = %[5]t vlan = %[4]d } -`, cid, rName, bgpAsn, vlan, sitelink_enabled) +`, cid, rName, bgpAsn, vlan, sitelink_enabled)) } func testAccTransitVirtualInterfaceConfig_siteLinkUpdated(cid, rName string, amzAsn, bgpAsn, vlan int, sitelink_enabled bool) string { - return testAccTransitVirtualInterfaceConfig_base(rName, amzAsn) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccTransitVirtualInterfaceConfig_base(rName, amzAsn), fmt.Sprintf(` resource "aws_dx_transit_virtual_interface" "test" { address_family = "ipv4" bgp_asn = %[3]d @@ -373,5 +357,5 @@ resource "aws_dx_transit_virtual_interface" "test" { sitelink_enabled = %[5]t vlan = %[4]d } -`, cid, rName, bgpAsn, vlan, sitelink_enabled) +`, cid, rName, bgpAsn, vlan, sitelink_enabled)) } From ff072678ae9cc30c91ac373333ac31e732d34657 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:37:45 -0400 Subject: [PATCH 29/59] d/aws_dx_router_configuration: Tidy up. --- .../service/directconnect/exports_test.go | 1 - .../router_configuration_data_source.go | 41 +++++++-------- .../router_configuration_data_source_test.go | 11 ++-- .../directconnect/service_package_gen.go | 3 +- .../service/directconnect/validate_test.go | 50 ------------------- 5 files changed, 22 insertions(+), 84 deletions(-) delete mode 100644 internal/service/directconnect/validate_test.go diff --git a/internal/service/directconnect/exports_test.go b/internal/service/directconnect/exports_test.go index 304dba27682..51b9e0438af 100644 --- a/internal/service/directconnect/exports_test.go +++ b/internal/service/directconnect/exports_test.go @@ -36,5 +36,4 @@ var ( FindMacSecKeyByTwoPartKey = findMacSecKeyByTwoPartKey FindVirtualInterfaceByID = findVirtualInterfaceByID GatewayAssociationStateUpgradeV0 = gatewayAssociationStateUpgradeV0 - ValidConnectionBandWidth = validConnectionBandWidth ) diff --git a/internal/service/directconnect/router_configuration_data_source.go b/internal/service/directconnect/router_configuration_data_source.go index 3af336103e4..647fd32cb02 100644 --- a/internal/service/directconnect/router_configuration_data_source.go +++ b/internal/service/directconnect/router_configuration_data_source.go @@ -13,13 +13,12 @@ import ( "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" - "github.com/hashicorp/terraform-provider-aws/internal/create" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" - "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_dx_router_configuration") -func DataSourceRouterConfiguration() *schema.Resource { +// @SDKDataSource("aws_dx_router_configuration", name="Router Configuration") +func dataSourceRouterConfiguration() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceRouterConfigurationRead, @@ -77,41 +76,35 @@ func DataSourceRouterConfiguration() *schema.Resource { } } -const ( - DSNameRouterConfiguration = "Router Configuration Data Source" -) - func dataSourceRouterConfigurationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) routerTypeIdentifier := d.Get("router_type_identifier").(string) - virtualInterfaceId := d.Get("virtual_interface_id").(string) + vifID := d.Get("virtual_interface_id").(string) + id := fmt.Sprintf("%s:%s", vifID, routerTypeIdentifier) + output, err := findRouterConfigurationByTwoPartKey(ctx, conn, routerTypeIdentifier, vifID) - out, err := findRouterConfigurationByTypeAndVif(ctx, conn, routerTypeIdentifier, virtualInterfaceId) if err != nil { - return create.AppendDiagError(diags, names.DirectConnect, create.ErrActionReading, DSNameRouterConfiguration, virtualInterfaceId, err) + return sdkdiag.AppendErrorf(diags, "reading Direct Connect Router Configuration (%s): %s", id, err) } - d.SetId(fmt.Sprintf("%s:%s", virtualInterfaceId, routerTypeIdentifier)) - - d.Set("customer_router_config", out.CustomerRouterConfig) - d.Set("router_type_identifier", out.Router.RouterTypeIdentifier) - d.Set("virtual_interface_id", out.VirtualInterfaceId) - d.Set("virtual_interface_name", out.VirtualInterfaceName) - - if err := d.Set("router", flattenRouter(out.Router)); err != nil { - return create.AppendDiagError(diags, names.DirectConnect, create.ErrActionSetting, DSNameRouterConfiguration, d.Id(), err) + d.SetId(id) + d.Set("customer_router_config", output.CustomerRouterConfig) + if err := d.Set("router", flattenRouter(output.Router)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting router: %s", err) } + d.Set("router_type_identifier", output.Router.RouterTypeIdentifier) + d.Set("virtual_interface_id", output.VirtualInterfaceId) + d.Set("virtual_interface_name", output.VirtualInterfaceName) return diags } -func findRouterConfigurationByTypeAndVif(ctx context.Context, conn *directconnect.Client, routerTypeIdentifier string, virtualInterfaceId string) (*directconnect.DescribeRouterConfigurationOutput, error) { +func findRouterConfigurationByTwoPartKey(ctx context.Context, conn *directconnect.Client, routerTypeIdentifier, vifID string) (*directconnect.DescribeRouterConfigurationOutput, error) { input := &directconnect.DescribeRouterConfigurationInput{ RouterTypeIdentifier: aws.String(routerTypeIdentifier), - VirtualInterfaceId: aws.String(virtualInterfaceId), + VirtualInterfaceId: aws.String(vifID), } output, err := conn.DescribeRouterConfiguration(ctx, input) @@ -120,7 +113,7 @@ func findRouterConfigurationByTypeAndVif(ctx context.Context, conn *directconnec return nil, err } - if output == nil { + if output == nil || output.Router == nil { return nil, tfresource.NewEmptyResultError(input) } diff --git a/internal/service/directconnect/router_configuration_data_source_test.go b/internal/service/directconnect/router_configuration_data_source_test.go index 09c3ee2c69d..12211bf6952 100644 --- a/internal/service/directconnect/router_configuration_data_source_test.go +++ b/internal/service/directconnect/router_configuration_data_source_test.go @@ -5,7 +5,6 @@ package directconnect_test import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -15,11 +14,7 @@ import ( func TestAccDirectConnectRouterConfigurationDataSource_basic(t *testing.T) { ctx := acctest.Context(t) - key := "VIRTUAL_INTERFACE_ID" - virtualInterfaceId := os.Getenv(key) - if virtualInterfaceId == "" { - t.Skipf("Environment variable %s is not set", key) - } + vifID := acctest.SkipIfEnvVarNotSet(t, "VIRTUAL_INTERFACE_ID") dataSourceName := "data.aws_dx_router_configuration.test" routerTypeIdentifier := "CiscoSystemsInc-2900SeriesRouters-IOS124" @@ -33,9 +28,9 @@ func TestAccDirectConnectRouterConfigurationDataSource_basic(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccRouterConfigurationDataSourceConfig_basic(virtualInterfaceId, routerTypeIdentifier), + Config: testAccRouterConfigurationDataSourceConfig_basic(vifID, routerTypeIdentifier), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "virtual_interface_id", virtualInterfaceId), + resource.TestCheckResourceAttr(dataSourceName, "virtual_interface_id", vifID), resource.TestCheckResourceAttr(dataSourceName, "router_type_identifier", routerTypeIdentifier), resource.TestCheckResourceAttrSet(dataSourceName, "virtual_interface_name"), resource.TestCheckResourceAttr(dataSourceName, "router.0.platform", "2900 Series Routers"), diff --git a/internal/service/directconnect/service_package_gen.go b/internal/service/directconnect/service_package_gen.go index 3ea89e87038..c8565890e3f 100644 --- a/internal/service/directconnect/service_package_gen.go +++ b/internal/service/directconnect/service_package_gen.go @@ -45,8 +45,9 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Locations", }, { - Factory: DataSourceRouterConfiguration, + Factory: dataSourceRouterConfiguration, TypeName: "aws_dx_router_configuration", + Name: "Router Configuration", }, } } diff --git a/internal/service/directconnect/validate_test.go b/internal/service/directconnect/validate_test.go deleted file mode 100644 index 79bea3a65f7..00000000000 --- a/internal/service/directconnect/validate_test.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package directconnect_test - -import ( - "testing" - - "github.com/hashicorp/terraform-provider-aws/internal/acctest" - tfdirectconnect "github.com/hashicorp/terraform-provider-aws/internal/service/directconnect" -) - -func TestValidConnectionBandWidth(t *testing.T) { - t.Parallel() - - validBandwidths := []string{ - "1Gbps", - "2Gbps", - "5Gbps", - "10Gbps", - "100Gbps", - "50Mbps", - "100Mbps", - "200Mbps", - "300Mbps", - "400Mbps", - "500Mbps", - } - for _, v := range validBandwidths { - _, errors := tfdirectconnect.ValidConnectionBandWidth()(v, "bandwidth") - if len(errors) != 0 { - t.Fatalf("%q should be a valid bandwidth: %q", v, errors) - } - } - - invalidBandwidths := []string{ - "1Tbps", - "10GBpS", - "42Mbps", - acctest.Ct0, - "???", - "a lot", - } - for _, v := range invalidBandwidths { - _, errors := tfdirectconnect.ValidConnectionBandWidth()(v, "bandwidth") - if len(errors) == 0 { - t.Fatalf("%q should be an invalid bandwidth", v) - } - } -} From 75ca632e9662716e4a1c84648e576d0fc276433c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:41:21 -0400 Subject: [PATCH 30/59] Remove unused functions. --- internal/service/directconnect/vif.go | 53 --------------------------- 1 file changed, 53 deletions(-) diff --git a/internal/service/directconnect/vif.go b/internal/service/directconnect/vif.go index 93bc831dbfd..10f26bc01ff 100644 --- a/internal/service/directconnect/vif.go +++ b/internal/service/directconnect/vif.go @@ -5,7 +5,6 @@ package directconnect import ( "context" - "fmt" "log" "time" @@ -23,18 +22,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func virtualInterfaceRead(ctx context.Context, id string, conn *directconnect.Client) (*awstypes.VirtualInterface, error) { - resp, state, err := virtualInterfaceStateRefresh(ctx, conn, id)() - if err != nil { - return nil, fmt.Errorf("reading Direct Connect virtual interface (%s): %s", id, err) - } - if state == string(awstypes.VirtualInterfaceStateDeleted) { - return nil, nil - } - - return resp.(*awstypes.VirtualInterface), nil -} - func virtualInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).DirectConnectClient(ctx) @@ -92,46 +79,6 @@ func virtualInterfaceDelete(ctx context.Context, d *schema.ResourceData, meta in return diags } -func virtualInterfaceStateRefresh(ctx context.Context, conn *directconnect.Client, vifId string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - resp, err := conn.DescribeVirtualInterfaces(ctx, &directconnect.DescribeVirtualInterfacesInput{ - VirtualInterfaceId: aws.String(vifId), - }) - if err != nil { - return nil, "", err - } - - n := len(resp.VirtualInterfaces) - switch n { - case 0: - return "", string(awstypes.VirtualInterfaceStateDeleted), nil - - case 1: - vif := resp.VirtualInterfaces[0] - return vif, string(vif.VirtualInterfaceState), nil - - default: - return nil, "", fmt.Errorf("Found %d Direct Connect virtual interfaces for %s, expected 1", n, vifId) - } - } -} - -func virtualInterfaceWaitUntilAvailable(ctx context.Context, conn *directconnect.Client, vifId string, timeout time.Duration, pending, target []string) error { - stateConf := &retry.StateChangeConf{ - Pending: pending, - Target: target, - Refresh: virtualInterfaceStateRefresh(ctx, conn, vifId), - Timeout: timeout, - Delay: 10 * time.Second, - MinTimeout: 5 * time.Second, - } - if _, err := stateConf.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("waiting for Direct Connect virtual interface (%s) to become available: %s", vifId, err) - } - - return nil -} - func findVirtualInterfaceByID(ctx context.Context, conn *directconnect.Client, id string) (*awstypes.VirtualInterface, error) { input := &directconnect.DescribeVirtualInterfacesInput{ VirtualInterfaceId: aws.String(id), From f8f34dbc0f24029635dc600aaaa7bcdfd0ad2f62 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 15:46:27 -0400 Subject: [PATCH 31/59] Add 'TestAccDirectConnectConnectionAssociation_disappears'. --- .../connection_association_test.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/service/directconnect/connection_association_test.go b/internal/service/directconnect/connection_association_test.go index 9738eac4a75..69d99859595 100644 --- a/internal/service/directconnect/connection_association_test.go +++ b/internal/service/directconnect/connection_association_test.go @@ -39,6 +39,29 @@ func TestAccDirectConnectConnectionAssociation_basic(t *testing.T) { }) } +func TestAccDirectConnectConnectionAssociation_disappears(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_dx_connection_association.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.DirectConnectServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckConnectionAssociationDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccConnectionAssociationConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckConnectionAssociationExists(ctx, resourceName), + acctest.CheckResourceDisappears(ctx, acctest.Provider, tfdirectconnect.ResourceConnectionAssociation(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func TestAccDirectConnectConnectionAssociation_lagOnConnection(t *testing.T) { ctx := acctest.Context(t) resourceName := "aws_dx_connection_association.test" From 7acd0066ba0a650bb351a329405737562f29039a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 16:07:34 -0400 Subject: [PATCH 32/59] directconnect: Tidy up sweepers. --- internal/service/directconnect/sweep.go | 200 +++++++++--------------- 1 file changed, 76 insertions(+), 124 deletions(-) diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index 23b3fbea81d..e53d5be01fb 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -13,10 +13,8 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2" ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go-v2/service/secretsmanager" - "github.com/hashicorp/go-multierror" "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" ) @@ -66,49 +64,36 @@ func sweepConnections(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - + input := &directconnect.DescribeConnectionsInput{} conn := client.DirectConnectClient(ctx) - sweepResources := make([]sweep.Sweepable, 0) - var sweeperErrs *multierror.Error - input := &directconnect.DescribeConnectionsInput{} - - // DescribeConnections has no pagination support output, err := conn.DescribeConnections(ctx, input) - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect Connection sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() + return nil } if err != nil { - sweeperErr := fmt.Errorf("error listing Direct Connect Connections for %s: %w", region, err) - log.Printf("[ERROR] %s", sweeperErr) - sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) - return sweeperErrs.ErrorOrNil() + return fmt.Errorf("error listing Direct Connect Connections (%s): %w", region, err) } - if output == nil { - log.Printf("[WARN] Skipping Direct Connect Connection sweep for %s: empty response", region) - return sweeperErrs.ErrorOrNil() - } - - for _, connection := range output.Connections { - id := aws.ToString(connection.ConnectionId) - + for _, v := range output.Connections { r := resourceConnection() d := r.Data(nil) - d.SetId(id) + d.SetId(aws.ToString(v.ConnectionId)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Direct Connect Connection: %w", err)) + err = sweep.SweepOrchestrator(ctx, sweepResources) + + if err != nil { + return fmt.Errorf("error sweeping Direct Connect Connections (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepGatewayAssociationProposals(region string) error { @@ -117,8 +102,8 @@ func sweepGatewayAssociationProposals(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewayAssociationProposalsInput{} + conn := client.DirectConnectClient(ctx) sweepResources := make([]sweep.Sweepable, 0) err = describeDirectConnectGatewayAssociationProposalsPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewayAssociationProposalsOutput, lastPage bool) bool { @@ -126,22 +111,22 @@ func sweepGatewayAssociationProposals(region string) error { return !lastPage } - for _, proposal := range page.DirectConnectGatewayAssociationProposals { - proposalID := aws.ToString(proposal.ProposalId) + for _, v := range page.DirectConnectGatewayAssociationProposals { + id := aws.ToString(v.ProposalId) - if proposalRegion := aws.ToString(proposal.AssociatedGateway.Region); proposalRegion != region { - log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal (%s) in different home region: %s", proposalID, proposalRegion) + if proposalRegion := aws.ToString(v.AssociatedGateway.Region); proposalRegion != region { + log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal %s: AssociatedGateway.Region=%s", id, proposalRegion) continue } - if proposal.ProposalState != awstypes.DirectConnectGatewayAssociationProposalStateAccepted { - log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal (%s) in non-accepted (%s) state", proposalID, string(proposal.ProposalState)) + if state := v.ProposalState; state != awstypes.DirectConnectGatewayAssociationProposalStateAccepted { + log.Printf("[INFO] Skipping Direct Connect Gateway Association Proposal %s: ProposalState=%s", id, state) continue } r := resourceGatewayAssociationProposal() d := r.Data(nil) - d.SetId(proposalID) + d.SetId(id) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } @@ -150,7 +135,7 @@ func sweepGatewayAssociationProposals(region string) error { }) if awsv2.SkipSweepError(err) { - log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway Association Proposal sweep for %s: %w", region, err)) + log.Printf("[WARN] Skipping Direct Connect Gateway Association Proposal sweep for %s: %w", region, err) return nil } @@ -173,9 +158,8 @@ func sweepGatewayAssociations(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewaysInput{} - var sweeperErrs *multierror.Error + conn := client.DirectConnectClient(ctx) sweepResources := make([]sweep.Sweepable, 0) err = describeDirectConnectGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { @@ -183,8 +167,8 @@ func sweepGatewayAssociations(region string) error { return !lastPage } - for _, gateway := range page.DirectConnectGateways { - directConnectGatewayID := aws.ToString(gateway.DirectConnectGatewayId) + for _, v := range page.DirectConnectGateways { + directConnectGatewayID := aws.ToString(v.DirectConnectGatewayId) input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ DirectConnectGatewayId: aws.String(directConnectGatewayID), @@ -195,23 +179,23 @@ func sweepGatewayAssociations(region string) error { return !lastPage } - for _, association := range page.DirectConnectGatewayAssociations { - gatewayID := aws.ToString(association.AssociatedGateway.Id) + for _, v := range page.DirectConnectGatewayAssociations { + gatewayID := aws.ToString(v.AssociatedGateway.Id) - if aws.ToString(association.AssociatedGateway.Region) != region { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s) in different home region: %s", directConnectGatewayID, gatewayID, aws.ToString(association.AssociatedGateway.Region)) + if gatewayRegion := aws.ToString(v.AssociatedGateway.Region); gatewayRegion != region { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s): AssociatedGateway.Region=%s", directConnectGatewayID, gatewayID, gatewayRegion) continue } - if association.AssociationState != awstypes.DirectConnectGatewayAssociationStateAssociated { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, string(association.AssociationState), gatewayID) + if state := v.AssociationState; state != awstypes.DirectConnectGatewayAssociationStateAssociated { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s): AssociationState=%s", directConnectGatewayID, gatewayID, state) continue } r := resourceGatewayAssociation() d := r.Data(nil) d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, gatewayID)) - d.Set("dx_gateway_association_id", association.AssociationId) + d.Set("dx_gateway_association_id", v.AssociationId) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } @@ -220,7 +204,7 @@ func sweepGatewayAssociations(region string) error { }) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Direct Connect Gateway Associations (%s): %w", region, err)) + continue } } @@ -228,12 +212,12 @@ func sweepGatewayAssociations(region string) error { }) if awsv2.SkipSweepError(err) { - log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway Association sweep for %s: %w", region, err)) - return sweeperErrs // In case we have completed some pages, but had errors + log.Printf("[WARN] Skipping Direct Connect Gateway Association sweep for %s: %w", region, err) + return nil } if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err)) + fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err) } // Handle cross-account EC2 Transit Gateway associations. @@ -248,15 +232,15 @@ func sweepGatewayAssociations(region string) error { page, err := pages.NextPage(ctx) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EC2 Transit Gateways (%s): %w", region, err)) + return fmt.Errorf("error listing EC2 Transit Gateways (%s): %w", region, err) } - for _, transitGateway := range page.TransitGateways { - if transitGateway.State == ec2types.TransitGatewayStateDeleted { + for _, v := range page.TransitGateways { + if v.State == ec2types.TransitGatewayStateDeleted { continue } - transitGatewayID := aws.ToString(transitGateway.TransitGatewayId) + transitGatewayID := aws.ToString(v.TransitGatewayId) input := &directconnect.DescribeDirectConnectGatewayAssociationsInput{ AssociatedGatewayId: aws.String(transitGatewayID), @@ -267,18 +251,18 @@ func sweepGatewayAssociations(region string) error { return !lastPage } - for _, association := range page.DirectConnectGatewayAssociations { - directConnectGatewayID := aws.ToString(association.DirectConnectGatewayId) + for _, v := range page.DirectConnectGatewayAssociations { + directConnectGatewayID := aws.ToString(v.DirectConnectGatewayId) - if association.AssociationState != awstypes.DirectConnectGatewayAssociationStateAssociated { - log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association in non-available (%s) state: %s", directConnectGatewayID, string(association.AssociationState), transitGatewayID) + if state := v.AssociationState; state != awstypes.DirectConnectGatewayAssociationStateAssociated { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s) Association (%s): %s", directConnectGatewayID, transitGatewayID, state) continue } r := resourceGatewayAssociation() d := r.Data(nil) d.SetId(gatewayAssociationCreateResourceID(directConnectGatewayID, transitGatewayID)) - d.Set("dx_gateway_association_id", association.AssociationId) + d.Set("dx_gateway_association_id", v.AssociationId) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } @@ -287,7 +271,7 @@ func sweepGatewayAssociations(region string) error { }) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Direct Connect Gateway Associations (%s): %w", region, err)) + continue } } } @@ -295,10 +279,10 @@ func sweepGatewayAssociations(region string) error { err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Direct Connect Gateway Associations (%s): %w", region, err)) + return fmt.Errorf("error sweeping Direct Connect Gateway Associations (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepGateways(region string) error { @@ -307,9 +291,8 @@ func sweepGateways(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - conn := client.DirectConnectClient(ctx) input := &directconnect.DescribeDirectConnectGatewaysInput{} - var sweeperErrs *multierror.Error + conn := client.DirectConnectClient(ctx) sweepResources := make([]sweep.Sweepable, 0) err = describeDirectConnectGatewaysPages(ctx, conn, input, func(page *directconnect.DescribeDirectConnectGatewaysOutput, lastPage bool) bool { @@ -317,11 +300,11 @@ func sweepGateways(region string) error { return !lastPage } - for _, gateway := range page.DirectConnectGateways { - directConnectGatewayID := aws.ToString(gateway.DirectConnectGatewayId) + for _, v := range page.DirectConnectGateways { + directConnectGatewayID := aws.ToString(v.DirectConnectGatewayId) - if gateway.DirectConnectGatewayState != awstypes.DirectConnectGatewayStateAvailable { - log.Printf("[INFO] Skipping Direct Connect Gateway in non-available (%s) state: %s", string(gateway.DirectConnectGatewayState), directConnectGatewayID) + if state := v.DirectConnectGatewayState; state != awstypes.DirectConnectGatewayStateAvailable { + log.Printf("[INFO] Skipping Direct Connect Gateway (%s): DirectConnectGatewayState=%s", directConnectGatewayID, state) continue } @@ -350,7 +333,7 @@ func sweepGateways(region string) error { }) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Direct Connect Gateway Associations (%s): %w", region, err)) + continue } if associations { @@ -369,21 +352,21 @@ func sweepGateways(region string) error { }) if awsv2.SkipSweepError(err) { - log.Print(fmt.Errorf("[WARN] Skipping Direct Connect Gateway sweep for %s: %w", region, err)) - return sweeperErrs // In case we have completed some pages, but had errors + log.Printf("[WARN] Skipping Direct Connect Gateway sweep for %s: %w", region, err) + return nil } if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err)) + return fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err) } err = sweep.SweepOrchestrator(ctx, sweepResources) if err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Direct Connect Gateways (%s): %w", region, err)) + return fmt.Errorf("error sweeping Direct Connect Gateways (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepLags(region string) error { @@ -392,49 +375,36 @@ func sweepLags(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - + input := &directconnect.DescribeLagsInput{} conn := client.DirectConnectClient(ctx) - sweepResources := make([]sweep.Sweepable, 0) - var sweeperErrs *multierror.Error - - input := &directconnect.DescribeLagsInput{} - // DescribeLags has no pagination support output, err := conn.DescribeLags(ctx, input) if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect LAG sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() + return nil } if err != nil { - sweeperErr := fmt.Errorf("error listing Direct Connect LAGs for %s: %w", region, err) - log.Printf("[ERROR] %s", sweeperErr) - sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) - return sweeperErrs.ErrorOrNil() - } - - if output == nil { - log.Printf("[WARN] Skipping Direct Connect LAG sweep for %s: empty response", region) - return sweeperErrs.ErrorOrNil() + return fmt.Errorf("error listing Direct Connect LAGs for %s: %w", region, err) } - for _, lag := range output.Lags { - id := aws.ToString(lag.LagId) - + for _, v := range output.Lags { r := resourceLag() d := r.Data(nil) - d.SetId(id) + d.SetId(aws.ToString(v.LagId)) sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) } - if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil { - sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error sweeping Direct Connect LAG: %w", err)) + err = sweep.SweepOrchestrator(ctx, sweepResources) + + if err != nil { + return fmt.Errorf("error sweeping Direct Connect LAGs (%s): %w", region, err) } - return sweeperErrs.ErrorOrNil() + return nil } func sweepMacSecKeys(region string) error { @@ -443,60 +413,42 @@ func sweepMacSecKeys(region string) error { if err != nil { return fmt.Errorf("error getting client: %w", err) } - + input := &directconnect.DescribeConnectionsInput{} dxConn := client.DirectConnectClient(ctx) - // Clean up leaked Secrets Manager resources created by Direct Connect. // Direct Connect does not remove the corresponding Secrets Manager // key when deleting the MACsec key association. The only option to // clean up the dangling resource is to use Secrets Manager to delete // the MACsec key secret. smConn := client.SecretsManagerClient(ctx) - dxInput := &directconnect.DescribeConnectionsInput{} - var sweeperErrs *multierror.Error - output, err := dxConn.DescribeConnections(ctx, dxInput) + output, err := dxConn.DescribeConnections(ctx, input) if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Direct Connect MACsec Keys sweep for %s: %s", region, err) - return sweeperErrs.ErrorOrNil() + return nil } if err != nil { - sweeperErr := fmt.Errorf(" listing Direct Connect Connections for %s: %w", region, err) - log.Printf("[ERROR] %s", sweeperErr) - sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) - return sweeperErrs.ErrorOrNil() - } - - if output == nil { - log.Printf("[WARN] Skipping Direct Connect MACsec Keys sweep for %s: empty response", region) - return sweeperErrs.ErrorOrNil() + return fmt.Errorf("error listing Direct Connect Connections (%s): %w", region, err) } - for _, connection := range output.Connections { - if connection.MacSecKeys == nil { - continue - } - - for _, key := range connection.MacSecKeys { - arn := aws.ToString(key.SecretARN) + for _, v := range output.Connections { + for _, v := range v.MacSecKeys { + arn := aws.ToString(v.SecretARN) input := &secretsmanager.DeleteSecretInput{ SecretId: aws.String(arn), } - log.Printf("[DEBUG] Deleting MACSec secret key: %s", *input.SecretId) + log.Printf("[DEBUG] Deleting MACSec secret key: %s", arn) _, err := smConn.DeleteSecret(ctx, input) if err != nil { - sweeperErr := fmt.Errorf("error deleting MACsec Secret (%s): %w", arn, err) - log.Printf("[ERROR] %s", sweeperErr) - sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) continue } } } - return sweeperErrs.ErrorOrNil() + return nil } From 984843dfa65390fc331a68d402a12403ee16543e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 7 Aug 2024 16:09:57 -0400 Subject: [PATCH 33/59] Fix compilation errors. --- internal/service/directconnect/sweep.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/directconnect/sweep.go b/internal/service/directconnect/sweep.go index e53d5be01fb..05e8540561a 100644 --- a/internal/service/directconnect/sweep.go +++ b/internal/service/directconnect/sweep.go @@ -135,7 +135,7 @@ func sweepGatewayAssociationProposals(region string) error { }) if awsv2.SkipSweepError(err) { - log.Printf("[WARN] Skipping Direct Connect Gateway Association Proposal sweep for %s: %w", region, err) + log.Printf("[WARN] Skipping Direct Connect Gateway Association Proposal sweep for %s: %s", region, err) return nil } @@ -212,12 +212,12 @@ func sweepGatewayAssociations(region string) error { }) if awsv2.SkipSweepError(err) { - log.Printf("[WARN] Skipping Direct Connect Gateway Association sweep for %s: %w", region, err) + log.Printf("[WARN] Skipping Direct Connect Gateway Association sweep for %s: %s", region, err) return nil } if err != nil { - fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err) + return fmt.Errorf("error listing Direct Connect Gateways (%s): %w", region, err) } // Handle cross-account EC2 Transit Gateway associations. @@ -352,7 +352,7 @@ func sweepGateways(region string) error { }) if awsv2.SkipSweepError(err) { - log.Printf("[WARN] Skipping Direct Connect Gateway sweep for %s: %w", region, err) + log.Printf("[WARN] Skipping Direct Connect Gateway sweep for %s: %s", region, err) return nil } From ad556710cfaa361145f60b51a3351389b6150a7d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 08:01:59 -0400 Subject: [PATCH 34/59] Fix golangci-lint 'unparam'. --- internal/service/directconnect/gateway_association.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/directconnect/gateway_association.go b/internal/service/directconnect/gateway_association.go index e477d51957d..d734cd74783 100644 --- a/internal/service/directconnect/gateway_association.go +++ b/internal/service/directconnect/gateway_association.go @@ -317,7 +317,7 @@ func findGatewayAssociationByGatewayIDAndVirtualGatewayID(ctx context.Context, c } func findNonDisassociatedGatewayAssociation(ctx context.Context, conn *directconnect.Client, input *directconnect.DescribeDirectConnectGatewayAssociationsInput, filter tfslices.Predicate[*awstypes.DirectConnectGatewayAssociation]) (*awstypes.DirectConnectGatewayAssociation, error) { - output, err := findGatewayAssociation(ctx, conn, input, tfslices.PredicateTrue[*awstypes.DirectConnectGatewayAssociation]()) + output, err := findGatewayAssociation(ctx, conn, input, filter) if err != nil { return nil, err From 5101e3e59dc59aa8d04d42f139f23a305aafa46e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 08:03:35 -0400 Subject: [PATCH 35/59] r/aws_dx_lag: Fix acceptance tests. --- internal/service/directconnect/lag_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/directconnect/lag_test.go b/internal/service/directconnect/lag_test.go index 2247ab21e94..38b03430280 100644 --- a/internal/service/directconnect/lag_test.go +++ b/internal/service/directconnect/lag_test.go @@ -39,7 +39,7 @@ func TestAccDirectConnectLag_basic(t *testing.T) { testAccCheckLagExists(ctx, resourceName, &lag), acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(`dxlag/.+`)), resource.TestCheckNoResourceAttr(resourceName, names.AttrConnectionID), - resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "1Gbps"), + resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "10Gbps"), resource.TestCheckResourceAttr(resourceName, names.AttrForceDestroy, acctest.CtFalse), resource.TestCheckResourceAttrSet(resourceName, "has_logical_redundancy"), resource.TestCheckResourceAttrSet(resourceName, "jumbo_frame_capable"), @@ -56,7 +56,7 @@ func TestAccDirectConnectLag_basic(t *testing.T) { testAccCheckLagExists(ctx, resourceName, &lag), acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(`dxlag/.+`)), resource.TestCheckNoResourceAttr(resourceName, names.AttrConnectionID), - resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "1Gbps"), + resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "10Gbps"), resource.TestCheckResourceAttr(resourceName, names.AttrForceDestroy, acctest.CtFalse), resource.TestCheckResourceAttrSet(resourceName, "has_logical_redundancy"), resource.TestCheckResourceAttrSet(resourceName, "jumbo_frame_capable"), @@ -120,7 +120,7 @@ func TestAccDirectConnectLag_connectionID(t *testing.T) { testAccCheckLagExists(ctx, resourceName, &lag), acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(`dxlag/.+`)), resource.TestCheckResourceAttrPair(resourceName, names.AttrConnectionID, connectionResourceName, names.AttrID), - resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "1Gbps"), + resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "10Gbps"), resource.TestCheckResourceAttr(resourceName, names.AttrForceDestroy, acctest.CtFalse), resource.TestCheckResourceAttrSet(resourceName, "has_logical_redundancy"), resource.TestCheckResourceAttrSet(resourceName, "jumbo_frame_capable"), @@ -159,7 +159,7 @@ func TestAccDirectConnectLag_providerName(t *testing.T) { testAccCheckLagExists(ctx, resourceName, &lag), acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "directconnect", regexache.MustCompile(`dxlag/.+`)), resource.TestCheckNoResourceAttr(resourceName, names.AttrConnectionID), - resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "1Gbps"), + resource.TestCheckResourceAttr(resourceName, "connections_bandwidth", "10Gbps"), resource.TestCheckResourceAttr(resourceName, names.AttrForceDestroy, acctest.CtFalse), resource.TestCheckResourceAttrSet(resourceName, "has_logical_redundancy"), resource.TestCheckResourceAttrSet(resourceName, "jumbo_frame_capable"), @@ -283,7 +283,7 @@ data "aws_dx_locations" "test" {} resource "aws_dx_lag" "test" { name = %[1]q - connections_bandwidth = "1Gbps" + connections_bandwidth = "10Gbps" location = tolist(data.aws_dx_locations.test.location_codes)[0] } `, rName) @@ -302,7 +302,7 @@ resource "aws_dx_lag" "test" { resource "aws_dx_connection" "test" { name = %[1]q - bandwidth = "1Gbps" + bandwidth = "10Gbps" location = tolist(data.aws_dx_locations.test.location_codes)[1] } `, rName) @@ -318,7 +318,7 @@ data "aws_dx_location" "test" { resource "aws_dx_lag" "test" { name = %[1]q - connections_bandwidth = "1Gbps" + connections_bandwidth = "10Gbps" location = data.aws_dx_location.test.location_code provider_name = data.aws_dx_location.test.available_providers[0] @@ -332,7 +332,7 @@ data "aws_dx_locations" "test" {} resource "aws_dx_lag" "test" { name = %[1]q - connections_bandwidth = "1Gbps" + connections_bandwidth = "10Gbps" location = tolist(data.aws_dx_locations.test.location_codes)[0] force_destroy = true @@ -349,7 +349,7 @@ data "aws_dx_locations" "test" {} resource "aws_dx_lag" "test" { name = %[1]q - connections_bandwidth = "1Gbps" + connections_bandwidth = "10Gbps" location = tolist(data.aws_dx_locations.test.location_codes)[0] force_destroy = true From 2841d35d1eb349589b38008b21c13a216ccb41b9 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 08:25:39 -0400 Subject: [PATCH 36/59] Fix 'TestAccDirectConnectConnection_vlanIDMigration510'. --- internal/service/directconnect/connection_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/service/directconnect/connection_test.go b/internal/service/directconnect/connection_test.go index 1d8b04ff5f5..a0bfb205051 100644 --- a/internal/service/directconnect/connection_test.go +++ b/internal/service/directconnect/connection_test.go @@ -340,7 +340,10 @@ func TestAccDirectConnectConnection_vlanIDMigration510(t *testing.T) { { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Config: testAccConnectionConfig_basic(rName), - PlanOnly: true, + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckConnectionExists(ctx, resourceName, &connection), + resource.TestCheckResourceAttr(resourceName, "vlan_id", acctest.Ct0), + ), }, }, }) From ecd06a1fe67832f8e9df40d02047408acc8cc655 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:10 -0400 Subject: [PATCH 37/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - apigatewayv2. --- internal/service/apigatewayv2/model_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/apigatewayv2/model_test.go b/internal/service/apigatewayv2/model_test.go index 82a5703b446..b656ef90088 100644 --- a/internal/service/apigatewayv2/model_test.go +++ b/internal/service/apigatewayv2/model_test.go @@ -255,18 +255,18 @@ resource "aws_apigatewayv2_api" "test" { } func testAccModelConfig_basic(rName, schema string) string { - return testAccModelConfig_api(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccModelConfig_api(rName), fmt.Sprintf(` resource "aws_apigatewayv2_model" "test" { api_id = aws_apigatewayv2_api.test.id content_type = "application/json" name = %[1]q schema = %[2]q } -`, rName, schema) +`, rName, schema)) } func testAccModelConfig_allAttributes(rName, schema string) string { - return testAccModelConfig_api(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccModelConfig_api(rName), fmt.Sprintf(` resource "aws_apigatewayv2_model" "test" { api_id = aws_apigatewayv2_api.test.id content_type = "text/x-json" @@ -274,5 +274,5 @@ resource "aws_apigatewayv2_model" "test" { description = "test" schema = %[2]q } -`, rName, schema) +`, rName, schema)) } From 34e3d15e97030acad0f538f811e2809343b2a32f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:11 -0400 Subject: [PATCH 38/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - appautoscaling. --- internal/service/appautoscaling/policy_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/appautoscaling/policy_test.go b/internal/service/appautoscaling/policy_test.go index 61d0b17e8e5..ad996edb2e5 100644 --- a/internal/service/appautoscaling/policy_test.go +++ b/internal/service/appautoscaling/policy_test.go @@ -1145,7 +1145,7 @@ resource "aws_ecs_service" "test2" { } func testAccPolicyConfig_resourceIDForceNew1(rName string) string { - return testAccPolicyConfig_resourceIDForceNewBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPolicyConfig_resourceIDForceNewBase(rName), fmt.Sprintf(` resource "aws_appautoscaling_target" "test" { max_capacity = 4 min_capacity = 0 @@ -1190,11 +1190,11 @@ resource "aws_cloudwatch_metric_alarm" "test" { ClusterName = aws_ecs_cluster.test.name } } -`, rName) +`, rName)) } func testAccPolicyConfig_resourceIDForceNew2(rName string) string { - return testAccPolicyConfig_resourceIDForceNewBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccPolicyConfig_resourceIDForceNewBase(rName), fmt.Sprintf(` resource "aws_appautoscaling_target" "test" { max_capacity = 4 min_capacity = 0 @@ -1239,7 +1239,7 @@ resource "aws_cloudwatch_metric_alarm" "test" { ClusterName = aws_ecs_cluster.test.name } } -`, rName) +`, rName)) } func testAccPolicyImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { From 9319944a4505817a5d59fa71f43991212e6768cb Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:11 -0400 Subject: [PATCH 39/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - appsync. --- internal/service/appsync/resolver_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/appsync/resolver_test.go b/internal/service/appsync/resolver_test.go index 300dcef8cbe..dc657363e69 100644 --- a/internal/service/appsync/resolver_test.go +++ b/internal/service/appsync/resolver_test.go @@ -797,7 +797,7 @@ EOF } func testAccResolverConfig_sync(rName string) string { - return acctest.ConfigCompose(testAccDatasourceConfig_baseDynamoDB(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccDatasourceConfig_baseDynamoDB(rName), fmt.Sprintf(` resource "aws_appsync_graphql_api" "test" { authentication_type = "API_KEY" name = %[1]q From 9aff365bb2471856de80945346de263ef0e1b2f8 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:12 -0400 Subject: [PATCH 40/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - backup. --- internal/service/backup/plan_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/backup/plan_test.go b/internal/service/backup/plan_test.go index 192d0a2e17b..7b6989e4980 100644 --- a/internal/service/backup/plan_test.go +++ b/internal/service/backup/plan_test.go @@ -1062,7 +1062,7 @@ resource "aws_backup_plan" "test" { } func testAccPlanConfig_ruleCopyActionCrossRegion(rName string) string { - return acctest.ConfigAlternateRegionProvider() + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAlternateRegionProvider(), fmt.Sprintf(` resource "aws_backup_vault" "test" { name = "%[1]s-1" } @@ -1095,7 +1095,7 @@ resource "aws_backup_plan" "test" { } } } -`, rName) +`, rName)) } func testAccPlanConfig_ruleCopyActionNoLifecycle(rName string) string { From 7d809a4f02ef42c3daefa06cdcb7f8bec6f2037e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:14 -0400 Subject: [PATCH 41/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - codeartifact. --- internal/service/codeartifact/repository_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/service/codeartifact/repository_test.go b/internal/service/codeartifact/repository_test.go index f5252b19bb7..bf9ba771308 100644 --- a/internal/service/codeartifact/repository_test.go +++ b/internal/service/codeartifact/repository_test.go @@ -384,7 +384,7 @@ resource "aws_codeartifact_repository" "test" { } func testAccRepositoryConfig_upstreams2(rName string) string { - return testAccRepositoryConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccRepositoryConfig_base(rName), fmt.Sprintf(` resource "aws_codeartifact_repository" "upstream1" { repository = "%[1]s-upstream1" domain = aws_codeartifact_domain.test.domain @@ -407,11 +407,11 @@ resource "aws_codeartifact_repository" "test" { repository_name = aws_codeartifact_repository.upstream2.repository } } -`, rName) +`, rName)) } func testAccRepositoryConfig_externalConnection(rName string) string { - return testAccRepositoryConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccRepositoryConfig_base(rName), fmt.Sprintf(` resource "aws_codeartifact_repository" "test" { repository = %[1]q domain = aws_codeartifact_domain.test.domain @@ -420,11 +420,11 @@ resource "aws_codeartifact_repository" "test" { external_connection_name = "public:npmjs" } } -`, rName) +`, rName)) } func testAccRepositoryConfig_tags1(rName, tagKey1, tagValue1 string) string { - return testAccRepositoryConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccRepositoryConfig_base(rName), fmt.Sprintf(` resource "aws_codeartifact_repository" "test" { repository = %[1]q domain = aws_codeartifact_domain.test.domain @@ -433,11 +433,11 @@ resource "aws_codeartifact_repository" "test" { %[2]q = %[3]q } } -`, rName, tagKey1, tagValue1) +`, rName, tagKey1, tagValue1)) } func testAccRepositoryConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { - return testAccRepositoryConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccRepositoryConfig_base(rName), fmt.Sprintf(` resource "aws_codeartifact_repository" "test" { repository = %[1]q domain = aws_codeartifact_domain.test.domain @@ -447,5 +447,5 @@ resource "aws_codeartifact_repository" "test" { %[4]q = %[5]q } } -`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +`, rName, tagKey1, tagValue1, tagKey2, tagValue2)) } From aa811da256b3d99e5cdd988eb59889d14c14a9cc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:15 -0400 Subject: [PATCH 42/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - configservice. --- .../organization_custom_rule_test.go | 58 +++++++++---------- .../organization_managed_rule_test.go | 46 +++++++-------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/internal/service/configservice/organization_custom_rule_test.go b/internal/service/configservice/organization_custom_rule_test.go index 7f759fbcb71..426ea10ff05 100644 --- a/internal/service/configservice/organization_custom_rule_test.go +++ b/internal/service/configservice/organization_custom_rule_test.go @@ -505,7 +505,7 @@ func testAccCheckOrganizationCustomRuleDestroy(ctx context.Context) resource.Tes } } -func testAccOrganizationCustomRuleConfigBase(rName string) string { +func testAccOrganizationCustomRuleConfig_base(rName string) string { return fmt.Sprintf(` data "aws_partition" "current" { } @@ -590,7 +590,7 @@ resource "aws_organizations_organization" "test" { } func testAccOrganizationCustomRuleConfig_description(rName, description string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -599,7 +599,7 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName, description) +`, rName, description)) } func testAccOrganizationCustomRuleConfig_errorHandling(rName string) string { @@ -656,7 +656,7 @@ resource "aws_config_organization_custom_rule" "test" { } func testAccOrganizationCustomRuleConfig_excludedAccounts1(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -665,11 +665,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_excludedAccounts2(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -678,11 +678,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_inputParameters(rName, inputParameters string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -694,11 +694,11 @@ PARAMS name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName, inputParameters) +`, rName, inputParameters)) } func testAccOrganizationCustomRuleConfig_lambdaFunctionARN1(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -706,11 +706,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_lambdaFunctionARN2(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_lambda_function" "test2" { filename = "test-fixtures/lambdatest.zip" function_name = "%[1]s2" @@ -733,11 +733,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_maximumExecutionFrequency(rName, maximumExecutionFrequency string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -746,11 +746,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = ["ScheduledNotification"] } -`, rName, maximumExecutionFrequency) +`, rName, maximumExecutionFrequency)) } func testAccOrganizationCustomRuleConfig_resourceIdScope(rName, resourceIdScope string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -760,11 +760,11 @@ resource "aws_config_organization_custom_rule" "test" { resource_types_scope = ["AWS::EC2::Instance"] trigger_types = ["ScheduledNotification"] } -`, rName, resourceIdScope) +`, rName, resourceIdScope)) } func testAccOrganizationCustomRuleConfig_resourceTypesScope1(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -773,11 +773,11 @@ resource "aws_config_organization_custom_rule" "test" { resource_types_scope = ["AWS::EC2::Instance"] trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_resourceTypesScope2(rName string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -786,11 +786,11 @@ resource "aws_config_organization_custom_rule" "test" { resource_types_scope = ["AWS::EC2::Instance", "AWS::EC2::VPC"] trigger_types = ["ScheduledNotification"] } -`, rName) +`, rName)) } func testAccOrganizationCustomRuleConfig_tagKeyScope(rName, tagKeyScope string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -799,11 +799,11 @@ resource "aws_config_organization_custom_rule" "test" { tag_key_scope = %[2]q trigger_types = ["ScheduledNotification"] } -`, rName, tagKeyScope) +`, rName, tagKeyScope)) } func testAccOrganizationCustomRuleConfig_tagValueScope(rName, tagValueScope string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -813,11 +813,11 @@ resource "aws_config_organization_custom_rule" "test" { tag_value_scope = %[2]q trigger_types = ["ScheduledNotification"] } -`, rName, tagValueScope) +`, rName, tagValueScope)) } func testAccOrganizationCustomRuleConfig_triggerTypes1(rName, triggerType1 string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -825,11 +825,11 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = [%[2]q] } -`, rName, triggerType1) +`, rName, triggerType1)) } func testAccOrganizationCustomRuleConfig_triggerTypes2(rName, triggerType1, triggerType2 string) string { - return testAccOrganizationCustomRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationCustomRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_custom_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_lambda_permission.test, aws_organizations_organization.test] @@ -837,5 +837,5 @@ resource "aws_config_organization_custom_rule" "test" { name = %[1]q trigger_types = [%[2]q, %[3]q] } -`, rName, triggerType1, triggerType2) +`, rName, triggerType1, triggerType2)) } diff --git a/internal/service/configservice/organization_managed_rule_test.go b/internal/service/configservice/organization_managed_rule_test.go index 4905e1375cc..d9bcfb9e3a8 100644 --- a/internal/service/configservice/organization_managed_rule_test.go +++ b/internal/service/configservice/organization_managed_rule_test.go @@ -466,7 +466,7 @@ func testAccCheckOrganizationManagedRuleDestroy(ctx context.Context) resource.Te } } -func testAccOrganizationManagedRuleConfigBase(rName string) string { +func testAccOrganizationManagedRuleConfig_base(rName string) string { return fmt.Sprintf(` data "aws_partition" "current" { } @@ -511,7 +511,7 @@ resource "aws_organizations_organization" "test" { } func testAccOrganizationManagedRuleConfig_description(rName, description string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -519,7 +519,7 @@ resource "aws_config_organization_managed_rule" "test" { name = %[1]q rule_identifier = "IAM_PASSWORD_POLICY" } -`, rName, description) +`, rName, description)) } func testAccOrganizationManagedRuleConfig_errorHandling(rName string) string { @@ -539,7 +539,7 @@ resource "aws_config_organization_managed_rule" "test" { } func testAccOrganizationManagedRuleConfig_excludedAccounts1(rName string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -547,11 +547,11 @@ resource "aws_config_organization_managed_rule" "test" { name = %[1]q rule_identifier = "IAM_PASSWORD_POLICY" } -`, rName) +`, rName)) } func testAccOrganizationManagedRuleConfig_excludedAccounts2(rName string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -559,11 +559,11 @@ resource "aws_config_organization_managed_rule" "test" { name = %[1]q rule_identifier = "IAM_PASSWORD_POLICY" } -`, rName) +`, rName)) } func testAccOrganizationManagedRuleConfig_inputParameters(rName, inputParameters string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -574,11 +574,11 @@ PARAMS name = %[1]q rule_identifier = "REQUIRED_TAGS" } -`, rName, inputParameters) +`, rName, inputParameters)) } func testAccOrganizationManagedRuleConfig_maximumExecutionFrequency(rName, maximumExecutionFrequency string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -586,11 +586,11 @@ resource "aws_config_organization_managed_rule" "test" { name = %[1]q rule_identifier = "IAM_PASSWORD_POLICY" } -`, rName, maximumExecutionFrequency) +`, rName, maximumExecutionFrequency)) } func testAccOrganizationManagedRuleConfig_resourceIdScope(rName, resourceIdScope string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -599,11 +599,11 @@ resource "aws_config_organization_managed_rule" "test" { resource_types_scope = ["AWS::EC2::Instance"] rule_identifier = "EC2_INSTANCE_DETAILED_MONITORING_ENABLED" } -`, rName, resourceIdScope) +`, rName, resourceIdScope)) } func testAccOrganizationManagedRuleConfig_resourceTypesScope1(rName string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -618,11 +618,11 @@ EOF resource_types_scope = ["AWS::EC2::Instance"] rule_identifier = "REQUIRED_TAGS" } -`, rName) +`, rName)) } func testAccOrganizationManagedRuleConfig_resourceTypesScope2(rName string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -637,22 +637,22 @@ EOF resource_types_scope = ["AWS::EC2::Instance", "AWS::EC2::VPC"] rule_identifier = "REQUIRED_TAGS" } -`, rName) +`, rName)) } func testAccOrganizationManagedRuleConfig_identifier(rName, ruleIdentifier string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] name = %[1]q rule_identifier = %[2]q } -`, rName, ruleIdentifier) +`, rName, ruleIdentifier)) } func testAccOrganizationManagedRuleConfig_tagKeyScope(rName, tagKeyScope string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -660,11 +660,11 @@ resource "aws_config_organization_managed_rule" "test" { rule_identifier = "EC2_INSTANCE_DETAILED_MONITORING_ENABLED" tag_key_scope = %[2]q } -`, rName, tagKeyScope) +`, rName, tagKeyScope)) } func testAccOrganizationManagedRuleConfig_tagValueScope(rName, tagValueScope string) string { - return testAccOrganizationManagedRuleConfigBase(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccOrganizationManagedRuleConfig_base(rName), fmt.Sprintf(` resource "aws_config_organization_managed_rule" "test" { depends_on = [aws_config_configuration_recorder.test, aws_organizations_organization.test] @@ -673,5 +673,5 @@ resource "aws_config_organization_managed_rule" "test" { tag_key_scope = "key1" tag_value_scope = %[2]q } -`, rName, tagValueScope) +`, rName, tagValueScope)) } From 180a0d18d7ebada4d8666e141c3d7fcf47757dee Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:16 -0400 Subject: [PATCH 43/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - devicefarm. --- internal/service/devicefarm/device_pool_test.go | 12 ++++++------ internal/service/devicefarm/network_profile_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/service/devicefarm/device_pool_test.go b/internal/service/devicefarm/device_pool_test.go index c512424c392..c406e3d74cd 100644 --- a/internal/service/devicefarm/device_pool_test.go +++ b/internal/service/devicefarm/device_pool_test.go @@ -235,7 +235,7 @@ func testAccCheckDevicePoolDestroy(ctx context.Context) resource.TestCheckFunc { } func testAccDevicePoolConfig_basic(rName string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_device_pool" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn @@ -245,11 +245,11 @@ resource "aws_devicefarm_device_pool" "test" { value = "\"AVAILABLE\"" } } -`, rName) +`, rName)) } func testAccDevicePoolConfig_tags1(rName, tagKey1, tagValue1 string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_device_pool" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn @@ -262,11 +262,11 @@ resource "aws_devicefarm_device_pool" "test" { %[2]q = %[3]q } } -`, rName, tagKey1, tagValue1) +`, rName, tagKey1, tagValue1)) } func testAccDevicePoolConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_device_pool" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn @@ -280,5 +280,5 @@ resource "aws_devicefarm_device_pool" "test" { %[4]q = %[5]q } } -`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +`, rName, tagKey1, tagValue1, tagKey2, tagValue2)) } diff --git a/internal/service/devicefarm/network_profile_test.go b/internal/service/devicefarm/network_profile_test.go index df565390d43..e4f493f0590 100644 --- a/internal/service/devicefarm/network_profile_test.go +++ b/internal/service/devicefarm/network_profile_test.go @@ -242,16 +242,16 @@ func testAccCheckNetworkProfileDestroy(ctx context.Context) resource.TestCheckFu } func testAccNetworkProfileConfig_basic(rName string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_network_profile" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn } -`, rName) +`, rName)) } func testAccNetworkProfileConfig_tags1(rName, tagKey1, tagValue1 string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_network_profile" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn @@ -260,11 +260,11 @@ resource "aws_devicefarm_network_profile" "test" { %[2]q = %[3]q } } -`, rName, tagKey1, tagValue1) +`, rName, tagKey1, tagValue1)) } func testAccNetworkProfileConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { - return testAccProjectConfig_basic(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProjectConfig_basic(rName), fmt.Sprintf(` resource "aws_devicefarm_network_profile" "test" { name = %[1]q project_arn = aws_devicefarm_project.test.arn @@ -274,5 +274,5 @@ resource "aws_devicefarm_network_profile" "test" { %[4]q = %[5]q } } -`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +`, rName, tagKey1, tagValue1, tagKey2, tagValue2)) } From 7b7af6a38b6de4a4d4f8aac3ab74733e581e78b5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:17 -0400 Subject: [PATCH 44/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - ec2. --- .../service/ec2/ipam_preview_next_cidr_data_source_test.go | 4 ++-- internal/service/ec2/ipam_preview_next_cidr_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/ec2/ipam_preview_next_cidr_data_source_test.go b/internal/service/ec2/ipam_preview_next_cidr_data_source_test.go index 4c5d9c781a8..b3ce2ff73a4 100644 --- a/internal/service/ec2/ipam_preview_next_cidr_data_source_test.go +++ b/internal/service/ec2/ipam_preview_next_cidr_data_source_test.go @@ -157,7 +157,7 @@ resource "aws_vpc_ipam_pool_cidr_allocation" "test" { } func testAccIPAMPreviewNextCIDRDataSourceConfig_ipv4Disallowed(netmaskLength, disallowedCidr string) string { - return testAccIPAMPreviewNextCIDRDataSourceConfig_base + fmt.Sprintf(` + return acctest.ConfigCompose(testAccIPAMPreviewNextCIDRDataSourceConfig_base, fmt.Sprintf(` data "aws_vpc_ipam_preview_next_cidr" "test" { ipam_pool_id = aws_vpc_ipam_pool.test.id netmask_length = %[1]q @@ -170,5 +170,5 @@ data "aws_vpc_ipam_preview_next_cidr" "test" { aws_vpc_ipam_pool_cidr.test ] } -`, netmaskLength, disallowedCidr) +`, netmaskLength, disallowedCidr)) } diff --git a/internal/service/ec2/ipam_preview_next_cidr_test.go b/internal/service/ec2/ipam_preview_next_cidr_test.go index c9cf3750234..afec7ab7fe6 100644 --- a/internal/service/ec2/ipam_preview_next_cidr_test.go +++ b/internal/service/ec2/ipam_preview_next_cidr_test.go @@ -157,7 +157,7 @@ resource "aws_vpc_ipam_pool_cidr_allocation" "test" { } func testAccIPAMPreviewNextCIDRConfig_ipv4Disallowed(netmaskLength, disallowedCidr string) string { - return testAccIPAMPreviewNextCIDRConfig_ipv4Base + fmt.Sprintf(` + return acctest.ConfigCompose(testAccIPAMPreviewNextCIDRConfig_ipv4Base, fmt.Sprintf(` resource "aws_vpc_ipam_preview_next_cidr" "test" { ipam_pool_id = aws_vpc_ipam_pool.test.id netmask_length = %[1]q @@ -170,5 +170,5 @@ resource "aws_vpc_ipam_preview_next_cidr" "test" { aws_vpc_ipam_pool_cidr.test ] } -`, netmaskLength, disallowedCidr) +`, netmaskLength, disallowedCidr)) } From 236773694154cb1a215e0b5d8357b1eea35d21db Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:18 -0400 Subject: [PATCH 45/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - elasticache. --- .../service/elasticache/replication_group_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/elasticache/replication_group_data_source_test.go b/internal/service/elasticache/replication_group_data_source_test.go index 8ed72bcd723..fd1107ff4b6 100644 --- a/internal/service/elasticache/replication_group_data_source_test.go +++ b/internal/service/elasticache/replication_group_data_source_test.go @@ -144,7 +144,7 @@ func TestAccElastiCacheReplicationGroupDataSource_Engine_Redis_LogDeliveryConfig } func testAccReplicationGroupDataSourceConfig_basic(rName string) string { - return acctest.ConfigAvailableAZsNoOptIn() + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(` resource "aws_elasticache_replication_group" "test" { replication_group_id = %[1]q description = "test description" @@ -159,7 +159,7 @@ resource "aws_elasticache_replication_group" "test" { data "aws_elasticache_replication_group" "test" { replication_group_id = aws_elasticache_replication_group.test.replication_group_id } -`, rName) +`, rName)) } func testAccReplicationGroupDataSourceConfig_clusterMode(rName string) string { From 7caba11c8ec095406378d408a2c1539198e61b1a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:18 -0400 Subject: [PATCH 46/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - elbv2. --- internal/service/elbv2/listener_certificate_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/elbv2/listener_certificate_test.go b/internal/service/elbv2/listener_certificate_test.go index 06c19cf55e0..564d5d931ed 100644 --- a/internal/service/elbv2/listener_certificate_test.go +++ b/internal/service/elbv2/listener_certificate_test.go @@ -402,7 +402,7 @@ resource "aws_lb_listener_certificate" "test" { } func testAccListenerCertificateConfig_multiple(rName string, keys, certificates []string) string { - return testAccListenerCertificateConfig_base(rName, keys[0], certificates[0]) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccListenerCertificateConfig_base(rName, keys[0], certificates[0]), fmt.Sprintf(` resource "aws_lb_listener_certificate" "default" { listener_arn = aws_lb_listener.test.arn certificate_arn = aws_iam_server_certificate.test.arn @@ -429,11 +429,11 @@ resource "aws_iam_server_certificate" "additional_2" { certificate_body = "%[4]s" private_key = "%[5]s" } -`, rName, acctest.TLSPEMEscapeNewlines(certificates[1]), acctest.TLSPEMEscapeNewlines(keys[1]), acctest.TLSPEMEscapeNewlines(certificates[2]), acctest.TLSPEMEscapeNewlines(keys[2])) +`, rName, acctest.TLSPEMEscapeNewlines(certificates[1]), acctest.TLSPEMEscapeNewlines(keys[1]), acctest.TLSPEMEscapeNewlines(certificates[2]), acctest.TLSPEMEscapeNewlines(keys[2]))) } func testAccListenerCertificateConfig_multipleAddNew(rName string, keys, certificates []string) string { - return testAccListenerCertificateConfig_multiple(rName, keys, certificates) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccListenerCertificateConfig_multiple(rName, keys, certificates), fmt.Sprintf(` resource "aws_iam_server_certificate" "additional_3" { name = "%[1]s-additional-3" certificate_body = "%[2]s" @@ -444,5 +444,5 @@ resource "aws_lb_listener_certificate" "additional_3" { listener_arn = aws_lb_listener.test.arn certificate_arn = aws_iam_server_certificate.additional_3.arn } -`, rName, acctest.TLSPEMEscapeNewlines(certificates[3]), acctest.TLSPEMEscapeNewlines(keys[3])) +`, rName, acctest.TLSPEMEscapeNewlines(certificates[3]), acctest.TLSPEMEscapeNewlines(keys[3]))) } From 9a9f2b7c8b5b153a9f6ac0b8d2fbdcca9109a907 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:26 -0400 Subject: [PATCH 47/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - rds. --- internal/service/rds/proxy_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/rds/proxy_test.go b/internal/service/rds/proxy_test.go index 77f58e1e4e1..92e434b6073 100644 --- a/internal/service/rds/proxy_test.go +++ b/internal/service/rds/proxy_test.go @@ -956,7 +956,7 @@ resource "aws_secretsmanager_secret_version" "test2" { } func testAccProxyConfig_tags1(rName, tagKey1, tagValue1 string) string { - return testAccProxyConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProxyConfig_base(rName), fmt.Sprintf(` resource "aws_db_proxy" "test" { depends_on = [ aws_secretsmanager_secret_version.test, @@ -980,11 +980,11 @@ resource "aws_db_proxy" "test" { %[2]q = %[3]q } } -`, rName, tagKey1, tagValue1) +`, rName, tagKey1, tagValue1)) } func testAccProxyConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { - return testAccProxyConfig_base(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccProxyConfig_base(rName), fmt.Sprintf(` resource "aws_db_proxy" "test" { depends_on = [ aws_secretsmanager_secret_version.test, @@ -1009,5 +1009,5 @@ resource "aws_db_proxy" "test" { %[4]q = %[5]q } } -`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +`, rName, tagKey1, tagValue1, tagKey2, tagValue2)) } From 8232b0fb48e8a10fb9306524d6bda4748ba9f43d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:27 -0400 Subject: [PATCH 48/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - s3. --- internal/service/s3/bucket_object_test.go | 4 ++-- internal/service/s3/bucket_objects_data_source_test.go | 10 +++++----- internal/service/s3/object_test.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/s3/bucket_object_test.go b/internal/service/s3/bucket_object_test.go index a8ccc10d0df..a8ef9a9b4a8 100644 --- a/internal/service/s3/bucket_object_test.go +++ b/internal/service/s3/bucket_object_test.go @@ -1849,7 +1849,7 @@ func testAccBucketObjectConfig_nonVersioned(rName string, source string) string ] }` - return acctest.ConfigAssumeRolePolicy(policy) + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAssumeRolePolicy(policy), fmt.Sprintf(` resource "aws_s3_bucket" "object_bucket_3" { bucket = %[1]q } @@ -1860,7 +1860,7 @@ resource "aws_s3_bucket_object" "object" { source = %[2]q etag = filemd5(%[2]q) } -`, rName, source) +`, rName, source)) } func testAccBucketObjectConfig_objectKeyEnabled(rName string, content string) string { diff --git a/internal/service/s3/bucket_objects_data_source_test.go b/internal/service/s3/bucket_objects_data_source_test.go index 08967d751fd..75ee0790d9e 100644 --- a/internal/service/s3/bucket_objects_data_source_test.go +++ b/internal/service/s3/bucket_objects_data_source_test.go @@ -283,17 +283,17 @@ resource "aws_s3_object" "object7" { } func testAccBucketObjectsDataSourceConfig_resourcesPlusAccessPoint(randInt int) string { - return testAccBucketObjectsDataSourceConfig_resources(randInt) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccBucketObjectsDataSourceConfig_resources(randInt), fmt.Sprintf(` resource "aws_s3_access_point" "test" { bucket = aws_s3_bucket.objects_bucket.bucket name = "tf-objects-test-access-point-%[1]d" } -`, randInt) +`, randInt)) } func testAccBucketObjectsDataSourceConfig_basic(randInt int) string { return fmt.Sprintf(` -%s +%[1]s data "aws_s3_objects" "yesh" { bucket = aws_s3_bucket.objects_bucket.id @@ -304,13 +304,13 @@ data "aws_s3_objects" "yesh" { } func testAccBucketObjectsDataSourceConfig_basicViaAccessPoint(randInt int) string { - return testAccBucketObjectsDataSourceConfig_resourcesPlusAccessPoint(randInt) + ` + return acctest.ConfigCompose(testAccBucketObjectsDataSourceConfig_resourcesPlusAccessPoint(randInt), ` data "aws_s3_objects" "yesh" { bucket = aws_s3_access_point.test.arn prefix = "arch/navajo/" delimiter = "/" } -` +`) } func testAccBucketObjectsDataSourceConfig_all(randInt int) string { diff --git a/internal/service/s3/object_test.go b/internal/service/s3/object_test.go index 036ae3f5379..d638d307865 100644 --- a/internal/service/s3/object_test.go +++ b/internal/service/s3/object_test.go @@ -2790,7 +2790,7 @@ func testAccObjectConfig_nonVersioned(rName string, source string) string { ] }` - return acctest.ConfigAssumeRolePolicy(policy) + fmt.Sprintf(` + return acctest.ConfigCompose(acctest.ConfigAssumeRolePolicy(policy), fmt.Sprintf(` resource "aws_s3_bucket" "object_bucket_3" { bucket = %[1]q } @@ -2801,7 +2801,7 @@ resource "aws_s3_object" "object" { source = %[2]q etag = filemd5(%[2]q) } -`, rName, source) +`, rName, source)) } func testAccObjectConfig_bucketKeyEnabled(rName string, content string) string { From 5e4652145062800cb03216e75d052227e1135431 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:29 -0400 Subject: [PATCH 49/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - ssm. --- internal/service/ssm/maintenance_window_task_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/service/ssm/maintenance_window_task_test.go b/internal/service/ssm/maintenance_window_task_test.go index d05fff898cd..3b3c72b5a44 100644 --- a/internal/service/ssm/maintenance_window_task_test.go +++ b/internal/service/ssm/maintenance_window_task_test.go @@ -1076,7 +1076,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_stepFunction(rName string) string { - return testAccMaintenanceWindowTaskBaseConfig(rName) + fmt.Sprintf(` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskBaseConfig(rName), fmt.Sprintf(` resource "aws_sfn_activity" "test" { name = %[1]q } @@ -1106,17 +1106,17 @@ resource "aws_ssm_maintenance_window_task" "test" { } } } -`, rName) +`, rName)) } func testAccLambdaBasicConfig(funcName, policyName, roleName, sgName string) string { - return fmt.Sprintf(acctest.ConfigLambdaBase(policyName, roleName, sgName)+` + return acctest.ConfigCompose(acctest.ConfigLambdaBase(policyName, roleName, sgName), fmt.Sprintf(` resource "aws_lambda_function" "test" { filename = "test-fixtures/lambdatest.zip" - function_name = "%s" + function_name = %[1]q role = aws_iam_role.iam_for_lambda.arn handler = "exports.example" runtime = "nodejs16.x" } -`, funcName) +`, funcName)) } From 68b20e2e326a5dc308bb881adc5985c659b1f056 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 10:56:30 -0400 Subject: [PATCH 50/59] Replace '+ fmt.Sprintf' in acceptance tests with 'acctest.ConfigCompose' - storagegateway. --- .../cached_iscsi_volume_test.go | 18 +++-- .../file_system_association_test.go | 27 +++---- .../service/storagegateway/gateway_test.go | 46 +++++------ .../storagegateway/nfs_file_share_test.go | 78 +++++++++---------- 4 files changed, 86 insertions(+), 83 deletions(-) diff --git a/internal/service/storagegateway/cached_iscsi_volume_test.go b/internal/service/storagegateway/cached_iscsi_volume_test.go index ac44748848b..9d0922df9aa 100644 --- a/internal/service/storagegateway/cached_iscsi_volume_test.go +++ b/internal/service/storagegateway/cached_iscsi_volume_test.go @@ -347,7 +347,7 @@ func testAccCheckCachediSCSIVolumeDestroy(ctx context.Context) resource.TestChec } } -func testAccCachediSCSIVolumeBaseConfig(rName string) string { +func testAccCachediSCSIVolumeConfig_base(rName string) string { return acctest.ConfigCompose( testAccGatewayConfig_typeCached(rName), fmt.Sprintf(` @@ -392,7 +392,7 @@ resource "aws_storagegateway_cache" "test" { func testAccCachediSCSIVolumeConfig_basic(rName string) string { return acctest.ConfigCompose( - testAccCachediSCSIVolumeBaseConfig(rName), + testAccCachediSCSIVolumeConfig_base(rName), fmt.Sprintf(` resource "aws_storagegateway_cached_iscsi_volume" "test" { gateway_arn = aws_storagegateway_cache.test.gateway_arn @@ -404,7 +404,9 @@ resource "aws_storagegateway_cached_iscsi_volume" "test" { } func testAccCachediSCSIVolumeConfig_kmsEncrypted(rName string) string { - return testAccCachediSCSIVolumeBaseConfig(rName) + fmt.Sprintf(` + return acctest.ConfigCompose( + testAccCachediSCSIVolumeConfig_base(rName), + fmt.Sprintf(` resource "aws_kms_key" "test" { description = "Terraform acc test %[1]s" policy = < Date: Thu, 8 Aug 2024 11:05:36 -0400 Subject: [PATCH 51/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - apigatewayv2. --- .../apigatewayv2/integration_response_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/service/apigatewayv2/integration_response_test.go b/internal/service/apigatewayv2/integration_response_test.go index 0d8ece594a3..9255021c055 100644 --- a/internal/service/apigatewayv2/integration_response_test.go +++ b/internal/service/apigatewayv2/integration_response_test.go @@ -189,17 +189,17 @@ func testAccIntegrationResponseImportStateIdFunc(resourceName string) resource.I } func testAccIntegrationResponseConfig_basic(rName string) string { - return testAccIntegrationConfig_basic(rName) + ` + return acctest.ConfigCompose(testAccIntegrationConfig_basic(rName), ` resource "aws_apigatewayv2_integration_response" "test" { api_id = aws_apigatewayv2_api.test.id integration_id = aws_apigatewayv2_integration.test.id integration_response_key = "/200/" } -` +`) } func testAccIntegrationResponseConfig_allAttributes(rName string) string { - return testAccIntegrationConfig_basic(rName) + ` + return acctest.ConfigCompose(testAccIntegrationConfig_basic(rName), ` resource "aws_apigatewayv2_integration_response" "test" { api_id = aws_apigatewayv2_api.test.id integration_id = aws_apigatewayv2_integration.test.id @@ -212,11 +212,11 @@ resource "aws_apigatewayv2_integration_response" "test" { "application/json" = "" } } -` +`) } func testAccIntegrationResponseConfig_allAttributesUpdated(rName string) string { - return testAccIntegrationConfig_basic(rName) + ` + return acctest.ConfigCompose(testAccIntegrationConfig_basic(rName), ` resource "aws_apigatewayv2_integration_response" "test" { api_id = aws_apigatewayv2_api.test.id integration_id = aws_apigatewayv2_integration.test.id @@ -230,5 +230,5 @@ resource "aws_apigatewayv2_integration_response" "test" { "application/xml" = "#set($percent=$number/100)" } } -` +`) } From e44a346dfaf286472f2b9afb166794a9a3d1beaa Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:38 -0400 Subject: [PATCH 52/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - batch. --- internal/service/batch/scheduling_policy_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/batch/scheduling_policy_data_source_test.go b/internal/service/batch/scheduling_policy_data_source_test.go index 2e661daed42..9c02ea4727f 100644 --- a/internal/service/batch/scheduling_policy_data_source_test.go +++ b/internal/service/batch/scheduling_policy_data_source_test.go @@ -45,7 +45,7 @@ func TestAccBatchSchedulingPolicyDataSource_basic(t *testing.T) { }) } -func testAccSchedulingPolicyDataSourceConfig(rName string) string { +func testAccSchedulingPolicyDataSourceConfig_base(rName string) string { return fmt.Sprintf(` resource "aws_batch_scheduling_policy" "test" { name = %[1]q @@ -69,7 +69,7 @@ resource "aws_batch_scheduling_policy" "test" { } func testAccSchedulingPolicyDataSourceConfig_basic(rName string) string { - return fmt.Sprintf(testAccSchedulingPolicyDataSourceConfig(rName) + ` + return acctest.ConfigCompose(testAccSchedulingPolicyDataSourceConfig_base(rName), ` data "aws_batch_scheduling_policy" "test" { arn = aws_batch_scheduling_policy.test.arn } From 01ab01ba912b03f732fecbaf4cbe2e416a41cea2 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:41 -0400 Subject: [PATCH 53/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - cognitoidentity. --- .../cognitoidentity/pool_roles_attachment_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/service/cognitoidentity/pool_roles_attachment_test.go b/internal/service/cognitoidentity/pool_roles_attachment_test.go index 82854d54ed6..704d84677f3 100644 --- a/internal/service/cognitoidentity/pool_roles_attachment_test.go +++ b/internal/service/cognitoidentity/pool_roles_attachment_test.go @@ -239,7 +239,7 @@ func testAccCheckPoolRolesAttachmentDestroy(ctx context.Context) resource.TestCh } } -func testAccPoolRolesAttachmentConfig(name string) string { +func testAccPoolRolesAttachmentConfig_base(name string) string { return fmt.Sprintf(` resource "aws_cognito_identity_pool" "main" { identity_pool_name = "identity pool %[1]s" @@ -356,7 +356,7 @@ EOF } func testAccPoolRolesAttachmentConfig_basic(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id @@ -368,7 +368,7 @@ resource "aws_cognito_identity_pool_roles_attachment" "test" { } func testAccPoolRolesAttachmentConfig_roleMappings(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id @@ -393,7 +393,7 @@ resource "aws_cognito_identity_pool_roles_attachment" "test" { } func testAccPoolRolesAttachmentConfig_roleMappingsUpdated(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id @@ -425,7 +425,7 @@ resource "aws_cognito_identity_pool_roles_attachment" "test" { } func testAccPoolRolesAttachmentConfig_roleMappingsWithAmbiguousRoleResolutionError(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id @@ -449,7 +449,7 @@ resource "aws_cognito_identity_pool_roles_attachment" "test" { } func testAccPoolRolesAttachmentConfig_roleMappingsWithRulesTypeError(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id @@ -467,7 +467,7 @@ resource "aws_cognito_identity_pool_roles_attachment" "test" { } func testAccPoolRolesAttachmentConfig_roleMappingsWithTokenTypeError(name string) string { - return fmt.Sprintf(testAccPoolRolesAttachmentConfig(name) + ` + return acctest.ConfigCompose(testAccPoolRolesAttachmentConfig_base(name), ` resource "aws_cognito_identity_pool_roles_attachment" "test" { identity_pool_id = aws_cognito_identity_pool.main.id From db7bf6ac24a76846b6413f98f91f3814a26761fd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:43 -0400 Subject: [PATCH 54/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - ec2. --- .../ec2/ec2_instance_type_offerings_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/ec2_instance_type_offerings_data_source_test.go b/internal/service/ec2/ec2_instance_type_offerings_data_source_test.go index be1d36c26a4..893f14be9f1 100644 --- a/internal/service/ec2/ec2_instance_type_offerings_data_source_test.go +++ b/internal/service/ec2/ec2_instance_type_offerings_data_source_test.go @@ -89,7 +89,7 @@ data "aws_ec2_instance_type_offerings" "test" { } func testAccInstanceTypeOfferingsDataSourceConfig_location() string { - return acctest.ConfigAvailableAZsNoOptIn() + ` + return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), ` data "aws_ec2_instance_type_offerings" "test" { filter { name = "location" @@ -98,5 +98,5 @@ data "aws_ec2_instance_type_offerings" "test" { location_type = "availability-zone" } -` +`) } From 6abd5569b4975458684954de3fd61bd8bced9b85 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:44 -0400 Subject: [PATCH 55/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - elbv2. --- internal/service/elbv2/listener_certificate_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/elbv2/listener_certificate_test.go b/internal/service/elbv2/listener_certificate_test.go index 564d5d931ed..cda26656fd9 100644 --- a/internal/service/elbv2/listener_certificate_test.go +++ b/internal/service/elbv2/listener_certificate_test.go @@ -324,12 +324,12 @@ resource "aws_lb_listener" "test" { } func testAccListenerCertificateConfig_basic(rName, key, certificate string) string { - return testAccListenerCertificateConfig_base(rName, key, certificate) + ` + return acctest.ConfigCompose(testAccListenerCertificateConfig_base(rName, key, certificate), ` resource "aws_lb_listener_certificate" "test" { certificate_arn = aws_iam_server_certificate.test.arn listener_arn = aws_lb_listener.test.arn } -` +`) } func testAccListenerCertificateConfig_arnUnderscores(rName, key, certificate string) string { From 0f72bc4b5d761f37a097036dc2e08fd0307ea2ec Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:45 -0400 Subject: [PATCH 56/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - events. --- internal/service/events/target_test.go | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/service/events/target_test.go b/internal/service/events/target_test.go index c0b23f46abf..38b784f64b4 100644 --- a/internal/service/events/target_test.go +++ b/internal/service/events/target_test.go @@ -1656,7 +1656,7 @@ data "aws_partition" "current" {} `, rName) } -func testAccTargetHTTPConfigBase(rName string) string { +func testAccTargetConfig_baseHTTP(rName string) string { return fmt.Sprintf(` resource "aws_cloudwatch_event_rule" "test" { name = %[1]q @@ -1711,7 +1711,7 @@ data "aws_partition" "current" {} } func testAccTargetConfig_http(rName string) string { - return testAccTargetHTTPConfigBase(rName) + ` + return acctest.ConfigCompose(testAccTargetConfig_baseHTTP(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = "${aws_api_gateway_stage.test.execution_arn}/GET" rule = aws_cloudwatch_event_rule.test.id @@ -1727,11 +1727,11 @@ resource "aws_cloudwatch_event_target" "test" { } } } -` +`) } func testAccTargetConfig_httpParameter(rName string) string { - return testAccTargetHTTPConfigBase(rName) + ` + return acctest.ConfigCompose(testAccTargetConfig_baseHTTP(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = "${aws_api_gateway_stage.test.execution_arn}/*/*/GET" rule = aws_cloudwatch_event_rule.test.id @@ -1747,11 +1747,11 @@ resource "aws_cloudwatch_event_target" "test" { } } } -` +`) } func testAccTargetConfig_httpParameterUpdated(rName string) string { - return testAccTargetHTTPConfigBase(rName) + ` + return acctest.ConfigCompose(testAccTargetConfig_baseHTTP(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = "${aws_api_gateway_stage.test.execution_arn}/*/*/*/GET" rule = aws_cloudwatch_event_rule.test.id @@ -1767,10 +1767,10 @@ resource "aws_cloudwatch_event_target" "test" { } } } -` +`) } -func testAccTargetConfig_ecsBase(rName string) string { +func testAccTargetConfig_baseECS(rName string) string { return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 1), fmt.Sprintf(` resource "aws_iam_role" "test" { name = %[1]q @@ -1841,7 +1841,7 @@ resource "aws_cloudwatch_event_rule" "test" { } func testAccTargetConfig_ecs(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id @@ -1932,7 +1932,7 @@ resource "aws_redshift_cluster" "test" { } func testAccTargetConfig_ecsNoLaunchType(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id @@ -1951,7 +1951,7 @@ resource "aws_cloudwatch_event_target" "test" { } func testAccTargetConfig_ecsBlankLaunchType(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id @@ -1971,7 +1971,7 @@ resource "aws_cloudwatch_event_target" "test" { } func testAccTargetConfig_ecsBlankTaskCount(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id @@ -1990,7 +1990,7 @@ resource "aws_cloudwatch_event_target" "test" { } func testAccTargetConfig_ecsBlankTaskCountFull(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id @@ -2022,7 +2022,7 @@ resource "aws_cloudwatch_event_target" "test" { func testAccTargetConfig_ecsCapacityProvider(rName string) string { return acctest.ConfigCompose( acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(), - testAccTargetConfig_ecsBase(rName), + testAccTargetConfig_baseECS(rName), fmt.Sprintf(` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id @@ -2088,7 +2088,7 @@ resource "aws_ecs_capacity_provider" "test" { func testAccTargetConfig_ecsPlacementStrategy(rName string) string { return acctest.ConfigCompose( acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(), - testAccTargetConfig_ecsBase(rName), + testAccTargetConfig_baseECS(rName), fmt.Sprintf(` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id @@ -2629,7 +2629,7 @@ resource "aws_sns_topic" "test" { } func testAccTargetConfig_ecsNoPropagateTags(rName string) string { - return acctest.ConfigCompose(testAccTargetConfig_ecsBase(rName), ` + return acctest.ConfigCompose(testAccTargetConfig_baseECS(rName), ` resource "aws_cloudwatch_event_target" "test" { arn = aws_ecs_cluster.test.id rule = aws_cloudwatch_event_rule.test.id From 644aa93a0f40beb289300e3ece444065435c7cb0 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:56 -0400 Subject: [PATCH 57/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - ssm. --- .../ssm/maintenance_window_task_test.go | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/service/ssm/maintenance_window_task_test.go b/internal/service/ssm/maintenance_window_task_test.go index 3b3c72b5a44..bface56b2f4 100644 --- a/internal/service/ssm/maintenance_window_task_test.go +++ b/internal/service/ssm/maintenance_window_task_test.go @@ -545,7 +545,7 @@ func testAccMaintenanceWindowTaskImportStateIdFunc(resourceName string) resource } } -func testAccMaintenanceWindowTaskBaseConfig(rName string) string { +func testAccMaintenanceWindowTaskConfig_base(rName string) string { return fmt.Sprintf(` resource "aws_ssm_maintenance_window" "test" { cutoff = 1 @@ -604,7 +604,7 @@ POLICY } func testAccMaintenanceWindowTaskConfig_basic(rName string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName) + ` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), ` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -633,7 +633,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_noTarget(rName string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName) + ` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), ` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -646,7 +646,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_cutoff(rName, cutoff string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -660,7 +660,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_basicUpdate(rName, description, taskType, taskArn string, priority, maxConcurrency, maxErrors int) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -727,7 +727,7 @@ EOF } func testAccMaintenanceWindowTaskConfig_basicUpdated(rName string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName) + ` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), ` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -759,7 +759,7 @@ resource "aws_ssm_maintenance_window_task" "test" { func testAccMaintenanceWindowTaskConfig_description(rName string, description string) string { return acctest.ConfigCompose( - testAccMaintenanceWindowTaskBaseConfig(rName), + testAccMaintenanceWindowTaskConfig_base(rName), fmt.Sprintf(` resource "aws_ssm_maintenance_window_task" "test" { description = %[1]q @@ -787,7 +787,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_emptyNotifcation(rName string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName) + ` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), ` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -820,7 +820,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_noRole(rName string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName) + ` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), ` resource "aws_ssm_maintenance_window_task" "test" { description = "This resource is for test purpose only" max_concurrency = 2 @@ -849,7 +849,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_automation(rName, version string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -885,7 +885,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_automationUpdate(rName, version string) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_s3_bucket" "test" { bucket = %[1]q force_destroy = true @@ -926,7 +926,7 @@ resource "aws_ssm_maintenance_window_task" "test" { func testAccMaintenanceWindowTaskConfig_lambda(funcName, policyName, roleName, sgName, rName string, rInt int) string { return fmt.Sprintf(testAccLambdaBasicConfig(funcName, policyName, roleName, sgName)+ - testAccMaintenanceWindowTaskBaseConfig(rName)+` + testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -959,7 +959,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_runCommand(rName, comment string, timeoutSeconds int) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_ssm_maintenance_window_task" "test" { window_id = aws_ssm_maintenance_window.test.id @@ -994,7 +994,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_runCommandUpdate(rName, comment string, timeoutSeconds int) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_s3_bucket" "test" { bucket = %[1]q force_destroy = true @@ -1035,7 +1035,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_runCommandCloudWatch(rName string, enabled bool) string { - return fmt.Sprintf(testAccMaintenanceWindowTaskBaseConfig(rName)+` + return fmt.Sprintf(testAccMaintenanceWindowTaskConfig_base(rName)+` resource "aws_cloudwatch_log_group" "test" { name = %[1]q } @@ -1076,7 +1076,7 @@ resource "aws_ssm_maintenance_window_task" "test" { } func testAccMaintenanceWindowTaskConfig_stepFunction(rName string) string { - return acctest.ConfigCompose(testAccMaintenanceWindowTaskBaseConfig(rName), fmt.Sprintf(` + return acctest.ConfigCompose(testAccMaintenanceWindowTaskConfig_base(rName), fmt.Sprintf(` resource "aws_sfn_activity" "test" { name = %[1]q } From eb05751dc2e7d5a8eff82c15449e22e838759eac Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:56 -0400 Subject: [PATCH 58/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - storagegateway. --- .../storagegateway/nfs_file_share_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/service/storagegateway/nfs_file_share_test.go b/internal/service/storagegateway/nfs_file_share_test.go index 5faf0a8d83e..671190463b4 100644 --- a/internal/service/storagegateway/nfs_file_share_test.go +++ b/internal/service/storagegateway/nfs_file_share_test.go @@ -800,14 +800,14 @@ resource "aws_storagegateway_gateway" "test" { } func testAccNFSFileShareConfig_required(rName string) string { - return testAccNFSFileShareConfig_baseS3(rName) + ` + return acctest.ConfigCompose(testAccNFSFileShareConfig_baseS3(rName), ` resource "aws_storagegateway_nfs_file_share" "test" { client_list = ["0.0.0.0/0"] gateway_arn = aws_storagegateway_gateway.test.arn location_arn = aws_s3_bucket.test.arn role_arn = aws_iam_role.test.arn } -` +`) } func testAccNFSFileShareConfig_name(rName, fsName string) string { @@ -912,7 +912,7 @@ resource "aws_storagegateway_nfs_file_share" "test" { } func testAccNFSFileShareConfig_kmsKeyARN(rName string) string { - return testAccNFSFileShareConfig_baseS3(rName) + ` + return acctest.ConfigCompose(testAccNFSFileShareConfig_baseS3(rName), ` resource "aws_kms_key" "test" { count = 2 @@ -928,11 +928,11 @@ resource "aws_storagegateway_nfs_file_share" "test" { location_arn = aws_s3_bucket.test.arn role_arn = aws_iam_role.test.arn } -` +`) } func testAccNFSFileShareConfig_kmsKeyARNUpdate(rName string) string { - return testAccNFSFileShareConfig_baseS3(rName) + ` + return acctest.ConfigCompose(testAccNFSFileShareConfig_baseS3(rName), ` resource "aws_kms_key" "test" { count = 2 @@ -948,7 +948,7 @@ resource "aws_storagegateway_nfs_file_share" "test" { location_arn = aws_s3_bucket.test.arn role_arn = aws_iam_role.test.arn } -` +`) } func testAccNFSFileShareConfig_defaults(rName, directoryMode, fileMode string, groupID, ownerID int) string { @@ -1033,7 +1033,7 @@ resource "aws_storagegateway_nfs_file_share" "test" { } func testAccNFSFileShareConfig_notificationPolicy(rName string) string { - return testAccNFSFileShareConfig_baseS3(rName) + ` + return acctest.ConfigCompose(testAccNFSFileShareConfig_baseS3(rName), ` resource "aws_storagegateway_nfs_file_share" "test" { client_list = ["0.0.0.0/0"] gateway_arn = aws_storagegateway_gateway.test.arn @@ -1041,7 +1041,7 @@ resource "aws_storagegateway_nfs_file_share" "test" { role_arn = aws_iam_role.test.arn notification_policy = "{\"Upload\": {\"SettlingTimeInSeconds\": 60}}" } -` +`) } func testAccNFSFileShareConfig_audit(rName string) string { From 077fc896388ea132ab442560ef70eb2994f219cb Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 8 Aug 2024 11:05:57 -0400 Subject: [PATCH 59/59] Replace '+ ' in acceptance tests with 'acctest.ConfigCompose' - wafregional. --- internal/service/wafregional/web_acl_association_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/wafregional/web_acl_association_test.go b/internal/service/wafregional/web_acl_association_test.go index 46754d30709..a2519901e11 100644 --- a/internal/service/wafregional/web_acl_association_test.go +++ b/internal/service/wafregional/web_acl_association_test.go @@ -222,7 +222,7 @@ resource "aws_wafregional_web_acl_association" "foo" { } ` -const testAccWebACLAssociationConfig_multiples = testAccWebACLAssociationConfig_basic + ` +var testAccWebACLAssociationConfig_multiples = acctest.ConfigCompose(testAccWebACLAssociationConfig_basic, ` resource "aws_alb" "bar" { internal = true subnets = [aws_subnet.foo.id, aws_subnet.bar.id] @@ -232,7 +232,7 @@ resource "aws_wafregional_web_acl_association" "bar" { resource_arn = aws_alb.bar.arn web_acl_id = aws_wafregional_web_acl.foo.id } -` +`) func testAccWebACLAssociationConfig_checkResourceARNAPIGatewayStage(rName string) string { return fmt.Sprintf(`