Пример #1
0
    def parse_batch_get_request_items(cls, request_items_json):
        request_list = []
        for table_name, request_body in request_items_json.iteritems():
            validation.validate_table_name(table_name)
            validation.validate_object(request_body, table_name)

            consistent = request_body.pop(Props.CONSISTENT_READ, False)

            validation.validate_boolean(consistent, Props.CONSISTENT_READ)

            attributes_to_get = request_body.pop(Props.ATTRIBUTES_TO_GET, None)

            if attributes_to_get is not None:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            keys = request_body.pop(Props.KEYS, None)

            validation.validate_list(keys, Props.KEYS)

            validation.validate_unexpected_props(request_body, table_name)

            for key in keys:
                key_attribute_map = cls.parse_item_attributes(key)
                request_list.append(
                    models.GetItemRequest(table_name,
                                          key_attribute_map,
                                          attributes_to_get,
                                          consistent=consistent))
        return request_list
Пример #2
0
    def process_request(self, req, body, project_id, table_name):
        utils.check_project_id(req.context, project_id)
        req.context.tenant = project_id

        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, parser.Props.ATTRIBUTES_TO_GET
                )
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)
                select_type = models.SelectType.specific_attributes(
                    attributes_to_get
                )
            else:
                select_type = models.SelectType.all()

            key = body.pop(parser.Props.KEY, None)
            validation.validate_object(key, parser.Props.KEY)

            # parse consistent_read
            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)

            validation.validate_unexpected_props(body, "body")

            # parse key_attributes
            key_attributes = parser.Parser.parse_item_attributes(key)

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

        # 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
Пример #3
0
def get_item(req, project_id, table_name):
    """The Getitem operation returns an item with the given primary key. """

    with probe.Probe(__name__ + '.validate'):
        body = req.json_body
        validation.validate_object(body, "body")

        # get attributes_to_get
        attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
        if attributes_to_get:
            attributes_to_get = validation.validate_set(
                attributes_to_get, parser.Props.ATTRIBUTES_TO_GET
            )
            for attr_name in attributes_to_get:
                validation.validate_attr_name(attr_name)
            select_type = models.SelectType.specific_attributes(
                attributes_to_get
            )
        else:
            select_type = models.SelectType.all()

        key = body.pop(parser.Props.KEY, None)
        validation.validate_object(key, parser.Props.KEY)

        # parse consistent_read
        consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
        validation.validate_boolean(consistent_read,
                                    parser.Props.CONSISTENT_READ)

        validation.validate_unexpected_props(body, "body")

        # parse key_attributes
        key_attributes = parser.Parser.parse_item_attributes(key)

    # get item
    result = storage.get_item(
        project_id, table_name, key_attributes,
        select_type=select_type, consistent=consistent_read)

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

    response = {
        parser.Props.ITEM: parser.Parser.format_item_attributes(
            result.items[0])
    }
    return response
Пример #4
0
    def process_request(self, req, body, project_id, table_name):
        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, parser.Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)
                select_type = models.SelectType.specific_attributes(
                    attributes_to_get)
            else:
                select_type = models.SelectType.all()

            key = body.pop(parser.Props.KEY, None)
            validation.validate_object(key, parser.Props.KEY)

            # parse consistent_read
            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)

            validation.validate_unexpected_props(body, "body")

            # parse key_attributes
            key_attributes = parser.Parser.parse_item_attributes(key)

        # get item
        result = storage.get_item(req.context,
                                  table_name,
                                  key_attributes,
                                  select_type=select_type,
                                  consistent=consistent_read)

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

        response = {
            parser.Props.ITEM:
            parser.Parser.format_item_attributes(result.items[0])
        }
        return response
