Skip to content

Improve ALZ Module CLI #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions actions_bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ $null = $modulesToInstall.Add(([PSCustomObject]@{
# Required dependency of the ALZ module itself.
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'Az.Resources'
ModuleVersion = '5.6.0'
ModuleVersion = '6.5.2'
}))



'Installing PowerShell Modules'
foreach ($module in $modulesToInstall) {
$installSplat = @{
Expand All @@ -52,8 +50,7 @@ foreach ($module in $modulesToInstall) {
Install-Module @installSplat
Import-Module -Name $module.ModuleName -ErrorAction Stop
' - Successfully installed {0}' -f $module.ModuleName
}
catch {
} catch {
$message = 'Failed to install {0}' -f $module.ModuleName
" - $message"
throw
Expand Down
2 changes: 1 addition & 1 deletion src/ALZ/ALZ.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
RequiredModules = @(
@{
ModuleName = 'Az.Resources'
ModuleVersion = '5.6.0'
ModuleVersion = '6.5.2'
}
)

Expand Down
205 changes: 205 additions & 0 deletions src/ALZ/Assets/alz-bicep-config/v0.13.0.ux.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"Prefix": {
"Type": "UserInput",
"Description": "The prefix that will be added to all resources created by this deployment. (e.g. 'alz')",
"Targets": [
{
"Name": "parTopLevelManagementGroupPrefix",
"Destination": "Parameters"
},
{
"Name": "parCompanyPrefix",
"Destination": "Parameters"
},
{
"Name": "parTargetManagementGroupId",
"Destination": "Parameters"
},
{
"Name": "parAssignableScopeManagementGroupId",
"Destination": "Parameters"
}
],
"Value": "",
"DefaultValue": "alz",
"Valid": "^[a-zA-Z]{3,5}$"
},
"Suffix": {
"Type": "UserInput",
"Description": "The suffix that will be added to all resources created by this deployment. (e.g. 'test')",
"Targets": [
{
"Name": "parTopLevelManagementGroupSuffix",
"Destination": "Parameters"
}
],
"Value": "",
"DefaultValue": "",
"Valid": "^[a-zA-Z]{0,5}$"
},
"Location": {
"Type": "UserInput",
"Description": "Deployment location.",
"Value": "",
"Targets": [
{
"Name": "parLocation",
"Destination": "Parameters"
},
{
"Name": "parAutomationAccountLocation",
"Destination": "Parameters"
},
{
"Name": "parLogAnalyticsWorkspaceLocation",
"Destination": "Parameters"
}
],
"AllowedValues": [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to find a nice way to do this dynamically - I could embed PS script in here I suppose...

"asia",
"asiapacific",
"australia",
"australiacentral",
"australiacentral2",
"australiaeast",
"australiasoutheast",
"brazil",
"brazilsouth",
"brazilsoutheast",
"canada",
"canadacentral",
"canadaeast",
"centralindia",
"centralus",
"centraluseuap",
"centralusstage",
"eastasia",
"eastasiastage",
"eastus",
"eastus2",
"eastus2euap",
"eastus2stage",
"eastusstg",
"europe",
"france",
"francecentral",
"francesouth",
"germany",
"germanynorth",
"germanywestcentral",
"global",
"india",
"japan",
"japaneast",
"japanwest",
"jioindiacentral",
"jioindiawest",
"korea",
"koreacentral",
"koreasouth",
"northcentralus",
"northcentralusstage",
"northeurope",
"norway",
"norwayeast",
"norwaywest",
"qatarcentral",
"singapore",
"southafrica",
"southafricanorth",
"southafricawest",
"southcentralus",
"southcentralusstage",
"southeastasia",
"southindia",
"swedencentral",
"switzerland",
"switzerlandnorth",
"switzerlandwest",
"uaecentral",
"uaenorth",
"uksouth",
"ukwest",
"unitedstates",
"westcentralus",
"westeurope",
"westindia",
"westus",
"westus2",
"westus2stage",
"westus3",
"westusstage"
]
},
"Environment": {
"Type": "UserInput",
"Description": "The Type of environment that will be created. (e.g. 'dev', 'test', 'qa', 'staging', 'prod')",
"Targets": [
{
"Name": "parEnvironment",
"Destination": "Parameters"
}
],
"Value": "",
"DefaultValue": "prod",
"Valid": "^[a-zA-Z0-9]{2,10}$"
},
"IdentitySubscriptionId": {
"Type": "UserInput",
"Description": "The identifier of the Identity Subscription. (e.g '00000000-0000-0000-0000-000000000000')",
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
"Targets": [
{
"Name": "IdentitySubscriptionId",
"Destination": "Environment"
}
],
"Value": ""
},
"ConnectivitySubscriptionId": {
"Type": "UserInput",
"Description": "The identifier of the Connectivity Subscription. (e.g '00000000-0000-0000-0000-000000000000')",
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
"Targets": [
{
"Name": "ConnectivitySubscriptionId",
"Destination": "Environment"
}
],
"Value": ""
},
"ManagementSubscriptionId": {
"Type": "UserInput",
"Description": "The identifier of the Management Subscription. (e.g 00000000-0000-0000-0000-000000000000)",
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
"Targets": [
{
"Name": "ManagementSubscriptionId",
"Destination": "Environment"
}
],
"Value": ""
},
"BillingAccountId": {
"Type": "UserInput",
"Description": "The identifier of the Billing Account. (e.g 00000000-0000-0000-0000-000000000000)",
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
"Value": ""
},
"EnrollmentAccountId": {
"Type": "UserInput",
"Description": "The identifier of the Enrollment Account. (e.g 00000000-0000-0000-0000-000000000000)",
"Valid": "^( {){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(}){0,1}$",
"Value": ""
},
"LogAnalyticsResourceId": {
"Type": "Computed",
"Value": "/subscriptions/{%ManagementSubscriptionId%}/resourcegroups/alz-logging/providers/microsoft.operationalinsights/workspaces/alz-log-analytics",
"Targets": [
{
"Name": "parLogAnalyticsWorkspaceResourceId",
"Destination": "Parameters"
}
]
}
}
31 changes: 31 additions & 0 deletions src/ALZ/Private/Build-ALZDeploymentEnvFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Build-ALZDeploymentEnvFile {
param (
[Parameter(Mandatory = $true)]
[PSCustomObject] $configuration,

[Parameter(Mandatory = $false)]
[string] $destination = "."
)
<#
.SYNOPSIS
This function uses configuration to build a .env file for use in the deployment pipeline.
.EXAMPLE
Build-ALZDeploymentEnvFile -configuration configuration
.EXAMPLE
Build-ALZDeploymentEnvFile -configuration configuration -destination "."
.OUTPUTS
N/A
#>

$envFile = Join-Path $destination ".env"

New-Item -Path $envFile -ItemType file -Force | Out-Null

foreach ($configurationValue in $configuration.PsObject.Properties) {
foreach ($target in $configurationValue.Value.Targets) {
if ($target.Destination -eq "Environment") {
Add-Content -Path $envFile -Value "$($($target.Name))=`"$($configurationValue.Value.Value)`"" | Out-Null
}
}
}
}
10 changes: 7 additions & 3 deletions src/ALZ/Private/Edit-ALZConfigurationFilesInPlace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ function Edit-ALZConfigurationFilesInPlace {
$bicepConfiguration = Get-Content $file.FullName | ConvertFrom-Json -AsHashtable
$modified = $false
foreach ($configKey in $configuration.PsObject.Properties) {
foreach ($name in $configKey.Value.Names) {
if ($null -ne $bicepConfiguration.parameters[$name]) {
$bicepConfiguration.parameters[$name].value = $configKey.Value.Value
foreach ($target in $configKey.Value.Targets) {
if ($target.Destination -eq "Parameters" -and $null -ne $bicepConfiguration.parameters[$target.Name]) {
if ($configKey.Value.Type -eq "Computed") {
$bicepConfiguration.parameters[$target.Name].value = Format-TokenizedConfigurationString $configKey.Value.Value $configuration
} else {
$bicepConfiguration.parameters[$target.Name].value = $configKey.Value.Value
}
$modified = $true
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/ALZ/Private/Format-TokenizedConfigurationString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Format-TokenizedConfigurationString {
param(
[Parameter(Mandatory = $true)]
[string] $tokenizedString,

[Parameter(Mandatory = $true)]
[object] $configuration
)
$values = $tokenizedString -split "\{\%|\%\}"

$returnValue = ""
foreach ($value in $values) {
$isToken = $tokenizedString -contains "{%$value%}"
if ($null -ne $configuration.$value) {
$returnValue += $configuration.$value.Value
} elseif (($null -eq $configuration.$value) -and $isToken) {
Write-InformationColored "Specified replacement token '${value}' not found in configuration." -ForegroundColor Yellow -InformationAction Continue
$returnValue += "{%$value%}"
} else {
$returnValue += $value
}
}

return $returnValue
}
31 changes: 31 additions & 0 deletions src/ALZ/Private/Get-Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Get-Configuration {
param(
[Parameter(Mandatory = $false)]
[ValidateSet("bicep", "terraform")]
[string] $alzIacProvider = "bicep",

[Parameter(Mandatory = $false)]
[string] $alzEnvironmentDestination = ".",

[Parameter(Mandatory = $false)]
[string] $alzBicepVersion = "v0.13.0"
)
<#
.SYNOPSIS
This function uses a template configuration to prompt for and return a user specified/modified configuration object.
.EXAMPLE
Get-Configuration
.EXAMPLE
Get-Configuration -alzIacProvider "bicep"
.OUTPUTS
System.Object. The resultant configuration values.
#>

if ($alzIacProvider -eq "terraform") {
throw "Terraform is not yet supported."
}

$uxConfigurationFile = Join-Path $alzEnvironmentDestination "alz-bicep-config" "$alzBicepVersion.ux.config.json"
return Get-Content -Path $uxConfigurationFile -Raw | ConvertFrom-Json
}

50 changes: 0 additions & 50 deletions src/ALZ/Private/Initialize-ConfigurationObject.ps1

This file was deleted.

Loading