Example #1
0
def validate_table_delete(table_id):
    if not table_id:
        raise abort(400, 'No table id specified.')
    table = table_wrapper.get_table(table_id)
    if not table:
        raise abort(400, 'The table doesn\'t exists')
    return table
Example #2
0
def validate_chain_delete(chain_id):
    if not chain_id:
        raise abort(400, 'No chain id specified.')
    chain = chain_wrapper.get_chain(chain_id)
    if not chain:
        raise abort(400, 'The chain doesn\'t exists')
    return chain
Example #3
0
def validate_table_delete(table_id):
    if not table_id:
        raise abort(400, 'No table id specified.')
    table = table_wrapper.get_table(table_id)
    if not table:
        raise abort(400, 'The table doesn\'t exists')
    return table
Example #4
0
def validate_new_chain(chain_json):
    chain = chain_json['chain']
    validation_error = NFTValidationError('chain')
    # JSON errors
    if not chain:
        raise abort(400, 'No "chain" field in chain json')
    if not chain['table']:
        raise abort(400, 'Chain has no table associated')
    if not chain['table'] in [
            t['id'] for t in table_wrapper.list_all_tables()
    ]:
        raise abort(
            400, 'The table {table_id} doesn\'t exist'.format(chain['table']))
    # Validate hook and type?
    # Validation errors
    if not chain['name']:
        validation_error.add_error('name', 'Este campo es necesario.')
    # If no hook or type is specified, no priority is needed
    # if chain['priority'] == None:  # Use this instead of "not" because 0 is true
    # validation_error.errors.append({'priority': 'Este campo es necesario.'})
    # Return
    if validation_error.has_errors():
        raise validation_error
    else:
        return chain
Example #5
0
def validate_chain_delete(chain_id):
    if not chain_id:
        raise abort(400, 'No chain id specified.')
    chain = chain_wrapper.get_chain(chain_id)
    if not chain:
        raise abort(400, 'The chain doesn\'t exists')
    return chain
Example #6
0
def dictionaries():
    '''
    GET:
      List all dictionaries in the system
    POST:
      Create a new dictionary in the system
    '''
    if request.method == 'POST':
        try:
            dictionary_json = dictionary_validator.validate_new_dictionary(request.get_json())
            dictionary = dictionary_wrapper.create_dictionary(dictionary_json)
            return jsonify(dictionary=dictionary)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(dictionaries=dictionary_wrapper.list_all_dictionaries())
Example #7
0
def dictionary(dictionary_id):
    '''
    GET:
      Get a dictionary by it's id
    PUT:
      Update the dictionary with the specified id
    '''
    if request.method == 'PUT':
        try:
            dictionary_json = dictionary_validator.validate_dictionary_update(request.get_json())
            dictionary = dictionary_wrapper.update_dictionary(dictionary_json)
            return jsonify(dictionary=dictionary)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(dictionary=dictionary_wrapper.get_dictionary(dictionary_id))
Example #8
0
def sets():
    '''
    GET:
      List all sets in the system
    POST:
      Create a new set in the system
    '''
    if request.method == 'POST':
        try:
            set_json = set_validator.validate_new_set(request.get_json())
            set = set_wrapper.create_set(set_json)
            return jsonify(set=set)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(sets=set_wrapper.list_all_sets())
Example #9
0
def set(set_id):
    '''
    GET:
      Get a set by it's id
    PUT:
      Update a set in the system with the specified id
    '''
    if request.method == 'PUT':
        try:
            set_json = set_validator.validate_set_update(request.get_json())
            set = set_wrapper.update_set(set_json)
            return jsonify(set=set)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(set=set_wrapper.get_set(set_id))
Example #10
0
def chain(chain_id):
    '''
    GET:
      Get a chain by it's id
    DELETE:
      Delete the chain with the specified id
    '''
    if request.method == 'DELETE':
        try:
            chain = chain_validator.validate_chain_delete(chain_id)
            chain = chain_wrapper.delete_chain(chain_id)
            return jsonify(chain=chain)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(chain=chain_wrapper.get_chain(chain_id))
Example #11
0
def dictionaries():
    '''
    GET:
      List all dictionaries in the system
    POST:
      Create a new dictionary in the system
    '''
    if request.method == 'POST':
        try:
            dictionary_json = dictionary_validator.validate_new_dictionary(
                request.get_json())
            dictionary = dictionary_wrapper.create_dictionary(dictionary_json)
            return jsonify(dictionary=dictionary)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(dictionaries=dictionary_wrapper.list_all_dictionaries())
