|
| 1 | +function New-AcceleratorFolderStructure { |
| 2 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 3 | + param ( |
| 4 | + [Parameter( |
| 5 | + Mandatory = $false, |
| 6 | + HelpMessage = "[REQUIRED] The infrastructure as code type for the accelerator. Options are 'terraform', 'bicep' or 'bicep-classic'" |
| 7 | + )] |
| 8 | + [string] $iacType = "terraform", |
| 9 | + [Parameter( |
| 10 | + Mandatory = $false, |
| 11 | + HelpMessage = "[REQUIRED] The version of the accelerator to use for the bootstrap and starter configuration files" |
| 12 | + )] |
| 13 | + [string] $versionControl = "github", |
| 14 | + [Parameter( |
| 15 | + Mandatory = $false, |
| 16 | + HelpMessage = "[OPTIONAL] The scenario number to use for the starter configuration files" |
| 17 | + )] |
| 18 | + [int] $scenarioNumber = 1, |
| 19 | + [Parameter( |
| 20 | + Mandatory = $false, |
| 21 | + HelpMessage = "[REQUIRED] The target folder to create the accelerator bootstrap and platform landing zone configuration files in" |
| 22 | + )] |
| 23 | + [string] $targetFolderPath = "~/accelerator", |
| 24 | + [Parameter( |
| 25 | + Mandatory = $false, |
| 26 | + HelpMessage = "[OPTIONAL] Forece recreate of the target folder if it already exists" |
| 27 | + )] |
| 28 | + [switch] $force |
| 29 | + ) |
| 30 | + |
| 31 | + if ($PSCmdlet.ShouldProcess("Accelerator folder create", "modify")) { |
| 32 | + |
| 33 | + $currentPath = Get-Location |
| 34 | + |
| 35 | + # Normalize target folder path |
| 36 | + if($targetFolderPath.StartsWith("~/" )) { |
| 37 | + $targetFolderPath = Join-Path $HOME $targetFolderPath.Replace("~/", "") |
| 38 | + } |
| 39 | + if(Test-Path -Path $targetFolderPath) { |
| 40 | + if($force.IsPresent) { |
| 41 | + Write-Host "Force flag is set, removing existing target folder at $targetFolderPath" |
| 42 | + Remove-Item -Recurse -Force -Path $targetFolderPath | Write-Verbose | Out-Null |
| 43 | + } else { |
| 44 | + throw "Target folder $targetFolderPath already exists. Please specify a different folder path or remove the existing folder." |
| 45 | + } |
| 46 | + } |
| 47 | + Write-Host "Creating target folder at $targetFolderPath" |
| 48 | + New-Item -ItemType "directory" -Path $targetFolderPath -Force | Write-Verbose | Out-Null |
| 49 | + $targetFolderPath = (Resolve-Path -Path $targetFolderPath).Path |
| 50 | + |
| 51 | + # Create target folder structure |
| 52 | + $outputFolder = Join-Path $targetFolderPath "output" |
| 53 | + Write-Host "Creating output folder at $outputFolder" |
| 54 | + New-Item -ItemType "directory" $outputFolder -Force | Write-Verbose | Out-Null |
| 55 | + |
| 56 | + # Create temp folder |
| 57 | + $tempFolderPath = Join-Path $targetFolderPath "temp" |
| 58 | + Write-Host "Creating temp folder at $tempFolderPath" |
| 59 | + New-Item -ItemType "directory" $tempFolderPath -Force | Write-Verbose | Out-Null |
| 60 | + |
| 61 | + # Map the repo |
| 62 | + $repos = @{ |
| 63 | + "terraform" = @{ |
| 64 | + repoName = "alz-terraform-accelerator" |
| 65 | + folderToClone = "templates/platform_landing_zone" |
| 66 | + libraryFolderPath = "lib" |
| 67 | + exampleFolderPath = "examples" |
| 68 | + bootstrapExampleFolderPath = "bootstrap" |
| 69 | + hasScenarios = $true |
| 70 | + hasLibrary = $true |
| 71 | + } |
| 72 | + |
| 73 | + "bicep" = @{ |
| 74 | + repoName = "alz-bicep-accelerator" |
| 75 | + folderToClone = "" |
| 76 | + libraryFolderPath = "" |
| 77 | + exampleFolderPath = "examples" |
| 78 | + bootstrapExampleFolderPath = "bootstrap" |
| 79 | + hasScenarios = $false |
| 80 | + hasLibrary = $false |
| 81 | + platformLandingZoneFilePath = "platform-landing-zone.yaml" |
| 82 | + } |
| 83 | + |
| 84 | + "bicep-classic" = @{ |
| 85 | + repoName = "alz-bicep" |
| 86 | + folderToClone = "accelerator" |
| 87 | + libraryFolderPath = "" |
| 88 | + exampleFolderPath = "examples" |
| 89 | + bootstrapExampleFolderPath = "bootstrap" |
| 90 | + hasScenarios = $false |
| 91 | + hasLibrary = $false |
| 92 | + platformLandingZoneFilePath = "" |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + # Clone the repo and copy the bootstrap and starter configuration files |
| 97 | + $repo = $repos[$iacType] |
| 98 | + Write-Host "Cloning repo $($repo.repoName)" |
| 99 | + git clone -n --depth=1 --filter=tree:0 "https://github.com/Azure/$($repo.repoName)" "$tempFolderPath" | Write-Verbose | Out-Null |
| 100 | + Set-Location $tempFolderPath |
| 101 | + |
| 102 | + Write-Host "Checking out folder $($repo.folderToClone)" |
| 103 | + git sparse-checkout set --no-cone $repo.folderToClone | Write-Verbose | Out-Null |
| 104 | + git checkout | Write-Verbose | Out-Null |
| 105 | + |
| 106 | + Set-Location $currentPath |
| 107 | + $exampleFolderPath = "$($repo.folderToClone)/$($repo.exampleFolderPath)" |
| 108 | + $bootstrapExampleFolderPath = "$exampleFolderPath/$($repo.bootstrapExampleFolderPath)" |
| 109 | + |
| 110 | + $configFolderPath = Join-Path $targetFolderPath "config" |
| 111 | + Write-Host "Creating config folder at $configFolderPath" |
| 112 | + New-Item -ItemType "directory" $configFolderPath -Force | Write-Verbose | Out-Null |
| 113 | + |
| 114 | + # Copy the bootstrap configuration file |
| 115 | + Write-Host "Copying bootstrap configuration file to $($targetFolderPath)/config/inputs.yaml" |
| 116 | + Copy-Item -Path "$tempFolderPath/$bootstrapExampleFolderPath/inputs-$versionControl.yaml" -Destination "$targetFolderPath/config/inputs.yaml" -Force | Write-Verbose | Out-Null |
| 117 | + |
| 118 | + if ($repo.hasLibrary) { |
| 119 | + $libFolderPath = "$($repo.folderToClone)/$($repo.libraryFolderPath)" |
| 120 | + Write-Host "Copying library files to $($targetFolderPath)/config" |
| 121 | + Copy-Item -Path "$tempFolderPath/$libFolderPath" -Destination "$targetFolderPath/config" -Recurse -Force | Write-Verbose | Out-Null |
| 122 | + } |
| 123 | + |
| 124 | + # Copy the platform landing zone configuration files based on scenario number or specific file path |
| 125 | + if ($repo.hasScenarios) { |
| 126 | + $scenarios = @{ |
| 127 | + 1 = "full-multi-region/hub-and-spoke-vnet" |
| 128 | + 2 = "full-multi-region/virtual-wan" |
| 129 | + 3 = "full-multi-region-nva/hub-and-spoke-vnet" |
| 130 | + 4 = "full-multi-region-nva/virtual-wan" |
| 131 | + 5 = "management-only/management" |
| 132 | + 6 = "full-single-region/hub-and-spoke-vnet" |
| 133 | + 7 = "full-single-region/virtual-wan" |
| 134 | + 8 = "full-single-region-nva/hub-and-spoke-vnet" |
| 135 | + 9 = "full-single-region-nva/virtual-wan" |
| 136 | + } |
| 137 | + |
| 138 | + Write-Host "Copying platform landing zone configuration file for scenario $scenarioNumber to $($targetFolderPath)/config/platform-landing-zone.tfvars" |
| 139 | + Copy-Item -Path "$tempFolderPath/templates/platform_landing_zone/examples/$($scenarios[$scenarioNumber]).tfvars" -Destination "$targetFolderPath/config/platform-landing-zone.tfvars" -Force | Write-Verbose | Out-Null |
| 140 | + |
| 141 | + } elseif ($repo.platformLandingZoneFilePath -ne "") { |
| 142 | + Write-Host "Copying platform landing zone configuration file to $($targetFolderPath)/config/platform-landing-zone.yaml" |
| 143 | + Copy-Item -Path "$tempFolderPath/$($repo.platformLandingZoneFilePath)" -Destination "$targetFolderPath/config/platform-landing-zone.yaml" -Force | Write-Verbose | Out-Null |
| 144 | + } |
| 145 | + |
| 146 | + Remove-Item -Path $tempFolderPath -Recurse -Force | Write-Verbose | Out-Null |
| 147 | + } |
| 148 | +} |
0 commit comments