From 419f68831b23a2112741b554347beb098a585860 Mon Sep 17 00:00:00 2001 From: Antonio Osorio Date: Mon, 22 Jul 2019 16:08:43 -0700 Subject: [PATCH 1/4] Fixes s3 website endpoint for cn-northwest-1 --- aws/resource_aws_s3_bucket.go | 15 +++++++++++++-- aws/website_endpoint_url_test.go | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_s3_bucket.go b/aws/resource_aws_s3_bucket.go index 9fe56005699..1c0c3601762 100644 --- a/aws/resource_aws_s3_bucket.go +++ b/aws/resource_aws_s3_bucket.go @@ -1589,11 +1589,15 @@ func WebsiteEndpoint(bucket string, region string) *S3Website { func WebsiteDomainUrl(region string) string { region = normalizeRegion(region) - // New regions uses different syntax for website endpoints - // http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html + // Different regions have different syntax for website endpoints + // https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html + // https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints if isOldRegion(region) { return fmt.Sprintf("s3-website-%s.amazonaws.com", region) } + if isInChina(region) { + return fmt.Sprintf("s3-website.%s.amazonaws.com.cn", region) + } return fmt.Sprintf("s3-website.%s.amazonaws.com", region) } @@ -1617,6 +1621,13 @@ func isOldRegion(region string) bool { return false } +func isInChina(region string) bool { + if region == "cn-northwest-1" { + return true + } + return false +} + func resourceAwsS3BucketAclUpdate(s3conn *s3.S3, d *schema.ResourceData) error { acl := d.Get("acl").(string) bucket := d.Get("bucket").(string) diff --git a/aws/website_endpoint_url_test.go b/aws/website_endpoint_url_test.go index 24d2137b0e0..fece42042d6 100644 --- a/aws/website_endpoint_url_test.go +++ b/aws/website_endpoint_url_test.go @@ -19,6 +19,7 @@ var websiteEndpoints = []struct { {"ap-southeast-2", "bucket-name.s3-website-ap-southeast-2.amazonaws.com"}, {"ap-northeast-2", "bucket-name.s3-website.ap-northeast-2.amazonaws.com"}, {"sa-east-1", "bucket-name.s3-website-sa-east-1.amazonaws.com"}, + {"cn-northwest-1","bucket-name.s3-website.cn-northwest-1.amazonaws.com.cn"}, } func TestWebsiteEndpointUrl(t *testing.T) { From 082674fa4873156081daee16b778fa0e7e885fc9 Mon Sep 17 00:00:00 2001 From: Antonio Osorio Date: Mon, 22 Jul 2019 16:41:40 -0700 Subject: [PATCH 2/4] Adds other chinese regions supported in the sdk --- aws/resource_aws_s3_bucket.go | 14 ++++++++++---- aws/website_endpoint_url_test.go | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_s3_bucket.go b/aws/resource_aws_s3_bucket.go index 1c0c3601762..b3e34b0c0c2 100644 --- a/aws/resource_aws_s3_bucket.go +++ b/aws/resource_aws_s3_bucket.go @@ -1595,7 +1595,7 @@ func WebsiteDomainUrl(region string) string { if isOldRegion(region) { return fmt.Sprintf("s3-website-%s.amazonaws.com", region) } - if isInChina(region) { + if isChineseRegion(region) { return fmt.Sprintf("s3-website.%s.amazonaws.com.cn", region) } return fmt.Sprintf("s3-website.%s.amazonaws.com", region) @@ -1621,9 +1621,15 @@ func isOldRegion(region string) bool { return false } -func isInChina(region string) bool { - if region == "cn-northwest-1" { - return true +func isChineseRegion(region string) bool { + chineseRegions := []string{ + "cn-north-1", + "cn-northwest-1", + } + for _, r := range chineseRegions { + if region == r { + return true + } } return false } diff --git a/aws/website_endpoint_url_test.go b/aws/website_endpoint_url_test.go index fece42042d6..09f736f05a8 100644 --- a/aws/website_endpoint_url_test.go +++ b/aws/website_endpoint_url_test.go @@ -20,6 +20,7 @@ var websiteEndpoints = []struct { {"ap-northeast-2", "bucket-name.s3-website.ap-northeast-2.amazonaws.com"}, {"sa-east-1", "bucket-name.s3-website-sa-east-1.amazonaws.com"}, {"cn-northwest-1","bucket-name.s3-website.cn-northwest-1.amazonaws.com.cn"}, + {"cn-north-1", "bucket-name.s3-website.cn-north-1.amazonaws.com.cn"}, } func TestWebsiteEndpointUrl(t *testing.T) { From d15a42a724808908f94a5a49b0881d19cf31cc08 Mon Sep 17 00:00:00 2001 From: Antonio Osorio Date: Mon, 22 Jul 2019 17:22:44 -0700 Subject: [PATCH 3/4] Fix formatting issues --- aws/website_endpoint_url_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/website_endpoint_url_test.go b/aws/website_endpoint_url_test.go index 09f736f05a8..06cfd3a58b1 100644 --- a/aws/website_endpoint_url_test.go +++ b/aws/website_endpoint_url_test.go @@ -19,7 +19,7 @@ var websiteEndpoints = []struct { {"ap-southeast-2", "bucket-name.s3-website-ap-southeast-2.amazonaws.com"}, {"ap-northeast-2", "bucket-name.s3-website.ap-northeast-2.amazonaws.com"}, {"sa-east-1", "bucket-name.s3-website-sa-east-1.amazonaws.com"}, - {"cn-northwest-1","bucket-name.s3-website.cn-northwest-1.amazonaws.com.cn"}, + {"cn-northwest-1", "bucket-name.s3-website.cn-northwest-1.amazonaws.com.cn"}, {"cn-north-1", "bucket-name.s3-website.cn-north-1.amazonaws.com.cn"}, } From b5042c060448b003d6e7f82edc658ff206746a2f Mon Sep 17 00:00:00 2001 From: Antonio Osorio Date: Tue, 23 Jul 2019 14:56:52 -0700 Subject: [PATCH 4/4] Uses the endpoints module in the aws sdk to verify if the the regions is a Chinese region --- aws/resource_aws_s3_bucket.go | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/aws/resource_aws_s3_bucket.go b/aws/resource_aws_s3_bucket.go index b3e34b0c0c2..b554d3d1b34 100644 --- a/aws/resource_aws_s3_bucket.go +++ b/aws/resource_aws_s3_bucket.go @@ -1595,7 +1595,7 @@ func WebsiteDomainUrl(region string) string { if isOldRegion(region) { return fmt.Sprintf("s3-website-%s.amazonaws.com", region) } - if isChineseRegion(region) { + if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok && partition.ID() == endpoints.AwsCnPartitionID { return fmt.Sprintf("s3-website.%s.amazonaws.com.cn", region) } return fmt.Sprintf("s3-website.%s.amazonaws.com", region) @@ -1621,19 +1621,6 @@ func isOldRegion(region string) bool { return false } -func isChineseRegion(region string) bool { - chineseRegions := []string{ - "cn-north-1", - "cn-northwest-1", - } - for _, r := range chineseRegions { - if region == r { - return true - } - } - return false -} - func resourceAwsS3BucketAclUpdate(s3conn *s3.S3, d *schema.ResourceData) error { acl := d.Get("acl").(string) bucket := d.Get("bucket").(string)