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

name:                                  Create Deployer Configuration

trigger:                               none

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

  - name:                              deployer_region_parameter
    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
      - 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

variables:
  - name:                              deployer_environment
    value:                             ${{ parameters.deployer_environment_parameter }}

  - name:                              deployer_region
    value:                             ${{ parameters.deployer_region_parameter }}

  - name:                              deployer_folder
    value:                             ${{ format('{0}-{1}-DEP01-INFRASTRUCTURE', parameters.deployer_environment_parameter, parameters.deployer_region_parameter) }}

  - name:                              deployer_file
    value:                             ${{ format('{0}-{1}-DEP01-INFRASTRUCTURE.tfvars', parameters.deployer_environment_parameter, parameters.deployer_region_parameter) }}

  - name:                              library_folder
    value:                             ${{ format('{0}-{1}-SAP_LIBRARY', parameters.deployer_environment_parameter, parameters.deployer_region_parameter) }}

  - name:                              library_file
    value:                             ${{ format('{0}-{1}-SAP_LIBRARY.tfvars', parameters.deployer_environment_parameter, parameters.deployer_region_parameter) }}

stages:
  - stage:                             Create_Configuration
    displayName:                       Create Deployer Configuration
    jobs:
      - job:                           Create_Deployer_Configuration
        displayName:                   Create Deployer Configuration
        workspace:
          clean:                       all
        steps:
          - task:                      PostBuildCleanup@4
          - checkout:                  self
            persistCredentials:        true
          - task:                      PowerShell@2
            displayName:               "Create Sample configuration for the control plane"
            inputs:
              targetType:              "inline"

              script: |
                    git fetch -q --all
                    git checkout -q $(Build.SourceBranchName)
                    git pull

                    $FolderName = "WORKSPACES"
                    $region = switch ("$(deployer_region)") {
                        "AUCE" { "australiacentral" }
                        "AUC2" { "australiacentral2" }
                        "AUEA" { "australiaeast" }
                        "AUSE" { "australiasoutheast" }
                        "BRSO" { "brazilsouth" }
                        "BRSE" { "brazilsoutheast" }
                        "BRUS" { "brazilus" }
                        "CACE" { "canadacentral" }
                        "CAEA" { "canadaeast" }
                        "CEIN" { "centralindia" }
                        "CEUS" { "centralus" }
                        "CEUA" { "centraluseuap" }
                        "EAAS" { "eastasia" }
                        "EAUS" { "eastus" }
                        "EUS2" { "eastus2" }
                        "EUSG" { "eastusstg"}
                        "FRCE" { "francecentral" }
                        "FRSO" { "francesouth" }
                        "GENO" { "germanynorth" }
                        "GEWC" { "germanywestcentral" }
                        "ISCE" { "israelcentral" }
                        "ITNO" { "italynorth" }
                        "JAEA" { "japaneast" }
                        "JAWE" { "japanwest" }
                        "JINC" { "jioindiacentral" }
                        "JINW" { "jioindiawest" }
                        "KOCE" { "koreacentral" }
                        "KOSO" { "koreasouth" }
                        "NCUS" { "northcentralus" }
                        "NOEU" { "northeurope" }
                        "NOEA" { "norwayeast" }
                        "NOWE" { "norwaywest" }
                        "NZNO" { "newzealandnorth" }
                        "PLCE" { "polandcentral" }
                        "QACE" { "qatarcentral" }
                        "SANO" { "southafricanorth" }
                        "SAWE" { "southafricawest" }
                        "SCUS" { "southcentralus" }
                        "SCUG" { "southcentralusstg" }
                        "SOEA" { "southeastasia" }
                        "SOIN" { "southindia" }
                        "SECE" { "swedencentral" }
                        "SWNO" { "switzerlandnorth" }
                        "SWWE" { "switzerlandwest" }
                        "UACE" { "uaecentral" }
                        "UANO" { "uaenorth" }
                        "UKSO" { "uksouth" }
                        "UKWE" { "ukwest" }
                        "WCUS" { "westcentralus" }
                        "WEEU" { "westeurope" }
                        "WEIN" { "westindia" }
                        "WEUS" { "westus" }
                        "WUS2" { "westus2" }
                        "WUS3" { "westus3" }
                    }

                    $Full = Join-Path -Path $($FolderName) -ChildPath (Join-Path -Path "DEPLOYER" -ChildPath $(deployer_folder))
                    $Full_FileName = (Join-Path -path $Full -ChildPath "$(deployer_file)")

                    if (Test-Path $Full) {
                        cd $Full

                        if (Test-Path $(deployer_file)) {
                        }
                        else {
                            $DeployerFile = New-Item -Path . -Name $(deployer_file) -ItemType "file" -Value ("# Deployer Configuration File" + [Environment]::NewLine)
                            Add-Content $(deployer_file) "environment = ""$(deployer_environment)"""
                            Add-Content $(deployer_file) "location = ""$region"""
                            Add-Content $(deployer_file) ""
                            Add-Content $(deployer_file) "management_network_logical_name = ""DEP01"""
                            Add-Content $(deployer_file) "management_network_address_space = ""10.170.20.0/24"""
                            Add-Content $(deployer_file) "management_subnet_address_prefix = ""10.170.20.64/28"""

                            Add-Content $(deployer_file) "firewall_deployment = true"
                            Add-Content $(deployer_file) "management_firewall_subnet_address_prefix = ""10.170.20.0/26"""

                            Add-Content $(deployer_file) "bastion_deployment = true"
                            Add-Content $(deployer_file) "management_bastion_subnet_address_prefix = ""10.170.20.128/26"""

                            Add-Content $(deployer_file) "webapp_subnet_address_prefix = ""10.170.20.80/28"""

                            git config --global user.email "$(Build.RequestedForEmail)"
                            git config --global user.name "$(Build.RequestedFor)"
                            git add -f $(deployer_file)
                            git commit -m "Added Control Plane configuration[skip ci]"

                            git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $(Build.SourceBranchName)
                        }

                    }
                    else {
                        #PowerShell Create directory if not exists
                        cd $(Build.Repository.LocalPath)
                        $Folder = New-Item $Full -ItemType Directory
                        cd $Folder.FullName
                        $DeployerFile = New-Item -Path . -Name $(deployer_file) -ItemType "file" -Value ("# Deployer Configuration File" + [Environment]::NewLine)
                        Add-Content $(deployer_file) "environment = ""$(deployer_environment)"""
                        Add-Content $(deployer_file) "location = ""$region"""
                        Add-Content $(deployer_file) ""
                        Add-Content $(deployer_file) "management_network_logical_name = ""DEP00"""
                        Add-Content $(deployer_file) "management_network_address_space = ""10.170.20.0/24"""
                        Add-Content $(deployer_file) "management_subnet_address_prefix = ""10.170.20.64/28"""

                        Add-Content $(deployer_file) "firewall_deployment = true"
                        Add-Content $(deployer_file) "management_firewall_subnet_address_prefix = ""10.170.20.0/26"""

                        Add-Content $(deployer_file) "bastion_deployment = true"
                        Add-Content $(deployer_file) "management_bastion_subnet_address_prefix = ""10.170.20.128/26"""

                        Add-Content $(deployer_file) "webapp_subnet_address_prefix = ""10.170.20.80/28"""

                        git config --global user.email "$(Build.RequestedForEmail)"
                        git config --global user.name "$(Build.RequestedFor)"
                        git add -f $(deployer_file)
                        git commit -m "Added Control Plane configuration[skip ci]"

                        git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $(Build.SourceBranchName)
                    }

                    $Full = Join-Path -Path $($FolderName) -ChildPath (Join-Path -Path "LIBRARY" -ChildPath $(library_folder))
                    $Full_FileName = (Join-Path -path $Full -ChildPath "$(library_file)")
                    cd $(Build.Repository.LocalPath)

                    if (Test-Path $Full) {
                        cd $Full

                        if (Test-Path $(library_file)) {
                        }
                        else {
                            $LibraryFile = New-Item -Path . -Name $(library_file) -ItemType "file" -Value ("# Library Configuration File" + [Environment]::NewLine)
                            Add-Content $(library_file) "environment = ""$(deployer_environment)"""
                            Add-Content $(library_file) "location = ""$region"""
                            Add-Content $(library_file) ""
                            Add-Content $(library_file) "dns_label = ""sap.contoso.net"""
                            git config --global user.email "$(Build.RequestedForEmail)"
                            git config --global user.name "$(Build.RequestedFor)"
                            git add -f $(library_file)
                            git commit -m "Added Control Plane Library configuration[skip ci]"

                            git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $(Build.SourceBranchName)
                        }

                    }
                    else {
                        #PowerShell Create directory if not exists
                        Write-Host "Creating Library directory"
                        cd $(Build.Repository.LocalPath)
                        $Folder = New-Item $Full -ItemType Directory
                        cd $Full
                        Write-Host "Creating Library file"
                        $LibraryFile = New-Item -Path . -Name $(library_file) -ItemType "file" -Value ("# Library Configuration File" + [Environment]::NewLine)
                        Add-Content $(library_file) "environment = ""$(deployer_environment)"""
                        Add-Content $(library_file) "location = ""$region"""
                        Add-Content $(library_file) ""
                        Add-Content $(library_file) "dns_label = ""azure.sdaf.contoso.net"""
                        git config --global user.email "$(Build.RequestedForEmail)"
                        git config --global user.name "$(Build.RequestedFor)"
                        git add -f $(library_file)
                        git commit -m "Added Control Plane Library configuration[skip ci]"

                        git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $(Build.SourceBranchName)

                    }

              failOnStderr:            false
              showWarnings:            true
              pwsh:                    true
