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

---
# /*---------------------------------------------------------------------------8
# |                                                                            |
# |               This pipeline deploys the Workload Zone                      |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

parameters:
  - name:                              workload_zone
    displayName:                       "Workload zone configuration name, use the following syntax: ENV-LOCA-VNET-INFRASTRUCTURE"
    type:                              string
    default:                           DEV-WEEU-SAP01-INFRASTRUCTURE

  - name:                              workload_environment
    displayName:                       Workload Environment (DEV, QA, PRD, ...)
    type:                              string
    default:                           DEV

  - name:                              deployer_environment
    displayName:                       Deployer Environment name (MGMT, DEV, QA, PRD, ...)
    type:                              string
    default:                           MGMT

  - name:                              deployer_region
    displayName:                       Deployer region name code (MGMT, DEV, QA, PRD, ...)
    type:                              string
    default:                           WEEU
    values:
      - AUCE
      - AUC2
      - AUEA
      - AUSE
      - BRSO
      - BRSE
      - BRUS
      - CACE
      - CAEA
      - CEIN
      - CEUS
      - CEUA
      - EAAS
      - EAUS
      - EUSA
      - EUS2
      - FRCE
      - FRSO
      - GENO
      - GEWC
      - JAEA
      - JAWE
      - JINC
      - JINW
      - KOCE
      - KOSO
      - NCUS
      - NOEU
      - NOEA
      - NOWE
      - NZNO
      - SANO
      - SAWE
      - SCUS
      - SCUG
      - SOEA
      - SOIN
      - SECE
      - SWNO
      - SWWE
      - UACE
      - UANO
      - UKSO
      - UKWE
      - WCUS
      - WEEU
      - WEIN
      - WEUS
      - WUS2
      - WUS3

  - name:                              inherit_settings
    displayName:                       Inherit Terraform state file information from control plane
    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

  - name:                              test
    type:                              boolean
    default:                           false

stages:
  - stage: Deploy_SAP_workload_zone
    condition: and(not(failed()), not(canceled()))
    displayName: Deploy SAP workload zone
    variables:
      - template:                      variables/02-sap-workload-zone-variables.yaml
        parameters:
          workload_zone:               ${{ parameters.workload_zone }}
          workload_environment:        ${{ parameters.workload_environment }}
          deployer_environment:        ${{ parameters.deployer_environment }}
          deployer_region:             ${{ parameters.deployer_region }}
          inherit_settings:            ${{ parameters.inherit_settings }}
    jobs:
      - job:                           Deploy_SAP_workload_zone
        displayName:                   Deploy SAP workload zone
        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/02-sap-workload-zone.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"
            displayName:               Deploy SAP Workload Zone
            env:
              SYSTEM_ACCESSTOKEN:      $(System.AccessToken)
              WL_ARM_SUBSCRIPTION_ID:  $(ARM_SUBSCRIPTION_ID)
              WL_ARM_TENANT_ID:        $(ARM_TENANT_ID)
              WL_ARM_CLIENT_ID:        $(ARM_CLIENT_ID)
              WL_ARM_CLIENT_SECRET:    $(ARM_CLIENT_SECRET)
              WL_ARM_OBJECT_ID:        $(ARM_OBJECT_ID)
              CP_ARM_SUBSCRIPTION_ID:  $(CP_ARM_SUBSCRIPTION_ID)
              CP_ARM_CLIENT_ID:        $(CP_ARM_CLIENT_ID)
              CP_ARM_CLIENT_SECRET:    $(CP_ARM_CLIENT_SECRET)
              CP_ARM_TENANT_ID:        $(CP_ARM_TENANT_ID)
              CP_ARM_OBJECT_ID:        $(CP_ARM_OBJECT_ID)
              FENCING_SPN_PWD:         $(FENCING_SPN_PWD)
              SAPBITS:                 $(INSTALLATION_MEDIA_ACCOUNT)
              TEST_ONLY:               ${{ parameters.test }}
              AZURE_DEVOPS_EXT_PAT:    $(System.AccessToken)
              TF_LOG:                  $(TF_LOG)
              TF_IN_AUTOMATION:        true
              SAP_AUTOMATION_REPO_PATH: ${{ parameters.sap_automation_repo_path }}
              CONFIG_REPO_PATH:        ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              LOGON_USING_SPN:         $(Logon_Using_SPN)
              USE_MSI:                 $(Use_MSI)

