#!/bin/bash
set -eu
set -o pipefail

OVERCLOUD_NAME="overcloud"
SANITYTEST_CONTENT_NAME={{sanity_content_name}}

function source_rc {
    if [ $1 = "stackrc" ]; then
        cloud="Undercloud"
    else
        cloud="Overcloud"
    fi
    echo "You must source a $1 file for the $cloud."
    echo "Attempting to source $HOME/$1"
    source $HOME/$1
    echo "Done"
}

function stackrc_check {
    source_rc "stackrc"
}

function overcloudrc_check {
    source_rc "overcloudrc"
}

function run_cmd {
    if ! $@; then
        echo "Command: $@ FAILED" >&2
        exitval=1
    else
        echo "Command: $@ OK"
    fi
}

function overcloud_sanitytest_create {
    ENABLED_SERVICES=$@
    for service in $ENABLED_SERVICES; do
        case $service in
            "keystone" )
                run_cmd openstack user create ${SANITYTEST_CONTENT_NAME}
                run_cmd openstack user list
                ;;
            "glance_api" )
                run_cmd openstack image create ${SANITYTEST_CONTENT_NAME}
                run_cmd openstack image list
                ;;
            "neutron_api" )
                run_cmd openstack network create ${SANITYTEST_CONTENT_NAME}
                run_cmd openstack network list
                ;;
            "cinder_api" )
                run_cmd openstack volume create ${SANITYTEST_CONTENT_NAME} --size 1
                run_cmd openstack volume list
                ;;
            "heat_api" )
                echo "heat_template_version: newton" > /tmp/${SANITYTEST_CONTENT_NAME}.yaml
                run_cmd openstack stack create ${SANITYTEST_CONTENT_NAME} --template /tmp/${SANITYTEST_CONTENT_NAME}.yaml
                run_cmd openstack stack list
                ;;
            "swift_proxy" )
                run_cmd openstack container create ${SANITYTEST_CONTENT_NAME}
                run_cmd openstack container list
                ;;
            "sahara_api" )
                # glance_api must also be enabled
                run_cmd openstack image create sahara_${SANITYTEST_CONTENT_NAME}
                run_cmd openstack dataprocessing image register sahara_${SANITYTEST_CONTENT_NAME} --username centos
                run_cmd openstack dataprocessing image list
                ;;
        esac
    done
}

function overcloud_sanitytest_check {
    ENABLED_SERVICES=$@
    for service in $ENABLED_SERVICES; do
        case $service in
            "keystone" )
                run_cmd openstack user show ${SANITYTEST_CONTENT_NAME}
                ;;
            "glance_api" )
                run_cmd openstack image show ${SANITYTEST_CONTENT_NAME}
                ;;
            "neutron_api" )
                run_cmd openstack network show ${SANITYTEST_CONTENT_NAME}
                ;;
            "cinder_api" )
                run_cmd openstack volume show ${SANITYTEST_CONTENT_NAME}
                ;;
            "heat_api" )
                run_cmd openstack stack show ${SANITYTEST_CONTENT_NAME}
                # FIXME(shardy): It'd be good to add pre/post upgrade checks
                # on the actual version, but this is still good for debugging
                run_cmd openstack orchestration template version list
                ;;
            "swift_proxy" )
                run_cmd openstack container show ${SANITYTEST_CONTENT_NAME}
                ;;
            "sahara_api" )
                run_cmd openstack dataprocessing image show sahara_${SANITYTEST_CONTENT_NAME}
                ;;
        esac
    done
}

function overcloud_sanitytest_cleanup {
    ENABLED_SERVICES=$@
    for service in $ENABLED_SERVICES; do
        case $service in
            "keystone" )
                echo "Sanity test keystone"
                run_cmd openstack user delete ${SANITYTEST_CONTENT_NAME}
                ;;
            "glance_api" )
                run_cmd openstack image delete ${SANITYTEST_CONTENT_NAME}
                ;;
            "neutron_api" )
                run_cmd openstack network delete ${SANITYTEST_CONTENT_NAME}
                ;;
            "cinder_api" )
                run_cmd openstack volume delete ${SANITYTEST_CONTENT_NAME}
                ;;
            "heat_api" )
                run_cmd openstack stack delete --yes ${SANITYTEST_CONTENT_NAME}
                ;;
            "swift_proxy" )
                run_cmd openstack container delete ${SANITYTEST_CONTENT_NAME}
                ;;
            "sahara_api" )
                run_cmd openstack dataprocessing image unregister sahara_${SANITYTEST_CONTENT_NAME}
                run_cmd openstack image delete sahara_${SANITYTEST_CONTENT_NAME}
                ;;
        esac
    done
}

echo "Overcloud sanitytest"
exitval=0
stackrc_check

if heat stack-show "$OVERCLOUD_NAME" | grep "stack_status " | egrep -q "(CREATE|UPDATE)_COMPLETE"; then

    ENABLED_SERVICES=$(openstack stack output show overcloud EnabledServices -f json | \
                    jq -r ".output_value" | jq '.Controller | .[]' | tr "\n" " " | sed "s/\"//g")
    echo "Sanity Test, ENABLED_SERVICES=$ENABLED_SERVICES"

    overcloudrc_check

{% if sanitytest_create|bool %}
        overcloud_sanitytest_create $ENABLED_SERVICES
{% endif %}

    overcloud_sanitytest_check $ENABLED_SERVICES

{% if sanitytest_cleanup|bool %}
        overcloud_sanitytest_cleanup $ENABLED_SERVICES
{% endif %}

    if [ $exitval -eq 0 ]; then
        echo "Overcloud sanitytest SUCCEEDED"
    else
        echo "Overcloud sanitytest FAILED"
    fi
    exit $exitval
else
    echo "Overcloud sanitytest FAILED - No stack $OVERCLOUD_NAME."
    exit 1
fi
