1
# Copyright 2013 Mirantis Inc.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from magnetodb.api.amz.dynamodb.action import DynamoDBAction
17
from magnetodb.api.amz.dynamodb import parser
19
from magnetodb import storage
20
from magnetodb.common import exception
21
from magnetodb.storage import models
24
class GetItemDynamoDBAction(DynamoDBAction):
26
"required": [parser.Props.KEY,
27
parser.Props.TABLE_NAME],
29
parser.Props.ATTRIBUTES_TO_GET: {
33
"pattern": parser.ATTRIBUTE_NAME_PATTERN
36
parser.Props.CONSISTENT_READ: {
41
"patternProperties": {
42
parser.ATTRIBUTE_NAME_PATTERN: parser.Types.ITEM_VALUE
45
parser.Props.RETURN_CONSUMED_CAPACITY: (
46
parser.Types.RETURN_CONSUMED_CAPACITY
48
parser.Props.TABLE_NAME: parser.Types.TABLE_NAME
54
table_name = self.action_params.get(parser.Props.TABLE_NAME, None)
56
# get attributes_to_get
57
attributes_to_get = self.action_params.get(
58
parser.Props.ATTRIBUTES_TO_GET, None
62
models.SelectType.all()
63
if attributes_to_get is None else
64
models.AttributeToGet.specified(attributes_to_get)
67
# parse key_attributes
68
key_attributes = parser.Parser.parse_item_attributes(
69
self.action_params[parser.Props.KEY]
73
# it would be nice to validate given table_name, key_attributes and
74
# attributes_to_get to schema expectation
76
consistent_read = self.action_params.get(
77
parser.Props.CONSISTENT_READ, False
80
return_consumed_capacity = self.action_params.get(
81
parser.Props.RETURN_CONSUMED_CAPACITY,
82
parser.Values.RETURN_CONSUMED_CAPACITY_NONE
85
# format conditions to get item
86
indexed_condition_map = {
87
name: [models.IndexedCondition.eq(value)]
88
for name, value in key_attributes.iteritems()
91
raise exception.ValidationException()
95
result = storage.select_item(
96
self.context, table_name, indexed_condition_map,
97
select_type=select_type, limit=2, consistent=consistent_read)
100
if result.count == 0:
103
assert result.count == 1
106
parser.Props.ITEM: parser.Parser.format_item_attributes(
110
if (return_consumed_capacity !=
111
parser.Values.RETURN_CONSUMED_CAPACITY_NONE):
112
response[parser.Props.CONSUMED_CAPACITY] = (
113
parser.Parser.format_consumed_capacity(
114
return_consumed_capacity, None
119
except exception.AWSErrorResponseException as e:
122
raise exception.AWSErrorResponseException()