'Update config'

@api.validate(raw_config_schema)
@api.response(200, model=base_message_schema, description=u'Successfully    updated    config')
@api.response(BadRequest)
@api.response(APIError)
@api.doc(description=u'Config    file    must    be    base64    encoded.    A    backup    will    be    created,    and    if    successful    config    will    be    loaded    and    saved    to    original    file.')
def post(self, session=None):

    data = request.json
    try:
        raw_config = base64.b64decode(data[u'raw_config'])
    except (TypeError, binascii.Error):
        raise BadRequest(message=u'payload    was    not    a    valid    base64    encoded    string')
    try:
        config = yaml.safe_load(raw_config)
    except YAMLError as e:
        if (hasattr(e, u'problem') and hasattr(e, u'context_mark') and hasattr(e, u'problem_mark')):
            error = {}
            if (e.problem is not None):
                error.update({u'reason': e.problem})
            if (e.context_mark is not None):
                error.update({u'line': e.context_mark.line, u'column': e.context_mark.column})
            if (e.problem_mark is not None):
                error.update({u'line': e.problem_mark.line, u'column': e.problem_mark.column})
            raise BadRequest(message=u'Invalid    YAML    syntax', payload=error)
    try:
        backup_path = self.manager.update_config(config)
    except ValueError as e:
        errors = []
        for er in e.errors:
            errors.append({u'error': er.message, u'config_path': er.json_pointer})
        raise BadRequest(message=(u'Error    loading    config:    %s' % e.args[0]), payload={u'errors': errors})
    try:
        self.manager.backup_config()
    except Exception as e:
        raise APIError(message=u'Failed    to    create    config    backup,    config    updated    but    NOT    written    to    file', payload={u'reason': str(e)})
    try:
        with open(self.manager.config_path, u'w', encoding=u'utf-8') as f:
            f.write(raw_config.decode(u'utf-8').replace(u'\r\n', u'\n'))
    except Exception as e:
        raise APIError(message=u'Failed    to    write    new    config    to    file,    please    load    from    backup', payload={u'reason': str(e), u'backup_path': backup_path})
    return success_response(u'Config    was    loaded    and    successfully    updated    to    file')
