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

---
# /*---------------------------------------------------------------------------8
# |                                                                            |
# |               This pipeline downloads the SAP software                     |
# |                                                                            |
# +------------------------------------4--------------------------------------*/
parameters:
  - name:                              bom_base_name
    displayName:                       Name of Bill of Materials (BoM)
    type:                              string

  - name:                              environment
    displayName:                       Control Plane Environment
    default:                           MGMT

  - name:                              region
    displayName:                       Control Plane (SAP Library) location code
    type:                              string
    default:                           WEEU

  - name:                              re_download
    displayName:                       Re-download software from SAP
    type:                              boolean
    default:                           false

  - 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:                              sample_repo_path
    displayName:                       The local path on the agent where the config repo can be found
    type:                              string

  - name:                              ExtraParams
    displayName:                       Extra parameters to pass to Ansible
    type:                              string

stages:
  - stage: Prepare_download
    condition: and(not(failed()), not(canceled()))
    variables:
      - template:                      variables/04-sap-software-download-variables.yaml
        parameters:
          bom_base_name:               ${{ parameters.bom_base_name }}
          environment:                 ${{ parameters.environment }}
          re_download:                 ${{ parameters.re_download }}
          region:                      ${{ parameters.region }}
    displayName:                       Preparation
    jobs:
      - job: Prepare_download_job
        displayName:                   Preparation
        workspace:
          clean:                       all
        steps:
          - template:                  templates\download_samples.yaml
          - task:                      PostBuildCleanup@4
          - task:                      Bash@3
            inputs:
              targetType:              'filePath'
              filePath:                "$(System.DefaultWorkingDirectory)/sap-automation/deploy/scripts/pipeline_scripts/04-sap-software-download-prepare.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"
            displayName:               Prepare download
            name:                      Preparation
            env:
              SYSTEM_ACCESSTOKEN:       $(System.AccessToken)
              ARM_CLIENT_ID:            $(CP_ARM_CLIENT_ID)
              ARM_CLIENT_SECRET:        $(CP_ARM_CLIENT_SECRET)
              ARM_TENANT_ID:            $(CP_ARM_TENANT_ID)
              ARM_SUBSCRIPTION_ID:      $(CP_ARM_SUBSCRIPTION_ID)
              SUSERNAME:                $(S-Username)
              SPASSWORD:                $(S-Password)
              SAP_AUTOMATION_REPO_PATH: ${{ parameters.sap_automation_repo_path }}
              SAMPLE_REPO_PATH:         ${{ parameters.sample_repo_path }}
              CONFIG_REPO_PATH:         ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              USE_MSI:                  $(Use_MSI)

  - stage:                             Software_download
    variables:
      - template:                      variables/04-sap-software-download-variables.yaml
        parameters:
          bom_base_name:               ${{ parameters.bom_base_name }}
          environment:                 ${{ parameters.environment }}
          re_download:                 ${{ parameters.re_download }}
          region:                      ${{ parameters.region }}
    displayName:                       Download software

    jobs:
      - job:                           Software_download
        displayName:                   Download software
        variables:
          BOM_NAME:                    $[ stageDependencies.Prepare_download.Prepare_download_job.outputs['Preparation.BOM_NAME'] ]
          KV_NAME:                     $[ stageDependencies.Prepare_download.Prepare_download_job.outputs['Preparation.KV_NAME'] ]
          SUSERNAME:                   $[ stageDependencies.Prepare_download.Prepare_download_job.outputs['Preparation.SUSERNAME'] ]
        timeoutInMinutes:              0
        steps:
          - template:                  templates\download_samples.yaml
          - task:                      PostBuildCleanup@4
          - task:                      Bash@3
            inputs:
              targetType:              'filePath'
              filePath:                "$(System.DefaultWorkingDirectory)/sap-automation/deploy/scripts/pipeline_scripts/04-sap-software-download.sh"
              failOnStderr:            false
              workingDirectory:        "$(System.DefaultWorkingDirectory)"
            displayName:               Download
            name:                      download
            env:
              ARM_CLIENT_ID:                 $(CP_ARM_CLIENT_ID)
              ARM_CLIENT_SECRET:             $(CP_ARM_CLIENT_SECRET)
              ARM_TENANT_ID:                 $(CP_ARM_TENANT_ID)
              ARM_SUBSCRIPTION_ID:           $(CP_ARM_SUBSCRIPTION_ID)
              SAP_AUTOMATION_REPO_PATH:      ${{ parameters.sap_automation_repo_path }}
              SAMPLE_REPO_PATH:              ${{ parameters.sample_repo_path }}
              CONFIG_REPO_PATH:              ${{ parameters.config_repo_path }}/$(Deployment_Configuration_Path)
              ANSIBLE_COLLECTIONS_PATH:      ~/.ansible/collections:/opt/ansible/collections
              USE_MSI:                       $(Use_MSI)
              EXTRA_PARAMETERS:              ${{ parameters.ExtraParams }}
              AGENT_TEMP_DIRECTORY:          $(Agent.TempDirectory)
