Skip to content

Commit

Permalink
Merge pull request #40335 from hashicorp/td-names-use-endpoints-regions
Browse files Browse the repository at this point in the history
Replace AWS Region metadata from `names` with `endpoints`
  • Loading branch information
ewbankkit authored Nov 28, 2024
2 parents dd0feab + 878bb94 commit c2a971e
Show file tree
Hide file tree
Showing 1,202 changed files with 4,495 additions and 4,464 deletions.
54 changes: 27 additions & 27 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func PreCheck(ctx context.Context, t *testing.T) {
}

// ProviderAccountID returns the account ID of an AWS provider
func ProviderAccountID(provider *schema.Provider) string {
func ProviderAccountID(ctx context.Context, provider *schema.Provider) string {
if provider == nil {
log.Print("[DEBUG] Unable to read account ID from test provider: empty provider")
return ""
Expand All @@ -335,7 +335,7 @@ func ProviderAccountID(provider *schema.Provider) string {
log.Print("[DEBUG] Unable to read account ID from test provider: non-AWS or unconfigured AWS provider")
return ""
}
return client.AccountID
return client.AccountID(ctx)
}

// CheckDestroyNoop is a TestCheckFunc to be used as a TestCase's CheckDestroy when no such check can be made.
Expand All @@ -355,17 +355,17 @@ func CheckSleep(t *testing.T, d time.Duration) resource.TestCheckFunc {
}

// CheckResourceAttrAccountID ensures the Terraform state exactly matches the account ID
func CheckResourceAttrAccountID(resourceName, attributeName string) resource.TestCheckFunc {
func CheckResourceAttrAccountID(ctx context.Context, resourceName, attributeName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return resource.TestCheckResourceAttr(resourceName, attributeName, AccountID())(s)
return resource.TestCheckResourceAttr(resourceName, attributeName, AccountID(ctx))(s)
}
}

// CheckResourceAttrRegionalARN ensures the Terraform state exactly matches a formatted ARN with region
func CheckResourceAttrRegionalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
func CheckResourceAttrRegionalARN(ctx context.Context, resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := arn.ARN{
AccountID: AccountID(),
AccountID: AccountID(ctx),
Partition: Partition(),
Region: Region(),
Resource: arnResource,
Expand Down Expand Up @@ -458,10 +458,10 @@ func MatchResourceAttrAccountID(resourceName, attributeName string) resource.Tes
}

// MatchResourceAttrRegionalARN ensures the Terraform state regexp matches a formatted ARN with region
func MatchResourceAttrRegionalARN(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
func MatchResourceAttrRegionalARN(ctx context.Context, resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: AccountID(),
AccountID: AccountID(ctx),
Partition: Partition(),
Region: Region(),
Resource: arnResourceRegexp.String(),
Expand All @@ -479,10 +479,10 @@ func MatchResourceAttrRegionalARN(resourceName, attributeName, arnService string
}

// MatchResourceAttrRegionalARNRegion ensures the Terraform state regexp matches a formatted ARN with the specified region
func MatchResourceAttrRegionalARNRegion(resourceName, attributeName, arnService, region string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
func MatchResourceAttrRegionalARNRegion(ctx context.Context, resourceName, attributeName, arnService, region string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: AccountID(),
AccountID: AccountID(ctx),
Partition: Partition(),
Region: region,
Resource: arnResourceRegexp.String(),
Expand Down Expand Up @@ -570,19 +570,19 @@ func MatchResourceAttrGlobalHostname(resourceName, attributeName, serviceName st
}
}

func globalARNValue(arnService, arnResource string) string {
func globalARNValue(ctx context.Context, arnService, arnResource string) string {
return arn.ARN{
AccountID: AccountID(),
AccountID: AccountID(ctx),
Partition: Partition(),
Resource: arnResource,
Service: arnService,
}.String()
}

// CheckResourceAttrGlobalARN ensures the Terraform state exactly matches a formatted ARN without region
func CheckResourceAttrGlobalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
func CheckResourceAttrGlobalARN(ctx context.Context, resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return resource.TestCheckResourceAttr(resourceName, attributeName, globalARNValue(arnService, arnResource))(s)
return resource.TestCheckResourceAttr(resourceName, attributeName, globalARNValue(ctx, arnService, arnResource))(s)
}
}

Expand Down Expand Up @@ -612,10 +612,10 @@ func CheckResourceAttrGlobalARNAccountID(resourceName, attributeName, accountID,
}

// MatchResourceAttrGlobalARN ensures the Terraform state regexp matches a formatted ARN without region
func MatchResourceAttrGlobalARN(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
func MatchResourceAttrGlobalARN(ctx context.Context, resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: AccountID(),
AccountID: AccountID(ctx),
Partition: Partition(),
Resource: arnResourceRegexp.String(),
Service: arnService,
Expand Down Expand Up @@ -892,20 +892,20 @@ func PrimaryInstanceState(s *terraform.State, name string) (*terraform.InstanceS

// AccountID returns the account ID of Provider
// Must be used within a resource.TestCheckFunc
func AccountID() string {
return ProviderAccountID(Provider)
func AccountID(ctx context.Context) string {
return ProviderAccountID(ctx, Provider)
}

func Region() string {
return envvar.GetWithDefault(envvar.DefaultRegion, names.USWest2RegionID)
return envvar.GetWithDefault(envvar.DefaultRegion, endpoints.UsWest2RegionID)
}

func AlternateRegion() string {
return envvar.GetWithDefault(envvar.AlternateRegion, names.USEast1RegionID)
return envvar.GetWithDefault(envvar.AlternateRegion, endpoints.UsEast1RegionID)
}

func ThirdRegion() string {
return envvar.GetWithDefault(envvar.ThirdRegion, names.USEast2RegionID)
return envvar.GetWithDefault(envvar.ThirdRegion, endpoints.UsEast2RegionID)
}

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

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

conn := Provider.Meta().(*conns.AWSClient).WAFV2Client(ctx)
Expand Down Expand Up @@ -1569,7 +1569,7 @@ provider %[1]q {
`, providerName, os.Getenv(envvar.AlternateAccessKeyId), os.Getenv(envvar.AlternateProfile), AlternateRegion(), os.Getenv(envvar.AlternateSecretAccessKey))
}

func RegionProviderFunc(region string, providers *[]*schema.Provider) ProviderFunc {
func RegionProviderFunc(ctx context.Context, region string, providers *[]*schema.Provider) ProviderFunc {
return func() *schema.Provider {
if region == "" {
log.Println("[DEBUG] No region given")
Expand All @@ -1595,7 +1595,7 @@ func RegionProviderFunc(region string, providers *[]*schema.Provider) ProviderFu
continue
}

clientRegion := client.Region
clientRegion := client.Region(ctx)
log.Printf("[DEBUG] Checking AWS provider region %q against %q", clientRegion, region)
if clientRegion == region {
log.Printf("[DEBUG] Found AWS provider with region: %s", region)
Expand Down Expand Up @@ -2624,14 +2624,14 @@ func CheckVPCExists(ctx context.Context, n string, v *ec2types.Vpc) resource.Tes
}
}

func CheckCallerIdentityAccountID(n string) resource.TestCheckFunc {
func CheckCallerIdentityAccountID(ctx context.Context, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("can't find AccountID resource: %s", n)
}

expected := Provider.Meta().(*conns.AWSClient).AccountID
expected := Provider.Meta().(*conns.AWSClient).AccountID(ctx)
if rs.Primary.Attributes["account_id"] != expected {
return fmt.Errorf("incorrect Account ID: expected %q, got %q", expected, rs.Primary.Attributes["account_id"])
}
Expand Down
5 changes: 3 additions & 2 deletions internal/acctest/known_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package acctest

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-testing/knownvalue"
Expand All @@ -23,7 +24,7 @@ func (v globalARNCheck) CheckValue(other any) error {
return fmt.Errorf("expected string value for GlobalARN check, got: %T", other)
}

arnValue := globalARNValue(v.arnService, v.arnResource)
arnValue := globalARNValue(context.Background(), v.arnService, v.arnResource)

if otherVal != arnValue {
return fmt.Errorf("expected value %s for GlobalARN check, got: %s", arnValue, otherVal)
Expand All @@ -34,7 +35,7 @@ func (v globalARNCheck) CheckValue(other any) error {

// String returns the string representation of the value.
func (v globalARNCheck) String() string {
return globalARNValue(v.arnService, v.arnResource)
return globalARNValue(context.Background(), v.arnService, v.arnResource)
}

func GlobalARN(arnService, arnResource string) globalARNCheck {
Expand Down
25 changes: 12 additions & 13 deletions internal/acctest/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/hashicorp/aws-sdk-go-base/v2/endpoints"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestIsIsolatedPartition(t *testing.T) {
Expand Down Expand Up @@ -67,27 +66,27 @@ func TestIsIsolatedRegion(t *testing.T) {
expected bool
}{
{
input: names.USEast1RegionID,
input: endpoints.UsEast1RegionID,
expected: false,
},
{
input: names.CNNorth1RegionID,
input: endpoints.CnNorth1RegionID,
expected: false,
},
{
input: names.USGovEast1RegionID,
input: endpoints.UsGovEast1RegionID,
expected: false,
},
{
input: names.USISOEast1RegionID,
input: endpoints.UsIsoEast1RegionID,
expected: true,
},
{
input: names.USISOBEast1RegionID,
input: endpoints.UsIsobEast1RegionID,
expected: true,
},
{
input: names.EUISOEWest1RegionID,
input: endpoints.EuIsoeWest1RegionID,
expected: true,
},
}
Expand Down Expand Up @@ -159,27 +158,27 @@ func TestIsStandardRegion(t *testing.T) {
expected bool
}{
{
input: names.USEast1RegionID,
input: endpoints.UsEast1RegionID,
expected: true,
},
{
input: names.CNNorth1RegionID,
input: endpoints.CnNorth1RegionID,
expected: false,
},
{
input: names.USGovEast1RegionID,
input: endpoints.UsGovEast1RegionID,
expected: false,
},
{
input: names.USISOEast1RegionID,
input: endpoints.UsIsoEast1RegionID,
expected: false,
},
{
input: names.USISOBEast1RegionID,
input: endpoints.UsIsobEast1RegionID,
expected: false,
},
{
input: names.EUISOEWest1RegionID,
input: endpoints.EuIsoeWest1RegionID,
expected: false,
},
}
Expand Down
38 changes: 24 additions & 14 deletions internal/conns/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ import (
)

type AWSClient struct {
AccountID string
defaultTagsConfig *tftags.DefaultConfig
ignoreTagsConfig *tftags.IgnoreConfig
Region string
ServicePackages map[string]ServicePackage
ServicePackages map[string]ServicePackage

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

// AccountID returns the configured AWS account ID.
func (c *AWSClient) AccountID(context.Context) string {
return c.accountID
}

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

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

// 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.
Expand All @@ -93,8 +103,8 @@ func (c *AWSClient) RegionalARN(ctx context.Context, service, resource string) s
return arn.ARN{
Partition: c.Partition(ctx),
Service: service,
Region: c.Region,
AccountID: c.AccountID,
Region: c.Region(ctx),
AccountID: c.AccountID(ctx),
Resource: resource,
}.String()
}
Expand All @@ -104,7 +114,7 @@ func (c *AWSClient) RegionalARNNoAccount(ctx context.Context, service, resource
return arn.ARN{
Partition: c.Partition(ctx),
Service: service,
Region: c.Region,
Region: c.Region(ctx),
Resource: resource,
}.String()
}
Expand All @@ -113,7 +123,7 @@ func (c *AWSClient) RegionalARNNoAccount(ctx context.Context, service, resource
// e.g. PREFIX.us-west-2.amazonaws.com
// The prefix should not contain a trailing period.
func (c *AWSClient) RegionalHostname(ctx context.Context, prefix string) string {
return fmt.Sprintf("%s.%s.%s", prefix, c.Region, c.DNSSuffix(ctx))
return fmt.Sprintf("%s.%s.%s", prefix, c.Region(ctx), c.DNSSuffix(ctx))
}

// S3ExpressClient returns an AWS SDK for Go v2 S3 API client suitable for use with S3 Express (directory buckets).
Expand Down Expand Up @@ -209,7 +219,7 @@ func (c *AWSClient) DefaultKMSKeyPolicy(ctx context.Context) string {
}
]
}
`, c.Partition(ctx), c.AccountID)
`, c.Partition(ctx), c.AccountID(ctx))
}

// GlobalAcceleratorHostedZoneID returns the Route 53 hosted zone ID
Expand All @@ -234,8 +244,8 @@ func (c *AWSClient) ReverseDNSPrefix(ctx context.Context) string {
}

// EC2RegionalPrivateDNSSuffix returns the EC2 private DNS suffix for the configured AWS Region.
func (c *AWSClient) EC2RegionalPrivateDNSSuffix(context.Context) string {
region := c.Region
func (c *AWSClient) EC2RegionalPrivateDNSSuffix(ctx context.Context) string {
region := c.Region(ctx)
if region == endpoints.UsEast1RegionID {
return "ec2.internal"
}
Expand All @@ -244,8 +254,8 @@ func (c *AWSClient) EC2RegionalPrivateDNSSuffix(context.Context) string {
}

// EC2RegionalPublicDNSSuffix returns the EC2 public DNS suffix for the configured AWS Region.
func (c *AWSClient) EC2RegionalPublicDNSSuffix(context.Context) string {
region := c.Region
func (c *AWSClient) EC2RegionalPublicDNSSuffix(ctx context.Context) string {
region := c.Region(ctx)
if region == endpoints.UsEast1RegionID {
return "compute-1"
}
Expand Down
Loading

0 comments on commit c2a971e

Please sign in to comment.