Skip to content

Commit

Permalink
[Resolves Sceptre#494] Add support for s3 in china regions (Sceptre#668)
Browse files Browse the repository at this point in the history
Add support for s3 in china region
  • Loading branch information
jfgiroux authored and thawkson committed Feb 6, 2021
1 parent b13fda7 commit c52d96b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 4 additions & 1 deletion sceptre/config/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions sceptre/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand All @@ -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)

Expand Down
21 changes: 18 additions & 3 deletions tests/test_config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
18 changes: 12 additions & 6 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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 = [
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit c52d96b

Please sign in to comment.