Allow for ability to check properties that are stored in arrays (#167)

* Initial implementation for indexing into properties that are arrays

* Add check that an error record was not already created

* Add examples and unit tests

* Resolve linting

* Add break to catch to exit foreach

* Formatting and pr feedback
This commit is contained in:
Mary Sha 2023-02-23 16:03:17 -08:00 коммит произвёл GitHub
Родитель 2b7852ecda
Коммит 343858e022
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 90 добавлений и 17 удалений

Просмотреть файл

@ -63,10 +63,14 @@ Describe "Confirm-Resource" {
Mock -ModuleName Common Format-ErrorRecord{}
Mock -ModuleName Common Format-IncorrectValueError{}
Mock -ModuleName Common Format-PropertyDoesNotExistError{}
$script:ConfirmResult = [ConfirmResult]::new(
@{
TestKey = "TestValue"
TestArray = @(@{AnotherKey = "AnotherValue"})
}, $null)
}
It "Calls Get-ResourceByType; returns true when Get-ResourceByType returns a Success ConfirmResult." {
$ConfirmResult = [ConfirmResult]::new("resource", $null)
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName"
@ -78,11 +82,23 @@ Describe "Confirm-Resource" {
}
It "Calls Get-ResourceByType; returns true when property matches value." {
$ConfirmResult = [ConfirmResult]::new(@{TestKey = "RightValue"}, $null)
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName" `
-PropertyKey "TestKey" -PropertyValue "RightValue"
-PropertyKey "TestKey" -PropertyValue "TestValue"
Should -InvokeVerifiable
Should -Invoke -ModuleName Common -CommandName "Format-NotExistError" -Times 0
Should -Invoke -ModuleName Common -CommandName "Format-IncorrectValueError" -Times 0
$result.Success | Should -Be $true
}
It "Calls Get-ResourceByType; returns true when accessing property in array and matches value." {
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName" `
-PropertyKey "TestArray[0].AnotherKey" -PropertyValue "AnotherValue"
Should -InvokeVerifiable
Should -Invoke -ModuleName Common -CommandName "Format-NotExistError" -Times 0
@ -103,11 +119,10 @@ Describe "Confirm-Resource" {
}
It "Calls Get-ResourceByType and Format-IncorrectValueError; returns false when property does not match value." {
$ConfirmResult = [ConfirmResult]::new(@{TestKey = "WrongValue"}, $null)
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName" `
-PropertyKey "TestKey" -PropertyValue "RightValue"
-PropertyKey "TestKey" -PropertyValue "WrongValue"
Should -InvokeVerifiable
Should -Invoke -ModuleName Common -CommandName "Format-NotExistError" -Times 0
@ -116,8 +131,19 @@ Describe "Confirm-Resource" {
$result.Success | Should -Be $false
}
It "Calls Get-ResourceByType; returns false when indexing incorrectly into property array" {
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName" `
-PropertyKey "WrongArray[0].AnotherKey" -PropertyValue "AnotherValue"
Should -InvokeVerifiable
$result.Success | Should -Be $false
$result.ErrorRecord | Should -Not -Be $null
}
It "Calls Get-ResourceByType and Format-PropertyDoesNotExistError; returns false when property does not exist." {
$ConfirmResult = [ConfirmResult]::new(@{TestKey = "TestValue"}, $null)
Mock -ModuleName Common Get-ResourceByType{ $ConfirmResult } -Verifiable
$result = Confirm-Resource -ResourceType "ResourceGroup" -ResourceName "mockResourceName" `

Просмотреть файл

@ -91,7 +91,7 @@ function Get-ResourceByType {
ContainerRegistry { return Confirm-ContainerRegistry -Name $ResourceName -ResourceGroupName $ResourceGroupName }
KeyVault { return Confirm-KeyVault -Name $ResourceName -ResourceGroupName $ResourceGroupName }
ResourceGroup { return Confirm-ResourceGroup -ResourceGroupName $ResourceName }
SqlDatabase { return Confirm-SqlDatabase -ServerName $ServerName -DatabaseName $ResourceName -ResourceGroupName $ResourceGroupName}
SqlDatabase { return Confirm-SqlDatabase -ServerName $ServerName -DatabaseName $ResourceName -ResourceGroupName $ResourceGroupName }
SqlServer { return Confirm-SqlServer -ServerName $ResourceName -ResourceGroupName $ResourceGroupName }
VirtualMachine { return Confirm-VirtualMachine -VirtualMachineName $ResourceName -ResourceGroupName $ResourceGroupName }
WebApp { return Confirm-WebApp -WebAppName $ResourceName -ResourceGroupName $ResourceGroupName }
@ -242,9 +242,9 @@ function Confirm-Resource {
$ResourceParams = @{
ResourceGroupName = $ResourceGroupName
ResourceName = $ResourceName
ResourceType = $ResourceType
ServerName = $ServerName
ResourceName = $ResourceName
ResourceType = $ResourceType
ServerName = $ServerName
}
$ConfirmResult = Get-ResourceByType @ResourceParams
@ -252,19 +252,34 @@ function Confirm-Resource {
if ($null -eq $ConfirmResult) {
$ErrorRecord = Format-ErrorRecord -Message "ResourceType is invalid" -ErrorID "InvalidResourceType"
$ConfirmResult = [ConfirmResult]::new($ErrorRecord, $null)
} elseif ($ConfirmResult.Success) {
}
elseif ($ConfirmResult.Success) {
if ($PropertyKey) {
$ActualValue = $ConfirmResult.ResourceDetails
$Keys = $PropertyKey.Split(".")
foreach($Key in $Keys){
$ActualValue = $ActualValue.$Key
# Split property path on open and close square brackets and periods. Remove empty items from array.
$Keys = ($PropertyKey -split '[\[\]\.]').Where({ $_ -ne "" })
foreach ($Key in $Keys) {
# If key is a numerical value, index into array
if ($Key -match "^\d+$") {
try {
$ActualValue = $ActualValue[$Key]
}
catch {
$ErrorRecord = $_
$ConfirmResult = [ConfirmResult]::new($ErrorRecord, $null)
break
}
}
else {
$ActualValue = $ActualValue.$Key
}
}
if ($ActualValue -ne $PropertyValue) {
if ($ActualValue -ne $PropertyValue -and $ConfirmResult.Success -ne $false) {
if ($ActualValue) {
$ErrorRecord = `
Format-IncorrectValueError -ExpectedKey $PropertyKey `
-ExpectedValue $PropertyValue `
-ActualValue $ActualValue
-ExpectedValue $PropertyValue `
-ActualValue $ActualValue
$ConfirmResult = [ConfirmResult]::new($ErrorRecord, $null)
}
else {

Просмотреть файл

@ -157,6 +157,38 @@ Describe 'Use Confirm-AzBPResource to confirm resource and/or properties exist'{
$result.Success | Should -Be $true
}
}
Describe 'Verify Key Vault' {
it 'Should contain a key vault named testkv' {
#arrange
$params = @{
ResourceGroupName = "testrg";
ResourceType = "KeyVault";
ResourceName = "testkv";
}
#act
$result = Confirm-AzBPResource @params
#assert
$result.Success | Should -Be $true
}
it 'Should contain a key vault named testkv with an access policy for testsp service principal' {
#arrange
$params = @{
ResourceGroupName = "testrg";
ResourceType = "KeyVault";
ResourceName = "testkv";
PropertyKey = "AccessPolicies[1].DisplayName";
PropertyValue = "testsp (<your-testsp-appid>)"
}
#act
$result = Confirm-AzBPResource @params
#assert
$result.Success | Should -Be $true
}
}
}
Describe 'Verify Resource Group Does Not Exist' {