Beispiel #1
0
def delete_item(req, project_id, table_name):
    """Deletes a single item in a table by primary key. """

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

        # parse expected item conditions
        expected_item_conditions_json = body.pop(parser.Props.EXPECTED,
                                                 None)
        if expected_item_conditions_json:
            validation.validate_object(expected_item_conditions_json,
                                       parser.Props.EXPECTED)
            expected_item_conditions = (
                parser.Parser.parse_expected_attribute_conditions(
                    expected_item_conditions_json
                )
            )
        else:
            expected_item_conditions = None

        # parse key_attributes
        key_attributes_json = body.pop(parser.Props.KEY, None)
        validation.validate_object(key_attributes_json, parser.Props.KEY)

        key_attributes = parser.Parser.parse_item_attributes(
            key_attributes_json
        )

        # 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 = models.DeleteReturnValuesType(return_values_json)

        validation.validate_unexpected_props(body, "body")

    # delete item
    storage.delete_item(project_id, table_name, key_attributes,
                        expected_condition_map=expected_item_conditions)

    # format response
    response = {}

    if return_values.type != parser.Values.RETURN_VALUES_NONE:
        # TODO(cwang):
        # It is needed to return all deleted item attributes
        #
        response[parser.Props.ATTRIBUTES] = (
            parser.Parser.format_item_attributes(key_attributes)
        )

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

            # parse expected item conditions
            expected_item_conditions_json = body.pop(parser.Props.EXPECTED,
                                                     None)
            if expected_item_conditions_json:
                validation.validate_object(expected_item_conditions_json,
                                           parser.Props.EXPECTED)
                expected_item_conditions = (
                    parser.Parser.parse_expected_attribute_conditions(
                        expected_item_conditions_json))
            else:
                expected_item_conditions = None

            # parse key_attributes
            key_attributes_json = body.pop(parser.Props.KEY, None)
            validation.validate_object(key_attributes_json, parser.Props.KEY)

            key_attributes = parser.Parser.parse_item_attributes(
                key_attributes_json)

            # 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 = DeleteReturnValuesType(return_values_json)

            validation.validate_unexpected_props(body, "body")

        # delete item
        storage.delete_item(req.context,
                            table_name,
                            key_attributes,
                            expected_condition_map=expected_item_conditions)

        # format response
        response = {}

        if return_values.type != parser.Values.RETURN_VALUES_NONE:
            # TODO(cwang):
            # It is needed to return all deleted item attributes
            #
            response[parser.Props.ATTRIBUTES] = (
                parser.Parser.format_item_attributes(key_attributes))

        return response
    def test_delete_item(self):
        self.storage_mocker.StubOutWithMock(storage, 'delete_item')
        storage.delete_item(
            IgnoreArg(), IgnoreArg(),
            expected_condition_map=IgnoreArg()).AndReturn(True)
        self.storage_mocker.ReplayAll()

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

        table.delete_item(hash_key=1, range_key="range")

        self.storage_mocker.VerifyAll()
Beispiel #4
0
    def test_delete_item(self):
        self.storage_mocker.StubOutWithMock(storage, 'delete_item')
        storage.delete_item(IgnoreArg(),
                            IgnoreArg(),
                            expected_condition_map=IgnoreArg()).AndReturn(True)
        self.storage_mocker.ReplayAll()

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

        table.delete_item(hash_key=1, range_key="range")

        self.storage_mocker.VerifyAll()
Beispiel #5
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
            key_attributes = parser.Parser.parse_item_attributes(
                self.action_params[parser.Props.KEY])

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

        try:
            # put item
            result = storage.delete_item(
                self.context,
                table_name,
                key_attributes,
                expected_condition_map=expected_item_conditions)
        except AWSErrorResponseException as e:
            raise e
        except Exception:
            raise AWSErrorResponseException()

        if not result:
            raise AWSErrorResponseException()

        # format response
        response = {}

        try:
            if return_values != parser.Values.RETURN_VALUES_NONE:
                # TODO(dukhlov):
                # It is needed to return all deleted item attributes
                #
                response[parser.Props.ATTRIBUTES] = (
                    parser.Parser.format_item_attributes(key_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:
            raise exception.AWSErrorResponseException()
Beispiel #6
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
            key_attributes = parser.Parser.parse_item_attributes(
                self.action_params[parser.Props.KEY]
            )

            # 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.delete_item(
                self.context,
                models.DeleteItemRequest(table_name, key_attributes),
                expected_condition_map=expected_item_conditions)
        except exception.AWSErrorResponseException as e:
            raise e
        except Exception:
            raise exception.AWSErrorResponseException()

        if not result:
            raise exception.AWSErrorResponseException()

        # format response
        response = {}

        try:
            if return_values != parser.Values.RETURN_VALUES_NONE:
                # TODO(dukhlov):
                # It is needed to return all deleted item attributes
                #
                response[parser.Props.ATTRIBUTES] = (
                    parser.Parser.format_item_attributes(key_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:
            raise exception.AWSErrorResponseException()