Skip to content

Commit

Permalink
feat: add submodule support (#235)
Browse files Browse the repository at this point in the history
<!-- 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.
  • Loading branch information
jaredfholgate authored Oct 24, 2024
1 parent afcf62a commit 29b7d63
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 29b7d63

Please sign in to comment.