~isviridov/magnetodb/master

« back to all changes in this revision

Viewing changes to magnetodb/api/amz/dynamodb/action/get_item.py

  • Committer: Ilya Sviridov
  • Date: 2014-03-04 22:19:50 UTC
  • Revision ID: git-v1:23faa059f7fcfbd23ef709232bc14e7baac3ea50
Updated README with new links

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2013 Mirantis Inc.
2
 
# All Rights Reserved.
3
 
#
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
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
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
14
 
#    under the License.
15
 
 
16
 
from magnetodb.api.amz.dynamodb.action import DynamoDBAction
17
 
from magnetodb.api.amz.dynamodb import parser
18
 
 
19
 
from magnetodb import storage
20
 
from magnetodb.common import exception
21
 
from magnetodb.storage import models
22
 
 
23
 
 
24
 
class GetItemDynamoDBAction(DynamoDBAction):
25
 
    schema = {
26
 
        "required": [parser.Props.KEY,
27
 
                     parser.Props.TABLE_NAME],
28
 
        "properties": {
29
 
            parser.Props.ATTRIBUTES_TO_GET: {
30
 
                "type": "array",
31
 
                "items": {
32
 
                    "type": "string",
33
 
                    "pattern": parser.ATTRIBUTE_NAME_PATTERN
34
 
                }
35
 
            },
36
 
            parser.Props.CONSISTENT_READ: {
37
 
                "type": "boolean"
38
 
            },
39
 
            parser.Props.KEY: {
40
 
                "type": "object",
41
 
                "patternProperties": {
42
 
                    parser.ATTRIBUTE_NAME_PATTERN: parser.Types.ITEM_VALUE
43
 
                }
44
 
            },
45
 
            parser.Props.RETURN_CONSUMED_CAPACITY: (
46
 
                parser.Types.RETURN_CONSUMED_CAPACITY
47
 
            ),
48
 
            parser.Props.TABLE_NAME: parser.Types.TABLE_NAME
49
 
        }
50
 
    }
51
 
 
52
 
    def __call__(self):
53
 
        try:
54
 
            table_name = self.action_params.get(parser.Props.TABLE_NAME, None)
55
 
 
56
 
            # get attributes_to_get
57
 
            attributes_to_get = self.action_params.get(
58
 
                parser.Props.ATTRIBUTES_TO_GET, None
59
 
            )
60
 
 
61
 
            select_type = (
62
 
                models.SelectType.all()
63
 
                if attributes_to_get is None else
64
 
                models.AttributeToGet.specified(attributes_to_get)
65
 
            )
66
 
 
67
 
            # parse key_attributes
68
 
            key_attributes = parser.Parser.parse_item_attributes(
69
 
                self.action_params[parser.Props.KEY]
70
 
            )
71
 
 
72
 
            # TODO(dukhlov):
73
 
            # it would be nice to validate given table_name, key_attributes and
74
 
            # attributes_to_get  to schema expectation
75
 
 
76
 
            consistent_read = self.action_params.get(
77
 
                parser.Props.CONSISTENT_READ, False
78
 
            )
79
 
 
80
 
            return_consumed_capacity = self.action_params.get(
81
 
                parser.Props.RETURN_CONSUMED_CAPACITY,
82
 
                parser.Values.RETURN_CONSUMED_CAPACITY_NONE
83
 
            )
84
 
 
85
 
            # format conditions to get item
86
 
            indexed_condition_map = {
87
 
                name: [models.IndexedCondition.eq(value)]
88
 
                for name, value in key_attributes.iteritems()
89
 
            }
90
 
        except Exception:
91
 
            raise exception.ValidationException()
92
 
 
93
 
        try:
94
 
            # get item
95
 
            result = storage.select_item(
96
 
                self.context, table_name, indexed_condition_map,
97
 
                select_type=select_type, limit=2, consistent=consistent_read)
98
 
 
99
 
            # format response
100
 
            if result.count == 0:
101
 
                return {}
102
 
 
103
 
            assert result.count == 1
104
 
 
105
 
            response = {
106
 
                parser.Props.ITEM: parser.Parser.format_item_attributes(
107
 
                    result.items[0])
108
 
            }
109
 
 
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
115
 
                    )
116
 
                )
117
 
 
118
 
            return response
119
 
        except exception.AWSErrorResponseException as e:
120
 
            raise e
121
 
        except Exception:
122
 
            raise exception.AWSErrorResponseException()