|
| 1 | +function Get-AcceleratorFolderConfiguration { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Detects and validates accelerator folder configuration from existing files. |
| 5 | + .DESCRIPTION |
| 6 | + This function examines an existing accelerator folder to detect the IaC type, |
| 7 | + version control system, and validate the configuration files. |
| 8 | + .PARAMETER FolderPath |
| 9 | + The path to the accelerator folder to analyze. |
| 10 | + .OUTPUTS |
| 11 | + Returns a hashtable with the following keys: |
| 12 | + - IsValid: Boolean indicating if valid configuration was found |
| 13 | + - IacType: Detected IaC type (terraform, bicep, or $null) |
| 14 | + - VersionControl: Detected version control (github, azure-devops, local, or $null) |
| 15 | + - ConfigFolderPath: Path to the config folder |
| 16 | + - InputsYamlPath: Path to inputs.yaml |
| 17 | + - InputsYaml: Parsed inputs.yaml content (if valid) |
| 18 | + - InputsContent: Raw inputs.yaml content (if valid) |
| 19 | + - ErrorMessage: Error message if validation failed |
| 20 | + #> |
| 21 | + [CmdletBinding()] |
| 22 | + param( |
| 23 | + [Parameter(Mandatory = $true)] |
| 24 | + [string] $FolderPath |
| 25 | + ) |
| 26 | + |
| 27 | + $result = @{ |
| 28 | + FolderExists = $false |
| 29 | + IsValid = $false |
| 30 | + IacType = $null |
| 31 | + VersionControl = $null |
| 32 | + ConfigFolderPath = $null |
| 33 | + InputsYamlPath = $null |
| 34 | + InputsYaml = $null |
| 35 | + InputsContent = $null |
| 36 | + ErrorMessage = $null |
| 37 | + } |
| 38 | + |
| 39 | + # Check if folder exists |
| 40 | + if (-not (Test-Path -Path $FolderPath)) { |
| 41 | + $result.ErrorMessage = "Folder '$FolderPath' does not exist." |
| 42 | + return $result |
| 43 | + } |
| 44 | + |
| 45 | + $result.FolderExists = $true |
| 46 | + |
| 47 | + $configFolderPath = Join-Path $FolderPath "config" |
| 48 | + $inputsYamlPath = Join-Path $configFolderPath "inputs.yaml" |
| 49 | + |
| 50 | + $result.ConfigFolderPath = $configFolderPath |
| 51 | + $result.InputsYamlPath = $inputsYamlPath |
| 52 | + |
| 53 | + # Check if config folder exists |
| 54 | + if (-not (Test-Path -Path $configFolderPath)) { |
| 55 | + $result.ErrorMessage = "Config folder not found at '$configFolderPath'" |
| 56 | + return $result |
| 57 | + } |
| 58 | + |
| 59 | + # Check if inputs.yaml exists |
| 60 | + if (-not (Test-Path -Path $inputsYamlPath)) { |
| 61 | + $result.ErrorMessage = "Required configuration file not found: inputs.yaml" |
| 62 | + return $result |
| 63 | + } |
| 64 | + |
| 65 | + # Try to read and validate inputs.yaml |
| 66 | + try { |
| 67 | + $inputsContent = Get-Content -Path $inputsYamlPath -Raw |
| 68 | + $inputsYaml = $inputsContent | ConvertFrom-Yaml |
| 69 | + |
| 70 | + $result.InputsContent = $inputsContent |
| 71 | + $result.InputsYaml = $inputsYaml |
| 72 | + $result.IsValid = $true |
| 73 | + } catch { |
| 74 | + $result.ErrorMessage = "inputs.yaml is not valid YAML: $($_.Exception.Message)" |
| 75 | + return $result |
| 76 | + } |
| 77 | + |
| 78 | + # Detect IaC type from existing files |
| 79 | + $tfvarsPath = Join-Path $configFolderPath "platform-landing-zone.tfvars" |
| 80 | + $bicepYamlPath = Join-Path $configFolderPath "platform-landing-zone.yaml" |
| 81 | + |
| 82 | + if (Test-Path -Path $tfvarsPath) { |
| 83 | + $result.IacType = "terraform" |
| 84 | + } elseif (Test-Path -Path $bicepYamlPath) { |
| 85 | + $result.IacType = "bicep" |
| 86 | + } |
| 87 | + |
| 88 | + # Detect version control from bootstrap_module_name in inputs.yaml |
| 89 | + if ($inputsYaml.bootstrap_module_name) { |
| 90 | + $bootstrapModuleName = $inputsYaml.bootstrap_module_name |
| 91 | + switch ($bootstrapModuleName) { |
| 92 | + "alz_github" { $result.VersionControl = "github" } |
| 93 | + "alz_azuredevops" { $result.VersionControl = "azure-devops" } |
| 94 | + "alz_local" { $result.VersionControl = "local" } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $result |
| 99 | +} |
0 commit comments