#2445 - DynamoDB - Add validation for AttributeDefinitions
This commit is contained in:
parent
d4aa55760c
commit
6005b19ac2
@ -1,4 +1,5 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
import itertools
|
||||||
import json
|
import json
|
||||||
import six
|
import six
|
||||||
import re
|
import re
|
||||||
@ -113,6 +114,21 @@ class DynamoHandler(BaseResponse):
|
|||||||
# getting the indexes
|
# getting the indexes
|
||||||
global_indexes = body.get("GlobalSecondaryIndexes", [])
|
global_indexes = body.get("GlobalSecondaryIndexes", [])
|
||||||
local_secondary_indexes = body.get("LocalSecondaryIndexes", [])
|
local_secondary_indexes = body.get("LocalSecondaryIndexes", [])
|
||||||
|
# Verify AttributeDefinitions list all
|
||||||
|
expected_attrs = []
|
||||||
|
expected_attrs.extend([key['AttributeName'] for key in key_schema])
|
||||||
|
expected_attrs.extend(schema['AttributeName'] for schema in itertools.chain(*list(idx['KeySchema'] for idx in local_secondary_indexes)))
|
||||||
|
expected_attrs.extend(schema['AttributeName'] for schema in itertools.chain(*list(idx['KeySchema'] for idx in global_indexes)))
|
||||||
|
expected_attrs = list(set(expected_attrs))
|
||||||
|
expected_attrs.sort()
|
||||||
|
actual_attrs = [item['AttributeName'] for item in attr]
|
||||||
|
actual_attrs.sort()
|
||||||
|
if actual_attrs != expected_attrs:
|
||||||
|
er = 'com.amazonaws.dynamodb.v20111205#ValidationException'
|
||||||
|
return self.error(er,
|
||||||
|
'One or more parameter values were invalid: '
|
||||||
|
'Some index key attributes are not defined in AttributeDefinitions. '
|
||||||
|
'Keys: ' + str(expected_attrs) + ', AttributeDefinitions: ' + str(actual_attrs))
|
||||||
# get the stream specification
|
# get the stream specification
|
||||||
streams = body.get("StreamSpecification")
|
streams = body.get("StreamSpecification")
|
||||||
|
|
||||||
|
@ -1629,15 +1629,7 @@ def test_query_global_secondary_index_when_created_via_update_table_resource():
|
|||||||
{
|
{
|
||||||
'AttributeName': 'user_id',
|
'AttributeName': 'user_id',
|
||||||
'AttributeType': 'N',
|
'AttributeType': 'N',
|
||||||
},
|
}
|
||||||
{
|
|
||||||
'AttributeName': 'forum_name',
|
|
||||||
'AttributeType': 'S'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'AttributeName': 'subject',
|
|
||||||
'AttributeType': 'S'
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
ProvisionedThroughput={
|
ProvisionedThroughput={
|
||||||
'ReadCapacityUnits': 5,
|
'ReadCapacityUnits': 5,
|
||||||
@ -2181,6 +2173,34 @@ def test_batch_items_should_throw_exception_for_duplicate_request():
|
|||||||
ex.exception.response['Error']['Message'].should.equal('Provided list of item keys contains duplicates')
|
ex.exception.response['Error']['Message'].should.equal('Provided list of item keys contains duplicates')
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb2
|
||||||
|
def test_index_with_unknown_attributes_should_fail():
|
||||||
|
dynamodb = boto3.client('dynamodb', region_name='us-east-1')
|
||||||
|
|
||||||
|
expected_exception = 'Some index key attributes are not defined in AttributeDefinitions.'
|
||||||
|
|
||||||
|
with assert_raises(ClientError) as ex:
|
||||||
|
dynamodb.create_table(
|
||||||
|
AttributeDefinitions=[
|
||||||
|
{'AttributeName': 'customer_nr', 'AttributeType': 'S'},
|
||||||
|
{'AttributeName': 'last_name', 'AttributeType': 'S'}],
|
||||||
|
TableName='table_with_missing_attribute_definitions',
|
||||||
|
KeySchema=[
|
||||||
|
{'AttributeName': 'customer_nr', 'KeyType': 'HASH'},
|
||||||
|
{'AttributeName': 'last_name', 'KeyType': 'RANGE'}],
|
||||||
|
LocalSecondaryIndexes=[{
|
||||||
|
'IndexName': 'indexthataddsanadditionalattribute',
|
||||||
|
'KeySchema': [
|
||||||
|
{'AttributeName': 'customer_nr', 'KeyType': 'HASH'},
|
||||||
|
{'AttributeName': 'postcode', 'KeyType': 'RANGE'}],
|
||||||
|
'Projection': { 'ProjectionType': 'ALL' }
|
||||||
|
}],
|
||||||
|
BillingMode='PAY_PER_REQUEST')
|
||||||
|
|
||||||
|
ex.exception.response['Error']['Code'].should.equal('ValidationException')
|
||||||
|
ex.exception.response['Error']['Message'].should.contain(expected_exception)
|
||||||
|
|
||||||
|
|
||||||
def _create_user_table():
|
def _create_user_table():
|
||||||
client = boto3.client('dynamodb', region_name='us-east-1')
|
client = boto3.client('dynamodb', region_name='us-east-1')
|
||||||
client.create_table(
|
client.create_table(
|
||||||
|
Loading…
Reference in New Issue
Block a user