Example #12
0
def tables():
    '''
    GET:
      List all tables in the system
    POST:
      Create a new table in the system
    '''
    if request.method == 'POST':
        try:
            table_json = table_validator.validate_new_table(request.get_json())
            table = table_wrapper.create_table(table_json)
            response = jsonify(table=table)
            response.status_code = 201
            return response
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(tables=table_wrapper.list_all_tables())
Example #13
0
def chains():
    '''
    GET:
      List all chains in the system
    POST:
      Create a new chain in the system
    '''
    if request.method == 'POST':
        try:
            chain_json = chain_validator.validate_new_chain(request.get_json())
            chain = chain_wrapper.create_chain(chain_json)
            r = jsonify(chain=chain)
            r.status_code = 201
            return r
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(chains=chain_wrapper.list_all_chains())
Example #14
0
def table(table_id):
    '''
    GET:
      Get a table by it's id
    DELETE:
      Delete the table with the specified id
    '''
    if request.method == 'DELETE':
        try:
            table = table_validator.validate_table_delete(table_id)
            table = table_wrapper.delete_table(table_id)
            response = make_response()
            response.status_code = 204
            return response
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(table=table_wrapper.get_table(table_id))
Example #15
0
def dictionary(dictionary_id):
    '''
    GET:
      Get a dictionary by it's id
    PUT:
      Update the dictionary with the specified id
    '''
    if request.method == 'PUT':
        try:
            dictionary_json = dictionary_validator.validate_dictionary_update(
                request.get_json())
            dictionary = dictionary_wrapper.update_dictionary(dictionary_json)
            return jsonify(dictionary=dictionary)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(
            dictionary=dictionary_wrapper.get_dictionary(dictionary_id))
Example #16
0
def validate_new_dictionary(dictionary_json):
    dictionary = dictionary_json['dictionary']
    validation_error = NFTValidationError('dictionary')
    # JSON errors
    if not dictionary:
        raise abort(400, 'No "dictionary" field in dictionary json')
    # Validation errors
    if not dictionary['name']:
        validation_error.add_error('name', 'Este campo es necesario.')
    if validation_error.has_errors():
        raise validation_error
    else:
        return dictionary
Example #17
0
def validate_new_set(set_json):
    set = set_json['set']
    validation_error = NFTValidationError('set')
    # JSON errors
    if not set:
        raise abort(400, 'No "set" field in set json')
    # Validation errors
    if not set['name']:
        validation_error.add_error('name', 'Este campo es necesario.')
    if validation_error.has_errors():
        raise validation_error
    else:
        return set
Example #18
0
def validate_new_chain(chain_json):
    chain = chain_json['chain']
    validation_error = NFTValidationError('chain')
    # JSON errors
    if not chain:
        raise abort(400, 'No "chain" field in chain json')
    if not chain['table']:
        raise abort(400, 'Chain has no table associated')
    if not chain['table'] in [t['id'] for t in table_wrapper.list_all_tables()]:
        raise abort(400, 'The table {table_id} doesn\'t exist'.format(chain['table']))
    # Validate hook and type?
    # Validation errors
    if not chain['name']:
        validation_error.add_error('name', 'Este campo es necesario.')
    # If no hook or type is specified, no priority is needed
    # if chain['priority'] == None:  # Use this instead of "not" because 0 is true
        # validation_error.errors.append({'priority': 'Este campo es necesario.'})
    # Return
    if validation_error.has_errors():
        raise validation_error
    else:
        return chain
Example #19
0
def restore_backup():
    '''
    POST:
      Receive a backup file and load it into the system
    '''
    with tempfile.NamedTemporaryFile(suffix='.nft', delete=False) as tf:
        backup = request.files['file'].read()
        tf.write(backup)
    cmd = nft_utils.nft_command('-f ' + tf.name)
    cmd_result = cmd.wait()
    if cmd_result == 0:
        nft_utils.close_nft_command(cmd)
        os.remove(tf.name)
        return make_response('Backup restored')
    else:
        return abort(500, NFTError(Error(cmd.stdout.read())))
Example #20
0
def restore_backup():
    '''
    POST:
      Receive a backup file and load it into the system
    '''
    with tempfile.NamedTemporaryFile(suffix='.nft', delete=False) as tf:
        backup = request.files['file'].read()
        tf.write(backup)
    cmd = nft_utils.nft_command('-f ' + tf.name)
    cmd_result = cmd.wait()
    if cmd_result == 0:
        nft_utils.close_nft_command(cmd)
        os.remove(tf.name)
        return make_response('Backup restored')
    else:
        return abort(500, NFTError(Error(cmd.stdout.read())))
Example #21
0
def validate_set_update(set_json):
    set = set_json['set']
    # JSON errors
    if not set:
        raise abort(400, 'No "set" field in set json')
    return set
Example #22
0
def validate_dictionary_update(dictionary_json):
    dictionary = dictionary_json['dictionary']
    # JSON errors
    if not dictionary:
        raise abort(400, 'No "dictionary" field in dictionary json')
    return dictionary