From 6019d429093c6d2e002f1ad0319b5ad247ff9552 Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 16:02:35 +0800 Subject: [PATCH 01/30] [AWS SDK for Go Migration] Meta Data Sources (Region) Service --- internal/service/meta/region_data_source.go | 95 ++++++++----------- .../service/meta/region_data_source_test.go | 11 ++- .../meta/service_principal_data_source.go | 15 +-- 3 files changed, 58 insertions(+), 63 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index c97f4005c800..8a72a430e9aa 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -6,9 +6,10 @@ package meta import ( "context" "fmt" - "strings" - "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -67,63 +68,42 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ return } - var region *endpoints.Region + var region *awstypes.Region - if !data.Endpoint.IsNull() { - matchingRegion, err := FindRegionByEndpoint(data.Endpoint.ValueString()) + conn := d.Meta().EC2Client(ctx) + if !data.Endpoint.IsNull() { + matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) if err != nil { - response.Diagnostics.AddError("finding Region by endpoint", err.Error()) - + response.Diagnostics.AddError("finding region by endpoint", err.Error()) return } - region = matchingRegion } if !data.Name.IsNull() { - matchingRegion, err := FindRegionByName(data.Name.ValueString()) - + matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) - + response.Diagnostics.AddError("finding region by name", err.Error()) return } - - if region != nil && region.ID() != matchingRegion.ID() { - response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") - - return - } - region = matchingRegion } // Default to provider current region if no other filters matched if region == nil { - matchingRegion, err := FindRegionByName(d.Meta().Region) - + matchingRegion, err := FindRegionByName(ctx, conn, d.Meta().Region) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) - + response.Diagnostics.AddError("finding region by name", err.Error()) return } - region = matchingRegion } - regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) - - if err != nil { - response.Diagnostics.AddError("resolving EC2 endpoint", err.Error()) - - return - } - - data.Description = types.StringValue(region.Description()) - data.Endpoint = types.StringValue(strings.TrimPrefix(regionEndpointEC2.URL, "https://")) - data.ID = types.StringValue(region.ID()) - data.Name = types.StringValue(region.ID()) + data.Description = types.StringValue(aws.ToString(region.RegionName)) + data.Endpoint = types.StringValue(aws.ToString(region.Endpoint)) + data.ID = types.StringValue(aws.ToString(region.RegionName)) + data.Name = types.StringValue(aws.ToString(region.RegionName)) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } @@ -135,32 +115,39 @@ type dataSourceRegionData struct { Name types.String `tfsdk:"name"` } -func FindRegionByEndpoint(endpoint string) (*endpoints.Region, error) { - for _, partition := range endpoints.DefaultPartitions() { - for _, region := range partition.Regions() { - regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) +func FindRegionByEndpoint(ctx context.Context, conn *ec2.Client, endpoint string) (*awstypes.Region, error) { + input := &ec2.DescribeRegionsInput{ + AllRegions: aws.Bool(true), + } - if err != nil { - return nil, err - } + output, err := conn.DescribeRegions(ctx, input) + if err != nil { + return nil, err + } - if strings.TrimPrefix(regionEndpointEC2.URL, "https://") == endpoint { - return ®ion, nil - } + for _, region := range output.Regions { + if aws.ToString(region.Endpoint) == endpoint { + return ®ion, nil } } return nil, fmt.Errorf("region not found for endpoint %q", endpoint) } -func FindRegionByName(name string) (*endpoints.Region, error) { - for _, partition := range endpoints.DefaultPartitions() { - for _, region := range partition.Regions() { - if region.ID() == name { - return ®ion, nil - } - } +func FindRegionByName(ctx context.Context, conn *ec2.Client, name string) (*awstypes.Region, error) { + input := &ec2.DescribeRegionsInput{ + RegionNames: []string{name}, + AllRegions: aws.Bool(true), + } + + output, err := conn.DescribeRegions(ctx, input) + if err != nil { + return nil, err + } + + if len(output.Regions) == 0 { + return nil, fmt.Errorf("region not found for name %q", name) } - return nil, fmt.Errorf("region not found for name %q", name) + return &output.Regions[0], nil } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 376ffc913299..3363d7c79e89 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -10,6 +10,7 @@ import ( "github.com/YakDriver/regexache" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" tfmeta "github.com/hashicorp/terraform-provider-aws/internal/service/meta" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -17,6 +18,9 @@ import ( func TestFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() + ctx := acctest.Context(t) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) + var testCases = []struct { Value string ErrCount int @@ -40,7 +44,7 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByEndpoint(tc.Value) + _, err := tfmeta.FindRegionByEndpoint(ctx, conn, tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } @@ -53,6 +57,9 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { func TestFindRegionByName(t *testing.T) { t.Parallel() + ctx := acctest.Context(t) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) + var testCases = []struct { Value string ErrCount int @@ -72,7 +79,7 @@ func TestFindRegionByName(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByName(tc.Value) + _, err := tfmeta.FindRegionByName(ctx, conn, tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index 69405791f8e4..94245a29c424 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -6,7 +6,8 @@ package meta import ( "context" - "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -59,11 +60,11 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc return } - var region *endpoints.Region + var region *awstypes.Region // find the region given by the user if !data.Region.IsNull() { - matchingRegion, err := FindRegionByName(data.Region.ValueString()) + matchingRegion, err := FindRegionByName(ctx, d.Meta().EC2Client(ctx), data.Region.ValueString()) if err != nil { response.Diagnostics.AddError("finding Region by name", err.Error()) @@ -76,7 +77,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // Default to provider current region if no other filters matched if region == nil { - matchingRegion, err := FindRegionByName(d.Meta().Region) + matchingRegion, err := FindRegionByName(ctx, d.Meta().EC2Client(ctx), d.Meta().Region) if err != nil { response.Diagnostics.AddError("finding Region using the provider", err.Error()) @@ -87,7 +88,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc region = matchingRegion } - partition := names.PartitionForRegion(region.ID()) + partition := names.PartitionForRegion(*region.RegionName) serviceName := "" @@ -97,10 +98,10 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc SourceServicePrincipal := names.ServicePrincipalNameForPartition(serviceName, partition) - data.ID = types.StringValue(serviceName + "." + region.ID() + "." + SourceServicePrincipal) + data.ID = types.StringValue(serviceName + "." + aws.ToString(region.RegionName) + "." + SourceServicePrincipal) data.Name = types.StringValue(serviceName + "." + SourceServicePrincipal) data.Suffix = types.StringValue(SourceServicePrincipal) - data.Region = types.StringValue(region.ID()) + data.Region = types.StringValue(*region.RegionName) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } From 97c652194e132b73ef826d93b04c219249b67152 Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 16:46:40 +0800 Subject: [PATCH 02/30] restore prior formatting, convert unit tests to acceptance tests --- internal/service/meta/region_data_source.go | 18 +++++++++++++++--- .../service/meta/region_data_source_test.go | 10 ++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index 8a72a430e9aa..2d880647b49e 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -75,7 +75,8 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Endpoint.IsNull() { matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) if err != nil { - response.Diagnostics.AddError("finding region by endpoint", err.Error()) + response.Diagnostics.AddError("finding Region by endpoint", err.Error()) + return } region = matchingRegion @@ -84,19 +85,30 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Name.IsNull() { matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) if err != nil { - response.Diagnostics.AddError("finding region by name", err.Error()) + response.Diagnostics.AddError("finding Region by name", err.Error()) + + return + } + + if region != nil && region.RegionName != matchingRegion.RegionName { + response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") + return } + region = matchingRegion } // Default to provider current region if no other filters matched if region == nil { matchingRegion, err := FindRegionByName(ctx, conn, d.Meta().Region) + if err != nil { - response.Diagnostics.AddError("finding region by name", err.Error()) + response.Diagnostics.AddError("finding Region by name", err.Error()) + return } + region = matchingRegion } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 3363d7c79e89..7aa1a27e3152 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,10 +15,11 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestFindRegionByEC2Endpoint(t *testing.T) { +func TestAccFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) + acctest.PreCheck(ctx, t) conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) var testCases = []struct { @@ -54,10 +55,11 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { } } -func TestFindRegionByName(t *testing.T) { +func TestAccFindRegionByName(t *testing.T) { t.Parallel() ctx := acctest.Context(t) + acctest.PreCheck(ctx, t) conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) var testCases = []struct { @@ -76,6 +78,10 @@ func TestFindRegionByName(t *testing.T) { Value: "us-east-1", // lintignore:AWSAT003 ErrCount: 0, }, + { + Value: "ap-southeast-5", // lintignore:AWSAT003 + ErrCount: 0, + }, } for _, tc := range testCases { From 7c08b2a200ef212af53836f4ca2612243aa6603a Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 16:48:46 +0800 Subject: [PATCH 03/30] more formatting --- internal/service/meta/region_data_source.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index 2d880647b49e..a275718290d4 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -74,16 +74,19 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Endpoint.IsNull() { matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) + if err != nil { response.Diagnostics.AddError("finding Region by endpoint", err.Error()) return } + region = matchingRegion } if !data.Name.IsNull() { matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) + if err != nil { response.Diagnostics.AddError("finding Region by name", err.Error()) From 933af5c9fe8d62db0af0e7dfbf8922aa81fe02c8 Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 17:03:37 +0800 Subject: [PATCH 04/30] fix check for multiple region matching --- internal/service/meta/region_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index a275718290d4..2314766894fe 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -93,7 +93,7 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ return } - if region != nil && region.RegionName != matchingRegion.RegionName { + if region != nil && aws.ToString(region.RegionName) != aws.ToString(matchingRegion.RegionName) { response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") return From 54a4a42ec1fe97798cc51c7466339280cdd00c1c Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 17:06:37 +0800 Subject: [PATCH 05/30] update test names --- internal/service/meta/region_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 7aa1a27e3152..a3a38d94e614 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccFindRegionByEC2Endpoint(t *testing.T) { +func TestAccMetaRegionDataSource_FindRegionByEndpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) @@ -55,7 +55,7 @@ func TestAccFindRegionByEC2Endpoint(t *testing.T) { } } -func TestAccFindRegionByName(t *testing.T) { +func TestAccMetaRegionDataSource_FindRegionByName(t *testing.T) { t.Parallel() ctx := acctest.Context(t) From ee34887f72b7603fddc95d47075c47525914da43 Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 18:07:52 +0800 Subject: [PATCH 06/30] add partition checks to service principal data source tests --- internal/service/meta/region_data_source_test.go | 2 +- .../service/meta/service_principal_data_source_test.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index a3a38d94e614..70527e1a15b7 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccMetaRegionDataSource_FindRegionByEndpoint(t *testing.T) { +func TestAccMetaRegionDataSource_FindRegionByEC2Endpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) diff --git a/internal/service/meta/service_principal_data_source_test.go b/internal/service/meta/service_principal_data_source_test.go index b8431f54845c..a245e9b15352 100644 --- a/internal/service/meta/service_principal_data_source_test.go +++ b/internal/service/meta/service_principal_data_source_test.go @@ -63,7 +63,7 @@ func TestAccMetaServicePrincipal_ByRegion(t *testing.T) { t.Run(region, func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(ctx, t) }, + PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartition(t, names.PartitionForRegion(region)) }, ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ @@ -135,7 +135,10 @@ func TestAccMetaServicePrincipal_UniqueForServiceInRegion(t *testing.T) { t.Run(fmt.Sprintf("%s/%s", testCase.Region, testCase.Service), func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(ctx, t) }, + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartition(t, names.PartitionForRegion(testCase.Region)) + }, ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ From 07827d30d529f34fd0e743da9610733ba19a739a Mon Sep 17 00:00:00 2001 From: Henry Skiba Date: Thu, 19 Sep 2024 18:20:23 +0800 Subject: [PATCH 07/30] add changelog entry --- .changelog/39389.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/39389.txt diff --git a/.changelog/39389.txt b/.changelog/39389.txt new file mode 100644 index 000000000000..af3bc2bb0b6d --- /dev/null +++ b/.changelog/39389.txt @@ -0,0 +1,3 @@ +```release-note:bug +data/aws_region: Fix lookups for the `ap-southeast-5` region +``` From 877bcc97fb43553e93291eef7312c58aaba6f951 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:42:39 -0400 Subject: [PATCH 08/30] Revert "add partition checks to service principal data source tests" This reverts commit ee34887f72b7603fddc95d47075c47525914da43. --- internal/service/meta/region_data_source_test.go | 2 +- .../service/meta/service_principal_data_source_test.go | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 70527e1a15b7..a3a38d94e614 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccMetaRegionDataSource_FindRegionByEC2Endpoint(t *testing.T) { +func TestAccMetaRegionDataSource_FindRegionByEndpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) diff --git a/internal/service/meta/service_principal_data_source_test.go b/internal/service/meta/service_principal_data_source_test.go index a245e9b15352..b8431f54845c 100644 --- a/internal/service/meta/service_principal_data_source_test.go +++ b/internal/service/meta/service_principal_data_source_test.go @@ -63,7 +63,7 @@ func TestAccMetaServicePrincipal_ByRegion(t *testing.T) { t.Run(region, func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartition(t, names.PartitionForRegion(region)) }, + PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ @@ -135,10 +135,7 @@ func TestAccMetaServicePrincipal_UniqueForServiceInRegion(t *testing.T) { t.Run(fmt.Sprintf("%s/%s", testCase.Region, testCase.Service), func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { - acctest.PreCheck(ctx, t) - acctest.PreCheckPartition(t, names.PartitionForRegion(testCase.Region)) - }, + PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ From aca0cd3835f3024ab168aa54af25fa042e954435 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:42:55 -0400 Subject: [PATCH 09/30] Revert "update test names" This reverts commit 54a4a42ec1fe97798cc51c7466339280cdd00c1c. --- internal/service/meta/region_data_source_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index a3a38d94e614..7aa1a27e3152 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccMetaRegionDataSource_FindRegionByEndpoint(t *testing.T) { +func TestAccFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) @@ -55,7 +55,7 @@ func TestAccMetaRegionDataSource_FindRegionByEndpoint(t *testing.T) { } } -func TestAccMetaRegionDataSource_FindRegionByName(t *testing.T) { +func TestAccFindRegionByName(t *testing.T) { t.Parallel() ctx := acctest.Context(t) From cd55c11133274f738103e4c3459eaa91823b43c9 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:43:02 -0400 Subject: [PATCH 10/30] Revert "fix check for multiple region matching" This reverts commit 933af5c9fe8d62db0af0e7dfbf8922aa81fe02c8. --- internal/service/meta/region_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index 2314766894fe..a275718290d4 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -93,7 +93,7 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ return } - if region != nil && aws.ToString(region.RegionName) != aws.ToString(matchingRegion.RegionName) { + if region != nil && region.RegionName != matchingRegion.RegionName { response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") return From cfc57f9ba5ed90e812281690ed0464dbfc8d0cfe Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:43:10 -0400 Subject: [PATCH 11/30] Revert "more formatting" This reverts commit 7c08b2a200ef212af53836f4ca2612243aa6603a. --- internal/service/meta/region_data_source.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index a275718290d4..2d880647b49e 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -74,19 +74,16 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Endpoint.IsNull() { matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) - if err != nil { response.Diagnostics.AddError("finding Region by endpoint", err.Error()) return } - region = matchingRegion } if !data.Name.IsNull() { matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) - if err != nil { response.Diagnostics.AddError("finding Region by name", err.Error()) From 90cc1faaab6938e4c676b09aa16247b0ed292e80 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:43:17 -0400 Subject: [PATCH 12/30] Revert "restore prior formatting, convert unit tests to acceptance tests" This reverts commit 97c652194e132b73ef826d93b04c219249b67152. --- internal/service/meta/region_data_source.go | 18 +++--------------- .../service/meta/region_data_source_test.go | 10 ++-------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index 2d880647b49e..8a72a430e9aa 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -75,8 +75,7 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Endpoint.IsNull() { matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) if err != nil { - response.Diagnostics.AddError("finding Region by endpoint", err.Error()) - + response.Diagnostics.AddError("finding region by endpoint", err.Error()) return } region = matchingRegion @@ -85,30 +84,19 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ if !data.Name.IsNull() { matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) - - return - } - - if region != nil && region.RegionName != matchingRegion.RegionName { - response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") - + response.Diagnostics.AddError("finding region by name", err.Error()) return } - region = matchingRegion } // Default to provider current region if no other filters matched if region == nil { matchingRegion, err := FindRegionByName(ctx, conn, d.Meta().Region) - if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) - + response.Diagnostics.AddError("finding region by name", err.Error()) return } - region = matchingRegion } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 7aa1a27e3152..3363d7c79e89 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -15,11 +15,10 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccFindRegionByEC2Endpoint(t *testing.T) { +func TestFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() ctx := acctest.Context(t) - acctest.PreCheck(ctx, t) conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) var testCases = []struct { @@ -55,11 +54,10 @@ func TestAccFindRegionByEC2Endpoint(t *testing.T) { } } -func TestAccFindRegionByName(t *testing.T) { +func TestFindRegionByName(t *testing.T) { t.Parallel() ctx := acctest.Context(t) - acctest.PreCheck(ctx, t) conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) var testCases = []struct { @@ -78,10 +76,6 @@ func TestAccFindRegionByName(t *testing.T) { Value: "us-east-1", // lintignore:AWSAT003 ErrCount: 0, }, - { - Value: "ap-southeast-5", // lintignore:AWSAT003 - ErrCount: 0, - }, } for _, tc := range testCases { From b6e17685ff02c27f47e6485119a40dccfeaf92ac Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:43:23 -0400 Subject: [PATCH 13/30] Revert "[AWS SDK for Go Migration] Meta Data Sources (Region) Service" This reverts commit 6019d429093c6d2e002f1ad0319b5ad247ff9552. --- internal/service/meta/region_data_source.go | 95 +++++++++++-------- .../service/meta/region_data_source_test.go | 11 +-- .../meta/service_principal_data_source.go | 15 ++- 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index 8a72a430e9aa..c97f4005c800 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -6,10 +6,9 @@ package meta import ( "context" "fmt" + "strings" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/ec2" - awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -68,42 +67,63 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ return } - var region *awstypes.Region - - conn := d.Meta().EC2Client(ctx) + var region *endpoints.Region if !data.Endpoint.IsNull() { - matchingRegion, err := FindRegionByEndpoint(ctx, conn, data.Endpoint.ValueString()) + matchingRegion, err := FindRegionByEndpoint(data.Endpoint.ValueString()) + if err != nil { - response.Diagnostics.AddError("finding region by endpoint", err.Error()) + response.Diagnostics.AddError("finding Region by endpoint", err.Error()) + return } + region = matchingRegion } if !data.Name.IsNull() { - matchingRegion, err := FindRegionByName(ctx, conn, data.Name.ValueString()) + matchingRegion, err := FindRegionByName(data.Name.ValueString()) + if err != nil { - response.Diagnostics.AddError("finding region by name", err.Error()) + response.Diagnostics.AddError("finding Region by name", err.Error()) + return } + + if region != nil && region.ID() != matchingRegion.ID() { + response.Diagnostics.AddError("multiple Regions matched", "use additional constraints to reduce matches to a single Region") + + return + } + region = matchingRegion } // Default to provider current region if no other filters matched if region == nil { - matchingRegion, err := FindRegionByName(ctx, conn, d.Meta().Region) + matchingRegion, err := FindRegionByName(d.Meta().Region) + if err != nil { - response.Diagnostics.AddError("finding region by name", err.Error()) + response.Diagnostics.AddError("finding Region by name", err.Error()) + return } + region = matchingRegion } - data.Description = types.StringValue(aws.ToString(region.RegionName)) - data.Endpoint = types.StringValue(aws.ToString(region.Endpoint)) - data.ID = types.StringValue(aws.ToString(region.RegionName)) - data.Name = types.StringValue(aws.ToString(region.RegionName)) + regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) + + if err != nil { + response.Diagnostics.AddError("resolving EC2 endpoint", err.Error()) + + return + } + + data.Description = types.StringValue(region.Description()) + data.Endpoint = types.StringValue(strings.TrimPrefix(regionEndpointEC2.URL, "https://")) + data.ID = types.StringValue(region.ID()) + data.Name = types.StringValue(region.ID()) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } @@ -115,39 +135,32 @@ type dataSourceRegionData struct { Name types.String `tfsdk:"name"` } -func FindRegionByEndpoint(ctx context.Context, conn *ec2.Client, endpoint string) (*awstypes.Region, error) { - input := &ec2.DescribeRegionsInput{ - AllRegions: aws.Bool(true), - } +func FindRegionByEndpoint(endpoint string) (*endpoints.Region, error) { + for _, partition := range endpoints.DefaultPartitions() { + for _, region := range partition.Regions() { + regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) - output, err := conn.DescribeRegions(ctx, input) - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } - for _, region := range output.Regions { - if aws.ToString(region.Endpoint) == endpoint { - return ®ion, nil + if strings.TrimPrefix(regionEndpointEC2.URL, "https://") == endpoint { + return ®ion, nil + } } } return nil, fmt.Errorf("region not found for endpoint %q", endpoint) } -func FindRegionByName(ctx context.Context, conn *ec2.Client, name string) (*awstypes.Region, error) { - input := &ec2.DescribeRegionsInput{ - RegionNames: []string{name}, - AllRegions: aws.Bool(true), - } - - output, err := conn.DescribeRegions(ctx, input) - if err != nil { - return nil, err - } - - if len(output.Regions) == 0 { - return nil, fmt.Errorf("region not found for name %q", name) +func FindRegionByName(name string) (*endpoints.Region, error) { + for _, partition := range endpoints.DefaultPartitions() { + for _, region := range partition.Regions() { + if region.ID() == name { + return ®ion, nil + } + } } - return &output.Regions[0], nil + return nil, fmt.Errorf("region not found for name %q", name) } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 3363d7c79e89..376ffc913299 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -10,7 +10,6 @@ import ( "github.com/YakDriver/regexache" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" - "github.com/hashicorp/terraform-provider-aws/internal/conns" tfmeta "github.com/hashicorp/terraform-provider-aws/internal/service/meta" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -18,9 +17,6 @@ import ( func TestFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() - ctx := acctest.Context(t) - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - var testCases = []struct { Value string ErrCount int @@ -44,7 +40,7 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByEndpoint(ctx, conn, tc.Value) + _, err := tfmeta.FindRegionByEndpoint(tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } @@ -57,9 +53,6 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { func TestFindRegionByName(t *testing.T) { t.Parallel() - ctx := acctest.Context(t) - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - var testCases = []struct { Value string ErrCount int @@ -79,7 +72,7 @@ func TestFindRegionByName(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByName(ctx, conn, tc.Value) + _, err := tfmeta.FindRegionByName(tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index 94245a29c424..69405791f8e4 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -6,8 +6,7 @@ package meta import ( "context" - "github.com/aws/aws-sdk-go-v2/aws" - awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -60,11 +59,11 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc return } - var region *awstypes.Region + var region *endpoints.Region // find the region given by the user if !data.Region.IsNull() { - matchingRegion, err := FindRegionByName(ctx, d.Meta().EC2Client(ctx), data.Region.ValueString()) + matchingRegion, err := FindRegionByName(data.Region.ValueString()) if err != nil { response.Diagnostics.AddError("finding Region by name", err.Error()) @@ -77,7 +76,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // Default to provider current region if no other filters matched if region == nil { - matchingRegion, err := FindRegionByName(ctx, d.Meta().EC2Client(ctx), d.Meta().Region) + matchingRegion, err := FindRegionByName(d.Meta().Region) if err != nil { response.Diagnostics.AddError("finding Region using the provider", err.Error()) @@ -88,7 +87,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc region = matchingRegion } - partition := names.PartitionForRegion(*region.RegionName) + partition := names.PartitionForRegion(region.ID()) serviceName := "" @@ -98,10 +97,10 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc SourceServicePrincipal := names.ServicePrincipalNameForPartition(serviceName, partition) - data.ID = types.StringValue(serviceName + "." + aws.ToString(region.RegionName) + "." + SourceServicePrincipal) + data.ID = types.StringValue(serviceName + "." + region.ID() + "." + SourceServicePrincipal) data.Name = types.StringValue(serviceName + "." + SourceServicePrincipal) data.Suffix = types.StringValue(SourceServicePrincipal) - data.Region = types.StringValue(*region.RegionName) + data.Region = types.StringValue(region.ID()) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } From 226da0c21c1b0f4b1d99dc9125ef4ccd807ca1e7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 09:44:10 -0400 Subject: [PATCH 14/30] Tweak CHANGELOG entry. --- .changelog/39389.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/39389.txt b/.changelog/39389.txt index af3bc2bb0b6d..146e9896869d 100644 --- a/.changelog/39389.txt +++ b/.changelog/39389.txt @@ -1,3 +1,3 @@ ```release-note:bug -data/aws_region: Fix lookups for the `ap-southeast-5` region +data-source/aws_region: Fix lookups for the `ap-southeast-5` Region ``` From d3e8e4bf7f97c007e48beb8d6800d435648d1c9d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 10:48:47 -0400 Subject: [PATCH 15/30] d/aws_arn: Tidy up. --- internal/service/meta/arn_data_source.go | 42 ++++++++------------ internal/service/meta/service_package_gen.go | 3 +- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/internal/service/meta/arn_data_source.go b/internal/service/meta/arn_data_source.go index c3a320f69b1b..0851905c6c19 100644 --- a/internal/service/meta/arn_data_source.go +++ b/internal/service/meta/arn_data_source.go @@ -1,8 +1,6 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -// Code generated by tools/tfsdk2fw/main.go. Manual editing is required. - package meta import ( @@ -12,30 +10,28 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceARN(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceARN{} +// @FrameworkDataSource(name="ARN") +func newARNDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &arnDataSource{} return d, nil } -type dataSourceARN struct { +type arnDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceARN) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*arnDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_arn" } -// Schema returns the schema for this data source. -func (d *dataSourceARN) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *arnDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "account": schema.StringAttribute{ Computed: true, @@ -64,30 +60,26 @@ func (d *dataSourceARN) Schema(ctx context.Context, req datasource.SchemaRequest } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceARN) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceARNData - +func (d *arnDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data arnDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } arn := data.ARN.ValueARN() - data.Account = types.StringValue(arn.AccountID) - data.ID = types.StringValue(arn.String()) - data.Partition = types.StringValue(arn.Partition) - data.Region = types.StringValue(arn.Region) - data.Resource = types.StringValue(arn.Resource) - data.Service = types.StringValue(arn.Service) + data.Account = fwflex.StringValueToFrameworkLegacy(ctx, arn.AccountID) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, arn.String()) + data.Partition = fwflex.StringValueToFrameworkLegacy(ctx, arn.Partition) + data.Region = fwflex.StringValueToFrameworkLegacy(ctx, arn.Region) + data.Resource = fwflex.StringValueToFrameworkLegacy(ctx, arn.Resource) + data.Service = fwflex.StringValueToFrameworkLegacy(ctx, arn.Service) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceARNData struct { +type arnDataSourceModel struct { Account types.String `tfsdk:"account"` ARN fwtypes.ARN `tfsdk:"arn"` ID types.String `tfsdk:"id"` diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index 60399ce195f3..a1b4cc290078 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -14,7 +14,8 @@ type servicePackage struct{} func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.ServicePackageFrameworkDataSource { return []*types.ServicePackageFrameworkDataSource{ { - Factory: newDataSourceARN, + Factory: newARNDataSource, + Name: "ARN", }, { Factory: newDataSourceBillingServiceAccount, From c6153726e77d93a66a32f7597a821798cd13d42f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 10:57:48 -0400 Subject: [PATCH 16/30] d/aws_billing_service_account: Tidy up. --- .../billing_service_account_data_source.go | 33 ++++++++----------- internal/service/meta/service_package_gen.go | 3 +- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/internal/service/meta/billing_service_account_data_source.go b/internal/service/meta/billing_service_account_data_source.go index f3ebd8f8acca..bd98e3c37d7f 100644 --- a/internal/service/meta/billing_service_account_data_source.go +++ b/internal/service/meta/billing_service_account_data_source.go @@ -11,29 +11,27 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceBillingServiceAccount(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceBillingServiceAccount{} +// @FrameworkDataSource(name="Billing Service Account") +func newBillingServiceAccountDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &billingServiceAccountDataSource{} return d, nil } -type dataSourceBillingServiceAccount struct { +type billingServiceAccountDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceBillingServiceAccount) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*billingServiceAccountDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_billing_service_account" } -// Schema returns the schema for this data source. -func (d *dataSourceBillingServiceAccount) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *billingServiceAccountDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ names.AttrARN: schema.StringAttribute{ Computed: true, @@ -46,13 +44,9 @@ func (d *dataSourceBillingServiceAccount) Schema(ctx context.Context, req dataso } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceBillingServiceAccount) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceBillingServiceAccountData - +func (d *billingServiceAccountDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data billingServiceAccountDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } @@ -66,14 +60,13 @@ func (d *dataSourceBillingServiceAccount) Read(ctx context.Context, request data AccountID: billingAccountID, Resource: "root", } - - data.ARN = types.StringValue(arn.String()) - data.ID = types.StringValue(billingAccountID) + data.ARN = fwflex.StringValueToFrameworkLegacy(ctx, arn.String()) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, billingAccountID) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceBillingServiceAccountData struct { +type billingServiceAccountDataSourceModel struct { ARN types.String `tfsdk:"arn"` ID types.String `tfsdk:"id"` } diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index a1b4cc290078..d7ebbb9e9130 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -18,7 +18,8 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Name: "ARN", }, { - Factory: newDataSourceBillingServiceAccount, + Factory: newBillingServiceAccountDataSource, + Name: "Billing Service Account", }, { Factory: newDataSourceDefaultTags, From 0f5222a5984010fbfb095b3ec810ea52dc434f6d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 11:03:10 -0400 Subject: [PATCH 17/30] d/aws_default_tags: Tidy up. --- .../service/meta/default_tags_data_source.go | 32 +++++++------------ internal/service/meta/service_package_gen.go | 7 ++-- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/internal/service/meta/default_tags_data_source.go b/internal/service/meta/default_tags_data_source.go index 84d91b5a37c5..a4742e26a56d 100644 --- a/internal/service/meta/default_tags_data_source.go +++ b/internal/service/meta/default_tags_data_source.go @@ -1,8 +1,6 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -// Code generated by tools/tfsdk2fw/main.go. Manual editing is required. - package meta import ( @@ -12,30 +10,28 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceDefaultTags(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceDefaultTags{} +// @FrameworkDataSource(name="Default Tags") +func newDefaultTagsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &defaultTagsDataSource{} return d, nil } -type dataSourceDefaultTags struct { +type defaultTagsDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceDefaultTags) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*defaultTagsDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_default_tags" } -// Schema returns the schema for this data source. -func (d *dataSourceDefaultTags) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *defaultTagsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ names.AttrID: schema.StringAttribute{ Optional: true, @@ -46,13 +42,9 @@ func (d *dataSourceDefaultTags) Schema(ctx context.Context, req datasource.Schem } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceDefaultTags) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceDefaultTagsData - +func (d *defaultTagsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data defaultTagsDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } @@ -61,13 +53,13 @@ func (d *dataSourceDefaultTags) Read(ctx context.Context, request datasource.Rea ignoreTagsConfig := d.Meta().IgnoreTagsConfig tags := defaultTagsConfig.GetTags() - data.ID = types.StringValue(d.Meta().Partition) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().Partition) data.Tags = tftags.FlattenStringValueMap(ctx, tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceDefaultTagsData struct { +type defaultTagsDataSourceModel struct { ID types.String `tfsdk:"id"` Tags tftags.Map `tfsdk:"tags"` } diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index d7ebbb9e9130..c261fe0e34d0 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -21,9 +21,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newBillingServiceAccountDataSource, Name: "Billing Service Account", }, - { - Factory: newDataSourceDefaultTags, - }, { Factory: newDataSourceIPRanges, }, @@ -40,6 +37,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv { Factory: newDataSourceService, }, + { + Factory: newDefaultTagsDataSource, + Name: "Default Tags", + }, { Factory: newServicePrincipalDataSource, }, From 08e29a6cc887f6a8e99a02e57767604cd8fd99fc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 11:16:04 -0400 Subject: [PATCH 18/30] d/aws_ip_ranges: Tidy up. --- .../service/meta/ip_ranges_data_source.go | 55 ++++++------------- internal/service/meta/service_package_gen.go | 7 ++- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/internal/service/meta/ip_ranges_data_source.go b/internal/service/meta/ip_ranges_data_source.go index 9213f0cc0cb9..94d9456a775b 100644 --- a/internal/service/meta/ip_ranges_data_source.go +++ b/internal/service/meta/ip_ranges_data_source.go @@ -19,31 +19,28 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" - "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceIPRanges(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceIPRanges{} +// @FrameworkDataSource(name="IP Ranges") +func newIPRangesDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &ipRangesDataSource{} return d, nil } -type dataSourceIPRanges struct { +type ipRangesDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceIPRanges) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*ipRangesDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_ip_ranges" } -// Schema returns the schema for this data source. -func (d *dataSourceIPRanges) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *ipRangesDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "cidr_blocks": schema.ListAttribute{ ElementType: types.StringType, @@ -78,19 +75,14 @@ func (d *dataSourceIPRanges) Schema(ctx context.Context, req datasource.SchemaRe } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceIPRanges) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceIPRangesData - +func (d *ipRangesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data ipRangesDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } var url string - if data.URL.IsNull() { // Data sources make no use of AttributePlanModifiers to set default values. url = "https://ip-ranges.amazonaws.com/ip-ranges.json" @@ -99,7 +91,6 @@ func (d *dataSourceIPRanges) Read(ctx context.Context, request datasource.ReadRe } bytes, err := readAll(ctx, url) - if err != nil { response.Diagnostics.AddError("downloading IP ranges", err.Error()) @@ -107,7 +98,6 @@ func (d *dataSourceIPRanges) Read(ctx context.Context, request datasource.ReadRe } ipRanges := new(ipRanges) - if err := json.Unmarshal(bytes, ipRanges); err != nil { response.Diagnostics.AddError("parsing JSON", err.Error()) @@ -115,15 +105,14 @@ func (d *dataSourceIPRanges) Read(ctx context.Context, request datasource.ReadRe } syncToken, err := strconv.Atoi(ipRanges.SyncToken) - if err != nil { response.Diagnostics.AddError("parsing SyncToken", err.Error()) return } - regions := tfslices.ApplyToAll(flex.ExpandFrameworkStringValueSet(ctx, data.Regions), strings.ToLower) - services := tfslices.ApplyToAll(flex.ExpandFrameworkStringValueSet(ctx, data.Services), strings.ToLower) + regions := tfslices.ApplyToAll(fwflex.ExpandFrameworkStringValueSet(ctx, data.Regions), strings.ToLower) + services := tfslices.ApplyToAll(fwflex.ExpandFrameworkStringValueSet(ctx, data.Services), strings.ToLower) matchFilter := func(region, service string) bool { matchRegion := len(regions) == 0 || slices.Contains(regions, strings.ToLower(region)) matchService := slices.Contains(services, strings.ToLower(service)) @@ -132,36 +121,32 @@ func (d *dataSourceIPRanges) Read(ctx context.Context, request datasource.ReadRe } var ipv4Prefixes []string - for _, v := range ipRanges.IPv4Prefixes { if matchFilter(v.Region, v.Service) { ipv4Prefixes = append(ipv4Prefixes, v.Prefix) } } - sort.Strings(ipv4Prefixes) var ipv6Prefixes []string - for _, v := range ipRanges.IPv6Prefixes { if matchFilter(v.Region, v.Service) { ipv6Prefixes = append(ipv6Prefixes, v.Prefix) } } - sort.Strings(ipv6Prefixes) - data.CreateDate = types.StringValue(ipRanges.CreateDate) - data.ID = types.StringValue(ipRanges.SyncToken) - data.IPv4CIDRBlocks = flex.FlattenFrameworkStringValueListLegacy(ctx, ipv4Prefixes) - data.IPv6CIDRBlocks = flex.FlattenFrameworkStringValueListLegacy(ctx, ipv6Prefixes) + data.CreateDate = fwflex.StringValueToFrameworkLegacy(ctx, ipRanges.CreateDate) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, ipRanges.SyncToken) + data.IPv4CIDRBlocks = fwflex.FlattenFrameworkStringValueListLegacy(ctx, ipv4Prefixes) + data.IPv6CIDRBlocks = fwflex.FlattenFrameworkStringValueListLegacy(ctx, ipv6Prefixes) data.SyncToken = types.Int64Value(int64(syncToken)) - data.URL = types.StringValue(url) + data.URL = fwflex.StringValueToFrameworkLegacy(ctx, url) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceIPRangesData struct { +type ipRangesDataSourceModel struct { CreateDate types.String `tfsdk:"create_date"` ID types.String `tfsdk:"id"` IPv4CIDRBlocks types.List `tfsdk:"cidr_blocks"` @@ -174,21 +159,17 @@ type dataSourceIPRangesData struct { func readAll(ctx context.Context, url string) ([]byte, error) { request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { return nil, err } response, err := cleanhttp.DefaultClient().Do(request) - if err != nil { return nil, fmt.Errorf("HTTP GET (%s): %w", url, err) } - defer response.Body.Close() bytes, err := io.ReadAll(response.Body) - if err != nil { return nil, fmt.Errorf("reading response body (%s): %w", url, err) } diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index c261fe0e34d0..ba8a67d81f06 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -21,9 +21,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newBillingServiceAccountDataSource, Name: "Billing Service Account", }, - { - Factory: newDataSourceIPRanges, - }, { Factory: newDataSourcePartition, }, @@ -41,6 +38,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newDefaultTagsDataSource, Name: "Default Tags", }, + { + Factory: newIPRangesDataSource, + Name: "IP Ranges", + }, { Factory: newServicePrincipalDataSource, }, From a04ae1ac6b51dc410eab99e549b1fbecf43a041e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 11:21:07 -0400 Subject: [PATCH 19/30] d/aws_partition: Tidy up. --- .../service/meta/partition_data_source.go | 36 ++++++++----------- internal/service/meta/service_package_gen.go | 7 ++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/internal/service/meta/partition_data_source.go b/internal/service/meta/partition_data_source.go index 1efb18a2481f..26684a300a29 100644 --- a/internal/service/meta/partition_data_source.go +++ b/internal/service/meta/partition_data_source.go @@ -10,29 +10,27 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourcePartition(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourcePartition{} +// @FrameworkDataSource(name="Partition") +func newPartitionDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &partitionDataSource{} return d, nil } -type dataSourcePartition struct { +type partitionDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourcePartition) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*partitionDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_partition" } -// Schema returns the schema for this data source. -func (d *dataSourcePartition) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *partitionDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "dns_suffix": schema.StringAttribute{ Computed: true, @@ -51,26 +49,22 @@ func (d *dataSourcePartition) Schema(ctx context.Context, req datasource.SchemaR } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourcePartition) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourcePartitionData - +func (d *partitionDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data partitionDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } - data.DNSSuffix = types.StringValue(d.Meta().DNSSuffix(ctx)) - data.ID = types.StringValue(d.Meta().Partition) - data.Partition = types.StringValue(d.Meta().Partition) - data.ReverseDNSPrefix = types.StringValue(d.Meta().ReverseDNSPrefix(ctx)) + data.DNSSuffix = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().DNSSuffix(ctx)) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().Partition) + data.Partition = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().Partition) + data.ReverseDNSPrefix = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().ReverseDNSPrefix(ctx)) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourcePartitionData struct { +type partitionDataSourceModel struct { DNSSuffix types.String `tfsdk:"dns_suffix"` ID types.String `tfsdk:"id"` Partition types.String `tfsdk:"partition"` diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index ba8a67d81f06..5af5ce1b0e8a 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -21,9 +21,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newBillingServiceAccountDataSource, Name: "Billing Service Account", }, - { - Factory: newDataSourcePartition, - }, { Factory: newDataSourceRegion, }, @@ -42,6 +39,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newIPRangesDataSource, Name: "IP Ranges", }, + { + Factory: newPartitionDataSource, + Name: "Partition", + }, { Factory: newServicePrincipalDataSource, }, From 2a60e943ec2b9d182566d0581388ce5a0ff55ace Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 11:28:48 -0400 Subject: [PATCH 20/30] d/aws_regions: Tidy up. --- internal/service/meta/regions_data_source.go | 45 +++++++++----------- internal/service/meta/service_package_gen.go | 8 ++-- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/internal/service/meta/regions_data_source.go b/internal/service/meta/regions_data_source.go index b415b1c8c558..ed167c0efca7 100644 --- a/internal/service/meta/regions_data_source.go +++ b/internal/service/meta/regions_data_source.go @@ -8,35 +8,34 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" - "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource(name=Regions) -func newDataSourceRegions(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceRegions{} +// @FrameworkDataSource(name="Regions") +func newRegionsDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := ®ionsDataSource{} return d, nil } -type dataSourceRegions struct { +type regionsDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceRegions) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*regionsDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_regions" } -// Schema returns the schema for this data source. -func (d *dataSourceRegions) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *regionsDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "all_regions": schema.BoolAttribute{ Optional: true, @@ -56,13 +55,9 @@ func (d *dataSourceRegions) Schema(ctx context.Context, req datasource.SchemaReq } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceRegions) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceRegionsData - +func (d *regionsDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data regionsDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } @@ -70,7 +65,7 @@ func (d *dataSourceRegions) Read(ctx context.Context, request datasource.ReadReq conn := d.Meta().EC2Client(ctx) input := &ec2.DescribeRegionsInput{ - AllRegions: flex.BoolFromFramework(ctx, data.AllRegions), + AllRegions: fwflex.BoolFromFramework(ctx, data.AllRegions), Filters: tfec2.NewCustomFilterListFramework(ctx, data.Filters), } @@ -82,18 +77,16 @@ func (d *dataSourceRegions) Read(ctx context.Context, request datasource.ReadReq return } - var names []string - for _, v := range output.Regions { - names = append(names, aws.ToString(v.RegionName)) - } - - data.ID = types.StringValue(d.Meta().Partition) - data.Names = flex.FlattenFrameworkStringValueSetLegacy(ctx, names) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().Partition) + names := tfslices.ApplyToAll(output.Regions, func(v awstypes.Region) string { + return aws.ToString(v.RegionName) + }) + data.Names = fwflex.FlattenFrameworkStringValueSetLegacy(ctx, names) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceRegionsData struct { +type regionsDataSourceModel struct { AllRegions types.Bool `tfsdk:"all_regions"` Filters types.Set `tfsdk:"filter"` ID types.String `tfsdk:"id"` diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index 5af5ce1b0e8a..130a8bb2140f 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -24,10 +24,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv { Factory: newDataSourceRegion, }, - { - Factory: newDataSourceRegions, - Name: "Regions", - }, { Factory: newDataSourceService, }, @@ -43,6 +39,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newPartitionDataSource, Name: "Partition", }, + { + Factory: newRegionsDataSource, + Name: "Regions", + }, { Factory: newServicePrincipalDataSource, }, From 6ba5d5b443c8678b9ff578cf49c94eb26950fff9 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 12:09:19 -0400 Subject: [PATCH 21/30] d/aws_region: Tidy up and use 'endpoints' from 'aws-sdk-go-base'. --- internal/service/meta/exports_test.go | 10 +++ internal/service/meta/region_data_source.go | 80 ++++++++++--------- .../service/meta/region_data_source_test.go | 6 +- internal/service/meta/service_package_gen.go | 7 +- .../meta/service_principal_data_source.go | 15 ++-- 5 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 internal/service/meta/exports_test.go diff --git a/internal/service/meta/exports_test.go b/internal/service/meta/exports_test.go new file mode 100644 index 000000000000..f5b16159cfb6 --- /dev/null +++ b/internal/service/meta/exports_test.go @@ -0,0 +1,10 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package meta + +// Exports for use in tests only. +var ( + FindRegionByEC2Endpoint = findRegionByEC2Endpoint + FindRegionByName = findRegionByName +) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index c97f4005c800..af27a5beb857 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -8,34 +8,34 @@ import ( "fmt" "strings" - "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceRegion(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceRegion{} +// @FrameworkDataSource(name="Region") +func newRegionDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := ®ionDataSource{} return d, nil } -type dataSourceRegion struct { +type regionDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceRegion) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*regionDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_region" } -// Schema returns the schema for this data source. -func (d *dataSourceRegion) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *regionDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ names.AttrDescription: schema.StringAttribute{ Computed: true, @@ -56,13 +56,9 @@ func (d *dataSourceRegion) Schema(ctx context.Context, req datasource.SchemaRequ } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceRegionData - +func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data regionDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } @@ -70,10 +66,11 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ var region *endpoints.Region if !data.Endpoint.IsNull() { - matchingRegion, err := FindRegionByEndpoint(data.Endpoint.ValueString()) + endpoint := data.Endpoint.ValueString() + matchingRegion, err := findRegionByEC2Endpoint(endpoint) if err != nil { - response.Diagnostics.AddError("finding Region by endpoint", err.Error()) + response.Diagnostics.AddError(fmt.Sprintf("finding Region by endpoint (%s)", endpoint), err.Error()) return } @@ -82,10 +79,11 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ } if !data.Name.IsNull() { - matchingRegion, err := FindRegionByName(data.Name.ValueString()) + name := data.Name.ValueString() + matchingRegion, err := findRegionByName(name) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) + response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) return } @@ -99,12 +97,13 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ region = matchingRegion } - // Default to provider current region if no other filters matched + // Default to provider current Region if no other filters matched. if region == nil { - matchingRegion, err := FindRegionByName(d.Meta().Region) + name := d.Meta().Region + matchingRegion, err := findRegionByName(name) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) + response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) return } @@ -112,7 +111,7 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ region = matchingRegion } - regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) + regionEndpointEC2, err := ec2Endpoint(region) if err != nil { response.Diagnostics.AddError("resolving EC2 endpoint", err.Error()) @@ -120,40 +119,40 @@ func (d *dataSourceRegion) Read(ctx context.Context, request datasource.ReadRequ return } - data.Description = types.StringValue(region.Description()) - data.Endpoint = types.StringValue(strings.TrimPrefix(regionEndpointEC2.URL, "https://")) - data.ID = types.StringValue(region.ID()) - data.Name = types.StringValue(region.ID()) + data.Description = fwflex.StringValueToFrameworkLegacy(ctx, region.Description()) + data.Endpoint = fwflex.StringValueToFrameworkLegacy(ctx, strings.TrimPrefix(regionEndpointEC2, "https://")) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, region.ID()) + data.Name = fwflex.StringValueToFrameworkLegacy(ctx, region.ID()) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceRegionData struct { +type regionDataSourceModel struct { Description types.String `tfsdk:"description"` Endpoint types.String `tfsdk:"endpoint"` ID types.String `tfsdk:"id"` Name types.String `tfsdk:"name"` } -func FindRegionByEndpoint(endpoint string) (*endpoints.Region, error) { +func findRegionByEC2Endpoint(endpoint string) (*endpoints.Region, error) { for _, partition := range endpoints.DefaultPartitions() { for _, region := range partition.Regions() { - regionEndpointEC2, err := region.ResolveEndpoint(names.EC2) + regionEndpointEC2, err := ec2Endpoint(®ion) if err != nil { return nil, err } - if strings.TrimPrefix(regionEndpointEC2.URL, "https://") == endpoint { + if strings.TrimPrefix(regionEndpointEC2, "https://") == endpoint { return ®ion, nil } } } - return nil, fmt.Errorf("region not found for endpoint %q", endpoint) + return nil, &retry.NotFoundError{} } -func FindRegionByName(name string) (*endpoints.Region, error) { +func findRegionByName(name string) (*endpoints.Region, error) { for _, partition := range endpoints.DefaultPartitions() { for _, region := range partition.Regions() { if region.ID() == name { @@ -162,5 +161,14 @@ func FindRegionByName(name string) (*endpoints.Region, error) { } } - return nil, fmt.Errorf("region not found for name %q", name) + return nil, &retry.NotFoundError{} +} + +func ec2Endpoint(region *endpoints.Region) (string, error) { + endpoint, err := ec2.NewDefaultEndpointResolver().ResolveEndpoint(region.ID(), ec2.EndpointResolverOptions{}) + if err != nil { + return "", err + } + + return endpoint.URL, nil } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 376ffc913299..9ab60247b0c6 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -40,7 +40,7 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByEndpoint(tc.Value) + _, err := tfmeta.FindRegionByEC2Endpoint(tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } @@ -69,6 +69,10 @@ func TestFindRegionByName(t *testing.T) { Value: "us-east-1", // lintignore:AWSAT003 ErrCount: 0, }, + { + Value: "ap-southeast-5", // lintignore:AWSAT003 + ErrCount: 0, + }, } for _, tc := range testCases { diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index 130a8bb2140f..101c05064e5f 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -21,9 +21,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newBillingServiceAccountDataSource, Name: "Billing Service Account", }, - { - Factory: newDataSourceRegion, - }, { Factory: newDataSourceService, }, @@ -39,6 +36,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newPartitionDataSource, Name: "Partition", }, + { + Factory: newRegionDataSource, + Name: "Region", + }, { Factory: newRegionsDataSource, Name: "Regions", diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index 69405791f8e4..e4f7f8c04156 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -5,8 +5,9 @@ package meta import ( "context" + "fmt" - "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -63,10 +64,11 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // find the region given by the user if !data.Region.IsNull() { - matchingRegion, err := FindRegionByName(data.Region.ValueString()) + name := data.Name.ValueString() + matchingRegion, err := findRegionByName(data.Region.ValueString()) if err != nil { - response.Diagnostics.AddError("finding Region by name", err.Error()) + response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) return } @@ -74,12 +76,13 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc region = matchingRegion } - // Default to provider current region if no other filters matched + // Default to provider current Region if no other filters matched. if region == nil { - matchingRegion, err := FindRegionByName(d.Meta().Region) + name := d.Meta().Region + matchingRegion, err := findRegionByName(name) if err != nil { - response.Diagnostics.AddError("finding Region using the provider", err.Error()) + response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) return } From 628b6e754f01e2239e4c263c0d689eb8447b74ea Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 12:13:49 -0400 Subject: [PATCH 22/30] d/aws_service_principal: Tidy up. --- internal/service/meta/service_package_gen.go | 1 + .../service/meta/service_principal_data_source.go | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index 101c05064e5f..b7632ecd7c30 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -46,6 +46,7 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv }, { Factory: newServicePrincipalDataSource, + Name: "Service Principal", }, } } diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index e4f7f8c04156..4e3d9b5bf5dc 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -12,10 +12,11 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource +// @FrameworkDataSource(name="Service Principal") func newServicePrincipalDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { d := &servicePrincipalDataSource{} @@ -98,12 +99,13 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc serviceName = data.ServiceName.ValueString() } - SourceServicePrincipal := names.ServicePrincipalNameForPartition(serviceName, partition) + sourceServicePrincipal := names.ServicePrincipalNameForPartition(serviceName, partition) + + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, serviceName+"."+region.ID()+"."+sourceServicePrincipal) + data.Name = fwflex.StringValueToFrameworkLegacy(ctx, serviceName+"."+sourceServicePrincipal) + data.Suffix = fwflex.StringValueToFrameworkLegacy(ctx, sourceServicePrincipal) + data.Region = fwflex.StringValueToFrameworkLegacy(ctx, region.ID()) - data.ID = types.StringValue(serviceName + "." + region.ID() + "." + SourceServicePrincipal) - data.Name = types.StringValue(serviceName + "." + SourceServicePrincipal) - data.Suffix = types.StringValue(SourceServicePrincipal) - data.Region = types.StringValue(region.ID()) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } From 296f89e5d319bd6febe73a530a96fbb79a540828 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 14:09:52 -0400 Subject: [PATCH 23/30] d/aws_service: Start tidy up. --- internal/service/meta/service_data_source.go | 58 +++++++++----------- internal/service/meta/service_package_gen.go | 7 ++- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/internal/service/meta/service_data_source.go b/internal/service/meta/service_data_source.go index c1582c415f60..0e9157cec2e2 100644 --- a/internal/service/meta/service_data_source.go +++ b/internal/service/meta/service_data_source.go @@ -8,35 +8,33 @@ import ( "fmt" "strings" - "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-aws/internal/framework" - "github.com/hashicorp/terraform-provider-aws/internal/slices" + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/names" ) -// @FrameworkDataSource -func newDataSourceService(context.Context) (datasource.DataSourceWithConfigure, error) { - d := &dataSourceService{} +// @FrameworkDataSource(name="Service") +func newServiceDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { + d := &serviceDataSource{} return d, nil } -type dataSourceService struct { +type serviceDataSource struct { framework.DataSourceWithConfigure } -// Metadata should return the full name of the data source, such as -// examplecloud_thing. -func (d *dataSourceService) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name +func (*serviceDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name response.TypeName = "aws_service" } -// Schema returns the schema for this data source. -func (d *dataSourceService) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (d *serviceDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ names.AttrDNSName: schema.StringAttribute{ Optional: true, @@ -72,13 +70,9 @@ func (d *dataSourceService) Schema(ctx context.Context, req datasource.SchemaReq } } -// Read is called when the provider must read data source values in order to update state. -// Config values should be read from the ReadRequest and new state values set on the ReadResponse. -func (d *dataSourceService) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { - var data dataSourceServiceData - +func (d *serviceDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { + var data serviceDataSourceModel response.Diagnostics.Append(request.Config.Get(ctx, &data)...) - if response.Diagnostics.HasError() { return } @@ -94,14 +88,14 @@ func (d *dataSourceService) Read(ctx context.Context, request datasource.ReadReq return } - data.Region = types.StringValue(serviceParts[n-2]) - data.ReverseDNSPrefix = types.StringValue(strings.Join(serviceParts[0:n-2], ".")) - data.ServiceID = types.StringValue(serviceParts[n-1]) + data.Region = fwflex.StringValueToFrameworkLegacy(ctx, serviceParts[n-2]) + data.ReverseDNSPrefix = fwflex.StringValueToFrameworkLegacy(ctx, strings.Join(serviceParts[0:n-2], ".")) + data.ServiceID = fwflex.StringValueToFrameworkLegacy(ctx, serviceParts[n-1]) } if !data.DNSName.IsNull() { v := data.DNSName.ValueString() - serviceParts := slices.Reverse(strings.Split(v, ".")) + serviceParts := tfslices.Reverse(strings.Split(v, ".")) n := len(serviceParts) if n < 4 { @@ -110,13 +104,13 @@ func (d *dataSourceService) Read(ctx context.Context, request datasource.ReadReq return } - data.Region = types.StringValue(serviceParts[n-2]) - data.ReverseDNSPrefix = types.StringValue(strings.Join(serviceParts[0:n-2], ".")) - data.ServiceID = types.StringValue(serviceParts[n-1]) + data.Region = fwflex.StringValueToFrameworkLegacy(ctx, serviceParts[n-2]) + data.ReverseDNSPrefix = fwflex.StringValueToFrameworkLegacy(ctx, strings.Join(serviceParts[0:n-2], ".")) + data.ServiceID = fwflex.StringValueToFrameworkLegacy(ctx, serviceParts[n-1]) } if data.Region.IsNull() { - data.Region = types.StringValue(d.Meta().Region) + data.Region = fwflex.StringValueToFrameworkLegacy(ctx, d.Meta().Region) } if data.ServiceID.IsNull() { @@ -127,16 +121,16 @@ func (d *dataSourceService) Read(ctx context.Context, request datasource.ReadReq if data.ReverseDNSPrefix.IsNull() { dnsParts := strings.Split(d.Meta().DNSSuffix(ctx), ".") - data.ReverseDNSPrefix = types.StringValue(strings.Join(slices.Reverse(dnsParts), ".")) + data.ReverseDNSPrefix = fwflex.StringValueToFrameworkLegacy(ctx, strings.Join(tfslices.Reverse(dnsParts), ".")) } reverseDNSName := fmt.Sprintf("%s.%s.%s", data.ReverseDNSPrefix.ValueString(), data.Region.ValueString(), data.ServiceID.ValueString()) - data.ReverseDNSName = types.StringValue(reverseDNSName) - data.DNSName = types.StringValue(strings.ToLower(strings.Join(slices.Reverse(strings.Split(reverseDNSName, ".")), "."))) + data.ReverseDNSName = fwflex.StringValueToFrameworkLegacy(ctx, reverseDNSName) + data.DNSName = fwflex.StringValueToFrameworkLegacy(ctx, strings.ToLower(strings.Join(tfslices.Reverse(strings.Split(reverseDNSName, ".")), "."))) data.Supported = types.BoolValue(true) if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), data.Region.ValueString()); ok { - data.Partition = types.StringValue(partition.ID()) + data.Partition = fwflex.StringValueToFrameworkLegacy(ctx, partition.ID()) if _, ok := partition.Services()[data.ServiceID.ValueString()]; !ok { data.Supported = types.BoolValue(false) @@ -145,12 +139,12 @@ func (d *dataSourceService) Read(ctx context.Context, request datasource.ReadReq data.Partition = types.StringNull() } - data.ID = types.StringValue(reverseDNSName) + data.ID = fwflex.StringValueToFrameworkLegacy(ctx, reverseDNSName) response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -type dataSourceServiceData struct { +type serviceDataSourceModel struct { DNSName types.String `tfsdk:"dns_name"` ID types.String `tfsdk:"id"` Partition types.String `tfsdk:"partition"` diff --git a/internal/service/meta/service_package_gen.go b/internal/service/meta/service_package_gen.go index b7632ecd7c30..ab0666720bc0 100644 --- a/internal/service/meta/service_package_gen.go +++ b/internal/service/meta/service_package_gen.go @@ -21,9 +21,6 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newBillingServiceAccountDataSource, Name: "Billing Service Account", }, - { - Factory: newDataSourceService, - }, { Factory: newDefaultTagsDataSource, Name: "Default Tags", @@ -44,6 +41,10 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv Factory: newRegionsDataSource, Name: "Regions", }, + { + Factory: newServiceDataSource, + Name: "Service", + }, { Factory: newServicePrincipalDataSource, Name: "Service Principal", From 62bc6b06c4900aade38738aa985e499aa743019d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 14:18:06 -0400 Subject: [PATCH 24/30] Add 'TestAccMetaServiceDataSource_irregularServiceID'. --- .../service/meta/service_data_source_test.go | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/internal/service/meta/service_data_source_test.go b/internal/service/meta/service_data_source_test.go index 0c5443e5a038..3e370648a5ad 100644 --- a/internal/service/meta/service_data_source_test.go +++ b/internal/service/meta/service_data_source_test.go @@ -13,9 +13,10 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccMetaService_basic(t *testing.T) { +func TestAccMetaServiceDataSource_basic(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service.test" + serviceID := "ec2" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -23,14 +24,14 @@ func TestAccMetaService_basic(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccServiceDataSourceConfig_basic(), + Config: testAccServiceDataSourceConfig_serviceID(serviceID), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, names.AttrDNSName, fmt.Sprintf("%s.%s.%s", names.EC2, acctest.Region(), "amazonaws.com")), + resource.TestCheckResourceAttr(dataSourceName, names.AttrDNSName, fmt.Sprintf("%s.%s.%s", serviceID, acctest.Region(), "amazonaws.com")), resource.TestCheckResourceAttr(dataSourceName, "partition", acctest.Partition()), resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_prefix", "com.amazonaws"), resource.TestCheckResourceAttr(dataSourceName, names.AttrRegion, acctest.Region()), - resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_name", fmt.Sprintf("%s.%s.%s", "com.amazonaws", acctest.Region(), names.EC2)), - resource.TestCheckResourceAttr(dataSourceName, "service_id", names.EC2), + resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_name", fmt.Sprintf("%s.%s.%s", "com.amazonaws", acctest.Region(), serviceID)), + resource.TestCheckResourceAttr(dataSourceName, "service_id", serviceID), resource.TestCheckResourceAttr(dataSourceName, "supported", acctest.CtTrue), ), }, @@ -38,7 +39,33 @@ func TestAccMetaService_basic(t *testing.T) { }) } -func TestAccMetaService_byReverseDNSName(t *testing.T) { +func TestAccMetaServiceDataSource_irregularServiceID(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_service.test" + serviceID := "resource-explorer-2" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccServiceDataSourceConfig_serviceID(serviceID), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, names.AttrDNSName, fmt.Sprintf("%s.%s.%s", serviceID, acctest.Region(), "amazonaws.com")), + resource.TestCheckResourceAttr(dataSourceName, "partition", acctest.Partition()), + resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_prefix", "com.amazonaws"), + resource.TestCheckResourceAttr(dataSourceName, names.AttrRegion, acctest.Region()), + resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_name", fmt.Sprintf("%s.%s.%s", "com.amazonaws", acctest.Region(), serviceID)), + resource.TestCheckResourceAttr(dataSourceName, "service_id", serviceID), + resource.TestCheckResourceAttr(dataSourceName, "supported", acctest.CtTrue), + ), + }, + }, + }) +} + +func TestAccMetaServiceDataSource_byReverseDNSName(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service.test" @@ -61,7 +88,7 @@ func TestAccMetaService_byReverseDNSName(t *testing.T) { }) } -func TestAccMetaService_byDNSName(t *testing.T) { +func TestAccMetaServiceDataSource_byDNSName(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service.test" @@ -84,7 +111,7 @@ func TestAccMetaService_byDNSName(t *testing.T) { }) } -func TestAccMetaService_byParts(t *testing.T) { +func TestAccMetaServiceDataSource_byParts(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service.test" @@ -105,7 +132,7 @@ func TestAccMetaService_byParts(t *testing.T) { }) } -func TestAccMetaService_unsupported(t *testing.T) { +func TestAccMetaServiceDataSource_unsupported(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service.test" @@ -130,12 +157,12 @@ func TestAccMetaService_unsupported(t *testing.T) { }) } -func testAccServiceDataSourceConfig_basic() string { +func testAccServiceDataSourceConfig_serviceID(id string) string { return fmt.Sprintf(` data "aws_service" "test" { service_id = %[1]q } -`, names.EC2) +`, id) } func testAccServiceDataSourceConfig_byReverseDNSName() string { From 57522f6fa52e218e2e00054393b8e527ee78976a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 14:20:11 -0400 Subject: [PATCH 25/30] Add 'TestAccMetaServiceDataSource_irregularServiceIDUnsupported'. --- .../service/meta/service_data_source_test.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/internal/service/meta/service_data_source_test.go b/internal/service/meta/service_data_source_test.go index 3e370648a5ad..6541f7181009 100644 --- a/internal/service/meta/service_data_source_test.go +++ b/internal/service/meta/service_data_source_test.go @@ -64,6 +64,31 @@ func TestAccMetaServiceDataSource_irregularServiceID(t *testing.T) { }, }) } +func TestAccMetaServiceDataSource_irregularServiceIDUnsupported(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_service.test" + serviceID := "resourceexplorer2" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, tfmeta.PseudoServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccServiceDataSourceConfig_serviceID(serviceID), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, names.AttrDNSName, fmt.Sprintf("%s.%s.%s", serviceID, acctest.Region(), "amazonaws.com")), + resource.TestCheckResourceAttr(dataSourceName, "partition", acctest.Partition()), + resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_prefix", "com.amazonaws"), + resource.TestCheckResourceAttr(dataSourceName, names.AttrRegion, acctest.Region()), + resource.TestCheckResourceAttr(dataSourceName, "reverse_dns_name", fmt.Sprintf("%s.%s.%s", "com.amazonaws", acctest.Region(), serviceID)), + resource.TestCheckResourceAttr(dataSourceName, "service_id", serviceID), + resource.TestCheckResourceAttr(dataSourceName, "supported", acctest.CtFalse), + ), + }, + }, + }) +} func TestAccMetaServiceDataSource_byReverseDNSName(t *testing.T) { ctx := acctest.Context(t) From 762f098df9905a9ba94c1c6321656323fa4f80b4 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 15:20:12 -0400 Subject: [PATCH 26/30] d/aws_region: Use EndpointsV2. --- internal/service/meta/region_data_source.go | 31 ++++++++++--------- .../service/meta/region_data_source_test.go | 6 ++-- internal/service/meta/service_data_source.go | 7 +++-- .../meta/service_principal_data_source.go | 4 +-- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/internal/service/meta/region_data_source.go b/internal/service/meta/region_data_source.go index af27a5beb857..8c37e882fcaf 100644 --- a/internal/service/meta/region_data_source.go +++ b/internal/service/meta/region_data_source.go @@ -6,8 +6,9 @@ package meta import ( "context" "fmt" - "strings" + "net/url" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -67,7 +68,7 @@ func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequ if !data.Endpoint.IsNull() { endpoint := data.Endpoint.ValueString() - matchingRegion, err := findRegionByEC2Endpoint(endpoint) + matchingRegion, err := findRegionByEC2Endpoint(ctx, endpoint) if err != nil { response.Diagnostics.AddError(fmt.Sprintf("finding Region by endpoint (%s)", endpoint), err.Error()) @@ -80,7 +81,7 @@ func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequ if !data.Name.IsNull() { name := data.Name.ValueString() - matchingRegion, err := findRegionByName(name) + matchingRegion, err := findRegionByName(ctx, name) if err != nil { response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) @@ -100,7 +101,7 @@ func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequ // Default to provider current Region if no other filters matched. if region == nil { name := d.Meta().Region - matchingRegion, err := findRegionByName(name) + matchingRegion, err := findRegionByName(ctx, name) if err != nil { response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) @@ -111,7 +112,7 @@ func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequ region = matchingRegion } - regionEndpointEC2, err := ec2Endpoint(region) + regionEndpointEC2, err := ec2Endpoint(ctx, region) if err != nil { response.Diagnostics.AddError("resolving EC2 endpoint", err.Error()) @@ -120,7 +121,7 @@ func (d *regionDataSource) Read(ctx context.Context, request datasource.ReadRequ } data.Description = fwflex.StringValueToFrameworkLegacy(ctx, region.Description()) - data.Endpoint = fwflex.StringValueToFrameworkLegacy(ctx, strings.TrimPrefix(regionEndpointEC2, "https://")) + data.Endpoint = fwflex.StringValueToFrameworkLegacy(ctx, regionEndpointEC2.Host) data.ID = fwflex.StringValueToFrameworkLegacy(ctx, region.ID()) data.Name = fwflex.StringValueToFrameworkLegacy(ctx, region.ID()) @@ -134,16 +135,16 @@ type regionDataSourceModel struct { Name types.String `tfsdk:"name"` } -func findRegionByEC2Endpoint(endpoint string) (*endpoints.Region, error) { +func findRegionByEC2Endpoint(ctx context.Context, endpoint string) (*endpoints.Region, error) { for _, partition := range endpoints.DefaultPartitions() { for _, region := range partition.Regions() { - regionEndpointEC2, err := ec2Endpoint(®ion) + regionEndpointEC2, err := ec2Endpoint(ctx, ®ion) if err != nil { return nil, err } - if strings.TrimPrefix(regionEndpointEC2, "https://") == endpoint { + if regionEndpointEC2.Host == endpoint { return ®ion, nil } } @@ -152,7 +153,7 @@ func findRegionByEC2Endpoint(endpoint string) (*endpoints.Region, error) { return nil, &retry.NotFoundError{} } -func findRegionByName(name string) (*endpoints.Region, error) { +func findRegionByName(_ context.Context, name string) (*endpoints.Region, error) { for _, partition := range endpoints.DefaultPartitions() { for _, region := range partition.Regions() { if region.ID() == name { @@ -164,11 +165,13 @@ func findRegionByName(name string) (*endpoints.Region, error) { return nil, &retry.NotFoundError{} } -func ec2Endpoint(region *endpoints.Region) (string, error) { - endpoint, err := ec2.NewDefaultEndpointResolver().ResolveEndpoint(region.ID(), ec2.EndpointResolverOptions{}) +func ec2Endpoint(ctx context.Context, region *endpoints.Region) (*url.URL, error) { + endpoint, err := ec2.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, ec2.EndpointParameters{ + Region: aws.String(region.ID()), + }) if err != nil { - return "", err + return nil, err } - return endpoint.URL, nil + return &endpoint.URI, nil } diff --git a/internal/service/meta/region_data_source_test.go b/internal/service/meta/region_data_source_test.go index 9ab60247b0c6..37b5fe8072cc 100644 --- a/internal/service/meta/region_data_source_test.go +++ b/internal/service/meta/region_data_source_test.go @@ -17,6 +17,7 @@ import ( func TestFindRegionByEC2Endpoint(t *testing.T) { t.Parallel() + ctx := acctest.Context(t) var testCases = []struct { Value string ErrCount int @@ -40,7 +41,7 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByEC2Endpoint(tc.Value) + _, err := tfmeta.FindRegionByEC2Endpoint(ctx, tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } @@ -53,6 +54,7 @@ func TestFindRegionByEC2Endpoint(t *testing.T) { func TestFindRegionByName(t *testing.T) { t.Parallel() + ctx := acctest.Context(t) var testCases = []struct { Value string ErrCount int @@ -76,7 +78,7 @@ func TestFindRegionByName(t *testing.T) { } for _, tc := range testCases { - _, err := tfmeta.FindRegionByName(tc.Value) + _, err := tfmeta.FindRegionByName(ctx, tc.Value) if tc.ErrCount == 0 && err != nil { t.Fatalf("expected %q not to trigger an error, received: %s", tc.Value, err) } diff --git a/internal/service/meta/service_data_source.go b/internal/service/meta/service_data_source.go index 0e9157cec2e2..8adadf047e77 100644 --- a/internal/service/meta/service_data_source.go +++ b/internal/service/meta/service_data_source.go @@ -132,9 +132,10 @@ func (d *serviceDataSource) Read(ctx context.Context, request datasource.ReadReq if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), data.Region.ValueString()); ok { data.Partition = fwflex.StringValueToFrameworkLegacy(ctx, partition.ID()) - if _, ok := partition.Services()[data.ServiceID.ValueString()]; !ok { - data.Supported = types.BoolValue(false) - } + // TODO + // if _, ok := partition.Services()[data.ServiceID.ValueString()]; !ok { + // data.Supported = types.BoolValue(false) + // } } else { data.Partition = types.StringNull() } diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index 4e3d9b5bf5dc..a82281cd5b29 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -66,7 +66,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // find the region given by the user if !data.Region.IsNull() { name := data.Name.ValueString() - matchingRegion, err := findRegionByName(data.Region.ValueString()) + matchingRegion, err := findRegionByName(ctx, name) if err != nil { response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) @@ -80,7 +80,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // Default to provider current Region if no other filters matched. if region == nil { name := d.Meta().Region - matchingRegion, err := findRegionByName(name) + matchingRegion, err := findRegionByName(ctx, name) if err != nil { response.Diagnostics.AddError(fmt.Sprintf("finding Region by name (%s)", name), err.Error()) From 99aa28ac830491162b17086feac15992accbb941 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 15:20:20 -0400 Subject: [PATCH 27/30] Acceptance test output: % make testacc TESTARGS='-run=TestAccMetaRegionDataSource_' PKG=meta make: Verifying source code with gofmt... ==> Checking that code complies with gofmt requirements... TF_ACC=1 go1.22.7 test ./internal/service/meta/... -v -count 1 -parallel 20 -run=TestAccMetaRegionDataSource_ -timeout 360m === RUN TestAccMetaRegionDataSource_basic === PAUSE TestAccMetaRegionDataSource_basic === RUN TestAccMetaRegionDataSource_endpoint === PAUSE TestAccMetaRegionDataSource_endpoint === RUN TestAccMetaRegionDataSource_endpointAndName === PAUSE TestAccMetaRegionDataSource_endpointAndName === RUN TestAccMetaRegionDataSource_name === PAUSE TestAccMetaRegionDataSource_name === CONT TestAccMetaRegionDataSource_basic === CONT TestAccMetaRegionDataSource_endpointAndName === CONT TestAccMetaRegionDataSource_name === CONT TestAccMetaRegionDataSource_endpoint --- PASS: TestAccMetaRegionDataSource_basic (8.25s) --- PASS: TestAccMetaRegionDataSource_endpointAndName (9.59s) --- PASS: TestAccMetaRegionDataSource_name (9.59s) --- PASS: TestAccMetaRegionDataSource_endpoint (9.62s) PASS ok github.com/hashicorp/terraform-provider-aws/internal/service/meta 14.858s From a5133fef4e7daec0038988f4d166d527554bfad0 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 16:23:26 -0400 Subject: [PATCH 28/30] d/aws_service: Revert to AWS SDK for Go v1 endpoints package -- no migratable equivalent of 'Partition.Services()'. --- internal/service/meta/service_data_source.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/service/meta/service_data_source.go b/internal/service/meta/service_data_source.go index 8adadf047e77..8822a2eef30d 100644 --- a/internal/service/meta/service_data_source.go +++ b/internal/service/meta/service_data_source.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" + "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -132,10 +132,9 @@ func (d *serviceDataSource) Read(ctx context.Context, request datasource.ReadReq if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), data.Region.ValueString()); ok { data.Partition = fwflex.StringValueToFrameworkLegacy(ctx, partition.ID()) - // TODO - // if _, ok := partition.Services()[data.ServiceID.ValueString()]; !ok { - // data.Supported = types.BoolValue(false) - // } + if _, ok := partition.Services()[data.ServiceID.ValueString()]; !ok { + data.Supported = types.BoolValue(false) + } } else { data.Partition = types.StringNull() } From c6152c0d55bb83233398691fd907136467ddf9fe Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 16:28:17 -0400 Subject: [PATCH 29/30] d/aws_service_principal: Correct acceptance test function names. --- .../service_principal_data_source_test.go | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/service/meta/service_principal_data_source_test.go b/internal/service/meta/service_principal_data_source_test.go index b8431f54845c..ecba46ed4f02 100644 --- a/internal/service/meta/service_principal_data_source_test.go +++ b/internal/service/meta/service_principal_data_source_test.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -func TestAccMetaServicePrincipal_basic(t *testing.T) { +func TestAccMetaServicePrincipalDataSource_basic(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service_principal.test" @@ -24,7 +24,7 @@ func TestAccMetaServicePrincipal_basic(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccSPNDataSourceConfig_basic, + Config: testAccServicePrincipalDataSourceConfig_basic, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(dataSourceName, names.AttrID, "s3."+acctest.Region()+".amazonaws.com"), resource.TestCheckResourceAttr(dataSourceName, names.AttrName, "s3.amazonaws.com"), @@ -37,7 +37,7 @@ func TestAccMetaServicePrincipal_basic(t *testing.T) { }) } -func TestAccMetaServicePrincipal_MissingService(t *testing.T) { +func TestAccMetaServicePrincipalDataSource_MissingService(t *testing.T) { ctx := acctest.Context(t) resource.ParallelTest(t, resource.TestCase{ @@ -46,14 +46,14 @@ func TestAccMetaServicePrincipal_MissingService(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccSPNDataSourceConfig_empty, + Config: testAccServicePrincipalDataSourceConfig_empty, ExpectError: regexache.MustCompile(`The argument "service_name" is required, but no definition was found.`), }, }, }) } -func TestAccMetaServicePrincipal_ByRegion(t *testing.T) { +func TestAccMetaServicePrincipalDataSource_ByRegion(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service_principal.test" @@ -68,7 +68,7 @@ func TestAccMetaServicePrincipal_ByRegion(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccSPNDataSourceConfig_withRegion("s3", region), + Config: testAccServicePrincipalDataSourceConfig_withRegion("s3", region), Check: resource.ComposeTestCheckFunc( //lintignore:AWSR001 resource.TestCheckResourceAttr(dataSourceName, names.AttrID, fmt.Sprintf("s3.%s.amazonaws.com", region)), @@ -83,7 +83,7 @@ func TestAccMetaServicePrincipal_ByRegion(t *testing.T) { } } -func TestAccMetaServicePrincipal_UniqueForServiceInRegion(t *testing.T) { +func TestAccMetaServicePrincipalDataSource_UniqueForServiceInRegion(t *testing.T) { ctx := acctest.Context(t) dataSourceName := "data.aws_service_principal.test" @@ -140,7 +140,7 @@ func TestAccMetaServicePrincipal_UniqueForServiceInRegion(t *testing.T) { ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccSPNDataSourceConfig_withRegion(testCase.Service, testCase.Region), + Config: testAccServicePrincipalDataSourceConfig_withRegion(testCase.Service, testCase.Region), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(dataSourceName, names.AttrID, testCase.ID), resource.TestCheckResourceAttr(dataSourceName, names.AttrName, testCase.SPN), @@ -154,16 +154,17 @@ func TestAccMetaServicePrincipal_UniqueForServiceInRegion(t *testing.T) { } } -const testAccSPNDataSourceConfig_empty = ` +const testAccServicePrincipalDataSourceConfig_empty = ` data "aws_service_principal" "test" {} ` -const testAccSPNDataSourceConfig_basic = ` + +const testAccServicePrincipalDataSourceConfig_basic = ` data "aws_service_principal" "test" { service_name = "s3" } ` -func testAccSPNDataSourceConfig_withRegion(service string, region string) string { +func testAccServicePrincipalDataSourceConfig_withRegion(service string, region string) string { return fmt.Sprintf(` data "aws_service_principal" "test" { region = %[1]q From 3ea85cc83cf0d54eb94934e56cb39d58b0c4cd81 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 20 Sep 2024 16:30:54 -0400 Subject: [PATCH 30/30] Fix typo. --- internal/service/meta/service_principal_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/meta/service_principal_data_source.go b/internal/service/meta/service_principal_data_source.go index a82281cd5b29..79e372e4cbb8 100644 --- a/internal/service/meta/service_principal_data_source.go +++ b/internal/service/meta/service_principal_data_source.go @@ -65,7 +65,7 @@ func (d *servicePrincipalDataSource) Read(ctx context.Context, request datasourc // find the region given by the user if !data.Region.IsNull() { - name := data.Name.ValueString() + name := data.Region.ValueString() matchingRegion, err := findRegionByName(ctx, name) if err != nil {