Skip to content

Commit

Permalink
Merge pull request #275 from sbkok/fix/gen-params-intrinsic-upload
Browse files Browse the repository at this point in the history
Fix supported list of intrinsic upload path styles
  • Loading branch information
sbkok authored Sep 25, 2020
2 parents db9603c + df6da3f commit ebd29c8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def __init__(self, region, bucket):
self.resource = boto3.resource('s3', region_name=region)
self.bucket = bucket

@staticmethod
def supported_path_styles():
"""
Fetch the list of supported path styles.
Returns:
list(str): The list of supported path styles.
"""
return [
'path',
's3-key-only',
's3-uri',
's3-url',
'virtual-hosted',
]

def build_pathing_style(self, style, key):
if style == 's3-url':
return "s3://{bucket}/{key}".format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
from mock import Mock, patch, mock_open
from s3 import S3


@fixture
def us_east_1_cls():
return S3(
'us-east-1',
'some_bucket'
)


@fixture
def eu_west_1_cls():
cls = S3(
Expand All @@ -24,6 +26,27 @@ def eu_west_1_cls():
)
return cls


def test_supported_path_styles_path():
assert 'path' in S3.supported_path_styles()


def test_supported_path_styles_s3_key_only():
assert 's3-key-only' in S3.supported_path_styles()


def test_supported_path_styles_s3_uri():
assert 's3-uri' in S3.supported_path_styles()


def test_supported_path_styles_s3_url():
assert 's3-url' in S3.supported_path_styles()


def test_supported_path_styles_virtual_hosted():
assert 'virtual-hosted' in S3.supported_path_styles()


def test_build_pathing_style_s3_url_us_east_1(us_east_1_cls):
key = 'some/key'
assert us_east_1_cls.build_pathing_style('s3-url', key) == \
Expand All @@ -32,6 +55,7 @@ def test_build_pathing_style_s3_url_us_east_1(us_east_1_cls):
key=key,
)


def test_build_pathing_style_s3_url_any_other_region(eu_west_1_cls):
key = 'some/key'
assert eu_west_1_cls.build_pathing_style('s3-url', key) == \
Expand All @@ -40,6 +64,7 @@ def test_build_pathing_style_s3_url_any_other_region(eu_west_1_cls):
key=key,
)


def test_build_pathing_style_s3_uri_us_east_1(us_east_1_cls):
key = 'some/key'
assert us_east_1_cls.build_pathing_style('s3-uri', key) == \
Expand All @@ -48,6 +73,7 @@ def test_build_pathing_style_s3_uri_us_east_1(us_east_1_cls):
key=key,
)


def test_build_pathing_style_s3_uri_any_other_region(eu_west_1_cls):
key = 'some/key'
assert eu_west_1_cls.build_pathing_style('s3-uri', key) == \
Expand All @@ -56,20 +82,23 @@ def test_build_pathing_style_s3_uri_any_other_region(eu_west_1_cls):
key=key,
)


def test_build_pathing_style_s3_key_only_us_east_1(us_east_1_cls):
key = 'some/key'
assert us_east_1_cls.build_pathing_style('s3-key-only', key) == \
"{key}".format(
key=key,
)


def test_build_pathing_style_s3_key_only_any_other_region(eu_west_1_cls):
key = 'some/key'
assert eu_west_1_cls.build_pathing_style('s3-key-only', key) == \
"{key}".format(
key=key,
)


def test_build_pathing_style_path_us_east_1(us_east_1_cls):
key = 'some/key'
assert us_east_1_cls.build_pathing_style('path', key) == \
Expand All @@ -78,6 +107,7 @@ def test_build_pathing_style_path_us_east_1(us_east_1_cls):
key=key,
)


def test_build_pathing_style_path_any_other_region(eu_west_1_cls):
key = 'some/key'
assert eu_west_1_cls.build_pathing_style('path', key) == \
Expand All @@ -87,6 +117,7 @@ def test_build_pathing_style_path_any_other_region(eu_west_1_cls):
key=key,
)


def test_build_pathing_style_virtual_hosted_us_east_1(us_east_1_cls):
key = 'some/key'
assert us_east_1_cls.build_pathing_style('virtual-hosted', key) == \
Expand All @@ -95,6 +126,7 @@ def test_build_pathing_style_virtual_hosted_us_east_1(us_east_1_cls):
key=key,
)


def test_build_pathing_style_virtual_hosted_any_other_region(eu_west_1_cls):
key = 'some/key'
assert eu_west_1_cls.build_pathing_style('virtual-hosted', key) == \
Expand All @@ -104,6 +136,7 @@ def test_build_pathing_style_virtual_hosted_any_other_region(eu_west_1_cls):
key=key,
)


def test_build_pathing_style_unknown_style(us_east_1_cls):
key = 'some/key'
style = 'unknown'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ def fetch_stack_output(self, value, key, optional=False): # pylint: disable=too-
return True

def upload(self, value, key, file_name):
if not any(item in value for item in ['path', 'virtual-hosted', 's3-key-only']):
if not any(item in value for item in S3.supported_path_styles()):
raise Exception(
'When uploading to S3 you need to specify a '
'pathing style for the response either path or virtual-hosted, '
'read more: https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html'
'When uploading to S3 you need to specify a path style'
'to use for the returned value to be used. '
'Supported path styles include: {supported_list}'.format(
supported_list=S3.supported_path_styles(),
)
) from None
if str(value).count(':') > 2:
[_, region, style, value] = value.split(':')
Expand Down

0 comments on commit ebd29c8

Please sign in to comment.