Skip to content

Commit

Permalink
Migrate tracker_association
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilturek committed Jul 17, 2024
1 parent 28a4b0c commit 2c0f7e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
45 changes: 22 additions & 23 deletions internal/service/location/tracker_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/locationservice"
"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/location"
awstypes "github.com/aws/aws-sdk-go-v2/service/location/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/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -65,17 +66,17 @@ const (
func resourceTrackerAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).LocationConn(ctx)
conn := meta.(*conns.AWSClient).LocationClient(ctx)

consumerArn := d.Get("consumer_arn").(string)
trackerName := d.Get("tracker_name").(string)

in := &locationservice.AssociateTrackerConsumerInput{
in := &location.AssociateTrackerConsumerInput{
ConsumerArn: aws.String(consumerArn),
TrackerName: aws.String(trackerName),
}

out, err := conn.AssociateTrackerConsumerWithContext(ctx, in)
out, err := conn.AssociateTrackerConsumer(ctx, in)
if err != nil {
return create.AppendDiagError(diags, names.Location, create.ErrActionCreating, ResNameTrackerAssociation, d.Get(names.AttrName).(string), err)
}
Expand All @@ -92,7 +93,7 @@ func resourceTrackerAssociationCreate(ctx context.Context, d *schema.ResourceDat
func resourceTrackerAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).LocationConn(ctx)
conn := meta.(*conns.AWSClient).LocationClient(ctx)

trackerAssociationId, err := TrackerAssociationParseID(d.Id())
if err != nil {
Expand Down Expand Up @@ -120,7 +121,7 @@ func resourceTrackerAssociationRead(ctx context.Context, d *schema.ResourceData,
func resourceTrackerAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).LocationConn(ctx)
conn := meta.(*conns.AWSClient).LocationClient(ctx)

log.Printf("[INFO] Deleting Location TrackerAssociation %s", d.Id())

Expand All @@ -129,12 +130,12 @@ func resourceTrackerAssociationDelete(ctx context.Context, d *schema.ResourceDat
return create.AppendDiagError(diags, names.Location, create.ErrActionReading, ResNameTrackerAssociation, d.Id(), err)
}

_, err = conn.DisassociateTrackerConsumerWithContext(ctx, &locationservice.DisassociateTrackerConsumerInput{
_, err = conn.DisassociateTrackerConsumer(ctx, &location.DisassociateTrackerConsumerInput{
ConsumerArn: aws.String(trackerAssociationId.ConsumerARN),
TrackerName: aws.String(trackerAssociationId.TrackerName),
})

if tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand All @@ -146,30 +147,28 @@ func resourceTrackerAssociationDelete(ctx context.Context, d *schema.ResourceDat
}

// FindTrackerAssociationByTrackerNameAndConsumerARN returns an error if an association for specified tracker and consumer cannot be found
func FindTrackerAssociationByTrackerNameAndConsumerARN(ctx context.Context, conn *locationservice.LocationService, trackerName, consumerARN string) error {
in := &locationservice.ListTrackerConsumersInput{
func FindTrackerAssociationByTrackerNameAndConsumerARN(ctx context.Context, conn *location.Client, trackerName, consumerARN string) error {
in := &location.ListTrackerConsumersInput{
TrackerName: aws.String(trackerName),
}

found := false

err := conn.ListTrackerConsumersPagesWithContext(ctx, in, func(page *locationservice.ListTrackerConsumersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := location.NewListTrackerConsumersPaginator(conn, in)

for pages.HasMorePages() && !found {
page, err := pages.NextPage(ctx)

if err != nil {
return err
}

for _, arn := range page.ConsumerArns {
if aws.StringValue(arn) == consumerARN {
if arn == consumerARN {
found = true
return false
break
}
}

return !lastPage
})

if err != nil {
return err
}

if !found {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
func dataSourceTrackerAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).LocationConn(ctx)
conn := meta.(*conns.AWSClient).LocationClient(ctx)

consumerArn := d.Get("consumer_arn").(string)
trackerName := d.Get("tracker_name").(string)
Expand Down
10 changes: 5 additions & 5 deletions internal/service/location/tracker_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/locationservice"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
awstypes "github.com/aws/aws-sdk-go-v2/service/location/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/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
tflocation "github.com/hashicorp/terraform-provider-aws/internal/service/location"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestAccLocationTrackerAssociation_disappears(t *testing.T) {

func testAccCheckTrackerAssociationDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).LocationConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).LocationClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_location_tracker_association" {
Expand All @@ -145,7 +145,7 @@ func testAccCheckTrackerAssociationDestroy(ctx context.Context) resource.TestChe
err = tflocation.FindTrackerAssociationByTrackerNameAndConsumerARN(ctx, conn, trackerAssociationId.TrackerName, trackerAssociationId.ConsumerARN)

if err != nil {
if tfresource.NotFound(err) || tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) {
if tfresource.NotFound(err) || errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil
}
return err
Expand Down Expand Up @@ -175,7 +175,7 @@ func testAccCheckTrackerAssociationExists(ctx context.Context, name string) reso
return create.Error(names.Location, create.ErrActionCheckingExistence, tflocation.ResNameTrackerAssociation, name, err)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).LocationConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).LocationClient(ctx)

err = tflocation.FindTrackerAssociationByTrackerNameAndConsumerARN(ctx, conn, trackerAssociationId.TrackerName, trackerAssociationId.ConsumerARN)

Expand Down
30 changes: 12 additions & 18 deletions internal/service/location/tracker_associations_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package location
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/locationservice"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/location"
"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"
Expand Down Expand Up @@ -43,34 +43,28 @@ const (
func dataSourceTrackerAssociationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).LocationConn(ctx)
conn := meta.(*conns.AWSClient).LocationClient(ctx)

name := d.Get("tracker_name").(string)

in := &locationservice.ListTrackerConsumersInput{
in := &location.ListTrackerConsumersInput{
TrackerName: aws.String(name),
}

var arns []string

err := conn.ListTrackerConsumersPagesWithContext(ctx, in, func(page *locationservice.ListTrackerConsumersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
pages := location.NewListTrackerConsumersPaginator(conn, in)

for _, arn := range page.ConsumerArns {
if arn == nil {
continue
}
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

arns = append(arns, aws.StringValue(arn))
if err != nil {
return create.AppendDiagError(diags, names.Location, create.ErrActionReading, DSNameTrackerAssociations, name, err)
}

return !lastPage
})

if err != nil {
return create.AppendDiagError(diags, names.Location, create.ErrActionReading, DSNameTrackerAssociations, name, err)
for _, arn := range page.ConsumerArns {
arns = append(arns, arn)
}
}

d.SetId(meta.(*conns.AWSClient).Region)
Expand Down

0 comments on commit 2c0f7e8

Please sign in to comment.