-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ee571c1
Stowing this to test something else
f2bbe2e
Now with working
0dbc375
String replacement done
7d7d640
Adding tests
1a0952f
Adding more tests
c430582
Something is breaking trying this to see if something gives me an err…
132854e
Fix the import
4f4bbee
Tidy ups
7de8a03
Adding in tokenised strings as per discussion with Gabe
8b4781b
Unwinding a partial change to validation
075b2fe
Committing this with improved CLI and token replacement
ac288ab
Committing this with a json based configuration object
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 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) { | ||
| if ($configurationValue.Value.ForEnvironment -eq $true) { | ||
| Add-Content -Path $envFile -Value "$($($configurationValue.Name))=`"$($configurationValue.Value.Value)`"" | Out-Null | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,29 +21,56 @@ function Initialize-ConfigurationObject { | |
| } | ||
|
|
||
| return [pscustomobject]@{ | ||
| Prefix = [pscustomobject]@{ | ||
| description = "The prefix that will be added to all resources created by this deployment." | ||
| names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix") | ||
| value = "alz" | ||
| defaultValue = "alz" | ||
| Prefix = [pscustomobject]@{ | ||
| Type = "Configuration" | ||
| Description = "The prefix that will be added to all resources created by this deployment. (e.g. 'alz')" | ||
| Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix", "parTargetManagementGroupId", "parAssignableScopeManagementGroupId") | ||
| Value = "alz" | ||
| DefaultValue = "alz" | ||
| Valid = "^[a-zA-Z]{3,5}$" | ||
| } | ||
| Suffix = [pscustomobject]@{ | ||
| Description = "The suffix that will be added to all resources created by this deployment." | ||
| Suffix = [pscustomobject]@{ | ||
| Type = "Configuration" | ||
| Description = "The suffix that will be added to all resources created by this deployment. (e.g. 'test')" | ||
| Names = @("parTopLevelManagementGroupSuffix") | ||
| Value = "" | ||
| DefaultValue = "" | ||
| Valid = "^[a-zA-Z]{0,5}$" | ||
| } | ||
| Location = [pscustomobject]@{ | ||
| Location = [pscustomobject]@{ | ||
| Type = "Configuration" | ||
| Description = "Deployment location." | ||
| Names = @("parLocation") | ||
| AllowedValues = @(Get-AzLocation | Sort-Object Location | Select-Object -ExpandProperty Location ) | ||
| Names = @("parLocation", "parAutomationAccountLocation", "parLogAnalyticsWorkspaceLocation") | ||
| AllowedValues = @(Get-AzLocation | Sort-Object Location | Select-Object -ExpandProperty Location) | ||
| Value = "" | ||
| } | ||
| Environment = [pscustomobject]@{ | ||
| Description = "The type of environment that will be created . Example: dev, test, qa, staging, prod" | ||
| Environment = [pscustomobject]@{ | ||
| Type = "Configuration" | ||
| Description = "The type of environment that will be created. (e.g. 'dev', 'test', 'qa', 'staging', 'prod')" | ||
| Names = @("parEnvironment") | ||
| DefaultValue = 'prod' | ||
| Value = "" | ||
| Valid = "^[a-zA-Z0-9]{2,10}$" | ||
| } | ||
| IdentitySubscriptionId = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| 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}$" | ||
| Value = "" | ||
| } | ||
| ConnectivitySubscriptionId = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| 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}$" | ||
|
||
| Value = "" | ||
| } | ||
| ManagementSubscriptionId = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| 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}$" | ||
| Names = @("parLogAnalyticsWorkspaceResourceId") | ||
| Replace = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| Value = "" | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Tests/Unit/Private/Build-ALZDeploymentEnvFile.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #------------------------------------------------------------------------- | ||
| Set-Location -Path $PSScriptRoot | ||
| #------------------------------------------------------------------------- | ||
| $ModuleName = 'ALZ' | ||
| $PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") | ||
| #------------------------------------------------------------------------- | ||
| if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { | ||
| #if the module is already in memory, remove it | ||
| Remove-Module -Name $ModuleName -Force | ||
| } | ||
| Import-Module $PathToManifest -Force | ||
| #------------------------------------------------------------------------- | ||
|
|
||
| InModuleScope 'ALZ' { | ||
| Describe 'Build-AZLDeploymentEnvFile Private Function Tests' -Tag Unit { | ||
| BeforeAll { | ||
| $WarningPreference = 'SilentlyContinue' | ||
| $ErrorActionPreference = 'SilentlyContinue' | ||
| } | ||
| Context 'Build-AZLDeploymentEnvFile should create a .env file correctly' { | ||
| It 'Creates a config file based on configuration.' { | ||
|
|
||
| Mock -CommandName New-Item | ||
| Mock -CommandName Add-Content | ||
|
|
||
| $configuration = [pscustomobject]@{ | ||
| Setting1 = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| Value = "Test1" | ||
| } | ||
| Setting2 = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| Value = "Test2" | ||
| } | ||
| } | ||
|
|
||
| Build-ALZDeploymentEnvFile -configuration $configuration -destination "test" | ||
|
|
||
| Should -Invoke New-Item -ParameterFilter { $Path -match ".env$" } -Scope It -Times 1 -Exactly | ||
| Should -Invoke Add-Content -ParameterFilter { $Value -match "^Setting1=`"Test1`"$" } -Scope It -Times 1 -Exactly | ||
| Should -Invoke Add-Content -ParameterFilter { $Value -match "^Setting2=`"Test2`"$" } -Scope It -Times 1 -Exactly | ||
| } | ||
| It 'Omits configuration not intended for the .env file.' { | ||
|
|
||
| Mock -CommandName New-Item | ||
| Mock -CommandName Add-Content | ||
|
|
||
| $configuration = [pscustomobject]@{ | ||
| Setting1 = [pscustomobject]@{ | ||
| ForEnvironment = $true | ||
| Value = "Test1" | ||
| } | ||
| Setting2 = [pscustomobject]@{ | ||
| ForEnvironment = $false | ||
| Value = "Test2" | ||
| } | ||
| } | ||
|
|
||
| Build-ALZDeploymentEnvFile -configuration $configuration -destination "test" | ||
|
|
||
| Should -Invoke New-Item -ParameterFilter { $Path -match ".env$" } -Scope It -Times 1 -Exactly | ||
| Should -Invoke Add-Content -Scope It -Times 1 -Exactly | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this a function rather than a RegEx. We can have more nuanced handling of error.
We can nest the Regex validation for now - but gives us future scope for better validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I'm finding pscustomobject doesn't house functions all that well - we might need to change to genuine PS classes to get this kind of functionality...