Skip to content

Commit

Permalink
Merge pull request hashicorp#39874 from hashicorp/td-names-use-endpoints
Browse files Browse the repository at this point in the history
Replace AWS partition metadata from `names` with `endpoints`
  • Loading branch information
ewbankkit authored Oct 28, 2024
2 parents 34dc6e4 + 90ac671 commit afc50a0
Show file tree
Hide file tree
Showing 380 changed files with 792 additions and 1,118 deletions.
16 changes: 8 additions & 8 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,27 +890,27 @@ func ThirdRegion() string {
}

func Partition() string {
return names.PartitionForRegion(Region())
return names.PartitionForRegion(Region()).ID()
}

func PartitionRegions() []string {
return RegionsInPartition(Partition())
}

func PartitionDNSSuffix() string {
return names.DNSSuffixForPartition(Partition())
return names.PartitionForRegion(Region()).DNSSuffix()
}

func PartitionReverseDNSPrefix() string {
return conns.ReverseDNS(PartitionDNSSuffix())
}

func alternateRegionPartition() string {
return names.PartitionForRegion(AlternateRegion())
return names.PartitionForRegion(AlternateRegion()).ID()
}

func thirdRegionPartition() string {
return names.PartitionForRegion(ThirdRegion())
return names.PartitionForRegion(ThirdRegion()).ID()
}

