Skip to content

Commit

Permalink
Add S3 Transfer Acceleration to AWS::S3::Bucket (#833)
Browse files Browse the repository at this point in the history
* Add S3 Transfer Acceleration to AWS::S3::Bucket

* Fix style issues

* Add tests for AccelerateConfiguration

* Use a custom validator to validate s3 transfer acceleration status values
  • Loading branch information
danielpops authored and phobologic committed Oct 2, 2017
1 parent 334ca16 commit 4bdf6c7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/S3_Bucket_With_AccelerateConfiguration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Converted from S3_Bucket.template located at:
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/

from troposphere import Output, Ref, Template
from troposphere.s3 import Bucket, PublicRead, AccelerateConfiguration

t = Template()

t.add_description(
"AWS CloudFormation Sample Template S3_Bucket: Sample template showing :"
"How to create a publicly accessible S3 bucket. "
"How to enable S3 Transfer Acceleration. "
"**WARNING** This template creates an Amazon S3 Bucket. "
"You will be billed for the AWS resources used if you create "
"a stack from this template.")

s3bucket = t.add_resource(Bucket(
"S3Bucket",
# Make public Read
AccessControl=PublicRead,
# Enable s3 Transfer Acceleration
AccelerateConfiguration=AccelerateConfiguration(
AccelerationStatus="Enabled",
),
))

t.add_output(Output(
"BucketName",
Value=Ref(s3bucket),
Description="Name of S3 bucket with s3 transfer acceleration enabled",
))

print(t.to_json())
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing :How to create a publicly accessible S3 bucket. How to enable S3 Transfer Acceleration. **WARNING** This template creates an Amazon S3 Bucket. You will be billed for the AWS resources used if you create a stack from this template.",
"Outputs": {
"BucketName": {
"Description": "Name of S3 bucket with s3 transfer acceleration enabled",
"Value": {
"Ref": "S3Bucket"
}
}
},
"Resources": {
"S3Bucket": {
"Properties": {
"AccelerateConfiguration": {
"AccelerationStatus": "Enabled"
},
"AccessControl": "PublicRead"
},
"Type": "AWS::S3::Bucket"
}
}
}
29 changes: 28 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from troposphere import If, Join, Ref, Split, Sub, Template
from troposphere import depends_on_helper
from troposphere.ec2 import Instance, Route, SecurityGroupRule
from troposphere.s3 import Bucket
from troposphere.s3 import Bucket, AccelerateConfiguration
from troposphere.elasticloadbalancing import HealthCheck
from troposphere.validators import positive_integer

Expand Down Expand Up @@ -63,6 +63,33 @@ def test_resource_depends_on_attr(self):
self.assertEqual(b1.title, b2.DependsOn)


class TestS3AccelerateConfiguration(unittest.TestCase):
def test_accelerate_configuration_enabled(self):
ac = AccelerateConfiguration(
AccelerationStatus='Enabled',
)
self.assertEqual('Enabled', ac.AccelerationStatus)

def test_accelerate_configuration_suspended(self):
ac = AccelerateConfiguration(
AccelerationStatus='Suspended',
)
self.assertEqual('Suspended', ac.AccelerationStatus)

def test_accelerate_configuration_invalid_value(self):
with self.assertRaises(ValueError):
AccelerateConfiguration(AccelerationStatus='Invalid Value')

def test_s3_bucket_accelerate_configuration(self):
t = Template()
ac = AccelerateConfiguration(AccelerationStatus="Enabled")

b = Bucket("s3Bucket", AccelerateConfiguration=ac)
t.add_resource(b)
output = t.to_json()
self.assertIn('"AccelerationStatus": "Enabled"', output)


def call_correct(x):
return x

Expand Down
8 changes: 8 additions & 0 deletions troposphere/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from . import AWSObject, AWSProperty, Tags
from .validators import positive_integer, s3_bucket_name
from .validators import s3_transfer_acceleration_status

try:
from awacs.aws import Policy
Expand Down Expand Up @@ -46,6 +47,12 @@ class VersioningConfiguration(AWSProperty):
}


class AccelerateConfiguration(AWSProperty):
props = {
'AccelerationStatus': (s3_transfer_acceleration_status, True),
}


class RedirectAllRequestsTo(AWSProperty):
props = {
'HostName': (basestring, True),
Expand Down Expand Up @@ -269,6 +276,7 @@ class Bucket(AWSObject):

props = {
'AccessControl': (basestring, False),
'AccelerateConfiguration': (AccelerateConfiguration, False),
'BucketName': (s3_bucket_name, False),
'CorsConfiguration': (CorsConfiguration, False),
'LifecycleConfiguration': (LifecycleConfiguration, False),
Expand Down
11 changes: 11 additions & 0 deletions troposphere/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ def status(status):
return status


def s3_transfer_acceleration_status(value):
valid_status = ['Enabled', 'Suspended']
if value not in valid_status:
raise ValueError(
'AccelerationStatus must be one of: "%s"' % (
', '.join(valid_status)
)
)
return value


def iam_names(b):
iam_name_re = compile(r'^[a-zA-Z0-9_\.\+\=\@\-\,]+$')
if iam_name_re.match(b):
Expand Down

0 comments on commit 4bdf6c7

Please sign in to comment.