/**
 *
 * Delete heat stack pipeline
 *
 * General
 *  STACK_NAME                 Heat stack name
 *  STACK_TYPE                 Type of the stack (heat, aws)
 *
 * Heat parameters:
 *  OPENSTACK_API_URL            OpenStack API address
 *  OPENSTACK_API_CREDENTIALS    Credentials to the OpenStack API
 *  OPENSTACK_API_PROJECT        OpenStack project to connect to
 *  OPENSTACK_API_PROJECT_DOMAIN Domain for OpenStack project
 *  OPENSTACK_API_PROJECT_ID     ID for OpenStack project
 *  OPENSTACK_API_USER_DOMAIN    Domain for OpenStack user
 *  OPENSTACK_API_CLIENT         Versions of OpenStack python clients
 *  OPENSTACK_API_VERSION        Version of the OpenStack API (2/3)
 *
 * AWS parameters:
 *  AWS_API_CREDENTIALS        Credentials id AWS EC2 API
 *  AWS_DEFAULT_REGION         EC2 region
 */

common = new com.mirantis.mk.Common()
git = new com.mirantis.mk.Git()
openstack = new com.mirantis.mk.Openstack()
aws = new com.mirantis.mk.Aws()
salt = new com.mirantis.mk.Salt()
timeout(time: 12, unit: 'HOURS') {
    node {
        def workspace = common.getWorkspace()
        def venv_path = "${workspace}/venv"
        def env_vars

        // default STACK_TYPE is heat
        if (!common.validInputParam('STACK_TYPE')) {
            STACK_TYPE = 'heat'
        }

        stage('Install environment') {
            if (STACK_TYPE == 'heat') {

                def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'latest'
                openstack.setupOpenstackVirtualenv(venv_path, openstackVersion)

            } else if (STACK_TYPE == 'aws') {

                env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_DEFAULT_REGION)
                aws.setupVirtualEnv(venv_path)

            } else {
                throw new Exception('Stack type is not supported')
            }

        }

        stage('Delete stack') {
            if (STACK_TYPE == 'heat') {
                def openstackCloud = openstack.createOpenstackEnv(venv_path,
                    OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS,
                    OPENSTACK_API_PROJECT,OPENSTACK_API_PROJECT_DOMAIN,
                    OPENSTACK_API_PROJECT_ID, OPENSTACK_API_USER_DOMAIN,
                    OPENSTACK_API_VERSION)
                openstack.getKeystoneToken(openstackCloud, venv_path)

                common.infoMsg("Deleting Heat Stack " + STACK_NAME)
                openstack.deleteHeatStack(openstackCloud, STACK_NAME, venv_path)
            } else if (STACK_TYPE == 'aws') {

                aws.deleteStack(venv_path, env_vars, STACK_NAME)

            } else {
                throw new Exception('Stack type is not supported')
            }

        }
    }
}
