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

# /*---------------------------------------------------------------------------8
# |                                                                            |
# |               This pipeline deploys the SAP Infrastructure                 |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

parameters:
  - name:                              sap_system
    displayName:                       "SAP System configuration name, use the following syntax: ENV-LOCA-VNET-SID"
    type:                              string
    default:                           DEV-WEEU-SAP01-X00

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

  - 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
    displayName:                       Test deployment without applying the changes
    type:                              boolean

stages:
  - stage: Deploy_SAP_infrastructure
    condition: and(not(failed()), not(canceled()))
    variables:
      - template:                      variables/03-sap-system-deployment-variables.yaml
        parameters:
          sap_system:                  ${{ parameters.sap_system }}
          environment:                 ${{ parameters.environment }}
          test:                        ${{ parameters.test }}
    displayName:                       Deploy SAP infrastructure
    jobs:
      - job:                           Deploy_SAP_infrastructure
        displayName:                   Deploy SAP infrastructure
        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/03-sap-system-deployment.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"
            displayName:               Deploy_SAP_infrastructure
            env:
              CONFIG_REPO_PATH:         ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              LOGON_USING_SPN:          $(Logon_Using_SPN)
              AZURE_DEVOPS_EXT_PAT:     $(System.AccessToken)
              SAP_AUTOMATION_REPO_PATH: ${{ parameters.sap_automation_repo_path }}
              SYSTEM_ACCESSTOKEN:       $(System.AccessToken)
              TEST_ONLY:                ${{ parameters.test }}
              TF_IN_AUTOMATION:         true
              TF_LOG:                   $(TF_LOG)
              USE_MSI:                  $(Use_MSI)
              WL_ARM_CLIENT_ID:         $(ARM_CLIENT_ID)
              WL_ARM_CLIENT_SECRET:     $(ARM_CLIENT_SECRET)
              WL_ARM_SUBSCRIPTION_ID:   $(ARM_SUBSCRIPTION_ID)
              WL_ARM_TENANT_ID:         $(ARM_TENANT_ID)


