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

provider: Automatically validate new regions from AWS SDK #3159

Merged
merged 2 commits into from
Jan 30, 2018
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
7 changes: 3 additions & 4 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ into Terraform.

#### New Region

Implementing a new region gives Terraform the ability to connect and interact
with endpoints in a new geographic area. While baseline region support is fairly
trivial to add, new regions are generally limited in which services they support.
While region validation is automatically added with SDK updates, new regions
are generally limited in which services they support. Below are some
manually sourced values from documentation.

- [ ] Add region to `aws/config.go`
- [ ] Check [Regions and Endpoints ELB regions](https://docs.aws.amazon.com/general/latest/gr/rande.html#elb_region) and add Route53 Hosted Zone ID if available to `aws/data_source_aws_elb_hosted_zone_id.go`
- [ ] Check [Regions and Endpoints S3 website endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints) and add Route53 Hosted Zone ID if available to `aws/hosted_zones.go`
- [ ] Check [CloudTrail Supported Regions docs](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-supported-regions.html) and add AWS Account ID if available to `aws/data_source_aws_cloudtrail_service_account.go`
Expand Down
46 changes: 11 additions & 35 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/acm"
Expand Down Expand Up @@ -216,20 +217,13 @@ func (c *AWSClient) DynamoDB() *dynamodb.DynamoDB {
}

func (c *AWSClient) IsGovCloud() bool {
if c.region == "us-gov-west-1" {
return true
}
return false
_, isGovCloud := endpoints.PartitionForRegion([]endpoints.Partition{endpoints.AwsUsGovPartition()}, c.region)
return isGovCloud
}

func (c *AWSClient) IsChinaCloud() bool {
if c.region == "cn-north-1" {
return true
}
if c.region == "cn-northwest-1" {
return true
}
return false
_, isChinaCloud := endpoints.PartitionForRegion([]endpoints.Partition{endpoints.AwsCnPartition()}, c.region)
return isChinaCloud
}

// Client configures and returns a fully initialized AWSClient
Expand Down Expand Up @@ -501,32 +495,14 @@ func hasEc2Classic(platforms []string) bool {
// ValidateRegion returns an error if the configured region is not a
// valid aws region and nil otherwise.
func (c *Config) ValidateRegion() error {
var regions = []string{
"ap-northeast-1",
"ap-northeast-2",
"ap-south-1",
"ap-southeast-1",
"ap-southeast-2",
"ca-central-1",
"cn-north-1",
"cn-northwest-1",
"eu-central-1",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-gov-west-1",
"us-west-1",
"us-west-2",
}

for _, valid := range regions {
if c.Region == valid {
return nil
for _, partition := range endpoints.DefaultPartitions() {
for _, region := range partition.Regions() {
if c.Region == region.ID() {
return nil
}
}
}

return fmt.Errorf("Not a valid region: %s", c.Region)
}

Expand Down
8 changes: 5 additions & 3 deletions aws/data_source_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func bucketLocation(d *schema.ResourceData, bucket string, conn *s3.S3) error {
return err
}

hostedZoneID := HostedZoneIDForRegion(region)
if err := d.Set("hosted_zone_id", hostedZoneID); err != nil {
return err
hostedZoneID, err := HostedZoneIDForRegion(region)
if err != nil {
log.Printf("[WARN] %s", err)
} else {
d.Set("hosted_zone_id", hostedZoneID)
}

_, websiteErr := conn.GetBucketWebsite(
Expand Down
3 changes: 2 additions & 1 deletion aws/data_source_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestAccDataSourceS3Bucket_basic(t *testing.T) {
rInt := acctest.RandInt()
arnRegexp := regexp.MustCompile(
"^arn:aws:s3:::")
hostedZoneID, _ := HostedZoneIDForRegion("us-west-2")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -27,7 +28,7 @@ func TestAccDataSourceS3Bucket_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"data.aws_s3_bucket.bucket", "bucket_domain_name", testAccBucketDomainName(rInt)),
resource.TestCheckResourceAttr(
"data.aws_s3_bucket.bucket", "hosted_zone_id", HostedZoneIDForRegion("us-west-2")),
"data.aws_s3_bucket.bucket", "hosted_zone_id", hostedZoneID),
resource.TestCheckNoResourceAttr("data.aws_s3_bucket.bucket", "website_endpoint"),
),
},
Expand Down
9 changes: 7 additions & 2 deletions aws/hosted_zones.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aws

import "fmt"

// This list is copied from
// http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints
// It currently cannot be generated from the API json.
Expand All @@ -24,6 +26,9 @@ var hostedZoneIDsMap = map[string]string{

// Returns the hosted zone ID for an S3 website endpoint region. This can be
// used as input to the aws_route53_record resource's zone_id argument.
func HostedZoneIDForRegion(region string) string {
return hostedZoneIDsMap[region]
func HostedZoneIDForRegion(region string) (string, error) {
if v, ok := hostedZoneIDsMap[region]; ok {
return v, nil
}
return "", fmt.Errorf("S3 hosted zone ID not found for region: %s", region)
}
8 changes: 4 additions & 4 deletions aws/hosted_zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

func TestHostedZoneIDForRegion(t *testing.T) {
if r := HostedZoneIDForRegion("us-east-1"); r != "Z3AQBSTGFYJSTF" {
if r, _ := HostedZoneIDForRegion("us-east-1"); r != "Z3AQBSTGFYJSTF" {
t.Fatalf("bad: %s", r)
}
if r := HostedZoneIDForRegion("ap-southeast-2"); r != "Z1WCIGYICN2BYD" {
if r, _ := HostedZoneIDForRegion("ap-southeast-2"); r != "Z1WCIGYICN2BYD" {
t.Fatalf("bad: %s", r)
}

// Bad input should be empty string
if r := HostedZoneIDForRegion("not-a-region"); r != "" {
// Bad input should be error
if r, err := HostedZoneIDForRegion("not-a-region"); err == nil {
t.Fatalf("bad: %s", r)
}
}
8 changes: 5 additions & 3 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,9 +1033,11 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

// Add the hosted zone ID for this bucket's region as an attribute
hostedZoneID := HostedZoneIDForRegion(region)
if err := d.Set("hosted_zone_id", hostedZoneID); err != nil {
return err
hostedZoneID, err := HostedZoneIDForRegion(region)
if err != nil {
log.Printf("[WARN] %s", err)
} else {
d.Set("hosted_zone_id", hostedZoneID)
}

// Add website_endpoint as an attribute
Expand Down
3 changes: 2 additions & 1 deletion aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAccAWSS3Bucket_basic(t *testing.T) {
rInt := acctest.RandInt()
arnRegexp := regexp.MustCompile(
"^arn:aws:s3:::")
hostedZoneID, _ := HostedZoneIDForRegion("us-west-2")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -41,7 +42,7 @@ func TestAccAWSS3Bucket_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "hosted_zone_id", HostedZoneIDForRegion("us-west-2")),
"aws_s3_bucket.bucket", "hosted_zone_id", hostedZoneID),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "region", "us-west-2"),
resource.TestCheckNoResourceAttr(
Expand Down