def single_put(self, item): # Must be a full insert or update, don't support partial updates if self.schema is not None: # TODO: Be consistent, numeric strings in, numeric strings out # TODO: When debugging schema failure should throw try: self.schema(strict=True).validate(item) except marshmallow.exceptions.ValidationError as err: # TODO: Use JSON Encoder return { "error": "ValidationError", "detail": err.normalized_messages() }, 422 # Unprocessable Entity # Integer types need to be cast before insertion for name, field in self.schema._declared_fields.items(): if isinstance(field, marshmallow.fields.Integer): item[name] = int(item.get(name)) try: ret = get_table(self.table_name).put_item(Item=item, ReturnValues='ALL_OLD') except botocore.exceptions.ClientError as err: return { "error": "DatabaseError", "detail": err.response['Error']['Message'] }, 500 if ret.get('Attributes'): code = 200 # Update else: code = 201 # New return item, code
def _single_query(self, key): # Necessary when index lookup is performed qargs = { "KeyConditionExpression": Key(self.partition_key).eq(key), "ConsistentRead": False, } if self.index_name: qargs["IndexName"] = self.index_name # Optional try: response = get_table(self.table_name).query(**qargs) except botocore.exceptions.ClientError as err: return { "error": "DatabaseError", "detail": err.response['Error']['Message'] }, 500 if response.get('Count') == 0: return {}, 404 elif response.get('Count') == 1: return response[u'Items'][0] else: return { "error": "DatabaseError", "detail": "single_get() returned multiple values. This function is not suitable for ranged values." }, 500
def assert_has_unit(name): try: ret = get_table('unit').get_item(Key={'name':name}) except botocore.exceptions.ClientError as err: return {"error":"DatabaseError", "detail":err.response['Error']['Message']}, 500 if ret.get('Item') is None: return {}, 404
def _verify_unit_exists(name): try: unit_response = get_table('unit').query( KeyConditionExpression=Key('name').eq(name), ConsistentRead=False, ) return not unit_response.get('Count') == 0 except botocore.exceptions.ClientError: return False
def _single_get(self, key): try: ret = get_table(self.table_name).get_item(Key={self.partition_key:key}) except botocore.exceptions.ClientError as err: return {"error":"DatabaseError", "detail":err.response['Error']['Message']}, 500 if ret.get('Item'): return ret.get('Item'), 200 else: return {}, 404
def _verify_role_exists(name_set): logger = logging.getLogger(__name__) logger.debug('VERIFY ROLE: %s', name_set) if name_set == set(['none']): # Dummy entry put in to avoid empty list return True for name in name_set: try: role_response = get_table('role').query( KeyConditionExpression=Key('name').eq(name), ConsistentRead=False, ) if role_response.get('Count') == 0: return False except botocore.exceptions.ClientError: return False return True
def list_get(self, key): if self.schema is not None: # Integer types need to be cast before use field_type = self.schema._declared_fields.get(self.partition_key) if isinstance(field_type, marshmallow.fields.Integer): key = int(key) qargs = { "KeyConditionExpression" : Key(self.partition_key).eq(key), "ConsistentRead" : False, } if self.index_name: qargs["IndexName"] = self.index_name # Optional try: response = get_table(self.table_name).query(**qargs) except botocore.exceptions.ClientError as err: return {"error":"DatabaseError", "detail":err.response['Error']['Message']}, 500 return response[u'Items']