Example #1
0
    def test_put_item(self):
        self.storage_mocker.StubOutWithMock(storage, 'put_item')
        storage.put_item(IgnoreArg(),
                         IgnoreArg(),
                         if_not_exist=IgnoreArg(),
                         expected_condition_map=IgnoreArg()).AndReturn(True)
        self.storage_mocker.ReplayAll()

        table = Table('test_table', connection=self.DYNAMODB_CON)

        blob_data1 = bytes(bytearray([1, 2, 3, 4, 5]))
        blob_data2 = bytes(bytearray([5, 4, 3, 2, 1]))
        table.put_item(
            {
                "hash_key":
                1,
                "range_key":
                "range",
                "value_blob":
                types.Binary(blob_data1),
                "value_blob_set":
                set([types.Binary(blob_data1),
                     types.Binary(blob_data2)])
            }, False)

        self.storage_mocker.VerifyAll()
    def process_request(self, req, body, project_id, table_name):
        utils.check_project_id(req.context, project_id)
        jsonschema.validate(body, self.schema)

        # 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)

        # 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.InternalFailure()

        # format response
        response = {}

        if return_values != parser.Values.RETURN_VALUES_NONE:
            response[parser.Props.ATTRIBUTES] = parser.Parser.format_item_attributes(item_attributes)
        return response
Example #3
0
    def process_request(self, req, body, project_id, table_name):
        utils.check_project_id(req.context, project_id)
        jsonschema.validate(body, self.schema)

        # 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)

        # 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.InternalFailure()

        # format response
        response = {}

        if return_values != parser.Values.RETURN_VALUES_NONE:
            response[parser.Props.ATTRIBUTES] = (
                parser.Parser.format_item_attributes(item_attributes))
        return response
Example #4
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__ + '.validation'):
            validation.validate_object(body, "body")

            expected = body.pop(parser.Props.EXPECTED, {})
            validation.validate_object(expected, parser.Props.EXPECTED)
            # parse expected item conditions
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(expected)
            )

            item = body.pop(parser.Props.ITEM, None)
            validation.validate_object(item, parser.Props.ITEM)
            # parse item
            item_attributes = parser.Parser.parse_item_attributes(item)

            # parse return_values param
            return_values_json = body.pop(
                parser.Props.RETURN_VALUES, parser.Values.RETURN_VALUES_NONE
            )

            validation.validate_string(return_values_json,
                                       parser.Props.RETURN_VALUES)

            return_values = InsertReturnValuesType(return_values_json)

            # parse return_values param
            time_to_live = body.pop(
                parser.Props.TIME_TO_LIVE, None
            )

            if time_to_live is not None:
                time_to_live = validation.validate_integer(
                    time_to_live, parser.Props.TIME_TO_LIVE, min_val=0
                )

            validation.validate_unexpected_props(body, "body")

        # put item
        result, old_item = storage.put_item(
            req.context, table_name, item_attributes,
            return_values=return_values,
            if_not_exist=False,
            expected_condition_map=expected_item_conditions,
        )

        response = {}

        if old_item:
            response[parser.Props.ATTRIBUTES] = (
                parser.Parser.format_item_attributes(old_item)
            )

        return response
Example #5
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__ + '.validation'):
            validation.validate_object(body, "body")

            expected = body.pop(parser.Props.EXPECTED, {})
            validation.validate_object(expected, parser.Props.EXPECTED)
            # parse expected item conditions
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(expected))

            item = body.pop(parser.Props.ITEM, None)
            validation.validate_object(item, parser.Props.ITEM)
            # parse item
            item_attributes = parser.Parser.parse_item_attributes(item)

            # parse return_values param
            return_values_json = body.pop(parser.Props.RETURN_VALUES,
                                          parser.Values.RETURN_VALUES_NONE)

            validation.validate_string(return_values_json,
                                       parser.Props.RETURN_VALUES)

            return_values = InsertReturnValuesType(return_values_json)

            # parse return_values param
            time_to_live = body.pop(parser.Props.TIME_TO_LIVE, None)

            if time_to_live is not None:
                time_to_live = validation.validate_integer(
                    time_to_live, parser.Props.TIME_TO_LIVE, min_val=0)

            validation.validate_unexpected_props(body, "body")

        # put item
        result, old_item = storage.put_item(
            req.context,
            table_name,
            item_attributes,
            return_values=return_values,
            if_not_exist=False,
            expected_condition_map=expected_item_conditions,
        )

        response = {}

        if old_item:
            response[parser.Props.ATTRIBUTES] = (
                parser.Parser.format_item_attributes(old_item))

        return response
    def test_put_item(self):
        self.storage_mocker.StubOutWithMock(storage, 'put_item')
        storage.put_item(
            IgnoreArg(), IgnoreArg(),
            if_not_exist=IgnoreArg(),
            expected_condition_map=IgnoreArg()).AndReturn(True)
        self.storage_mocker.ReplayAll()

        table = Table('test_table', connection=self.DYNAMODB_CON)

        blob_data1 = bytes(bytearray([1, 2, 3, 4, 5]))
        blob_data2 = bytes(bytearray([5, 4, 3, 2, 1]))
        table.put_item(
            {
                "hash_key": 1,
                "range_key": "range",
                "value_blob": types.Binary(blob_data1),
                "value_blob_set": set([types.Binary(blob_data1),
                                       types.Binary(blob_data2)])
            },
            False
        )

        self.storage_mocker.VerifyAll()
