From c52d96bc48f7119ee695d478c83d99360d917f56 Mon Sep 17 00:00:00 2001 From: jfgiroux Date: Tue, 16 Apr 2019 09:46:18 -0400 Subject: [PATCH] [Resolves #494] Add support for s3 in china regions (#668) Add support for s3 in china region --- sceptre/config/reader.py | 5 ++++- sceptre/template.py | 14 +++++++++++--- tests/test_config_reader.py | 21 ++++++++++++++++++--- tests/test_template.py | 18 ++++++++++++------ 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/sceptre/config/reader.py b/sceptre/config/reader.py index fb1aadb99..b78d0ad3f 100644 --- a/sceptre/config/reader.py +++ b/sceptre/config/reader.py @@ -405,13 +405,16 @@ def _collect_s3_details(stack_name, config): ) ]) + bucket_region = config.get("region", None) + if "template_key_prefix" in config: prefix = config["template_key_prefix"] template_key = "/".join([prefix.strip("/"), template_key]) s3_details = { "bucket_name": config["template_bucket_name"], - "bucket_key": template_key + "bucket_key": template_key, + "bucket_region": bucket_region } return s3_details diff --git a/sceptre/template.py b/sceptre/template.py index cabc3da19..32e6c2177 100644 --- a/sceptre/template.py +++ b/sceptre/template.py @@ -159,6 +159,7 @@ def upload_to_s3(self): # Remove any leading or trailing slashes the user may have added. bucket_name = self.s3_details["bucket_name"] bucket_key = self.s3_details["bucket_key"] + bucket_region = self.s3_details["bucket_region"] self.logger.debug( "%s - Uploading template to: 's3://%s/%s'", @@ -175,9 +176,16 @@ def upload_to_s3(self): } ) - url = "https://{0}.s3.amazonaws.com/{1}".format( - bucket_name, bucket_key - ) + china_regions = ["cn-north-1", "cn-northwest-1"] + + if bucket_region in china_regions: + url = "https://{0}.s3.{1}.amazonaws.com.cn/{2}".format( + bucket_name, bucket_region, bucket_key + ) + else: + url = "https://{0}.s3.amazonaws.com/{1}".format( + bucket_name, bucket_key + ) self.logger.debug("%s - Template URL: '%s'", self.name, url) diff --git a/tests/test_config_reader.py b/tests/test_config_reader.py index bafce7d83..bf8680172 100644 --- a/tests/test_config_reader.py +++ b/tests/test_config_reader.py @@ -168,23 +168,38 @@ def test_aborts_on_incompatible_version_requirement(self): "name", { "template_bucket_name": "bucket-name", - "template_key_prefix": "prefix" + "template_key_prefix": "prefix", + "region": "eu-west-1" }, { "bucket_name": "bucket-name", - "bucket_key": "prefix/name/2012-01-01-00-00-00-000000Z.json" + "bucket_key": "prefix/name/2012-01-01-00-00-00-000000Z.json", + "bucket_region": "eu-west-1", } ), ( "name", { "template_bucket_name": "bucket-name", + "region": "eu-west-1" }, { "bucket_name": "bucket-name", - "bucket_key": "name/2012-01-01-00-00-00-000000Z.json" + "bucket_key": "name/2012-01-01-00-00-00-000000Z.json", + "bucket_region": "eu-west-1", } ), + ( + "name", + { + "template_bucket_name": "bucket-name", + }, + { + "bucket_name": "bucket-name", + "bucket_key": "name/2012-01-01-00-00-00-000000Z.json", + "bucket_region": None, + } + ), ( "name", {}, None ) diff --git a/tests/test_template.py b/tests/test_template.py index f86437989..e286275c2 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -59,7 +59,8 @@ def test_upload_to_s3_with_valid_s3_details(self, mock_bucket_exists): mock_bucket_exists.return_value = True self.template.s3_details = { "bucket_name": "bucket-name", - "bucket_key": "bucket-key" + "bucket_key": "bucket-key", + "bucket_region": "eu-west-1" } self.template.upload_to_s3() @@ -80,7 +81,8 @@ def test_bucket_exists_with_bucket_that_exists(self): # behaviour when head_bucket successfully executes. self.template.s3_details = { "bucket_name": "bucket-name", - "bucket_key": "bucket-key" + "bucket_key": "bucket-key", + "bucket_region": "eu-west-1" } assert self.template._bucket_exists() is True @@ -89,7 +91,8 @@ def test_create_bucket_with_unreadable_bucket(self): self.template.connection_manager.region = "eu-west-1" self.template.s3_details = { "bucket_name": "bucket-name", - "bucket_key": "bucket-key" + "bucket_key": "bucket-key", + "bucket_region": "eu-west-1" } self.template.connection_manager.call.side_effect = ClientError( @@ -111,7 +114,8 @@ def test_bucket_exists_with_non_existent_bucket(self): # Not Found ClientError only for the first call. self.template.s3_details = { "bucket_name": "bucket-name", - "bucket_key": "bucket-key" + "bucket_key": "bucket-key", + "bucket_region": "eu-west-1" } self.template.connection_manager.call.side_effect = [ @@ -137,7 +141,8 @@ def test_create_bucket_in_us_east_1(self): self.template.connection_manager.region = "us-east-1" self.template.s3_details = { "bucket_name": "bucket-name", - "bucket_key": "bucket-key" + "bucket_key": "bucket-key", + "bucket_region": "us-east-1" } self.template._create_bucket() @@ -154,7 +159,8 @@ def test_get_boto_call_parameter_with_s3_details(self, mock_upload_to_s3): mock_upload_to_s3.return_value = sentinel.template_url self.template.s3_details = { "bucket_name": sentinel.bucket_name, - "bucket_key": sentinel.bucket_key + "bucket_key": sentinel.bucket_key, + "bucket_region": sentinel.bucket_region } boto_parameter = self.template.get_boto_call_parameter()