Пример #5
0
    def parse_expected_attribute_conditions(
            cls, expected_attribute_conditions_json):
        expected_attribute_conditions = {}

        for (attr_name_json, condition_json) in (
                expected_attribute_conditions_json.iteritems()):
            validation.validate_attr_name(attr_name_json)
            validation.validate_object(condition_json, attr_name_json)

            if len(condition_json) != 1:
                raise exception.ValidationError(
                    _("Can't recognize attribute expected condition format: "
                      "'%(attr)s'"),
                    attr=json.dumps(condition_json)
                )

            (condition_type, condition_value) = condition_json.popitem()

            validation.validate_string(condition_type, "condition type")

            if condition_type == Props.VALUE:
                validation.validate_object(condition_value, Props.VALUE)
                expected_attribute_conditions[attr_name_json] = [
                    models.ExpectedCondition.eq(
                        cls.parse_typed_attr_value(condition_value)
                    )
                ]
            elif condition_type == Props.EXISTS:
                validation.validate_boolean(condition_value, Props.EXISTS)
                expected_attribute_conditions[attr_name_json] = [
                    models.ExpectedCondition.not_null() if condition_value else
                    models.ExpectedCondition.null()
                ]
            else:
                raise exception.ValidationError(
                    _("Unsupported condition type found: %(condition_type)s"),
                    condition_type=condition_type
                )

        return expected_attribute_conditions
Пример #6
0
    def parse_expected_attribute_conditions(
            cls, expected_attribute_conditions_json):
        expected_attribute_conditions = {}

        for (attr_name_json, condition_json) in (
                expected_attribute_conditions_json.iteritems()):
            validation.validate_attr_name(attr_name_json)
            validation.validate_object(condition_json, attr_name_json)

            if len(condition_json) != 1:
                raise ValidationError(
                    _("Can't recognize attribute expected condition format: "
                      "'%(attr)s'"),
                    attr=json.dumps(condition_json)
                )

            (condition_type, condition_value) = condition_json.popitem()

            validation.validate_string(condition_type, "condition type")

            if condition_type == Props.VALUE:
                validation.validate_object(condition_value, Props.VALUE)
                expected_attribute_conditions[attr_name_json] = [
                    ExpectedCondition.eq(
                        cls.parse_typed_attr_value(condition_value)
                    )
                ]
            elif condition_type == Props.EXISTS:
                validation.validate_boolean(condition_value, Props.EXISTS)
                expected_attribute_conditions[attr_name_json] = [
                    ExpectedCondition.not_null() if condition_value else
                    ExpectedCondition.null()
                ]
            else:
                raise ValidationError(
                    _("Unsupported condition type found: %(condition_type)s"),
                    condition_type=condition_type
                )

        return expected_attribute_conditions
Пример #7
0
    def parse_batch_get_request_items(cls, request_items_json):
        request_list = []
        for table_name, request_body in request_items_json.iteritems():
            validation.validate_table_name(table_name)
            validation.validate_object(request_body, table_name)

            consistent = request_body.pop(Props.CONSISTENT_READ, False)

            validation.validate_boolean(consistent, Props.CONSISTENT_READ)

            attributes_to_get = request_body.pop(
                Props.ATTRIBUTES_TO_GET, None
            )

            if attributes_to_get is not None:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, Props.ATTRIBUTES_TO_GET
                )
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            keys = request_body.pop(Props.KEYS, None)

            validation.validate_list(keys, Props.KEYS)

            validation.validate_unexpected_props(request_body, table_name)

            for key in keys:
                key_attribute_map = cls.parse_item_attributes(key)
                request_list.append(
                    models.GetItemRequest(
                        table_name, key_attribute_map, attributes_to_get,
                        consistent=consistent
                    )
                )
        return request_list
