示例#1
0
    def process_request(self, req, body, project_id):
        try:
            validation.validate_params(self.schema, body)

            # parse request_items
            request_items = parser.Parser.parse_request_items(
                body[parser.Props.REQUEST_ITEMS])
        except Exception:
            raise exception.ValidationException()

        try:
            req.context.tenant = project_id

            request_list = collections.deque()
            for rq_item in request_items:
                request_list.append(rq_item)

            response = {'unprocessed_items': {}}

            if not storage.execute_write_batch(req.context, request_list):
                raise exception.AWSErrorResponseException(
                    'Batch write failed')

            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
示例#2
0
    def scan(self, req, body, project_id, table_name):

        validation.validate_params(self.schema, body)

        req.context.tenant = project_id

        try:
            # TODO ikhudoshyn: table_name may be index name

            attrs_to_get = body.get(parser.Props.ATTRIBUTES_TO_GET)

            select = body.get(parser.Props.SELECT)

            select_type = parser.Parser.parse_select_type(select, attrs_to_get)

            limit = body.get(parser.Props.LIMIT)

            exclusive_start_key = body.get(parser.Props.EXCLUSIVE_START_KEY)

            exclusive_start_key = (
                parser.Parser.parse_item_attributes(exclusive_start_key) if exclusive_start_key else None
            )

            scan_filter = body.get(parser.Props.SCAN_FILTER, {})

            condition_map = parser.Parser.parse_attribute_conditions(scan_filter)

            segment = body.get(parser.Props.SEGMENT, 0)
            total_segments = body.get(parser.Props.TOTAL_SEGMENTS, 1)

            assert segment < total_segments
        except Exception:
            raise exception.AWSErrorResponseException()

        try:
            result = storage.scan(
                req.context,
                table_name,
                condition_map,
                attributes_to_get=attrs_to_get,
                limit=limit,
                exclusive_start_key=exclusive_start_key,
            )

            response = {parser.Props.COUNT: result.count, parser.Props.SCANNED_COUNT: result.scanned_count}

            if not select_type.is_count:
                response[parser.Props.ITEMS] = [parser.Parser.format_item_attributes(row) for row in result.items]

            if result.last_evaluated_key:
                response[parser.Props.LAST_EVALUATED_KEY] = parser.Parser.format_item_attributes(
                    result.last_evaluated_key
                )

            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
示例#3
0
    def process_request(self, req, body, project_id, table_name):
        try:
            validation.validate_params(self.schema, body)
            req.context.tenant = project_id

            # get attributes_to_get
            attributes_to_get = body.get(parser.Props.ATTRIBUTES_TO_GET)

            select_type = (
                models.SelectType.all()
                if attributes_to_get is None else
                models.SelectType.specified_attributes(attributes_to_get)
            )

            # parse key_attributes
            key_attributes = parser.Parser.parse_item_attributes(
                body[parser.Props.KEY]
            )

            # parse consistent_read
            consistent_read = body.get(
                parser.Props.CONSISTENT_READ, False
            )

            # format conditions to get item
            indexed_condition_map = {
                name: [models.IndexedCondition.eq(value)]
                for name, value in key_attributes.iteritems()
            }

        except Exception:
            raise exception.ValidationException()

        try:
            # get item
            result = storage.select_item(
                req.context, table_name, indexed_condition_map,
                select_type=select_type, limit=2, consistent=consistent_read)

            # format response
            if result.count == 0:
                return {}

            response = {
                parser.Props.ITEM: parser.Parser.format_item_attributes(
                    result.items[0])
            }
            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
示例#4
0
    def process_request(self, req, body, project_id, table_name):
        try:
            validation.validate_params(self.schema, body)
            # parse expected item conditions
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(
                    body.get(parser.Props.EXPECTED, {})
                )
            )

            # parse item
            item_attributes = parser.Parser.parse_item_attributes(
                body[parser.Props.ITEM]
            )

            # parse return_values param
            return_values = body.get(
                parser.Props.RETURN_VALUES, parser.Values.RETURN_VALUES_NONE
            )
        except Exception:
            raise exception.ValidationException()

        try:
            # put item
            req.context.tenant = project_id
            result = storage.put_item(
                req.context,
                models.PutItemRequest(table_name, item_attributes),
                if_not_exist=False,
                expected_condition_map=expected_item_conditions)

            if not result:
                raise exception.AWSErrorResponseException()

            # format response
            response = {}

            if return_values != parser.Values.RETURN_VALUES_NONE:
                response[parser.Props.ATTRIBUTES] = (
                    parser.Parser.format_item_attributes(item_attributes)
                )
            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
