# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

---
# /*---------------------------------------------------------------------------8
# |                                                                            |
# |      This pipeline removes the SAP systems, the workload zone (landscape)  |
# |      using Terraform.                                                      |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

parameters:
  - name:                              cleanup_sap
    displayName:                       Remove the SAP system
    type:                              boolean
    default:                           true

  - name:                              sap_system
    displayName:                       "SAP System configuration name, use this format: ENV-LOCA-VNET-SID"
    type:                              string
    default:                           DEV-WEEU-SAP01-X00

  - name:                              cleanup_zone
    displayName:                       Remove the SAP workload zone
    type:                              boolean
    default:                           true

  - name:                              workload_zone
    displayName:                       "SAP workload zone configuration name, use this format: ENV-LOCA-VNET-INFRASTRUCTURE"
    type:                              string
    default:                           DEV-WEEU-SAP01-INFRASTRUCTURE

  - name:                              workload_environment
    displayName:                       Environment (DEV, QUA, PRD)
    type:                              string
    default:                           DEV

  - name:                              deployer_environment
    displayName:                       Environment (MGMT)
    type:                              string
    default:                           MMGMT

  - name:                              use_deployer
    displayName:                       Run removal on self hosted agent
    type:                              boolean
    default:                           true

  - name:                              sap_automation_repo_path
    displayName:                       The local path on the agent where the sap_automation repo can be found
    type:                              string

  - name:                              config_repo_path
    displayName:                       The local path on the agent where the config repo can be found
    type:                              string

stages:
  - stage:                             Remove_SAP_systems
    displayName:                       "Removing the SAP System"
    condition:                         and(not(failed()), not(canceled()), eq(${{ parameters.cleanup_sap }}, true))
    variables:
      - template:                      variables/10-remover-terraform-variables.yaml
        parameters:
          deployer_environment:        ${{ parameters.deployer_environment }}
          workload_environment:        ${{ parameters.workload_environment }}
          workload_zone:               ${{ parameters.workload_zone }}
          sap_system:                  ${{ parameters.sap_system }}
    jobs:
      - job:                           Remove_SAP_systems
        displayName:                   "Removing the SAP System"
        variables:
          - group:                     SDAF-${{ parameters.workload_environment }}
        workspace:
          clean:                       all
        steps:
          - template: templates\download.yaml
          - task:                      PostBuildCleanup@4
          - task:                      Bash@3
            inputs:
              targetType:              'filePath'
              filePath:                "$(System.DefaultWorkingDirectory)/sap-automation/deploy/scripts/pipeline_scripts/10-remover-terraform-system.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"

            displayName:               "Remove SAP system"
            env:
              SYSTEM_ACCESSTOKEN:      $(System.AccessToken)
              WL_ARM_SUBSCRIPTION_ID:  $(ARM_SUBSCRIPTION_ID)
              WL_ARM_CLIENT_ID:        $(ARM_CLIENT_ID)
              WL_ARM_CLIENT_SECRET:    $(ARM_CLIENT_SECRET)
              WL_ARM_TENANT_ID:        $(ARM_TENANT_ID)
              AZURE_DEVOPS_EXT_PAT:    $(System.AccessToken)
              SAP_AUTOMATION_REPO_PATH: ${{ parameters.sap_automation_repo_path }}
              CONFIG_REPO_PATH:        ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              USE_MSI:                 $(Use_MSI)

  - stage:                             Remove_SAP_workload_zone
    displayName:                       "Removing the SAP workload zone"
    condition:                         and(not(failed()), not(canceled()), eq(${{ parameters.cleanup_zone }}, true))
    variables:
      - template:                      variables/10-remover-terraform-variables.yaml
        parameters:
          deployer_environment:        ${{ parameters.deployer_environment }}
          workload_environment:        ${{ parameters.workload_environment }}
          workload_zone:               ${{ parameters.workload_zone }}
          sap_system:                  ${{ parameters.sap_system }}

    jobs:
      - job:                           Remove_SAP_workload_zone
        displayName:                   Remove the SAP Workload Zone
        variables:
          - group:                     SDAF-${{ parameters.workload_environment }}
        workspace:
          clean:                       all
        steps:
          - template:                  templates\download.yaml
          - task:                      PostBuildCleanup@4
          - task:                      Bash@3
            inputs:
              targetType:              'filePath'
              filePath:                "$(System.DefaultWorkingDirectory)/sap-automation/deploy/scripts/pipeline_scripts/10-remover-terraform-workload-zone.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"
            displayName:               Remove SAP workload_zone
            env:
              SYSTEM_ACCESSTOKEN:      $(System.AccessToken)
              WL_ARM_SUBSCRIPTION_ID:  $(ARM_SUBSCRIPTION_ID)
              WL_ARM_CLIENT_ID:        $(ARM_CLIENT_ID)
              WL_ARM_CLIENT_SECRET:    $(ARM_CLIENT_SECRET)
              WL_ARM_TENANT_ID:        $(ARM_TENANT_ID)
              AZURE_DEVOPS_EXT_PAT:    $(System.AccessToken)
              SAP_AUTOMATION_REPO_PATH: ${{ parameters.sap_automation_repo_path }}
              CONFIG_REPO_PATH:        ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              USE_MSI:                 $(Use_MSI)
