diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index ad90fc66e4e..b6b8fac24e3 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -5,6 +5,7 @@ package ec2 // Exports for use in tests only. var ( + ResourceCarrierGateway = resourceCarrierGateway ResourceClientVPNAuthorizationRule = resourceClientVPNAuthorizationRule ResourceClientVPNEndpoint = resourceClientVPNEndpoint ResourceClientVPNNetworkAssociation = resourceClientVPNNetworkAssociation @@ -36,6 +37,8 @@ var ( ResourceVPNGatewayRoutePropagation = resourceVPNGatewayRoutePropagation CustomFiltersSchema = customFiltersSchema + FindAvailabilityZonesV2 = findAvailabilityZonesV2 + FindCarrierGatewayByID = findCarrierGatewayByID FindClientVPNAuthorizationRuleByThreePartKey = findClientVPNAuthorizationRuleByThreePartKey FindClientVPNEndpointByID = findClientVPNEndpointByID FindClientVPNNetworkAssociationByTwoPartKey = findClientVPNNetworkAssociationByTwoPartKey diff --git a/internal/service/ec2/find.go b/internal/service/ec2/find.go index c96e777b5db..584eff5c263 100644 --- a/internal/service/ec2/find.go +++ b/internal/service/ec2/find.go @@ -147,75 +147,6 @@ func FindCapacityReservationByID(ctx context.Context, conn *ec2.EC2, id string) return output, nil } -func FindCarrierGateway(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCarrierGatewaysInput) (*ec2.CarrierGateway, error) { - output, err := FindCarrierGateways(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSinglePtrResult(output) -} - -func FindCarrierGateways(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCarrierGatewaysInput) ([]*ec2.CarrierGateway, error) { - var output []*ec2.CarrierGateway - - err := conn.DescribeCarrierGatewaysPagesWithContext(ctx, input, func(page *ec2.DescribeCarrierGatewaysOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, v := range page.CarrierGateways { - if v != nil { - output = append(output, v) - } - } - - return !lastPage - }) - - if tfawserr.ErrCodeEquals(err, errCodeInvalidCarrierGatewayIDNotFound) { - return nil, &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - return output, nil -} - -func FindCarrierGatewayByID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.CarrierGateway, error) { - input := &ec2.DescribeCarrierGatewaysInput{ - CarrierGatewayIds: aws.StringSlice([]string{id}), - } - - output, err := FindCarrierGateway(ctx, conn, input) - - if err != nil { - return nil, err - } - - if state := aws.StringValue(output.State); state == ec2.CarrierGatewayStateDeleted { - return nil, &retry.NotFoundError{ - Message: state, - LastRequest: input, - } - } - - // Eventual consistency check. - if aws.StringValue(output.CarrierGatewayId) != id { - return nil, &retry.NotFoundError{ - LastRequest: input, - } - } - - return output, nil -} - func FindCOIPPools(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCoipPoolsInput) ([]*ec2.CoipPool, error) { var output []*ec2.CoipPool diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index b64d6f52aba..dcfd6f5c93b 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -18,6 +18,20 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) +func findAvailabilityZonesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeAvailabilityZonesInput) ([]awstypes.AvailabilityZone, error) { + output, err := conn.DescribeAvailabilityZones(ctx, input) + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.AvailabilityZones, nil +} + func findVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName) (bool, error) { input := &ec2.DescribeVpcAttributeInput{ Attribute: attribute, @@ -1234,3 +1248,65 @@ func findClientVPNRouteByThreePartKey(ctx context.Context, conn *ec2.Client, end return findClientVPNRoute(ctx, conn, input) } + +func findCarrierGateway(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCarrierGatewaysInput) (*awstypes.CarrierGateway, error) { + output, err := findCarrierGateways(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findCarrierGateways(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCarrierGatewaysInput) ([]awstypes.CarrierGateway, error) { + var output []awstypes.CarrierGateway + + pages := ec2.NewDescribeCarrierGatewaysPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidCarrierGatewayIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.CarrierGateways...) + } + + return output, nil +} + +func findCarrierGatewayByID(ctx context.Context, conn *ec2.Client, id string) (*awstypes.CarrierGateway, error) { + input := &ec2.DescribeCarrierGatewaysInput{ + CarrierGatewayIds: []string{id}, + } + + output, err := findCarrierGateway(ctx, conn, input) + + if err != nil { + return nil, err + } + + if state := output.State; state == awstypes.CarrierGatewayStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + // Eventual consistency check. + if aws.ToString(output.CarrierGatewayId) != id { + return nil, &retry.NotFoundError{ + LastRequest: input, + } + } + + return output, nil +} diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 669c7482c64..5119831ab9f 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -559,7 +559,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceCarrierGateway, + Factory: resourceCarrierGateway, TypeName: "aws_ec2_carrier_gateway", Name: "Carrier Gateway", Tags: &types.ServicePackageResourceTags{ diff --git a/internal/service/ec2/status.go b/internal/service/ec2/status.go index 96d175b3a87..dca4c72b813 100644 --- a/internal/service/ec2/status.go +++ b/internal/service/ec2/status.go @@ -47,22 +47,6 @@ func StatusCapacityReservationState(ctx context.Context, conn *ec2.EC2, id strin } } -func StatusCarrierGatewayState(ctx context.Context, conn *ec2.EC2, id string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - output, err := FindCarrierGatewayByID(ctx, conn, id) - - if tfresource.NotFound(err) { - return nil, "", nil - } - - if err != nil { - return nil, "", err - } - - return output, aws.StringValue(output.State), nil - } -} - // StatusLocalGatewayRouteTableVPCAssociationState fetches the LocalGatewayRouteTableVpcAssociation and its State func StatusLocalGatewayRouteTableVPCAssociationState(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableVpcAssociationID string) retry.StateRefreshFunc { return func() (interface{}, string, error) { diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index 89d9487b01e..491129ccd18 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -327,3 +327,19 @@ func statusClientVPNRoute(ctx context.Context, conn *ec2.Client, endpointID, tar return output, string(output.Status.Code), nil } } + +func statusCarrierGateway(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findCarrierGatewayByID(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.State), nil + } +} diff --git a/internal/service/ec2/sweep.go b/internal/service/ec2/sweep.go index 28505b3363a..2c16355ec13 100644 --- a/internal/service/ec2/sweep.go +++ b/internal/service/ec2/sweep.go @@ -520,7 +520,7 @@ func sweepCarrierGateways(region string) error { } for _, v := range page.CarrierGateways { - r := ResourceCarrierGateway() + r := resourceCarrierGateway() d := r.Data(nil) d.SetId(aws.StringValue(v.CarrierGatewayId)) diff --git a/internal/service/ec2/wait.go b/internal/service/ec2/wait.go index 879e7fbce9b..cbc5066043c 100644 --- a/internal/service/ec2/wait.go +++ b/internal/service/ec2/wait.go @@ -120,46 +120,6 @@ func WaitCapacityReservationDeleted(ctx context.Context, conn *ec2.EC2, id strin return nil, err } -const ( - CarrierGatewayAvailableTimeout = 5 * time.Minute - - CarrierGatewayDeletedTimeout = 5 * time.Minute -) - -func WaitCarrierGatewayCreated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.CarrierGateway, error) { - stateConf := &retry.StateChangeConf{ - Pending: []string{ec2.CarrierGatewayStatePending}, - Target: []string{ec2.CarrierGatewayStateAvailable}, - Refresh: StatusCarrierGatewayState(ctx, conn, id), - Timeout: CarrierGatewayAvailableTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*ec2.CarrierGateway); ok { - return output, err - } - - return nil, err -} - -func WaitCarrierGatewayDeleted(ctx context.Context, conn *ec2.EC2, id string) (*ec2.CarrierGateway, error) { - stateConf := &retry.StateChangeConf{ - Pending: []string{ec2.CarrierGatewayStateDeleting}, - Target: []string{}, - Refresh: StatusCarrierGatewayState(ctx, conn, id), - Timeout: CarrierGatewayDeletedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*ec2.CarrierGateway); ok { - return output, err - } - - return nil, err -} - const ( // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Associated LocalGatewayRouteTableVPCAssociationAssociatedTimeout = 5 * time.Minute diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 2b0d988ee51..31556fc1814 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -640,3 +640,43 @@ func waitClientVPNRouteDeleted(ctx context.Context, conn *ec2.Client, endpointID return nil, err } + +func waitCarrierGatewayCreated(ctx context.Context, conn *ec2.Client, id string) (*types.CarrierGateway, error) { + const ( + timeout = 5 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(types.CarrierGatewayStatePending), + Target: enum.Slice(types.CarrierGatewayStateAvailable), + Refresh: statusCarrierGateway(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.CarrierGateway); ok { + return output, err + } + + return nil, err +} + +func waitCarrierGatewayDeleted(ctx context.Context, conn *ec2.Client, id string) (*types.CarrierGateway, error) { + const ( + timeout = 5 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(types.CarrierGatewayStateDeleting), + Target: []string{}, + Refresh: statusCarrierGateway(ctx, conn, id), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.CarrierGateway); ok { + return output, err + } + + return nil, err +} diff --git a/internal/service/ec2/wavelength_carrier_gateway.go b/internal/service/ec2/wavelength_carrier_gateway.go index 6651e2f83b3..33392fbcf22 100644 --- a/internal/service/ec2/wavelength_carrier_gateway.go +++ b/internal/service/ec2/wavelength_carrier_gateway.go @@ -8,10 +8,11 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/ec2" - "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/aws/arn" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -25,7 +26,7 @@ import ( // @SDKResource("aws_ec2_carrier_gateway, name="Carrier Gateway") // @Tags(identifierAttribute="id") -func ResourceCarrierGateway() *schema.Resource { +func resourceCarrierGateway() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceCarrierGatewayCreate, ReadWithoutTimeout: resourceCarrierGatewayRead, @@ -60,23 +61,23 @@ func ResourceCarrierGateway() *schema.Resource { func resourceCarrierGatewayCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.CreateCarrierGatewayInput{ ClientToken: aws.String(id.UniqueId()), - TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeCarrierGateway), + TagSpecifications: getTagSpecificationsInV2(ctx, awstypes.ResourceTypeCarrierGateway), VpcId: aws.String(d.Get(names.AttrVPCID).(string)), } - output, err := conn.CreateCarrierGatewayWithContext(ctx, input) + output, err := conn.CreateCarrierGateway(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Carrier Gateway: %s", err) } - d.SetId(aws.StringValue(output.CarrierGateway.CarrierGatewayId)) + d.SetId(aws.ToString(output.CarrierGateway.CarrierGatewayId)) - if _, err := WaitCarrierGatewayCreated(ctx, conn, d.Id()); err != nil { + if _, err := waitCarrierGatewayCreated(ctx, conn, d.Id()); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Carrier Gateway (%s) create: %s", d.Id(), err) } @@ -85,9 +86,9 @@ func resourceCarrierGatewayCreate(ctx context.Context, d *schema.ResourceData, m func resourceCarrierGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) - carrierGateway, err := FindCarrierGatewayByID(ctx, conn, d.Id()) + carrierGateway, err := findCarrierGatewayByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] EC2 Carrier Gateway (%s) not found, removing from state", d.Id()) @@ -99,10 +100,10 @@ func resourceCarrierGatewayRead(ctx context.Context, d *schema.ResourceData, met return sdkdiag.AppendErrorf(diags, "reading EC2 Carrier Gateway (%s): %s", d.Id(), err) } - ownerID := aws.StringValue(carrierGateway.OwnerId) + ownerID := aws.ToString(carrierGateway.OwnerId) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, - Service: ec2.ServiceName, + Service: names.EC2, Region: meta.(*conns.AWSClient).Region, AccountID: ownerID, Resource: fmt.Sprintf("carrier-gateway/%s", d.Id()), @@ -111,7 +112,7 @@ func resourceCarrierGatewayRead(ctx context.Context, d *schema.ResourceData, met d.Set(names.AttrOwnerID, ownerID) d.Set(names.AttrVPCID, carrierGateway.VpcId) - setTagsOut(ctx, carrierGateway.Tags) + setTagsOutV2(ctx, carrierGateway.Tags) return diags } @@ -126,10 +127,10 @@ func resourceCarrierGatewayUpdate(ctx context.Context, d *schema.ResourceData, m func resourceCarrierGatewayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) log.Printf("[INFO] Deleting EC2 Carrier Gateway (%s)", d.Id()) - _, err := conn.DeleteCarrierGatewayWithContext(ctx, &ec2.DeleteCarrierGatewayInput{ + _, err := conn.DeleteCarrierGateway(ctx, &ec2.DeleteCarrierGatewayInput{ CarrierGatewayId: aws.String(d.Id()), }) @@ -141,7 +142,7 @@ func resourceCarrierGatewayDelete(ctx context.Context, d *schema.ResourceData, m return sdkdiag.AppendErrorf(diags, "deleting EC2 Carrier Gateway (%s): %s", d.Id(), err) } - if _, err := WaitCarrierGatewayDeleted(ctx, conn, d.Id()); err != nil { + if _, err := waitCarrierGatewayDeleted(ctx, conn, d.Id()); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Carrier Gateway (%s) delete: %s", d.Id(), err) } diff --git a/internal/service/ec2/wavelength_carrier_gateway_test.go b/internal/service/ec2/wavelength_carrier_gateway_test.go index f08d609ce68..62abbb61729 100644 --- a/internal/service/ec2/wavelength_carrier_gateway_test.go +++ b/internal/service/ec2/wavelength_carrier_gateway_test.go @@ -9,7 +9,8 @@ import ( "testing" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/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 +23,7 @@ import ( func TestAccWavelengthCarrierGateway_basic(t *testing.T) { ctx := acctest.Context(t) - var v ec2.CarrierGateway + var v awstypes.CarrierGateway resourceName := "aws_ec2_carrier_gateway.test" vpcResourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -54,7 +55,7 @@ func TestAccWavelengthCarrierGateway_basic(t *testing.T) { func TestAccWavelengthCarrierGateway_disappears(t *testing.T) { ctx := acctest.Context(t) - var v ec2.CarrierGateway + var v awstypes.CarrierGateway resourceName := "aws_ec2_carrier_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -78,7 +79,7 @@ func TestAccWavelengthCarrierGateway_disappears(t *testing.T) { func TestAccWavelengthCarrierGateway_tags(t *testing.T) { ctx := acctest.Context(t) - var v ec2.CarrierGateway + var v awstypes.CarrierGateway resourceName := "aws_ec2_carrier_gateway.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -124,7 +125,7 @@ func TestAccWavelengthCarrierGateway_tags(t *testing.T) { func testAccCheckCarrierGatewayDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_ec2_carrier_gateway" { @@ -148,7 +149,7 @@ func testAccCheckCarrierGatewayDestroy(ctx context.Context) resource.TestCheckFu } } -func testAccCheckCarrierGatewayExists(ctx context.Context, n string, v *ec2.CarrierGateway) resource.TestCheckFunc { +func testAccCheckCarrierGatewayExists(ctx context.Context, n string, v *awstypes.CarrierGateway) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -159,7 +160,7 @@ func testAccCheckCarrierGatewayExists(ctx context.Context, n string, v *ec2.Carr return fmt.Errorf("No EC2 Carrier Gateway ID is set") } - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) output, err := tfec2.FindCarrierGatewayByID(ctx, conn, rs.Primary.ID) @@ -174,16 +175,16 @@ func testAccCheckCarrierGatewayExists(ctx context.Context, n string, v *ec2.Carr } func testAccPreCheckWavelengthZoneAvailable(ctx context.Context, t *testing.T) { - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeAvailabilityZonesInput{ - Filters: tfec2.NewAttributeFilterList(map[string]string{ + Filters: tfec2.NewAttributeFilterListV2(map[string]string{ "zone-type": "wavelength-zone", "opt-in-status": "opted-in", }), } - output, err := tfec2.FindAvailabilityZones(ctx, conn, input) + output, err := tfec2.FindAvailabilityZonesV2(ctx, conn, input) if acctest.PreCheckSkipError(err) { t.Skipf("skipping acceptance testing: %s", err)