def batch_write_item(req, project_id): """The BatchWriteItem operation puts or deletes multiple items in one or more tables. """ with probe.Probe(__name__ + '.validation'): body = req.json_body validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_map = parser.Parser.parse_batch_write_request_items( request_items_json ) unprocessed_items = storage.execute_write_batch(project_id, request_map) return { 'unprocessed_items': parser.Parser.format_request_items( unprocessed_items)}
def parse_attribute_updates(cls, attribute_updates_json): attribute_updates = {} for attr, attr_update_json in attribute_updates_json.iteritems(): validation.validate_attr_name(attr) validation.validate_object(attr_update_json, attr) action_type_json = attr_update_json.pop(Props.ACTION, None) validation.validate_string(action_type_json, Props.ACTION) value_json = attr_update_json.pop(Props.VALUE, None) if value_json: validation.validate_object(value_json, Props.VALUE) value = cls.parse_typed_attr_value(value_json) else: value = None update_action = models.UpdateItemAction(action_type_json, value) validation.validate_unexpected_props(attr_update_json, attr) attribute_updates[attr] = update_action return attribute_updates
def parse_attribute_conditions(cls, attribute_conditions_json, condition_class=models.IndexedCondition): attribute_conditions = {} for (attr_name, condition_json) in ( attribute_conditions_json.iteritems()): validation.validate_attr_name(attr_name) validation.validate_object(condition_json, attr_name) condition_type_json = ( condition_json.pop(Props.COMPARISON_OPERATOR, None) ) attribute_list = condition_json.pop(Props.ATTRIBUTE_VALUE_LIST, None) condition_args = [] if attribute_list: validation.validate_list_of_objects( attribute_list, Props.ATTRIBUTE_VALUE_LIST ) for typed_attribute_value in attribute_list: condition_args.append( cls.parse_typed_attr_value(typed_attribute_value) ) attribute_conditions[attr_name] = ( cls.parse_attribute_condition( condition_type_json, condition_args, condition_class ) ) validation.validate_unexpected_props(condition_json, attr_name) return attribute_conditions
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
def process_request(self, req, body, project_id): with probe.Probe(__name__ + '.validation'): validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_list = parser.Parser.parse_batch_get_request_items( request_items_json) result, unprocessed = storage.execute_get_batch( req.context, request_list) responses = {} for tname, res in result: if not res.items: continue table_items = responses.get(tname, None) if table_items is None: table_items = [] responses[tname] = table_items item = parser.Parser.format_item_attributes(res.items[0]) table_items.append(item) return { 'responses': responses, 'unprocessed_keys': parser.Parser.format_batch_get_unprocessed(unprocessed, request_items_json) }
def create_backup(req, project_id, table_name): """Creates a backup for a table.""" utils.check_project_id(project_id) with probe.Probe(__name__ + '.validation'): body = req.json_body validation.validate_table_name(table_name) validation.validate_object(body, "body") backup_name = body.pop(parser.Props.BACKUP_NAME, None) strategy = body.pop(parser.Props.STRATEGY, {}) validation.validate_unexpected_props(body, "body") backup = storage.create_backup( project_id, table_name, backup_name, strategy ) href_prefix = req.path_url response = parser.Parser.format_backup(backup, href_prefix) return response
def process_request(self, req, body, project_id): with probe.Probe(__name__ + ".validation"): validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_list = parser.Parser.parse_batch_get_request_items(request_items_json) result, unprocessed = storage.execute_get_batch(req.context, request_list) responses = {} for tname, res in result: if not res.items: continue table_items = responses.get(tname, None) if table_items is None: table_items = [] responses[tname] = table_items item = parser.Parser.format_item_attributes(res.items[0]) table_items.append(item) return { "responses": responses, "unprocessed_keys": parser.Parser.format_batch_get_unprocessed(unprocessed, request_items_json), }
def parse_attribute_conditions(cls, attribute_conditions_json, condition_class=models.IndexedCondition): attribute_conditions = {} for (attr_name, condition_json) in (attribute_conditions_json.iteritems()): validation.validate_attr_name(attr_name) validation.validate_object(condition_json, attr_name) condition_type_json = (condition_json.pop( Props.COMPARISON_OPERATOR, None)) attribute_list = condition_json.pop(Props.ATTRIBUTE_VALUE_LIST, None) condition_args = [] if attribute_list: validation.validate_list_of_objects(attribute_list, Props.ATTRIBUTE_VALUE_LIST) for typed_attribute_value in attribute_list: condition_args.append( cls.parse_typed_attr_value(typed_attribute_value)) attribute_conditions[attr_name] = (cls.parse_attribute_condition( condition_type_json, condition_args, condition_class)) validation.validate_unexpected_props(condition_json, attr_name) return attribute_conditions
def create_restore_job(req, project_id, table_name): """Creates a restore job.""" utils.check_project_id(project_id) with probe.Probe(__name__ + '.validation'): validation.validate_table_name(table_name) body = req.json_body validation.validate_object(body, "body") backup_id = body.pop(parser.Props.BACKUP_ID, None) if backup_id: backup_id = uuid.UUID(backup_id) source = body.pop(parser.Props.SOURCE, None) validation.validate_unexpected_props(body, "body") restore_job = storage.create_restore_job( project_id, table_name, backup_id, source ) href_prefix = req.path_url response = parser.Parser.format_restore_job(restore_job, href_prefix) return response
def parse_item_attributes(cls, item_attributes_json): item = {} for (attr_name_json, typed_attr_value_json) in (item_attributes_json.iteritems()): validation.validate_attr_name(attr_name_json) validation.validate_object(typed_attr_value_json, attr_name_json) item[attr_name_json] = cls.parse_typed_attr_value( typed_attr_value_json) return item
def parse_item_attributes(cls, item_attributes_json): item = {} for (attr_name_json, typed_attr_value_json) in ( item_attributes_json.iteritems()): validation.validate_attr_name(attr_name_json) validation.validate_object(typed_attr_value_json, attr_name_json) item[attr_name_json] = cls.parse_typed_attr_value( typed_attr_value_json ) return item
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
def parse_local_secondary_index(cls, local_secondary_index_json): key_attrs_json = local_secondary_index_json.pop(Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, Props.KEY_SCHEMA) key_attrs_for_projection = cls.parse_key_schema(key_attrs_json) hash_key = key_attrs_for_projection[0] try: range_key = key_attrs_for_projection[1] except IndexError: raise exception.ValidationError( _("Range key in index wasn't specified")) index_name = local_secondary_index_json.pop(Props.INDEX_NAME, None) validation.validate_index_name(index_name) projection_json = local_secondary_index_json.pop(Props.PROJECTION, None) validation.validate_object(projection_json, Props.PROJECTION) validation.validate_unexpected_props( local_secondary_index_json, "local_secondary_index" ) projection_type = projection_json.pop( Props.PROJECTION_TYPE, Values.PROJECTION_TYPE_INCLUDE ) if projection_type == Values.PROJECTION_TYPE_ALL: projected_attrs = None elif projection_type == Values.PROJECTION_TYPE_KEYS_ONLY: projected_attrs = tuple() elif projection_type == Values.PROJECTION_TYPE_INCLUDE: projected_attrs = projection_json.pop( Props.NON_KEY_ATTRIBUTES, None ) else: raise exception.ValidationError( _("Only '%(pt_all)', '%(pt_ko)' of '%(pt_incl)' projection " "types are allowed, but '%(projection_type)s' is found"), pt_all=Values.PROJECTION_TYPE_ALL, pt_ko=Values.PROJECTION_TYPE_KEYS_ONLY, pt_incl=Values.PROJECTION_TYPE_INCLUDE, projection_type=projection_type ) validation.validate_unexpected_props(projection_json, Props.PROJECTION) return index_name, models.IndexDefinition( hash_key, range_key, projected_attrs )
def parse_local_secondary_index(cls, local_secondary_index_json): key_attrs_json = local_secondary_index_json.pop(Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, Props.KEY_SCHEMA) key_attrs_for_projection = cls.parse_key_schema(key_attrs_json) hash_key = key_attrs_for_projection[0] try: range_key = key_attrs_for_projection[1] except IndexError: raise ValidationError(_("Range key in index wasn't specified")) index_name = local_secondary_index_json.pop(Props.INDEX_NAME, None) validation.validate_index_name(index_name) projection_json = local_secondary_index_json.pop(Props.PROJECTION, None) validation.validate_object(projection_json, Props.PROJECTION) validation.validate_unexpected_props( local_secondary_index_json, "local_secondary_index" ) projection_type = projection_json.pop( Props.PROJECTION_TYPE, Values.PROJECTION_TYPE_INCLUDE ) if projection_type == Values.PROJECTION_TYPE_ALL: projected_attrs = None elif projection_type == Values.PROJECTION_TYPE_KEYS_ONLY: projected_attrs = tuple() elif projection_type == Values.PROJECTION_TYPE_INCLUDE: projected_attrs = projection_json.pop( Props.NON_KEY_ATTRIBUTES, None ) else: raise ValidationError( _("Only '%(pt_all)', '%(pt_ko)' of '%(pt_incl)' projection " "types are allowed, but '%(projection_type)s' is found"), pt_all=Values.PROJECTION_TYPE_ALL, pt_ko=Values.PROJECTION_TYPE_KEYS_ONLY, pt_incl=Values.PROJECTION_TYPE_INCLUDE, projection_type=projection_type ) validation.validate_unexpected_props(projection_json, Props.PROJECTION) return index_name, IndexDefinition( hash_key, range_key, projected_attrs )
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 parse_batch_write_request_items(cls, request_items_json): request_map = {} for table_name, request_list_json in request_items_json.iteritems(): validation.validate_table_name(table_name) validation.validate_list_of_objects(request_list_json, table_name) request_list_for_table = [] for request_json in request_list_json: for request_type, request_body in request_json.iteritems(): validation.validate_string(request_type, "request_type") if request_type == Props.REQUEST_PUT: validation.validate_object(request_body, request_type) item = request_body.pop(Props.ITEM, None) validation.validate_object(item, Props.ITEM) validation.validate_unexpected_props( request_body, request_type) request_list_for_table.append( models.WriteItemRequest.put( cls.parse_item_attributes(item))) elif request_type == Props.REQUEST_DELETE: validation.validate_object(request_body, request_type) key = request_body.pop(Props.KEY, None) validation.validate_object(key, Props.KEY) validation.validate_unexpected_props( request_body, request_type) request_list_for_table.append( models.WriteItemRequest.delete( cls.parse_item_attributes(key))) else: raise exception.ValidationError( _("Unsupported request type found: " "%(request_type)s"), request_type=request_type) request_map[table_name] = request_list_for_table return request_map
def process_request(self, req, body, project_id): with probe.Probe(__name__ + ".validation"): validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_map = parser.Parser.parse_batch_write_request_items(request_items_json) unprocessed_items = storage.execute_write_batch(req.context, request_map) return {"unprocessed_items": parser.Parser.format_request_items(unprocessed_items)}
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
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
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") # backup_id = body.pop(parser.Props.BACKUP_ID, None) # source = body.pop(parser.Props.SOURCE, None) validation.validate_unexpected_props(body, "body") restore_job = None href_prefix = req.path_url response = parser.Parser.format_restore_job(restore_job, href_prefix) return response
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_table_name(table_name) validation.validate_object(body, "body") backup_name = body.pop(parser.Props.BACKUP_NAME, None) strategy = body.pop(parser.Props.STRATEGY, {}) validation.validate_unexpected_props(body, "body") backup = storage.create_backup(req.context, table_name, backup_name, strategy) href_prefix = req.path_url response = parser.Parser.format_backup(backup, href_prefix) return response
def process_request(self, req, body, project_id): with probe.Probe(__name__ + '.validation'): validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_map = parser.Parser.parse_batch_write_request_items( request_items_json) unprocessed_items = storage.execute_write_batch( req.context, request_map) return { 'unprocessed_items': parser.Parser.format_request_items(unprocessed_items) }
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_table_name(table_name) validation.validate_object(body, "body") backup_name = body.pop(parser.Props.BACKUP_NAME, None) strategy = body.pop(parser.Props.STRATEGY, {}) validation.validate_unexpected_props(body, "body") backup = storage.create_backup(req.context, table_name, backup_name, strategy) href_prefix = req.path_url response = parser.Parser.format_backup(backup, href_prefix) return response
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
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
def batch_get_item(req, project_id): """The BatchGetitem operation returns the attributes of one or more items from one or more tables. """ with probe.Probe(__name__ + '.validation'): body = req.json_body validation.validate_object(body, "body") request_items_json = body.pop(parser.Props.REQUEST_ITEMS, None) validation.validate_object(request_items_json, parser.Props.REQUEST_ITEMS) validation.validate_unexpected_props(body, "body") # parse request_items request_list = parser.Parser.parse_batch_get_request_items( request_items_json ) result, unprocessed = storage.execute_get_batch(project_id, request_list) responses = {} for tname, res in result: if not res.items: continue table_items = responses.get(tname, None) if table_items is None: table_items = [] responses[tname] = table_items item = parser.Parser.format_item_attributes(res.items[0]) table_items.append(item) return { 'responses': responses, 'unprocessed_keys': parser.Parser.format_batch_get_unprocessed( unprocessed, request_items_json) }
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 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
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
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 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 parse_batch_write_request_items(cls, request_items_json): request_map = {} for table_name, request_list_json in request_items_json.iteritems(): validation.validate_table_name(table_name) validation.validate_list_of_objects(request_list_json, table_name) request_list_for_table = [] for request_json in request_list_json: for request_type, request_body in request_json.iteritems(): validation.validate_string(request_type, "request_type") if request_type == Props.REQUEST_PUT: validation.validate_object(request_body, request_type) item = request_body.pop(Props.ITEM, None) validation.validate_object(item, Props.ITEM) validation.validate_unexpected_props(request_body, request_type) request_list_for_table.append( models.WriteItemRequest.put( cls.parse_item_attributes(item) ) ) elif request_type == Props.REQUEST_DELETE: validation.validate_object(request_body, request_type) key = request_body.pop(Props.KEY, None) validation.validate_object(key, Props.KEY) validation.validate_unexpected_props(request_body, request_type) request_list_for_table.append( models.WriteItemRequest.delete( cls.parse_item_attributes(key) ) ) else: raise exception.ValidationError( _("Unsupported request type found: " "%(request_type)s"), request_type=request_type ) request_map[table_name] = request_list_for_table return request_map
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 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
def scan(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) select = body.pop(parser.Props.SELECT, None) if select is None: if attributes_to_get: select = models.SelectType.SELECT_TYPE_SPECIFIC else: select = models.SelectType.SELECT_TYPE_ALL else: validation.validate_string(select, parser.Props.SELECT) select_type = models.SelectType(select, attributes_to_get) limit = body.pop(parser.Props.LIMIT, None) if limit is not None: limit = validation.validate_integer(limit, parser.Props.LIMIT, min_val=0) # 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 scan_filter_json = body.pop(parser.Props.SCAN_FILTER, None) if scan_filter_json: validation.validate_object(scan_filter_json, parser.Props.SCAN_FILTER) condition_map = parser.Parser.parse_attribute_conditions( scan_filter_json, condition_class=ScanCondition ) else: condition_map = None total_segments = body.pop(parser.Props.TOTAL_SEGMENTS, 1) total_segments = validation.validate_integer( total_segments, parser.Props.TOTAL_SEGMENTS, min_val=1, max_val=4096 ) segment = body.pop(parser.Props.SEGMENT, 0) segment = validation.validate_integer( segment, parser.Props.SEGMENT, min_val=0, max_val=total_segments ) validation.validate_unexpected_props(body, "body") result = storage.scan( req.context, table_name, condition_map, attributes_to_get=attributes_to_get, limit=limit, exclusive_start_key=exclusive_start_key_attributes) 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
def create_table(self, req, body, project_id): with probe.Probe(__name__ + '.validate'): validation.validate_object(body, "body") table_name = body.pop(parser.Props.TABLE_NAME, None) validation.validate_table_name(table_name) # parse table attributes attribute_definitions_json = body.pop( parser.Props.ATTRIBUTE_DEFINITIONS, None) validation.validate_list_of_objects( attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS) attribute_definitions = parser.Parser.parse_attribute_definitions( attribute_definitions_json) # parse table key schema key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA) key_attrs = parser.Parser.parse_key_schema(key_attrs_json) # parse table indexed field list lsi_defs_json = body.pop(parser.Props.LOCAL_SECONDARY_INDEXES, None) if lsi_defs_json: validation.validate_list_of_objects( lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES) index_def_map = parser.Parser.parse_local_secondary_indexes( lsi_defs_json) else: index_def_map = {} validation.validate_unexpected_props(body, "body") # prepare table_schema structure table_schema = models.TableSchema(attribute_definitions, key_attrs, index_def_map) table_meta = storage.create_table(req.context, table_name, table_schema) url = req.path_url + "/" + table_name bookmark = req.path_url + "/" + table_name result = { parser.Props.TABLE_DESCRIPTION: { parser.Props.ATTRIBUTE_DEFINITIONS: (parser.Parser.format_attribute_definitions( table_meta.schema.attribute_type_map)), parser.Props.CREATION_DATE_TIME: table_meta.creation_date_time, parser.Props.ITEM_COUNT: 0, parser.Props.KEY_SCHEMA: (parser.Parser.format_key_schema( table_meta.schema.key_attributes)), parser.Props.TABLE_ID: str(table_meta.id), parser.Props.TABLE_NAME: table_name, parser.Props.TABLE_STATUS: (parser.Parser.format_table_status(table_meta.status)), 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 }] } } if table_meta.schema.index_def_map: table_def = result[parser.Props.TABLE_DESCRIPTION] table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = ( parser.Parser.format_local_secondary_indexes( table_meta.schema.key_attributes[0], table_meta.schema.index_def_map)) return result
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
def create_table(self, req, body, project_id): with probe.Probe(__name__ + '.validate'): validation.validate_object(body, "body") table_name = body.pop(parser.Props.TABLE_NAME, None) validation.validate_table_name(table_name) # parse table attributes attribute_definitions_json = body.pop( parser.Props.ATTRIBUTE_DEFINITIONS, None ) validation.validate_list_of_objects( attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS ) attribute_definitions = parser.Parser.parse_attribute_definitions( attribute_definitions_json ) # parse table key schema key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA) key_attrs = parser.Parser.parse_key_schema(key_attrs_json) # parse table indexed field list lsi_defs_json = body.pop( parser.Props.LOCAL_SECONDARY_INDEXES, None ) if lsi_defs_json: validation.validate_list_of_objects( lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES ) index_def_map = parser.Parser.parse_local_secondary_indexes( lsi_defs_json ) else: index_def_map = {} # validate the uniqueness of table and its indices' key schema range_keys = [] if len(key_attrs) > 1: range_keys.append(key_attrs[1]) else: # table has hash type primary key if len(index_def_map) > 0: raise exception.ValidationError( _("Table without range key in primary key schema " "can not have indices")) for index in index_def_map.values(): range_keys.append(index.alt_range_key_attr) try: validation.validate_set(range_keys, "key_schema") except exception.ValidationError: raise exception.ValidationError( _("Table and its indices must have unique key schema")) validation.validate_unexpected_props(body, "body") # prepare table_schema structure table_schema = models.TableSchema( attribute_definitions, key_attrs, index_def_map) table_meta = storage.create_table( req.context, table_name, table_schema) url = req.path_url + "/" + table_name bookmark = req.path_url + "/" + table_name result = { parser.Props.TABLE_DESCRIPTION: { parser.Props.ATTRIBUTE_DEFINITIONS: ( parser.Parser.format_attribute_definitions( table_meta.schema.attribute_type_map ) ), parser.Props.CREATION_DATE_TIME: table_meta.creation_date_time, parser.Props.ITEM_COUNT: 0, parser.Props.KEY_SCHEMA: ( parser.Parser.format_key_schema( table_meta.schema.key_attributes ) ), parser.Props.TABLE_ID: str(table_meta.id), parser.Props.TABLE_NAME: table_name, parser.Props.TABLE_STATUS: ( parser.Parser.format_table_status(table_meta.status) ), 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 } ] } } if table_meta.schema.index_def_map: table_def = result[parser.Props.TABLE_DESCRIPTION] table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = ( parser.Parser.format_local_secondary_indexes( table_meta.schema.key_attributes[0], table_meta.schema.index_def_map ) ) return result
def scan(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) select = body.pop(parser.Props.SELECT, None) if select is None: if attributes_to_get: select = models.SelectType.SELECT_TYPE_SPECIFIC else: select = models.SelectType.SELECT_TYPE_ALL else: validation.validate_string(select, parser.Props.SELECT) select_type = models.SelectType(select, attributes_to_get) limit = body.pop(parser.Props.LIMIT, None) if limit is not None: limit = validation.validate_integer(limit, parser.Props.LIMIT, min_val=0) # 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 scan_filter_json = body.pop(parser.Props.SCAN_FILTER, None) if scan_filter_json: validation.validate_object(scan_filter_json, parser.Props.SCAN_FILTER) condition_map = parser.Parser.parse_attribute_conditions( scan_filter_json, condition_class=ScanCondition) else: condition_map = None total_segments = body.pop(parser.Props.TOTAL_SEGMENTS, 1) total_segments = validation.validate_integer( total_segments, parser.Props.TOTAL_SEGMENTS, min_val=1, max_val=4096) segment = body.pop(parser.Props.SEGMENT, 0) segment = validation.validate_integer(segment, parser.Props.SEGMENT, min_val=0, max_val=total_segments) validation.validate_unexpected_props(body, "body") result = storage.scan( req.context, table_name, condition_map, attributes_to_get=attributes_to_get, limit=limit, exclusive_start_key=exclusive_start_key_attributes) 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
def create_table(self, req, body, project_id): utils.check_project_id(req.context, project_id) req.context.tenant = project_id with probe.Probe(__name__ + '.validate'): validation.validate_object(body, "body") table_name = body.pop(parser.Props.TABLE_NAME, None) validation.validate_table_name(table_name) # parse table attributes attribute_definitions_json = body.pop( parser.Props.ATTRIBUTE_DEFINITIONS, None ) validation.validate_list_of_objects( attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS ) attribute_definitions = parser.Parser.parse_attribute_definitions( attribute_definitions_json ) # parse table key schema key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA) key_attrs = parser.Parser.parse_key_schema(key_attrs_json) # parse table indexed field list lsi_defs_json = body.pop( parser.Props.LOCAL_SECONDARY_INDEXES, None ) if lsi_defs_json: validation.validate_list_of_objects( lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES ) index_def_map = parser.Parser.parse_local_secondary_indexes( lsi_defs_json ) else: index_def_map = {} validation.validate_unexpected_props(body, "body") # prepare table_schema structure table_schema = models.TableSchema( attribute_definitions, key_attrs, index_def_map) table_meta = storage.create_table( req.context, table_name, table_schema) url = req.path_url + "/" + table_name bookmark = req.path_url + "/" + table_name result = { parser.Props.TABLE_DESCRIPTION: { parser.Props.ATTRIBUTE_DEFINITIONS: ( parser.Parser.format_attribute_definitions( table_meta.schema.attribute_type_map ) ), parser.Props.CREATION_DATE_TIME: table_meta.creation_date_time, parser.Props.ITEM_COUNT: 0, parser.Props.KEY_SCHEMA: ( parser.Parser.format_key_schema( table_meta.schema.key_attributes ) ), parser.Props.TABLE_NAME: table_name, parser.Props.TABLE_STATUS: ( parser.Parser.format_table_status(table_meta.status) ), 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 } ] } } if table_meta.schema.index_def_map: table_def = result[parser.Props.TABLE_DESCRIPTION] table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = ( parser.Parser.format_local_secondary_indexes( table_meta.schema.key_attributes[0], table_meta.schema.index_def_map ) ) return result
def create_table(self, req, body, project_id): with probe.Probe(__name__ + '.validate'): validation.validate_object(body, "body") table_name = body.pop(parser.Props.TABLE_NAME, None) validation.validate_table_name(table_name) # parse table attributes attribute_definitions_json = body.pop( parser.Props.ATTRIBUTE_DEFINITIONS, None) validation.validate_list_of_objects( attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS) attribute_definitions = parser.Parser.parse_attribute_definitions( attribute_definitions_json) # parse table key schema key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None) validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA) key_attrs = parser.Parser.parse_key_schema(key_attrs_json) # parse table indexed field list lsi_defs_json = body.pop(parser.Props.LOCAL_SECONDARY_INDEXES, None) if lsi_defs_json: validation.validate_list_of_objects( lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES) index_def_map = parser.Parser.parse_local_secondary_indexes( lsi_defs_json) else: index_def_map = {} # validate the uniqueness of table and its indices' key schema range_keys = [] if len(key_attrs) > 1: range_keys.append(key_attrs[1]) else: # table has hash type primary key if len(index_def_map) > 0: raise exception.ValidationError( _("Table without range key in primary key schema " "can not have indices")) for index in index_def_map.values(): range_keys.append(index.alt_range_key_attr) try: validation.validate_set(range_keys, "key_schema") except exception.ValidationError: raise exception.ValidationError( _("Table and its indices must have unique key schema")) validation.validate_unexpected_props(body, "body") # prepare table_schema structure table_schema = models.TableSchema(attribute_definitions, key_attrs, index_def_map) table_meta = storage.create_table(req.context, table_name, table_schema) url = req.path_url + "/" + table_name bookmark = req.path_url + "/" + table_name result = { parser.Props.TABLE_DESCRIPTION: { parser.Props.ATTRIBUTE_DEFINITIONS: (parser.Parser.format_attribute_definitions( table_meta.schema.attribute_type_map)), parser.Props.CREATION_DATE_TIME: table_meta.creation_date_time, parser.Props.ITEM_COUNT: 0, parser.Props.KEY_SCHEMA: (parser.Parser.format_key_schema( table_meta.schema.key_attributes)), parser.Props.TABLE_ID: str(table_meta.id), parser.Props.TABLE_NAME: table_name, parser.Props.TABLE_STATUS: (parser.Parser.format_table_status(table_meta.status)), 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 }] } } if table_meta.schema.index_def_map: table_def = result[parser.Props.TABLE_DESCRIPTION] table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = ( parser.Parser.format_local_secondary_indexes( table_meta.schema.key_attributes[0], table_meta.schema.index_def_map)) return result
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