Listing user of specific or all compute host.
#!/bin/bash
#v1 by [email protected]
echo -e "\n=================================================="
echo "1 - JKT"
echo "2 - WJV" 
echo "3 - Exit"
echo "=================================================="
read -p "Choose Region : " REGION

F_LIST_HOST_COMPUTE () {
    openstack compute service list | grep -i compute | awk '{print $6 "\t" $8 "\t" $10}' | nl
}

F_COMPUTE_OPTION () { 
    TANGGAL=$(date +%d%h%Y)
    echo -e "\n=================================================="
    echo "1 - Specific Compute Host"
    echo "2 - All Compute Host" 
    echo "=================================================="
    read -p "Choose Option: " OPTION
    case $OPTION in
    1)
        echo ""
        read -p "Input Compute Name: " COMPUTE_NAME
        echo -e "\nThe program will start to generate Project ID of all instance in $COMPUTE_NAME ..."; sleep 3s
        echo "Wait a moment ..."; sleep 3s
        for i in `openstack server list --all --host $COMPUTE_NAME -f value -c ID`; do openstack server show $i | grep project_id | awk '{print $4}'; done | tee -a project_$COMPUTE_NAME$TANGGAL.txt
        mv project_$COMPUTE_NAME$TANGGAL.txt result/
        echo -e "\nFinish. Project ID of all instance data can be found at project_$COMPUTE_NAME$TANGGAL.txt ..."; sleep 3s

        echo -e "\nNext, the program will start to generate email user based on Project ID ..."; sleep 3s
        echo -e "Wait for 1 hour (depend on how many project is) ...\n"; sleep 3s

        for i in `cat result/project_$COMPUTE_NAME$TANGGAL.txt`; do openstack user list --project $i -f value -c Name | egrep -o "\b[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z0-9.-]+\b"; done | tee -a list_$COMPUTE_NAME$TANGGAL.txt
        mv list_$COMPUTE_NAME$TANGGAL.txt result/
        echo -e "\nFinish. Email user data can be found at list_$COMPUTE_NAME$TANGGAL.txt ..."; sleep 3s
        ;;
    2)
        echo -e "\nThe program will start to generate Project ID of all instance ..."; sleep 3s
        echo "Wait for 1 hour (depend on how many instance is) ..."; sleep 3s
        for i in `openstack server list --all -f value -c ID`; do openstack server show $i -f value -c project_id; done | tee -a project_list$TANGGAL.txt
        mv project_list$TANGGAL.txt result/
        echo "Finish. Project ID of all instance data can be found at projectid_list$TANGGAL.txt ..."; sleep 3s

        echo -e "\nNext, the program will start to generate email user based on Project ID ..."; sleep 3s
        echo "Wait for 1 hour (depend on how many project is) ..."; sleep 3s
        for i in `cat result/project_list$TANGGAL.txt`; do openstack user list --project $i -f value -c Name | egrep -o "\b[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z0-9.-]+\b"; done | tee -a email_list$TANGGAL.txt
        mv email_list$TANGGAL.txt result/
        echo -e "\nFinish. Email user data can be found at email_list$TANGGAL.txt ..."; sleep 3s
        ;;
    *)
        exit
        ;;
esac    
}

case $REGION in
    1)
        source xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-rc.sh
        echo -e "\nCompute Host List is ..."
        F_LIST_HOST_COMPUTE
        F_COMPUTE_OPTION
        ;;
    2)
        source xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-rc.sh
        echo -e "\nCompute Host List is ..."
        F_LIST_HOST_COMPUTE
        F_COMPUTE_OPTION
        ;;
    3)
        exit
        ;;
esac