From 94d0344d6346c770640ed9628319399751dda5c0 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 17 Sep 2024 15:04:59 -0400 Subject: [PATCH] Replace 'internal/endpoints' with top-level 'endpoints' package. --- aws_config.go | 6 +- internal/endpoints/endpoints.go | 129 --------------------------- internal/endpoints/endpoints_test.go | 37 -------- validation/region.go | 16 ++-- 4 files changed, 12 insertions(+), 176 deletions(-) delete mode 100644 internal/endpoints/endpoints.go delete mode 100644 internal/endpoints/endpoints_test.go diff --git a/aws_config.go b/aws_config.go index 069cf26b..eba1b817 100644 --- a/aws_config.go +++ b/aws_config.go @@ -24,9 +24,9 @@ import ( "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" "github.com/aws/smithy-go/middleware" "github.com/hashicorp/aws-sdk-go-base/v2/diag" + "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" "github.com/hashicorp/aws-sdk-go-base/v2/internal/awsconfig" "github.com/hashicorp/aws-sdk-go-base/v2/internal/constants" - "github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints" "github.com/hashicorp/aws-sdk-go-base/v2/logging" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -337,7 +337,9 @@ func GetAwsAccountIDAndPartition(ctx context.Context, awsConfig aws.Config, c *C "Errors: %w", err)) } - return "", endpoints.PartitionForRegion(awsConfig.Region), nil + partition, _ := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), awsConfig.Region) + + return "", partition.ID(), nil } func commonLoadOptions(ctx context.Context, c *Config) ([]func(*config.LoadOptions) error, error) { diff --git a/internal/endpoints/endpoints.go b/internal/endpoints/endpoints.go deleted file mode 100644 index e98bf508..00000000 --- a/internal/endpoints/endpoints.go +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package endpoints - -import ( - "regexp" -) - -func Partitions() []Partition { - ps := make([]Partition, len(partitions)) - for i := 0; i < len(partitions); i++ { - ps[i] = partitions[i].Partition() - } - return ps -} - -type Partition struct { - id string - p *partition -} - -func (p Partition) Regions() []string { - rs := make([]string, len(p.p.regions)) - copy(rs, p.p.regions) - return rs -} - -func PartitionForRegion(regionID string) string { - for _, p := range partitions { - if p.regionRegex.MatchString(regionID) { - return p.id - } - } - - return "" -} - -type partition struct { - id string - regionRegex *regexp.Regexp - regions []string -} - -func (p partition) Partition() Partition { - return Partition{ - id: p.id, - p: &p, - } -} - -// TODO: this should be generated from the AWS SDK source data -// Data from https://github.com/aws/aws-sdk-go/blob/main/models/endpoints/endpoints.json. -var partitions = []partition{ - { - id: "aws", - regionRegex: regexp.MustCompile(`^(us|eu|ap|sa|ca|me|af|il)\-\w+\-\d+$`), - regions: []string{ - "af-south-1", // Africa (Cape Town). - "ap-east-1", // Asia Pacific (Hong Kong). - "ap-northeast-1", // Asia Pacific (Tokyo). - "ap-northeast-2", // Asia Pacific (Seoul). - "ap-northeast-3", // Asia Pacific (Osaka). - "ap-south-1", // Asia Pacific (Mumbai). - "ap-south-2", // Asia Pacific (Hyderabad). - "ap-southeast-1", // Asia Pacific (Singapore). - "ap-southeast-2", // Asia Pacific (Sydney). - "ap-southeast-3", // Asia Pacific (Jakarta). - "ap-southeast-4", // Asia Pacific (Melbourne). - "ap-southeast-5", // Asia Pacific (Malaysia). - "ca-central-1", // Canada (Central). - "ca-west-1", // Canada (Calgary). - "eu-central-1", // Europe (Frankfurt). - "eu-central-2", // Europe (Zurich). - "eu-north-1", // Europe (Stockholm). - "eu-south-1", // Europe (Milan). - "eu-south-2", // Europe (Spain). - "eu-west-1", // Europe (Ireland). - "eu-west-2", // Europe (London). - "eu-west-3", // Europe (Paris). - "il-central-1", // Israel (Tel Aviv). - "me-central-1", // Middle East (UAE). - "me-south-1", // Middle East (Bahrain). - "sa-east-1", // South America (Sao Paulo). - "us-east-1", // US East (N. Virginia). - "us-east-2", // US East (Ohio). - "us-west-1", // US West (N. California). - "us-west-2", // US West (Oregon). - }, - }, - { - id: "aws-cn", - regionRegex: regexp.MustCompile(`^cn\-\w+\-\d+$`), - regions: []string{ - "cn-north-1", // China (Beijing). - "cn-northwest-1", // China (Ningxia). - }, - }, - { - id: "aws-us-gov", - regionRegex: regexp.MustCompile(`^us\-gov\-\w+\-\d+$`), - regions: []string{ - "us-gov-east-1", // AWS GovCloud (US-East). - "us-gov-west-1", // AWS GovCloud (US-West). - }, - }, - { - id: "aws-iso", - regionRegex: regexp.MustCompile(`^us\-iso\-\w+\-\d+$`), - regions: []string{ - "us-iso-east-1", // US ISO East. - "us-iso-west-1", // US ISO WEST. - }, - }, - { - id: "aws-iso-b", - regionRegex: regexp.MustCompile(`^us\-isob\-\w+\-\d+$`), - regions: []string{ - "us-isob-east-1", // US ISOB East (Ohio). - }, - }, - { - id: "aws-iso-e", - regionRegex: regexp.MustCompile(`^eu\-isoe\-\w+\-\d+$`), - regions: []string{ - "eu-isoe-west-1", // EU ISOE West. - }, - }, -} diff --git a/internal/endpoints/endpoints_test.go b/internal/endpoints/endpoints_test.go deleted file mode 100644 index fcd892e8..00000000 --- a/internal/endpoints/endpoints_test.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package endpoints_test - -import ( - "testing" - - "github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints" -) - -func TestPartitionForRegion(t *testing.T) { - testcases := map[string]struct { - expected string - }{ - "us-east-1": { - expected: "aws", - }, - "me-central-1": { - expected: "aws", - }, - "cn-north-1": { - expected: "aws-cn", - }, - "us-gov-west-1": { - expected: "aws-us-gov", - }, - } - - for region, testcase := range testcases { - got := endpoints.PartitionForRegion(region) - - if got != testcase.expected { - t.Errorf("expected Partition %q for Region %q, got %q", testcase.expected, region, got) - } - } -} diff --git a/validation/region.go b/validation/region.go index 4241a665..cd6aa651 100644 --- a/validation/region.go +++ b/validation/region.go @@ -5,8 +5,9 @@ package validation import ( "fmt" + "slices" - "github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints" + "github.com/hashicorp/aws-sdk-go-base/v2/endpoints" ) type InvalidRegionError struct { @@ -14,17 +15,16 @@ type InvalidRegionError struct { } func (e *InvalidRegionError) Error() string { - return fmt.Sprintf("Invalid AWS Region: %s", e.region) + return fmt.Sprintf("invalid AWS Region: %s", e.region) } // SupportedRegion checks if the given region is a valid AWS region. func SupportedRegion(region string) error { - for _, partition := range endpoints.Partitions() { - for _, partitionRegion := range partition.Regions() { - if region == partitionRegion { - return nil - } - } + if slices.ContainsFunc(endpoints.DefaultPartitions(), func(p endpoints.Partition) bool { + _, ok := p.Regions()[region] + return ok + }) { + return nil } return &InvalidRegionError{