Skip to content

Commit

Permalink
Merge pull request #37407 from DanielRieske/f/migrate-wavelength-sdkv2
Browse files Browse the repository at this point in the history
Migrate `wavelength` resource to AWS Go SDKv2
  • Loading branch information
ewbankkit authored May 20, 2024
2 parents cfd77cd + 5398ce1 commit eae23a8
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 155 deletions.
3 changes: 3 additions & 0 deletions internal/service/ec2/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ec2

// Exports for use in tests only.
var (
ResourceCarrierGateway = resourceCarrierGateway
ResourceClientVPNAuthorizationRule = resourceClientVPNAuthorizationRule
ResourceClientVPNEndpoint = resourceClientVPNEndpoint
ResourceClientVPNNetworkAssociation = resourceClientVPNNetworkAssociation
Expand Down Expand Up @@ -36,6 +37,8 @@ var (
ResourceVPNGatewayRoutePropagation = resourceVPNGatewayRoutePropagation

CustomFiltersSchema = customFiltersSchema
FindAvailabilityZonesV2 = findAvailabilityZonesV2
FindCarrierGatewayByID = findCarrierGatewayByID
FindClientVPNAuthorizationRuleByThreePartKey = findClientVPNAuthorizationRuleByThreePartKey
FindClientVPNEndpointByID = findClientVPNEndpointByID
FindClientVPNNetworkAssociationByTwoPartKey = findClientVPNNetworkAssociationByTwoPartKey
Expand Down
69 changes: 0 additions & 69 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
76 changes: 76 additions & 0 deletions internal/service/ec2/findv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions internal/service/ec2/statusv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion internal/service/ec2/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
40 changes: 0 additions & 40 deletions internal/service/ec2/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions internal/service/ec2/waitv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit eae23a8

Please sign in to comment.