Пример #8
0
    def query(self, req, body, project_id, table_name):
        utils.check_project_id(req.context, project_id)
        req.context.tenant = project_id

        with probe.Probe(__name__ + '.validation'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get:
                validation.validate_list(attributes_to_get,
                                         parser.Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            index_name = body.pop(parser.Props.INDEX_NAME, None)
            if index_name is not None:
                validation.validate_index_name(index_name)

            select = body.pop(parser.Props.SELECT, None)

            if select is None:
                if attributes_to_get:
                    select = models.SelectType.SELECT_TYPE_SPECIFIC
                else:
                    if index_name is not None:
                        select = models.SelectType.SELECT_TYPE_ALL_PROJECTED
                    else:
                        select = models.SelectType.SELECT_TYPE_ALL
            else:
                validation.validate_string(select, parser.Props.SELECT)

            select_type = models.SelectType(select, attributes_to_get)

            # parse exclusive_start_key_attributes
            exclusive_start_key_attributes_json = body.pop(
                parser.Props.EXCLUSIVE_START_KEY, None)

            if exclusive_start_key_attributes_json is not None:
                validation.validate_object(exclusive_start_key_attributes_json,
                                           parser.Props.EXCLUSIVE_START_KEY)
                exclusive_start_key_attributes = (
                    parser.Parser.parse_item_attributes(
                        exclusive_start_key_attributes_json
                    )
                )
            else:
                exclusive_start_key_attributes = None

            # parse indexed_condition_map
            key_conditions = body.pop(parser.Props.KEY_CONDITIONS, None)
            validation.validate_object(key_conditions,
                                       parser.Props.KEY_CONDITIONS)

            indexed_condition_map = parser.Parser.parse_attribute_conditions(
                key_conditions, condition_class=IndexedCondition
            )

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

            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)
            limit = body.pop(parser.Props.LIMIT, None)
            if limit is not None:
                limit = validation.validate_integer(limit, parser.Props.LIMIT,
                                                    min_val=0)

            scan_forward = body.pop(parser.Props.SCAN_INDEX_FORWARD, None)

            if scan_forward is not None:
                validation.validate_boolean(scan_forward,
                                            parser.Props.SCAN_INDEX_FORWARD)
                order_type = (
                    models.ORDER_TYPE_ASC if scan_forward else
                    models.ORDER_TYPE_DESC
                )
            else:
                order_type = None

            validation.validate_unexpected_props(body, "body")

        # 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.count
            }
        else:
            response = {
                parser.Props.COUNT: result.count,
                parser.Props.ITEMS: [
                    parser.Parser.format_item_attributes(row)
                    for row in result.items
                ]
            }

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

        return response
Пример #9
0
    def query(self, req, body, project_id, table_name):
        with probe.Probe(__name__ + '.validation'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get is not None:
                validation.validate_list(attributes_to_get,
                                         parser.Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            index_name = body.pop(parser.Props.INDEX_NAME, None)
            if index_name is not None:
                validation.validate_index_name(index_name)

            select = body.pop(parser.Props.SELECT, None)

            if select is None:
                if attributes_to_get:
                    select = models.SelectType.SELECT_TYPE_SPECIFIC
                else:
                    if index_name is not None:
                        select = models.SelectType.SELECT_TYPE_ALL_PROJECTED
                    else:
                        select = models.SelectType.SELECT_TYPE_ALL
            else:
                validation.validate_string(select, parser.Props.SELECT)

            select_type = models.SelectType(select, attributes_to_get)

            # parse exclusive_start_key_attributes
            exclusive_start_key_attributes_json = body.pop(
                parser.Props.EXCLUSIVE_START_KEY, None)

            if exclusive_start_key_attributes_json is not None:
                validation.validate_object(exclusive_start_key_attributes_json,
                                           parser.Props.EXCLUSIVE_START_KEY)
                exclusive_start_key_attributes = (
                    parser.Parser.parse_item_attributes(
                        exclusive_start_key_attributes_json))
            else:
                exclusive_start_key_attributes = None

            # parse indexed_condition_map
            key_conditions = body.pop(parser.Props.KEY_CONDITIONS, None)
            validation.validate_object(key_conditions,
                                       parser.Props.KEY_CONDITIONS)

            indexed_condition_map = parser.Parser.parse_attribute_conditions(
                key_conditions, condition_class=IndexedCondition)

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

            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)
            limit = body.pop(parser.Props.LIMIT, None)
            if limit is not None:
                limit = validation.validate_integer(limit,
                                                    parser.Props.LIMIT,
                                                    min_val=0)

            scan_forward = body.pop(parser.Props.SCAN_INDEX_FORWARD, None)

            if scan_forward is not None:
                validation.validate_boolean(scan_forward,
                                            parser.Props.SCAN_INDEX_FORWARD)
                order_type = (models.ORDER_TYPE_ASC
                              if scan_forward else models.ORDER_TYPE_DESC)
            else:
                order_type = None

            validation.validate_unexpected_props(body, "body")

        # select item
        result = storage.query(
            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.count}
        else:
            response = {
                parser.Props.COUNT:
                result.count,
                parser.Props.ITEMS: [
                    parser.Parser.format_item_attributes(row)
                    for row in result.items
                ]
            }

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

        return response