Example #7
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()
Example #8
0
    def __call__(self):
        try:
            table_name = self.action_params.get(parser.Props.TABLE_NAME, None)

            # parse expected item conditions
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(
                    self.action_params.get(parser.Props.EXPECTED, {})
                )
            )

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

            # parse return_values param
            return_values = self.action_params.get(
                parser.Props.RETURN_VALUES, parser.Values.RETURN_VALUES_NONE
            )

            # parse return_item_collection_metrics
            return_item_collection_metrics = self.action_params.get(
                parser.Props.RETURN_ITEM_COLLECTION_METRICS,
                parser.Values.RETURN_ITEM_COLLECTION_METRICS_NONE
            )

            return_consumed_capacity = self.action_params.get(
                parser.Props.RETURN_CONSUMED_CAPACITY,
                parser.Values.RETURN_CONSUMED_CAPACITY_NONE
            )
        except Exception:
            raise exception.ValidationException()

        try:
            # put item
            result = storage.put_item(
                self.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)
                )

            if (return_item_collection_metrics !=
                    parser.Values.RETURN_ITEM_COLLECTION_METRICS_NONE):
                response[parser.Props.ITEM_COLLECTION_METRICS] = {
                    parser.Props.ITEM_COLLECTION_KEY: {
                        parser.Parser.format_item_attributes(
                            models.AttributeValue(models.ATTRIBUTE_TYPE_STRING,
                                                  "key")
                        )
                    },
                    parser.Props.SIZE_ESTIMATED_RANGE_GB: [0]
                }

            if (return_consumed_capacity !=
                    parser.Values.RETURN_CONSUMED_CAPACITY_NONE):
                response[parser.Props.CONSUMED_CAPACITY] = (
                    parser.Parser.format_consumed_capacity(
                        return_consumed_capacity, None
                    )
                )

            return response
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()
Example #9
0
    def __call__(self):
        try:
            table_name = self.action_params.get(parser.Props.TABLE_NAME, None)

            # parse expected item conditions
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(
                    self.action_params.get(parser.Props.EXPECTED, {})
                )
            )

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

            # parse return_values param
            return_values_json = self.action_params.get(
                parser.Props.RETURN_VALUES, parser.Values.RETURN_VALUES_NONE
            )

            return_values = models.InsertReturnValuesType(return_values_json)

            # parse return_item_collection_metrics
            return_item_collection_metrics = self.action_params.get(
                parser.Props.RETURN_ITEM_COLLECTION_METRICS,
                parser.Values.RETURN_ITEM_COLLECTION_METRICS_NONE
            )

            return_consumed_capacity = self.action_params.get(
                parser.Props.RETURN_CONSUMED_CAPACITY,
                parser.Values.RETURN_CONSUMED_CAPACITY_NONE
            )
        except Exception:
            raise AWSValidationException()

        try:
            # put item
            result, old_item = storage.put_item(
                self.context,
                table_name,
                item_attributes,
                return_values,
                if_not_exist=False,
                expected_condition_map=expected_item_conditions
            )

            if not result:
                raise AWSErrorResponseException()

            # format response
            response = {}

            if old_item:
                response[parser.Props.ATTRIBUTES] = (
                    parser.Parser.format_item_attributes(item_attributes)
                )

            if (return_item_collection_metrics !=
                    parser.Values.RETURN_ITEM_COLLECTION_METRICS_NONE):
                response[parser.Props.ITEM_COLLECTION_METRICS] = {
                    parser.Props.ITEM_COLLECTION_KEY: {
                        parser.Parser.format_item_attributes(
                            models.AttributeValue("S", "key")
                        )
                    },
                    parser.Props.SIZE_ESTIMATED_RANGE_GB: [0]
                }

            if (return_consumed_capacity !=
                    parser.Values.RETURN_CONSUMED_CAPACITY_NONE):
                response[parser.Props.CONSUMED_CAPACITY] = (
                    parser.Parser.format_consumed_capacity(
                        return_consumed_capacity, None
                    )
                )

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