def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'get_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.get_item( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), select_type=mox.IgnoreArg(), consistent=mox.IgnoreArg() ).AndReturn( models.SelectResult( items=[ { "hash_key": models.AttributeValue('N', hash_key), "range_key": models.AttributeValue('S', range_key), "attr_value": models.AttributeValue('S', 'val') } ] ) ) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(mox.IgnoreArg(), 'test_table').AndReturn( models.TableMeta( '00000000-0000-0000-0000-000000000000', models.TableSchema( { 'hash_key': models.AttributeType('N'), 'range_key': models.AttributeType('S') }, ['hash_key', 'range_key'], ), models.TableMeta.TABLE_STATUS_ACTIVE, None ) ) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item( mox.IgnoreArg(), mox.IgnoreArg(), key_attribute_map=mox.IgnoreArg(), attribute_action_map=mox.IgnoreArg(), expected_condition_map=mox.IgnoreArg()).AndReturn((True, None)) self.storage_mocker.ReplayAll() table = ddb_table.Table( 'test_table', connection=self.DYNAMODB_CON ) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'get_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.get_item( IgnoreArg(), IgnoreArg(), IgnoreArg(), select_type=IgnoreArg(), consistent=IgnoreArg() ).AndReturn( models.SelectResult( items=[ { "hash_key": models.AttributeValue('N', hash_key), "range_key": models.AttributeValue('S', range_key), "attr_value": models.AttributeValue('S', 'val') } ] ) ) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(IgnoreArg(), 'test_table').AndReturn( models.TableMeta( '00000000-0000-0000-0000-000000000000', models.TableSchema( { 'hash_key': models.AttributeType('N'), 'range_key': models.AttributeType('S') }, ['hash_key', 'range_key'], ), models.TableMeta.TABLE_STATUS_ACTIVE, None ) ) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item( IgnoreArg(), IgnoreArg(), key_attribute_map=IgnoreArg(), attribute_action_map=IgnoreArg(), expected_condition_map=IgnoreArg()).AndReturn((True, None)) self.storage_mocker.ReplayAll() table = Table('test_table', connection=self.DYNAMODB_CON) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'select_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.select_item( IgnoreArg(), IgnoreArg(), IgnoreArg(), select_type=IgnoreArg(), limit=IgnoreArg(), consistent=IgnoreArg() ).AndReturn( models.SelectResult( items=[ { "hash_key": models.AttributeValue.number(hash_key), "range_key": models.AttributeValue.str(range_key), "attr_value": models.AttributeValue.str('val') } ] ) ) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(IgnoreArg(), 'test_table').AndReturn( models.TableSchema( 'test_table', { 'hash_key': models.ATTRIBUTE_TYPE_NUMBER, 'range_key': models.ATTRIBUTE_TYPE_STRING }, ['hash_key', 'range_key'], ) ) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item( IgnoreArg(), IgnoreArg(), key_attribute_map=IgnoreArg(), attribute_action_map=IgnoreArg(), expected_condition_map=IgnoreArg()).AndReturn(True) self.storage_mocker.ReplayAll() table = Table('test_table', connection=self.DYNAMODB_CON) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'select_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.select_item( IgnoreArg(), IgnoreArg(), IgnoreArg(), select_type=IgnoreArg(), limit=IgnoreArg(), consistent=IgnoreArg()).AndReturn( models.SelectResult( items=[{ "hash_key": models.AttributeValue.number(hash_key), "range_key": models.AttributeValue.str(range_key), "attr_value": models.AttributeValue.str('val') }])) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(IgnoreArg(), 'test_table').AndReturn( models.TableMeta( models.TableSchema( { 'hash_key': models.ATTRIBUTE_TYPE_NUMBER, 'range_key': models.ATTRIBUTE_TYPE_STRING }, ['hash_key', 'range_key'], ), models.TableMeta.TABLE_STATUS_ACTIVE)) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item(IgnoreArg(), IgnoreArg(), key_attribute_map=IgnoreArg(), attribute_action_map=IgnoreArg(), expected_condition_map=IgnoreArg()).AndReturn(True) self.storage_mocker.ReplayAll() table = Table('test_table', connection=self.DYNAMODB_CON) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def process_request(self, req, body, project_id, table_name): with probe.Probe(__name__ + '.validation'): validation.validate_object(body, "body") # parse expected item conditions expected_item_conditions_json = body.pop(parser.Props.EXPECTED, None) if expected_item_conditions_json is not None: 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 attribute_updates_json = body.pop(parser.Props.ATTRIBUTE_UPDATES, None) validation.validate_object(attribute_updates_json, parser.Props.ATTRIBUTE_UPDATES) # parse attribute updates attribute_updates = parser.Parser.parse_attribute_updates( attribute_updates_json) # parse key_attributes key_attributes_json = body.pop(parser.Props.KEY, None) validation.validate_object(key_attributes_json, parser.Props.KEY) key_attribute_map = 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 = UpdateReturnValuesType(return_values_json) validation.validate_unexpected_props(body, "body") result, old_item = storage.update_item( req.context, table_name, key_attribute_map=key_attribute_map, attribute_action_map=attribute_updates, expected_condition_map=expected_item_conditions) if not result: raise exception.BackendInteractionException() # format response response = {} if return_values.type != parser.Values.RETURN_VALUES_NONE: response[parser.Props.ATTRIBUTES] = ( parser.Parser.format_item_attributes(old_item)) return response
def process_request(self, req, body, project_id, table_name): with probe.Probe(__name__ + '.validation'): validation.validate_object(body, "body") # parse expected item conditions expected_item_conditions_json = body.pop(parser.Props.EXPECTED, None) if expected_item_conditions_json is not None: 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 attribute_updates_json = body.pop(parser.Props.ATTRIBUTE_UPDATES, None) validation.validate_object(attribute_updates_json, parser.Props.ATTRIBUTE_UPDATES) # parse attribute updates attribute_updates = parser.Parser.parse_attribute_updates( attribute_updates_json ) # parse key_attributes key_attributes_json = body.pop(parser.Props.KEY, None) validation.validate_object(key_attributes_json, parser.Props.KEY) key_attribute_map = 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 = UpdateReturnValuesType(return_values_json) validation.validate_unexpected_props(body, "body") result, old_item = storage.update_item( req.context, table_name, key_attribute_map=key_attribute_map, attribute_action_map=attribute_updates, expected_condition_map=expected_item_conditions) if not result: raise exception.BackendInteractionException() # format response response = {} if return_values.type != parser.Values.RETURN_VALUES_NONE: response[parser.Props.ATTRIBUTES] = ( parser.Parser.format_item_attributes(old_item) ) return response
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 attribute updates attribute_updates = parser.Parser.parse_attribute_updates( self.action_params.get(parser.Props.ATTRIBUTE_UPDATES, {}) ) # parse key 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.AWSValidationException() try: # update item result, old_item = storage.update_item( self.context, table_name, key_attribute_map=key_attributes, attribute_action_map=attribute_updates, 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(old_item) ) 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 exception.AWSErrorResponseException as e: raise e except Exception: raise exception.AWSErrorResponseException()
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 attribute updates attribute_updates = parser.Parser.parse_attribute_updates( self.action_params.get(parser.Props.ATTRIBUTE_UPDATES, {}) ) # parse key 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 ) select_result = None indexed_condition_map_for_select = { name: models.IndexedCondition.eq(value) for name, value in key_attributes.iteritems() } except Exception: raise exception.ValidationException() try: if return_values in (parser.Values.RETURN_VALUES_UPDATED_OLD, parser.Values.RETURN_VALUES_ALL_OLD): select_result = storage.select_item( self.context, table_name, indexed_condition_map_for_select) # update item result = storage.update_item( self.context, table_name, key_attribute_map=key_attributes, attribute_action_map=attribute_updates, expected_condition_map=expected_item_conditions) if not result: raise exception.AWSErrorResponseException() if return_values in (parser.Values.RETURN_VALUES_UPDATED_NEW, parser.Values.RETURN_VALUES_ALL_NEW): select_result = storage.select_item( self.context, table_name, indexed_condition_map_for_select) # format response response = {} if return_values != parser.Values.RETURN_VALUES_NONE: response[parser.Props.ATTRIBUTES] = ( parser.Parser.format_item_attributes( select_result.items[0]) ) 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()
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 attribute updates attribute_updates = parser.Parser.parse_attribute_updates( self.action_params.get(parser.Props.ATTRIBUTE_UPDATES, {})) # parse key 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.AWSValidationException() try: # update item result, old_item = storage.update_item( self.context, table_name, key_attribute_map=key_attributes, attribute_action_map=attribute_updates, 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(old_item)) 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 exception.AWSErrorResponseException as e: raise e except Exception: raise exception.AWSErrorResponseException()
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 attribute updates attribute_updates = parser.Parser.parse_attribute_updates( self.action_params.get(parser.Props.ATTRIBUTE_UPDATES, {})) # parse key 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) select_result = None indexed_condition_map_for_select = { name: models.IndexedCondition.eq(value) for name, value in key_attributes.iteritems() } except Exception: raise exception.ValidationException() try: if return_values in (parser.Values.RETURN_VALUES_UPDATED_OLD, parser.Values.RETURN_VALUES_ALL_OLD): select_result = storage.select_item( self.context, table_name, indexed_condition_map_for_select) # update item result = storage.update_item( self.context, table_name, key_attribute_map=key_attributes, attribute_action_map=attribute_updates, expected_condition_map=expected_item_conditions) if not result: raise exception.AWSErrorResponseException() if return_values in (parser.Values.RETURN_VALUES_UPDATED_NEW, parser.Values.RETURN_VALUES_ALL_NEW): select_result = storage.select_item( self.context, table_name, indexed_condition_map_for_select) # format response response = {} if return_values != parser.Values.RETURN_VALUES_NONE: response[parser.Props.ATTRIBUTES] = ( parser.Parser.format_item_attributes( select_result.items[0])) 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()
def update_item(req, project_id, table_name): """ Edits(or inserts if item does not already exist) an item's attributes. """ with probe.Probe(__name__ + '.validation'): 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 is not None: 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 attribute_updates_json = body.pop(parser.Props.ATTRIBUTE_UPDATES, None) validation.validate_object(attribute_updates_json, parser.Props.ATTRIBUTE_UPDATES) # parse attribute updates attribute_updates = parser.Parser.parse_attribute_updates( attribute_updates_json ) # parse key_attributes key_attributes_json = body.pop(parser.Props.KEY, None) validation.validate_object(key_attributes_json, parser.Props.KEY) key_attribute_map = 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.UpdateReturnValuesType(return_values_json) validation.validate_unexpected_props(body, "body") result, old_item = storage.update_item( project_id, table_name, key_attribute_map=key_attribute_map, attribute_action_map=attribute_updates, expected_condition_map=expected_item_conditions) if not result: raise exception.BackendInteractionError("Unexpected empty result") # format response response = {} if return_values.type != parser.Values.RETURN_VALUES_NONE: response[parser.Props.ATTRIBUTES] = ( parser.Parser.format_item_attributes(old_item) if old_item else {} ) return response