Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
support int/str types for DynamoDB GlobalSecondaryIndex in CF templat…
Browse files Browse the repository at this point in the history
  • Loading branch information
monty16597 authored Aug 15, 2020
1 parent 942196f commit ecac835
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
26 changes: 24 additions & 2 deletions localstack/utils/cloudformation/template_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ def replace(params, **kwargs):
return replace


def get_ddb_provisioned_throughput(params, **kwargs):
args = params.get('ProvisionedThroughput')
if args:
if isinstance(args['ReadCapacityUnits'], str):
args['ReadCapacityUnits'] = int(args['ReadCapacityUnits'])
if isinstance(args['WriteCapacityUnits'], str):
args['WriteCapacityUnits'] = int(args['WriteCapacityUnits'])
return args


def get_ddb_global_sec_indexes(params, **kwargs):
args = params.get('GlobalSecondaryIndexes')
if args:
for index in args:
provisoned_throughput = index['ProvisionedThroughput']
if isinstance(provisoned_throughput['ReadCapacityUnits'], str):
provisoned_throughput['ReadCapacityUnits'] = int(provisoned_throughput['ReadCapacityUnits'])
if isinstance(provisoned_throughput['WriteCapacityUnits'], str):
provisoned_throughput['WriteCapacityUnits'] = int(provisoned_throughput['WriteCapacityUnits'])
return args


# maps resource types to functions and parameters for creation
RESOURCE_TO_FUNCTION = {
'S3::Bucket': {
Expand Down Expand Up @@ -406,9 +428,9 @@ def replace(params, **kwargs):
'TableName': 'TableName',
'AttributeDefinitions': 'AttributeDefinitions',
'KeySchema': 'KeySchema',
'ProvisionedThroughput': 'ProvisionedThroughput',
'ProvisionedThroughput': get_ddb_provisioned_throughput,
'LocalSecondaryIndexes': 'LocalSecondaryIndexes',
'GlobalSecondaryIndexes': 'GlobalSecondaryIndexes',
'GlobalSecondaryIndexes': get_ddb_global_sec_indexes,
'StreamSpecification': lambda params, **kwargs: (
common.merge_dicts(params.get('StreamSpecification'), {'StreamEnabled': True}, default=None))
},
Expand Down
46 changes: 42 additions & 4 deletions tests/integration/test_cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ def handler(event, context):
- AttributeName: startTime
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
StreamSpecification:
StreamViewType: NEW_IMAGE
GlobalSecondaryIndexes:
Expand All @@ -571,8 +571,8 @@ def handler(event, context):
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
Outputs:
Name:
Value:
Expand Down Expand Up @@ -1736,3 +1736,41 @@ def test_delete_stack_across_regions(self):
)

cloudformation.delete_stack(StackName='myteststack')

def test_globalindex_read_write_provisioned_throughput_dynamodb_table(self):
cf_client = aws_stack.connect_to_service('cloudformation')
ddb_client = aws_stack.connect_to_service('dynamodb')
stack_name = 'test_dynamodb'

response = cf_client.create_stack(
StackName=stack_name,
TemplateBody=TEST_DEPLOY_BODY_3,
Parameters=[
{
'ParameterKey': 'tableName',
'ParameterValue': 'dynamodb'
},
{
'ParameterKey': 'env',
'ParameterValue': 'test'
}
]
)
self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200)
response = ddb_client.describe_table(
TableName='dynamodb-test'
)

if response['Table']['ProvisionedThroughput']:
throughput = response['Table']['ProvisionedThroughput']
self.assertTrue(isinstance(throughput['ReadCapacityUnits'], int))
self.assertTrue(isinstance(throughput['WriteCapacityUnits'], int))

for global_index in response['Table']['GlobalSecondaryIndexes']:
index_provisioned = global_index['ProvisionedThroughput']
test_read_capacity = index_provisioned['ReadCapacityUnits']
test_write_capacity = index_provisioned['WriteCapacityUnits']

self.assertTrue(isinstance(test_read_capacity, int))
self.assertTrue(isinstance(test_write_capacity, int))
cf_client.delete_stack(StackName=stack_name)

0 comments on commit ecac835

Please sign in to comment.