def GetBeboDomain(env) {
    if (env == "ptr") {
        return "bebo-ptr.com"
    }
    if (env == "bebo-prod") {
        return "bebo.com"
    }
    return "bebo-dev.com"
}
def ActivateVPC(env, params) {
    def beboDomain=GetBeboDomain(params.BEBO_ENV)
    echo (beboDomain)
    def url = "https://$params.VPC_ID-consul.$beboDomain/v1/kv/config/vpc/active"
    echo (url)
    def response = httpRequest url:url,
        httpMode:'PUT',
        requestBody:'true'

    echo ("Activate Response: "+response.getStatus())
}

def DestroyVpc(env, params) {
    sh "${env.WORKSPACE}/virtualenv/bin/python ${env.WORKSPACE}/terraform/terraform.py destroy --aws-region ${params.AWS_REGION} --vpc-id ${params.VPC_ID} -yf"
}

def PushToGit(env, params) {
    sh "git checkout -b ${params.VPC_ID} && git add . && git commit -am '${params.VPC_ID} build -- region: ${params.AWS_REGION}, status: ${currentBuild.result}, env: ${params.BEBO_ENV}' && git push --set-upstream origin ${params.VPC_ID}"
}

def CheckIfDestroy(env, params) {
    def shouldDestroy = input(
        id: 'destroy_prompt', message: 'Destroy resources?', parameters: [
            booleanParam(name:'destroy', defaultValue: true, description: 'By pressing yes, you will destroy this VPC. Are you sure?')
        ]
    )
    if(shouldDestroy) {
        DestroyVpc(env, params)
    }
    PushToGit(env, params)
}

pipeline {
    agent any
    stages {
        stage('Setup Virtualenv') {
            steps {
                sh 'rm -rf ./virtualenv && python3.5 -m venv virtualenv && . ./virtualenv/bin/activate && python --version && which python && pip --version && which pip && pip install wheel && pip install -r ./terraform/requirements.txt'
            }
        }
        stage('Gen') {
            steps {
                sh "${env.WORKSPACE}/virtualenv/bin/python ${env.WORKSPACE}/terraform/terraform.py create --aws-region ${params.AWS_REGION} --vpc-id ${params.VPC_ID} --bebo-env ${params.BEBO_ENV} --dry-run -y"
            }
        }
        stage('Prompt') {
            steps {
                slackSend channel: '#terraform', color: 'warning', message: "The ${params.VPC_ID} VPC (${params.BEBO_ENV}) needs confirmation before proceeding... Please do so @ http://jenkins-stretch.bebo-dev.com/job/bebo-terraform/ :)"
                input message: 'TFVars and Plan look good?', id: 'gen_prompt'
            }
        }
        stage('Apply Terraform') {
            steps {
                sh "${env.WORKSPACE}/virtualenv/bin/python ${env.WORKSPACE}/terraform/terraform.py create --aws-region ${params.AWS_REGION} --vpc-id ${params.VPC_ID} --bebo-env ${params.BEBO_ENV} -y"
            }
        }
        stage('Activate') {
            steps {
                slackSend channel: '#terraform', color: 'warning', message: "The ${params.VPC_ID} VPC (${params.BEBO_ENV}) needs confirmation before activating... Please do so @ http://jenkins-stretch.bebo-dev.com/job/bebo-terraform/ :)"
                script {
                    def activateInput = input(
                        id: 'activate_prompt', message: 'activate?', parameters: [
                            booleanParam(name:'activate', defaultValue: true, description: 'By pressing yes, you will make this VPC active in this region. Are you sure?')
                        ]
                    )
                    if(activateInput) {
                        ActivateVPC(env, params)
                    }
                }
            }
        }
    }
    post {
        success {
            slackSend channel: '#terraform', color: 'good', message: "The ${params.VPC_ID} (${params.BEBO_ENV}) VPC is up :) :tada:"
            PushToGit(env, params)
        }
        failure {
            slackSend channel: '#terraform', color: 'danger', message: "The ${params.VPC_ID} (${params.BEBO_ENV}) VPC has failed :feelsbadman: #blamejohnny? Please confirm manually in Jenkins (http://jenkins-stretch.bebo-dev.com/job/bebo-terraform/${env.BUILD_NUMBER}/consoleFull) and confirm VPC is failure."
            CheckIfDestroy(env, params)
        }
    }
}
