From 2295e6197b9c32bc68d722a7b9c82696bd8b527b Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:51:49 +0200 Subject: [PATCH 1/7] Use anonymous function for listing tracker associations --- internal/service/location/tracker_association.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/location/tracker_association.go b/internal/service/location/tracker_association.go index b63c053fd348..369da679e26e 100644 --- a/internal/service/location/tracker_association.go +++ b/internal/service/location/tracker_association.go @@ -142,7 +142,9 @@ func FindTrackerAssociationByTrackerNameAndConsumerARN(ctx context.Context, conn } found := false - fn := func(page *locationservice.ListTrackerConsumersOutput, lastPage bool) bool { + + err := conn.ListTrackerConsumersPagesWithContext(ctx, in, func(page *locationservice.ListTrackerConsumersOutput, lastPage bool) bool { + if page == nil { return !lastPage } @@ -155,9 +157,7 @@ func FindTrackerAssociationByTrackerNameAndConsumerARN(ctx context.Context, conn } return !lastPage - } - - err := conn.ListTrackerConsumersPagesWithContext(ctx, in, fn) + }) if err != nil { return err From 11f980f871a08c30542f9fcc26e219cfb7054e7a Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:52:14 +0200 Subject: [PATCH 2/7] Create new data source --- .../tracker_association_data_source.go | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 internal/service/location/tracker_association_data_source.go diff --git a/internal/service/location/tracker_association_data_source.go b/internal/service/location/tracker_association_data_source.go new file mode 100644 index 000000000000..d91ee176cdda --- /dev/null +++ b/internal/service/location/tracker_association_data_source.go @@ -0,0 +1,57 @@ +package location + +import ( + "context" + "fmt" + + "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/create" + "github.com/hashicorp/terraform-provider-aws/internal/verify" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func DataSourceTrackerAssociation() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceTrackerAssociationRead, + + Schema: map[string]*schema.Schema{ + "consumer_arn": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: verify.ValidARN, + }, + "tracker_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 100), + }, + }, + } +} + +const ( + DSNameTrackerAssociation = "Tracker Association Data Source" +) + +func dataSourceTrackerAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).LocationConn + + consumerArn := d.Get("consumer_arn").(string) + trackerName := d.Get("tracker_name").(string) + id := fmt.Sprintf("%s|%s", trackerName, consumerArn) + + err := FindTrackerAssociationByTrackerNameAndConsumerARN(ctx, conn, trackerName, consumerArn) + + if err != nil { + return create.DiagError(names.Location, create.ErrActionReading, DSNameTrackerAssociation, id, err) + } + + d.SetId(id) + + return nil +} From bba69a01f1410241822bb6c8023b543c5f1cb798 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:52:25 +0200 Subject: [PATCH 3/7] Register new data source in the provider --- internal/provider/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 579d8888de96..a6e8f5172b72 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -740,6 +740,7 @@ func New(_ context.Context) (*schema.Provider, error) { "aws_location_place_index": location.DataSourcePlaceIndex(), "aws_location_route_calculator": location.DataSourceRouteCalculator(), "aws_location_tracker": location.DataSourceTracker(), + "aws_location_tracker_association": location.DataSourceTrackerAssociation(), // "aws_arn": meta.DataSourceARN(), // Now implemented using Terraform Plugin Framework. "aws_billing_service_account": meta.DataSourceBillingServiceAccount(), From a96349cce872f786784f6a8083b18e056bd50f47 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:52:34 +0200 Subject: [PATCH 4/7] Create acceptance tests --- .../tracker_association_data_source_test.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 internal/service/location/tracker_association_data_source_test.go diff --git a/internal/service/location/tracker_association_data_source_test.go b/internal/service/location/tracker_association_data_source_test.go new file mode 100644 index 000000000000..af58e84b711b --- /dev/null +++ b/internal/service/location/tracker_association_data_source_test.go @@ -0,0 +1,56 @@ +package location_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/locationservice" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccLocationTrackerAssociationDataSource_basic(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceName := "data.aws_location_tracker_association.test" + resourceName := "aws_location_tracker_association.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, locationservice.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTrackerAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccTrackerAssociationDataSourceConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckTrackerAssociationExists(dataSourceName), + resource.TestCheckResourceAttrPair(resourceName, "consumer_arn", "aws_location_geofence_collection.test", "collection_arn"), + resource.TestCheckResourceAttrPair(resourceName, "tracker_name", "aws_location_tracker.test", "tracker_name"), + ), + }, + }, + }) +} + +func testAccTrackerAssociationDataSourceConfig_basic(rName string) string { + return fmt.Sprintf(` +resource "aws_location_geofence_collection" "test" { + collection_name = %[1]q +} + +resource "aws_location_tracker" "test" { + tracker_name = %[1]q +} + +resource "aws_location_tracker_association" "test" { + consumer_arn = aws_location_geofence_collection.test.collection_arn + tracker_name = aws_location_tracker.test.tracker_name +} + +data "aws_location_tracker_association" "test" { + consumer_arn = aws_location_tracker_association.test.consumer_arn + tracker_name = aws_location_tracker_association.test.tracker_name +} +`, rName) +} From 89b1449b026828f08aa544e9060e015100476732 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:52:40 +0200 Subject: [PATCH 5/7] Create a docs page --- ...location_tracker_association.html.markdown | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 website/docs/d/location_tracker_association.html.markdown diff --git a/website/docs/d/location_tracker_association.html.markdown b/website/docs/d/location_tracker_association.html.markdown new file mode 100644 index 000000000000..664ef17fce70 --- /dev/null +++ b/website/docs/d/location_tracker_association.html.markdown @@ -0,0 +1,33 @@ +--- +subcategory: "Location" +layout: "aws" +page_title: "AWS: aws_location_tracker_association" +description: |- + Retrieve information about a Location Service Tracker Association. +--- + +# Data Source: aws_location_tracker_association + +Retrieve information about a Location Service Tracker Association. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_location_tracker_association" "example" { + consumer_arn = "arn:aws:geo:region:account-id:geofence-collection/ExampleGeofenceCollectionConsumer" + tracker_name = "example" +} +``` + +## Argument Reference + +The following arguments are required: + +* `consumer_arn` - (Required) The Amazon Resource Name (ARN) of the geofence collection associated to tracker resource. +* `tracker_name` - (Required) The name of the tracker resource associated with a geofence collection. + +## Attributes Reference + +No additional attributes are exported. From 7703f33d61745d7bbbac358c6acf95c9985ab4ff Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:56:21 +0200 Subject: [PATCH 6/7] Add changelog --- .changelog/26404.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/26404.txt diff --git a/.changelog/26404.txt b/.changelog/26404.txt new file mode 100644 index 000000000000..1ed95d8da3fb --- /dev/null +++ b/.changelog/26404.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_location_tracker_association +``` From 148f41963a7ca64497d961fba5d3f7b7f5ff9cdb Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sat, 20 Aug 2022 11:57:00 +0200 Subject: [PATCH 7/7] Remove blank lines --- internal/service/location/tracker_association.go | 1 - internal/service/location/tracker_association_data_source.go | 1 - 2 files changed, 2 deletions(-) diff --git a/internal/service/location/tracker_association.go b/internal/service/location/tracker_association.go index 369da679e26e..330a3aa4ed63 100644 --- a/internal/service/location/tracker_association.go +++ b/internal/service/location/tracker_association.go @@ -144,7 +144,6 @@ func FindTrackerAssociationByTrackerNameAndConsumerARN(ctx context.Context, conn found := false err := conn.ListTrackerConsumersPagesWithContext(ctx, in, func(page *locationservice.ListTrackerConsumersOutput, lastPage bool) bool { - if page == nil { return !lastPage } diff --git a/internal/service/location/tracker_association_data_source.go b/internal/service/location/tracker_association_data_source.go index d91ee176cdda..7b1b03c480df 100644 --- a/internal/service/location/tracker_association_data_source.go +++ b/internal/service/location/tracker_association_data_source.go @@ -46,7 +46,6 @@ func dataSourceTrackerAssociationRead(ctx context.Context, d *schema.ResourceDat id := fmt.Sprintf("%s|%s", trackerName, consumerArn) err := FindTrackerAssociationByTrackerNameAndConsumerARN(ctx, conn, trackerName, consumerArn) - if err != nil { return create.DiagError(names.Location, create.ErrActionReading, DSNameTrackerAssociation, id, err) }