"""Routines that use libgfapi to perform gluster volume operations.

This exports:
  get_gluster_dir_list - Get the contents of a gluster dir using libgfapi.
  create_gluster_dir - Create a directory in a gluster volume using libgfapi.

"""
from glusterfs import gfapi


def get_gluster_dir_list(vol_name, path):
    dir_dict_list = []
    try:
        hostname = "127.0.0.1"
        vol = gfapi.Volume(hostname, vol_name)
        if not vol:
            raise Exception('Error accessing gluster volume')
        vol_mnt = vol.mount()
        if vol_mnt != 0:
            raise Exception(
                'Unable to mount the gluster volume : %s' % vol_name)
        if not path:
            d_dict = {'id': "/", 'text': "/", 'icon': 'fa fa-angle-right',
                      'children': True, 'data': {'dir': "/"}, 'parent': "#"}
            dir_dict_list.append(d_dict)
        else:
            dirs = vol.listdir(path)
            for d in dirs:
                true = True
                if vol.isdir(path + d + "/"):
                    parent = path
                    d_dict = {'id': path + d + "/", 'text': d, 'icon': 'fa fa-angle-right',
                              'children': True, 'data': {'dir': path + d + "/"}, 'parent': parent}
                    dir_dict_list.append(d_dict)
    except Exception, e:
        return None, 'Error getting gluster directory list : %s' % str(e)
    else:
        return dir_dict_list, None


def create_gluster_dir(vol_name, path, mode=0775):
    ret = False
    try:
        hostname = "127.0.0.1"
        vol = gfapi.Volume(hostname, vol_name)
        # print 'vol is ', vol
        vol_dir = None
        if vol:
            vol_mnt = vol.mount()
            exists = vol.exists(path)
            # print 'exists', exists
            if not exists:
                # print path,mode
                # print 'mode is ', oct(mode)
                vol_dir = vol.mkdir(path, mode)
                # print 'vol_dir', vol_dir
                vol_grp = vol.chown(path, uid=0, gid=1000)
                # print 'vol_grp', vol_grp
                if vol_dir == 0:
                    ret = True
                else:
                    ret = False
            else:
                raise Exception('A directory by that name already exists')
        else:
            raise Exception('Error accessing the volume')
    except Exception, e:
        return False, 'Error creating gluster directory : %s' % str(e)
    else:
        return ret, None


def remove_gluster_dir_tree(vol_name, path):
    try:
        hostname = "127.0.0.1"
        vol = gfapi.Volume(hostname, vol_name)
        vol_dir = None
        if vol:
            vol_mnt = vol.mount()
            exists = vol.exists(path)
            # print exists
            if not exists:
                raise Exception('The selected directory does not exist')
            vol_dir = vol.rmtree(path)
    except Exception, e:
        return False, 'Error removing gluster directory : %s' % str(e)
    else:
        return True, None

# vim: tabstop=8 softtabstop=0 expandtab ai shiftwidth=4 smarttab