示例#5
0
    def create_table(self, req, body, project_id):
        validation.validate_params(self.schema, body)

        try:
            table_name = body.get(parser.Props.TABLE_NAME, None)

            #parse table attributes
            attribute_definitions = parser.Parser.parse_attribute_definitions(
                body.get(parser.Props.ATTRIBUTE_DEFINITIONS, {})
            )

            #parse table key schema
            key_attrs = parser.Parser.parse_key_schema(
                body.get(parser.Props.KEY_SCHEMA, [])
            )

            #parse table indexed field list
            indexed_attr_names = parser.Parser.parse_local_secondary_indexes(
                body.get(
                    parser.Props.LOCAL_SECONDARY_INDEXES, [])
            )

            #prepare table_schema structure
            table_schema = models.TableSchema(
                table_name, attribute_definitions,
                key_attrs, indexed_attr_names)

        except Exception:
            raise exception.ValidationException()

        try:
            # creating table
            req.context.tenant = project_id
            storage.create_table(req.context, table_schema)

            url = req.path_url + "/" + table_name
            bookmark = req.path_url + "/" + table_name

            return {
                parser.Props.TABLE_DESCRIPTION: {
                    parser.Props.ATTRIBUTE_DEFINITIONS: (
                        parser.Parser.format_attribute_definitions(
                            attribute_definitions
                        )
                    ),
                    parser.Props.CREATION_DATE_TIME: 0,
                    parser.Props.ITEM_COUNT: 0,
                    parser.Props.KEY_SCHEMA: (
                        parser.Parser.format_key_schema(
                            key_attrs
                        )
                    ),
                    parser.Props.LOCAL_SECONDARY_INDEXES: (
                        parser.Parser.format_local_secondary_indexes(
                            key_attrs[0], indexed_attr_names
                        )
                    ),
                    parser.Props.TABLE_NAME: table_name,
                    parser.Props.TABLE_STATUS: (
                        parser.Values.TABLE_STATUS_ACTIVE
                    ),
                    parser.Props.TABLE_SIZE_BYTES: 0,
                    parser.Props.LINKS: [
                        {
                            parser.Props.HREF: url,
                            parser.Props.REL: parser.Values.SELF
                        },
                        {
                            parser.Props.HREF: bookmark,
                            parser.Props.REL: parser.Values.BOOKMARK
                        }
                    ]
                }
            }
        except exception.TableAlreadyExistsException:
            raise exception.ResourceInUseException()
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
示例#6
0
    def query(self, req, body, project_id, table_name):
        validation.validate_params(self.schema, body)
        req.context.tenant = project_id

        try:
            # parse select_type
            attributes_to_get = body.get(parser.Props.ATTRIBUTES_TO_GET)

            if attributes_to_get is not None:
                attributes_to_get = frozenset(attributes_to_get)

            select = body.get(parser.Props.SELECT)

            index_name = body.get(parser.Props.INDEX_NAME)

            select_type = parser.Parser.parse_select_type(select,
                                                          attributes_to_get,
                                                          index_name)

            # parse exclusive_start_key_attributes
            exclusive_start_key_attributes = body.get(
                parser.Props.EXCLUSIVE_START_KEY)

            if exclusive_start_key_attributes is not None:
                exclusive_start_key_attributes = (
                    parser.Parser.parse_item_attributes(
                        exclusive_start_key_attributes
                    )
                )

            # parse indexed_condition_map
            indexed_condition_map = parser.Parser.parse_attribute_conditions(
                body.get(parser.Props.KEY_CONDITIONS))

            # TODO(dukhlov):
            # it would be nice to validate given table_name, key_attributes and
            # attributes_to_get to schema expectation

            consistent_read = body.get(
                parser.Props.CONSISTENT_READ, False)

            limit = body.get(parser.Props.LIMIT)

            order_asc = body.get(parser.Props.SCAN_INDEX_FORWARD)

            if order_asc is None:
                order_type = None
            elif order_asc:
                order_type = models.ORDER_TYPE_ASC
            else:
                order_type = models.ORDER_TYPE_DESC

        except Exception:
            raise exception.ValidationException()

        try:
            # select item
            result = storage.select_item(
                req.context, table_name, indexed_condition_map,
                select_type=select_type, index_name=index_name, limit=limit,
                consistent=consistent_read, order_type=order_type,
                exclusive_start_key=exclusive_start_key_attributes
            )

            # format response
            if select_type.type == models.SelectType.SELECT_TYPE_COUNT:
                response = {
                    parser.Props.COUNT: result.items
                }
            else:
                response = {
                    parser.Props.COUNT: len(result.items),
                    parser.Props.ITEMS: [
                        parser.Parser.format_item_attributes(row)
                        for row in result.items
                    ]
                }

            if limit == len(result.items):
                response[parser.Props.LAST_EVALUATED_KEY] = (
                    parser.Parser.format_item_attributes(
                        result.last_evaluated_key)
                )

            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()