---
parameters:
  - name: terraform_action
    default: 'apply'
  - name: root_module_folder_relative_path
    default: '.'
  - name: terraform_cli_version
    default: 'latest'

stages:
  - stage: plan
    displayName: Plan
    variables:
      - group: ${variable_group_name}
      - name: 'selfHostedAgent'
        value: '${self_hosted_agent}'
    jobs:
      - deployment: plan
        displayName: Plan with Terraform
        pool:
          ${agent_pool_configuration}
        environment: ${environment_name_plan}
        timeoutInMinutes: 0
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                  displayName: Checkout Terraform Module
                - template: helpers/terraform-installer.yaml
                  parameters:
                    terraformVersion: $${{ parameters.terraform_cli_version }}
                - template: helpers/terraform-init.yaml
                  parameters:
                    serviceConnection: '${service_connection_name_plan}'
                    backendAzureResourceGroupName: $(BACKEND_AZURE_RESOURCE_GROUP_NAME)
                    backendAzureStorageAccountName: $(BACKEND_AZURE_STORAGE_ACCOUNT_NAME)
                    backendAzureStorageAccountContainerName: $(BACKEND_AZURE_STORAGE_ACCOUNT_CONTAINER_NAME)
                    root_module_folder_relative_path: $${{ parameters.root_module_folder_relative_path }}
                - template: helpers/terraform-plan.yaml
                  parameters:
                    terraform_action: $${{ parameters.terraform_action }}
                    serviceConnection: '${service_connection_name_plan}'
                    root_module_folder_relative_path: $${{ parameters.root_module_folder_relative_path }}
                - task: CopyFiles@2
                  displayName: Create Module Artifact
                  inputs:
                    SourceFolder: '$(Build.SourcesDirectory)'
                    Contents: |
                      **/*
                      !.terraform/**/*
                      !.git/**/*
                      !.pipelines/**/*
                      !**/.terraform/**/*
                      !**/.git/**/*
                      !**/.pipelines/**/*
                    TargetFolder: '$(Build.ArtifactsStagingDirectory)'
                    CleanTargetFolder: true
                    OverWrite: true
                - task: PublishPipelineArtifact@1
                  displayName: Publish Module Artifact
                  inputs:
                    targetPath: '$(Build.ArtifactsStagingDirectory)'
                    artifact: 'module'
                    publishLocation: 'pipeline'
                - pwsh: |
                    terraform `
                    -chdir="$${{ parameters.root_module_folder_relative_path }}" `
                    show `
                    tfplan
                  displayName: Show the Plan for Review
  - stage: apply
    displayName: Apply
    dependsOn: plan
    variables:
      - group: ${variable_group_name}
      - name: 'selfHostedAgent'
        value: '${self_hosted_agent}'
    jobs:
      - deployment: apply
        displayName: Apply with Terraform
        pool:
          ${agent_pool_configuration}
        environment: ${environment_name_apply}
        timeoutInMinutes: 0
        strategy:
          runOnce:
            deploy:
              steps:
                - download: none
                - task: DownloadPipelineArtifact@2
                  displayName: Download Module Artifact
                  inputs:
                    buildType: 'current'
                    artifactName: 'module'
                    targetPath: '$(Build.SourcesDirectory)'
                - template: helpers/terraform-installer.yaml
                  parameters:
                    terraformVersion: $${{ parameters.terraform_cli_version }}
                - template: helpers/terraform-init.yaml
                  parameters:
                    serviceConnection: '${service_connection_name_apply}'
                    backendAzureResourceGroupName: $(BACKEND_AZURE_RESOURCE_GROUP_NAME)
                    backendAzureStorageAccountName: $(BACKEND_AZURE_STORAGE_ACCOUNT_NAME)
                    backendAzureStorageAccountContainerName: $(BACKEND_AZURE_STORAGE_ACCOUNT_CONTAINER_NAME)
                    root_module_folder_relative_path: $${{ parameters.root_module_folder_relative_path }}
                - template: helpers/terraform-apply.yaml
                  parameters:
                    terraform_action: $${{ parameters.terraform_action }}
                    serviceConnection: '${service_connection_name_apply}'
                    root_module_folder_relative_path: $${{ parameters.root_module_folder_relative_path }}
