diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 index e52203a..157cee4 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 @@ -66,15 +66,42 @@ function New-Bootstrap { # Get starter module $starterModulePath = "" + $starterRootModuleFolder = "" + $starterRootModuleFolderPath = "" + $starterFoldersToRetain = @() if($hasStarter) { if($inputConfig.starter_module_name -eq "") { $inputConfig.starter_module_name = Request-SpecialInput -type "starter" -starterConfig $starterConfig } + $chosenStarterConfig = $starterConfig.starter_modules.$($inputConfig.starter_module_name) + Write-Verbose "Selected Starter: $($inputConfig.starter_module_name))" - $starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $starterConfig.starter_modules.$($inputConfig.starter_module_name).location)).Path + $starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $chosenStarterConfig.location)).Path + $starterRootModuleFolderPath = $starterModulePath Write-Verbose "Starter Module Path: $starterModulePath" + + if($chosenStarterConfig.PSObject.Properties.Name -contains "additional_retained_folders") { + $starterFoldersToRetain = $chosenStarterConfig.additional_retained_folders + Write-Verbose "Starter Additional folders to retain: $($starterFoldersToRetain -join ",")" + } + + if($chosenStarterConfig.PSObject.Properties.Name -contains "root_module_folder") { + $starterRootModuleFolder = $chosenStarterConfig.root_module_folder + + # Retain the root module folder + $starterFoldersToRetain += $starterRootModuleFolder + + # Add the root module folder to bootstrap input config + $inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue $starterRootModuleFolder + + # Set the starter root module folder full path + $starterRootModuleFolderPath = Join-Path -Path $starterModulePath -ChildPath $starterRootModuleFolder + + Write-Verbose "Starter root module folder: $starterRootModuleFolder" + Write-Verbose "Starter final folders to retain: $($starterFoldersToRetain -join ",")" + } } # Getting configuration for the bootstrap module user input @@ -92,7 +119,7 @@ function New-Bootstrap { if($hasStarter) { Write-Verbose "Getting the starter configuration..." if($iac -eq "terraform") { - $terraformFiles = Get-ChildItem -Path $starterModulePath -Filter "*.tf" -File + $terraformFiles = Get-ChildItem -Path $starterRootModuleFolderPath -Filter "*.tf" -File foreach($terraformFile in $terraformFiles) { $starterParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $starterParameters } @@ -140,13 +167,26 @@ function New-Bootstrap { # Creating the tfvars files for the bootstrap and starter module $tfVarsFileName = "terraform.tfvars.json" $bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName - $starterTfvarsPath = Join-Path -Path $starterModulePath -ChildPath "terraform.tfvars.json" + $starterTfvarsPath = Join-Path -Path $starterRootModuleFolderPath -ChildPath "terraform.tfvars.json" $starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.json" # Write the tfvars file for the bootstrap and starter module Write-TfvarsJsonFile -tfvarsFilePath $bootstrapTfvarsPath -configuration $bootstrapConfiguration if($iac -eq "terraform") { + if($starterFoldersToRetain.Length -gt 0) { + Write-Verbose "Removing unwanted folders from the starter module..." + $folders = Get-ChildItem -Path $starterModulePath -Directory + foreach($folder in $folders) { + if($starterFoldersToRetain -notcontains $folder.Name) { + Write-Verbose "Removing folder: $($folder.FullName)" + Remove-Item -Path $folder.FullName -Recurse -Force + } else { + Write-Verbose "Retaining folder: $($folder.FullName)" + Remove-TerraformMetaFileSet -path $folder.FullName -writeVerboseLogs:$writeVerboseLogs.IsPresent + } + } + } Remove-TerraformMetaFileSet -path $starterModulePath -writeVerboseLogs:$writeVerboseLogs.IsPresent Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration }