Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add top-level endpoints package containing AWS partition and Region metadata #1176

Merged
merged 16 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<!-- markdownlint-disable single-title -->
# v2.0.0 (Unreleased)

ENHANCEMENTS

* Adds top-level `endpoints` package containing AWS partition and Region metadata ([#1176](https://github.com/hashicorp/aws-sdk-go-base/pull/1176))

# v2.0.0-beta.56 (2024-09-11)

ENHANCEMENTS
Expand Down
6 changes: 4 additions & 2 deletions aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions endpoints/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package endpoints

const (
AwsGlobalRegionID = "aws-global" // AWS Standard global region.
)
282 changes: 282 additions & 0 deletions endpoints/endpoints_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions endpoints/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//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

package endpoints
66 changes: 66 additions & 0 deletions endpoints/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package endpoints

import (
"maps"
"regexp"
)

// Partition represents an AWS partition.
// See https://docs.aws.amazon.com/whitepapers/latest/aws-fault-isolation-boundaries/partitions.html.
type Partition struct {
id string
name string
dnsSuffix string
regionRegex *regexp.Regexp
regions map[string]Region
}

// ID returns the identifier of the partition.
func (p Partition) ID() string {
return p.id
}

// Name returns the name of the partition.
func (p Partition) Name() string {
return p.name
}

// DNSSuffix returns the base domain name of the partition.
func (p Partition) DNSSuffix() string {
return p.dnsSuffix
}

// RegionRegex return the regular expression that matches Region IDs for the partition.
func (p Partition) RegionRegex() *regexp.Regexp {
return p.regionRegex
}

// Regions returns a map of Regions for the partition, indexed by their ID.
func (p Partition) Regions() map[string]Region {
return maps.Clone(p.regions)
}

// DefaultPartitions returns a list of the partitions.
func DefaultPartitions() []Partition {
ps := make([]Partition, 0, len(partitions))

for _, p := range partitions {
ps = append(ps, p)
}

return ps
}

// PartitionForRegion returns the first partition which includes the specific Region.
func PartitionForRegion(ps []Partition, regionID string) (Partition, bool) {
for _, p := range ps {
if _, ok := p.regions[regionID]; ok || p.regionRegex.MatchString(regionID) {
return p, true
}
}

return Partition{}, false
}
Loading