func PreCheckAlternateAccount(t *testing.T) {
Expand Down Expand Up @@ -959,7 +959,7 @@ func PreCheckMultipleRegion(t *testing.T, regions int) {
}

if regions >= 3 {
if thirdRegionPartition() == names.USGovCloudPartitionID || Partition() == names.USGovCloudPartitionID {
if thirdRegionPartition() == endpoints.AwsUsGovPartitionID || Partition() == endpoints.AwsUsGovPartitionID {
t.Skipf("wanted %d regions, partition (%s) only has 2 regions", regions, Partition())
}

Expand Down Expand Up @@ -1340,9 +1340,9 @@ func PreCheckWAFV2CloudFrontScope(ctx context.Context, t *testing.T) {
t.Helper()

switch Partition() {
case names.StandardPartitionID:
case endpoints.AwsPartitionID:
PreCheckRegion(t, names.USEast1RegionID)
case names.ChinaPartitionID:
case endpoints.AwsCnPartitionID:
PreCheckRegion(t, names.CNNorthwest1RegionID)
}

Expand Down Expand Up @@ -2202,7 +2202,7 @@ func CheckACMPCACertificateAuthorityExists(ctx context.Context, n string, certif
func PreCheckAPIGatewayTypeEDGE(t *testing.T) {
t.Helper()

if Partition() != names.StandardPartitionID {
if Partition() != endpoints.AwsPartitionID {
t.Skipf("skipping test; Endpoint Configuration type EDGE is not supported in this partition (%s)", Partition())
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/acctest/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func IsIsolatedRegion(region string) bool {
partition := names.PartitionForRegion(region)

return IsIsolatedPartition(partition)
return IsIsolatedPartition(partition.ID())
}

func IsIsolatedPartition(partition string) bool {
Expand All @@ -26,11 +26,11 @@ func IsIsolatedPartition(partition string) bool {
func IsStandardRegion(region string) bool {
partition := names.PartitionForRegion(region)

return IsStandardPartition(partition)
return IsStandardPartition(partition.ID())
}

func IsStandardPartition(partitionId string) bool {
return partitionId == names.StandardPartitionID
func IsStandardPartition(partitionID string) bool {
return partitionID == endpoints.AwsPartitionID
}

func RegionsInPartition(partitionName string) []string {
Expand Down
29 changes: 15 additions & 14 deletions internal/acctest/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package acctest_test
import (
"testing"

"github.com/hashicorp/aws-sdk-go-base/v2/endpoints"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand All @@ -18,31 +19,31 @@ func TestIsIsolatedPartition(t *testing.T) {
expected bool
}{
{
input: names.StandardPartitionID,
input: endpoints.AwsPartitionID,
expected: false,
},
{
input: names.ChinaPartitionID,
input: endpoints.AwsCnPartitionID,
expected: false,
},
{
input: names.USGovCloudPartitionID,
input: endpoints.AwsUsGovPartitionID,
expected: false,
},
{
input: names.ISOPartitionID,
input: endpoints.AwsIsoPartitionID,
expected: true,
},
{
input: names.ISOBPartitionID,
input: endpoints.AwsIsoBPartitionID,
expected: true,
},
{
input: names.ISOEPartitionID,
input: endpoints.AwsIsoEPartitionID,
expected: true,
},
{
input: names.ISOFPartitionID,
input: endpoints.AwsIsoFPartitionID,
expected: true,
},
}
Expand Down Expand Up @@ -110,31 +111,31 @@ func TestIsStandardPartition(t *testing.T) {
expected bool
}{
{
input: names.StandardPartitionID,
input: endpoints.AwsPartitionID,
expected: true,
},
{
input: names.ChinaPartitionID,
input: endpoints.AwsCnPartitionID,
expected: false,
},
{
input: names.USGovCloudPartitionID,
input: endpoints.AwsUsGovPartitionID,
expected: false,
},
{
input: names.ISOPartitionID,
input: endpoints.AwsIsoPartitionID,
expected: false,
},
{
input: names.ISOBPartitionID,
input: endpoints.AwsIsoBPartitionID,
expected: false,
},
{
input: names.ISOEPartitionID,
input: endpoints.AwsIsoEPartitionID,
expected: false,
},
{
input: names.ISOFPartitionID,
input: endpoints.AwsIsoFPartitionID,
expected: false,
},
}
Expand Down
49 changes: 40 additions & 9 deletions internal/conns/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
apigatewayv2_types "github.com/aws/aws-sdk-go-v2/service/apigatewayv2/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
session_sdkv1 "github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -28,18 +29,17 @@ type AWSClient struct {
AccountID string
defaultTagsConfig *tftags.DefaultConfig
ignoreTagsConfig *tftags.IgnoreConfig
Partition string
Region string
ServicePackages map[string]ServicePackage

awsConfig *aws.Config
clients map[string]any
conns map[string]any
dnsSuffix string
endpoints map[string]string // From provider configuration.
httpClient *http.Client
lock sync.Mutex
logger baselogging.Logger
partition endpoints.Partition
session *session_sdkv1.Session
s3ExpressClient *s3.Client
s3UsePathStyle bool // From provider configuration.
Expand Down Expand Up @@ -76,13 +76,39 @@ func (c *AWSClient) Endpoints(context.Context) map[string]string {
return maps.Clone(c.endpoints)
}

// Partition returns the ID of the configured AWS partition.
func (c *AWSClient) Partition(context.Context) string {
return c.partition.ID()
}

// PartitionHostname returns a hostname with the provider domain suffix for the partition
// e.g. PREFIX.amazonaws.com
// The prefix should not contain a trailing period.
func (c *AWSClient) PartitionHostname(ctx context.Context, prefix string) string {
return fmt.Sprintf("%s.%s", prefix, c.DNSSuffix(ctx))
}

// RegionalARN returns a regional ARN for the specified service namespace and resource.
func (c *AWSClient) RegionalARN(ctx context.Context, service, resource string) string {
return arn.ARN{
Partition: c.Partition(ctx),
Service: service,
Region: c.Region,
AccountID: c.AccountID,
Resource: resource,
}.String()
}

// RegionalARNNoAccount returns a regional ARN for the specified service namespace and resource without AWS account ID.
func (c *AWSClient) RegionalARNNoAccount(ctx context.Context, service, resource string) string {
return arn.ARN{
Partition: c.Partition(ctx),
Service: service,
Region: c.Region,
Resource: resource,
}.String()
}

// RegionalHostname returns a hostname with the provider domain suffix for the region and partition
// e.g. PREFIX.us-west-2.amazonaws.com
// The prefix should not contain a trailing period.
Expand Down Expand Up @@ -158,15 +184,15 @@ func (c *AWSClient) APIGatewayV2InvokeURL(ctx context.Context, protocolType apig

// CloudFrontDistributionHostedZoneID returns the Route 53 hosted zone ID
// for Amazon CloudFront distributions in the configured AWS partition.
func (c *AWSClient) CloudFrontDistributionHostedZoneID(context.Context) string {
if c.Partition == endpoints.AwsCnPartitionID {
func (c *AWSClient) CloudFrontDistributionHostedZoneID(ctx context.Context) string {
if c.Partition(ctx) == endpoints.AwsCnPartitionID {
return "Z3RFFRIM2A3IF5" // See https://docs.amazonaws.cn/en_us/aws/latest/userguide/route53.html
}
return "Z2FDTNDATAQYW2" // See https://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html#Route53-Type-AliasTarget-HostedZoneId
}

// DefaultKMSKeyPolicy returns the default policy for KMS keys in the configured AWS partition.
func (c *AWSClient) DefaultKMSKeyPolicy(context.Context) string {
func (c *AWSClient) DefaultKMSKeyPolicy(ctx context.Context) string {
return fmt.Sprintf(`
{
"Id": "default",
Expand All @@ -183,7 +209,7 @@ func (c *AWSClient) DefaultKMSKeyPolicy(context.Context) string {
}
]
}
`, c.Partition, c.AccountID)
`, c.Partition(ctx), c.AccountID)
}

// GlobalAcceleratorHostedZoneID returns the Route 53 hosted zone ID
Expand All @@ -194,7 +220,12 @@ func (c *AWSClient) GlobalAcceleratorHostedZoneID(context.Context) string {

// DNSSuffix returns the domain suffix for the configured AWS partition.
func (c *AWSClient) DNSSuffix(context.Context) string {
return c.dnsSuffix
dnsSuffix := c.partition.DNSSuffix()
if dnsSuffix == "" {
dnsSuffix = "amazonaws.com"
}

return dnsSuffix
}

// ReverseDNSPrefix returns the reverse DNS prefix for the configured AWS partition.
Expand Down Expand Up @@ -248,11 +279,11 @@ func convertIPToDashIP(ip string) string {
}

// apiClientConfig returns the AWS API client configuration parameters for the specified service.
func (c *AWSClient) apiClientConfig(_ context.Context, servicePackageName string) map[string]any {
func (c *AWSClient) apiClientConfig(ctx context.Context, servicePackageName string) map[string]any {
m := map[string]any{
"aws_sdkv2_config": c.awsConfig,
"endpoint": c.endpoints[servicePackageName],
"partition": c.Partition,
"partition": c.Partition(ctx),
}
switch servicePackageName {
case names.S3:
Expand Down
23 changes: 15 additions & 8 deletions internal/conns/awsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ package conns
import (
"context"
"testing"

"github.com/hashicorp/aws-sdk-go-base/v2/endpoints"
)

var (
standardPartition, _ = endpoints.PartitionForRegion(endpoints.DefaultPartitions(), endpoints.UsEast1RegionID)
chinaPartition, _ = endpoints.PartitionForRegion(endpoints.DefaultPartitions(), endpoints.CnNorth1RegionID)
)

func TestAWSClientPartitionHostname(t *testing.T) { // nosemgrep:ci.aws-in-func-name
Expand All @@ -21,15 +28,15 @@ func TestAWSClientPartitionHostname(t *testing.T) { // nosemgrep:ci.aws-in-func-
{
Name: "AWS Commercial",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
},
Prefix: "test",
Expected: "test.amazonaws.com",
},
{
Name: "AWS China",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com.cn",
partition: chinaPartition,
},
Prefix: "test",
Expected: "test.amazonaws.com.cn",
Expand Down Expand Up @@ -62,7 +69,7 @@ func TestAWSClientRegionalHostname(t *testing.T) { // nosemgrep:ci.aws-in-func-n
{
Name: "AWS Commercial",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
Region: "us-west-2", //lintignore:AWSAT003
},
Prefix: "test",
Expand All @@ -71,7 +78,7 @@ func TestAWSClientRegionalHostname(t *testing.T) { // nosemgrep:ci.aws-in-func-n
{
Name: "AWS China",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com.cn",
partition: chinaPartition,
Region: "cn-northwest-1", //lintignore:AWSAT003
},
Prefix: "test",
Expand Down Expand Up @@ -105,7 +112,7 @@ func TestAWSClientEC2PrivateDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-
{
Name: "us-west-2",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
Region: "us-west-2", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expand All @@ -114,7 +121,7 @@ func TestAWSClientEC2PrivateDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-
{
Name: "us-east-1",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
Region: "us-east-1", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expand Down Expand Up @@ -148,7 +155,7 @@ func TestAWSClientEC2PublicDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-f
{
Name: "us-west-2",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
Region: "us-west-2", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expand All @@ -157,7 +164,7 @@ func TestAWSClientEC2PublicDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-f
{
Name: "us-east-1",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
partition: standardPartition,
Region: "us-east-1", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expand Down
Loading

0 comments on commit afc50a0

Please sign in to comment.