Skip to content

Commit 29b7d63

Browse files
feat: add submodule support (Azure#235)
<!-- Thank you for submitting a Pull Request. Please fill out the template below.--> ## Overview/Summary Add submodule support for starter modules ## This PR fixes/adds/changes/removes N/A ### Breaking Changes N/A ## Testing Evidence e2e tests running here: https://github.com/Azure/accelerator-bootstrap-modules/actions/runs/11487105261 ## As part of this Pull Request I have - [x] Checked for duplicate [Pull Requests](https://github.com/Azure/alz-terraform-accelerator/pulls) - [x] Associated it with relevant [issues](https://github.com/Azure/alz-terraform-accelerator/issues), for tracking and closure. - [x] Ensured my code/branch is up-to-date with the latest changes in the `main` [branch](https://github.com/Azure/alz-terraform-accelerator/tree/main) - [x] Performed testing and provided evidence. - [x] Updated relevant and associated documentation.
1 parent afcf62a commit 29b7d63

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,42 @@ function New-Bootstrap {
6666

6767
# Get starter module
6868
$starterModulePath = ""
69+
$starterRootModuleFolder = ""
70+
$starterRootModuleFolderPath = ""
71+
$starterFoldersToRetain = @()
6972

7073
if($hasStarter) {
7174
if($inputConfig.starter_module_name -eq "") {
7275
$inputConfig.starter_module_name = Request-SpecialInput -type "starter" -starterConfig $starterConfig
7376
}
7477

78+
$chosenStarterConfig = $starterConfig.starter_modules.$($inputConfig.starter_module_name)
79+
7580
Write-Verbose "Selected Starter: $($inputConfig.starter_module_name))"
76-
$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $starterConfig.starter_modules.$($inputConfig.starter_module_name).location)).Path
81+
$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $chosenStarterConfig.location)).Path
82+
$starterRootModuleFolderPath = $starterModulePath
7783
Write-Verbose "Starter Module Path: $starterModulePath"
84+
85+
if($chosenStarterConfig.PSObject.Properties.Name -contains "additional_retained_folders") {
86+
$starterFoldersToRetain = $chosenStarterConfig.additional_retained_folders
87+
Write-Verbose "Starter Additional folders to retain: $($starterFoldersToRetain -join ",")"
88+
}
89+
90+
if($chosenStarterConfig.PSObject.Properties.Name -contains "root_module_folder") {
91+
$starterRootModuleFolder = $chosenStarterConfig.root_module_folder
92+
93+
# Retain the root module folder
94+
$starterFoldersToRetain += $starterRootModuleFolder
95+
96+
# Add the root module folder to bootstrap input config
97+
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue $starterRootModuleFolder
98+
99+
# Set the starter root module folder full path
100+
$starterRootModuleFolderPath = Join-Path -Path $starterModulePath -ChildPath $starterRootModuleFolder
101+
102+
Write-Verbose "Starter root module folder: $starterRootModuleFolder"
103+
Write-Verbose "Starter final folders to retain: $($starterFoldersToRetain -join ",")"
104+
}
78105
}
79106

80107
# Getting configuration for the bootstrap module user input
@@ -92,7 +119,7 @@ function New-Bootstrap {
92119
if($hasStarter) {
93120
Write-Verbose "Getting the starter configuration..."
94121
if($iac -eq "terraform") {
95-
$terraformFiles = Get-ChildItem -Path $starterModulePath -Filter "*.tf" -File
122+
$terraformFiles = Get-ChildItem -Path $starterRootModuleFolderPath -Filter "*.tf" -File
96123
foreach($terraformFile in $terraformFiles) {
97124
$starterParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $starterParameters
98125
}
@@ -140,13 +167,26 @@ function New-Bootstrap {
140167
# Creating the tfvars files for the bootstrap and starter module
141168
$tfVarsFileName = "terraform.tfvars.json"
142169
$bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName
143-
$starterTfvarsPath = Join-Path -Path $starterModulePath -ChildPath "terraform.tfvars.json"
170+
$starterTfvarsPath = Join-Path -Path $starterRootModuleFolderPath -ChildPath "terraform.tfvars.json"
144171
$starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.json"
145172

146173
# Write the tfvars file for the bootstrap and starter module
147174
Write-TfvarsJsonFile -tfvarsFilePath $bootstrapTfvarsPath -configuration $bootstrapConfiguration
148175

149176
if($iac -eq "terraform") {
177+
if($starterFoldersToRetain.Length -gt 0) {
178+
Write-Verbose "Removing unwanted folders from the starter module..."
179+
$folders = Get-ChildItem -Path $starterModulePath -Directory
180+
foreach($folder in $folders) {
181+
if($starterFoldersToRetain -notcontains $folder.Name) {
182+
Write-Verbose "Removing folder: $($folder.FullName)"
183+
Remove-Item -Path $folder.FullName -Recurse -Force
184+
} else {
185+
Write-Verbose "Retaining folder: $($folder.FullName)"
186+
Remove-TerraformMetaFileSet -path $folder.FullName -writeVerboseLogs:$writeVerboseLogs.IsPresent
187+
}
188+
}
189+
}
150190
Remove-TerraformMetaFileSet -path $starterModulePath -writeVerboseLogs:$writeVerboseLogs.IsPresent
151191
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration
152192
}

0 commit comments

Comments
 (0)