def save_user():
    if request.method == 'POST':
        name = ""
        active_pools = []
        try:
            name = request.form['userName']
            auth_pools = request.form.getlist('pools[]')
            manage_pools = ManagePools()
            active_pools = manage_pools.get_active_pools()
            ManageUsers().add_user(name, auth_pools)

            user_info = ManageUsers().get_replication_user(name)
            session['success'] = "ui_admin_add_rep_user_success"
            result = session['success']
            session.pop("success")
            return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools ,user_info=user_info, success = result)

        except ReplicationException as e:
            if e.id == ReplicationException.CEPH_USER_EXIST or e.id == ReplicationException.SYSTEM_USER_EXIST:
                session['err'] = "ui_admin_add_rep_user_already_exist"
                logger.error(e)
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/add_user.html',name = name, err=result, active_pools = active_pools)
            elif e.id == ReplicationException.NOT_BACKUP_NODE:
                session['err'] = "ui_admin_no_backup_node"
                logger.error(e)
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/add_user.html',name = name, err=result, active_pools = active_pools)
            elif e.id == ReplicationException.GENERAL_EXCEPTION:
                session['err'] = "ui_admin_error_adding_user"
                logger.error(e)
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/add_user.html',name = name, err=result, active_pools = active_pools)

        except CephException as e:
            if e.id == CephException.CONNECTION_TIMEOUT:
                session['err'] = "ui_admin_ceph_time_out"
            elif e.id == CephException.GENERAL_EXCEPTION:
                session['err'] = "ui_admin_ceph_general_exception"
            logger.error(e)
            result = session["err"]
            session.pop("err")
            return render_template('/admin/replication/cluster_users/add_user.html',name = name, err=result, active_pools = active_pools)

        except ConsulException as e:
            logger.error(e)

        except Exception as e:
            logger.error(e)

        session['err'] = "ui_admin_error_adding_user"
        result = session["err"]
        session.pop("err")
        return render_template('/admin/replication/cluster_users/add_user.html', name = name,err=result, active_pools = active_pools)
def users_list():
    if request.method == 'GET' or request.method == 'POST':
        users_list = []
        message = "ui_admin_replication_user_info_message"
        try:
            users_list = ManageUsers().get_replication_users()
            if list_err in session:
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/users_list.html', users_list=users_list, err=result)

            elif list_success in session:
                result = session["success"]
                session.pop("success")
                return render_template('/admin/replication/cluster_users/users_list.html', users_list=users_list, success=result)

            elif list_warning in session:
                result = session["warning"]
                session.pop("warning")
                return render_template('/admin/replication/cluster_users/users_list.html', users_list=users_list, warning=result)

            else:
                return render_template('/admin/replication/cluster_users/users_list.html',info=message, users_list=users_list)

        except ConsulException as e:
            logger.error(e)

        except Exception as e:
            logger.error(e)

        return render_template('/admin/replication/cluster_users/users_list.html',info=message, users_list= users_list)
def reset_private_key(user_name):
    try:
        if request.method == 'GET' or request.method == 'POST':
            private_key = ManageUsers().reset_prv_key(user_name)
            return json.dumps(private_key)
    except ConsulException as e:
        logger.error(e)
def edit_user(name):
    auth_pools = []
    active_pools = []
    user_info = ""
    if request.method == 'GET' or request.method == 'POST':
        try:
            user_info = ManageUsers().get_replication_user(name)
            manage_pools = ManagePools()
            active_pools = manage_pools.get_active_pools()
            if list_err in session:
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/edit_user.html',user_info = user_info,active_pools = active_pools, err=result)

            elif list_success in session:
                result = session["success"]
                session.pop("success")
                return render_template('/admin/replication/cluster_users/edit_user.html',user_info = user_info,active_pools = active_pools, success=result)

            elif list_warning in session:
                result = session["warning"]
                session.pop("warning")
                return render_template('/admin/replication/cluster_users/edit_user.html',user_info = user_info,active_pools = active_pools, warning=result)

            else:
                return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools,user_info = user_info)

        except ConsulException as e:
            logger.error(e)

        except Exception as e:
            logger.error(e)

        return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools,user_info = user_info)
def remove_user(name):
    try:
        stat = ManageUsers().delete_replication_user(name)
        if stat:
            session['success'] = "ui_admin_delete_rep_user_success"
        else:
            session['err'] = "ui_admin_delete_rep_user_fail"

        return redirect(url_for('replication_users_controller.users_list'))

    except CephException as e:
            if e.id == CephException.CONNECTION_TIMEOUT:
                session['err'] = "ui_admin_ceph_time_out"
            elif e.id == CephException.GENERAL_EXCEPTION:
                session['err'] = "ui_admin_ceph_general_exception"
            logger.error(e)

    except ConsulException as e:
            logger.error(e)

    except Exception as e:
        logger.error(e)

    session['err'] = "ui_admin_delete_rep_user_fail"
    return redirect(url_for('replication_users_controller.users_list'))
def update_user(user_name):
    if request.method == 'GET' or request.method == 'POST':
        try:
            auth_pools = request.form.getlist('pools[]')
            manage_users = ManageUsers()
            status = manage_users.update_auth_pools(user_name, auth_pools)
            manage_pools = ManagePools()
            active_pools = manage_pools.get_active_pools()
            user_info = manage_users.get_replication_user(user_name)
            if not status:
                session['err'] = "ui_admin_error_updating_authorized_pools"
                result = session["err"]
                session.pop("err")
                return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools ,user_info = user_info, err = result)

            session['success'] = "ui_admin_update_authorized_pools_success"
            result = session['success']
            session.pop("success")
            return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools,user_info = user_info, success = result)

        except ReplicationException as e:
            logger.error(e)
            session['err'] = "ui_admin_error_updating_authorized_pools"
            result = session["err"]
            session.pop("err")
            return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools ,user_info = user_info, err = result)

        except CephException as e:
            if e.id == CephException.CONNECTION_TIMEOUT:
                session['err'] = "ui_admin_ceph_time_out"
            elif e.id == CephException.GENERAL_EXCEPTION:
                session['err'] = "ui_admin_ceph_general_exception"
            logger.error(e)
            result = session["err"]
            session.pop("err")
            return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools,user_info = user_info, err = result)

        except ConsulException as e:
            logger.error(e)

        except Exception as e:
            logger.error(e)

        session['err'] = "ui_admin_error_updating_authorized_pools"
        result = session["err"]
        session.pop("err")
        return render_template('/admin/replication/cluster_users/edit_user.html',active_pools = active_pools ,user_info = user_info, err = result)
def update_node_manage_roles(node_name):
    try:
        try:
            if request.form['option_iscsi'] == 'iscsi':
                is_iscsi = 1
        except Exception as ex:
            is_iscsi = 0
            logger.error(ex)
        try:
            if request.form['option_storage'] == 'storage':
                is_storage = 1
        except Exception as ex:
            is_storage = 0
            logger.error(ex)
        try:
            if request.form['backup'] == 'backup_node':
                is_backup = 1
        except Exception as ex:
            is_backup = 0
            logger.error(ex)
        manage_node = ManageNode()
        try:
            manage_node.update_node_role(node_name, is_storage, is_iscsi,
                                         is_backup)
            manage_users = ManageUsers()
            stat = manage_users.sync_users(node_name)
            session['success'] = "ui_node_update_node_role_succ"
            return redirect(url_for('node_controller.node_list'))

        except Exception as ex:
            session['err'] = "ui_node_update_node_role_err"
            return redirect(
                url_for('node_controller.node_manage_roles',
                        node_name=node_name))

    except Exception as e:
        session['err'] = "ui_node_update_node_role_exep"
        return redirect(
            url_for('node_controller.node_manage_roles', node_name=node_name),
            307)
def get_user_details(user_name):
    if request.method == 'GET' or request.method == 'POST':
        user = ManageUsers().get_replication_user(user_name)
        user_dict = user.__dict__
        return json.dumps(user_dict)