Skip to content

Commit 11f5f9c

Browse files
authored
Merge pull request #1176 from hashicorp/f-generate-partitions-regions
Add top-level `endpoints` package containing AWS partition and Region metadata
2 parents 37e6491 + 95086f4 commit 11f5f9c

File tree

14 files changed

+924
-176
lines changed

14 files changed

+924
-176
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<!-- markdownlint-disable single-title -->
22
# v2.0.0 (Unreleased)
33

4+
ENHANCEMENTS
5+
6+
* Adds top-level `endpoints` package containing AWS partition and Region metadata ([#1176](https://github.com/hashicorp/aws-sdk-go-base/pull/1176))
7+
48
# v2.0.0-beta.56 (2024-09-11)
59

610
ENHANCEMENTS

aws_config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
2525
"github.com/aws/smithy-go/middleware"
2626
"github.com/hashicorp/aws-sdk-go-base/v2/diag"
27+
"github.com/hashicorp/aws-sdk-go-base/v2/endpoints"
2728
"github.com/hashicorp/aws-sdk-go-base/v2/internal/awsconfig"
2829
"github.com/hashicorp/aws-sdk-go-base/v2/internal/constants"
29-
"github.com/hashicorp/aws-sdk-go-base/v2/internal/endpoints"
3030
"github.com/hashicorp/aws-sdk-go-base/v2/logging"
3131
"github.com/hashicorp/terraform-plugin-log/tflog"
3232
)
@@ -337,7 +337,9 @@ func GetAwsAccountIDAndPartition(ctx context.Context, awsConfig aws.Config, c *C
337337
"Errors: %w", err))
338338
}
339339

340-
return "", endpoints.PartitionForRegion(awsConfig.Region), nil
340+
partition, _ := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), awsConfig.Region)
341+
342+
return "", partition.ID(), nil
341343
}
342344

343345
func commonLoadOptions(ctx context.Context, c *Config) ([]func(*config.LoadOptions) error, error) {

endpoints/endpoints.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package endpoints
5+
6+
const (
7+
AwsGlobalRegionID = "aws-global" // AWS Standard global region.
8+
)

endpoints/endpoints_gen.go

Lines changed: 282 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

endpoints/generate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
//go:generate go run ../internal/generate/endpoints/main.go -- https://raw.githubusercontent.com/aws/aws-sdk-go-v2/main/codegen/smithy-aws-go-codegen/src/main/resources/software/amazon/smithy/aws/go/codegen/endpoints.json
5+
6+
package endpoints

endpoints/partition.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package endpoints
5+
6+
import (
7+
"maps"
8+
"regexp"
9+
)
10+
11+
// Partition represents an AWS partition.
12+
// See https://docs.aws.amazon.com/whitepapers/latest/aws-fault-isolation-boundaries/partitions.html.
13+
type Partition struct {
14+
id string
15+
name string
16+
dnsSuffix string
17+
regionRegex *regexp.Regexp
18+
regions map[string]Region
19+
}
20+
21+
// ID returns the identifier of the partition.
22+
func (p Partition) ID() string {
23+
return p.id
24+
}
25+
26+
// Name returns the name of the partition.
27+
func (p Partition) Name() string {
28+
return p.name
29+
}
30+
31+
// DNSSuffix returns the base domain name of the partition.
32+
func (p Partition) DNSSuffix() string {
33+
return p.dnsSuffix
34+
}
35+
36+
// RegionRegex return the regular expression that matches Region IDs for the partition.
37+
func (p Partition) RegionRegex() *regexp.Regexp {
38+
return p.regionRegex
39+
}
40+
41+
// Regions returns a map of Regions for the partition, indexed by their ID.
42+
func (p Partition) Regions() map[string]Region {
43+
return maps.Clone(p.regions)
44+
}
45+
46+
// DefaultPartitions returns a list of the partitions.
47+
func DefaultPartitions() []Partition {
48+
ps := make([]Partition, 0, len(partitions))
49+
50+
for _, p := range partitions {
51+
ps = append(ps, p)
52+
}
53+
54+
return ps
55+
}
56+
57+
// PartitionForRegion returns the first partition which includes the specific Region.
58+
func PartitionForRegion(ps []Partition, regionID string) (Partition, bool) {
59+
for _, p := range ps {
60+
if _, ok := p.regions[regionID]; ok || p.regionRegex.MatchString(regionID) {
61+
return p, true
62+
}
63+
}
64+
65+
return Partition{}, false
66+
}

0 commit comments

Comments
 (0)