diff --git a/CHANGELOG.md b/CHANGELOG.md index 6446be8a..ab31ae08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `.vscode` - `ResolveDependency.ps1` - `build.ps1` + - Change from using `Test-DnsDscParameterState` + to `Test-DscParameterState` (from _DscResource.Common_). + Fixes [[Issue #121](https://github.com/dsccommunity/DnsServerDsc/issues/121)]. + Affected resources: + - `DSC_DnsServerDiagnostics` + - `DSC_DnsServerRootHint` - DnsServerDsc.Common - Added unit tests for `Convert-RootHintsToHashtable`. @@ -54,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed `001.ResourceBase` and replaced with `DscResource.Base`. - Removed `Get-ClassName` and `Get-LocalizedDataRecursive` utilizing ones provided in `DscResource.Base`. +- DnsServerDsc.Common + - Removed `Test-DnsDscParameterState` and associated localization entries. ## [3.0.0] - 2021-05-26 diff --git a/source/DSCResources/DSC_DnsServerDiagnostics/DSC_DnsServerDiagnostics.psm1 b/source/DSCResources/DSC_DnsServerDiagnostics/DSC_DnsServerDiagnostics.psm1 index b68bd4aa..ea4d2802 100644 --- a/source/DSCResources/DSC_DnsServerDiagnostics/DSC_DnsServerDiagnostics.psm1 +++ b/source/DSCResources/DSC_DnsServerDiagnostics/DSC_DnsServerDiagnostics.psm1 @@ -523,9 +523,14 @@ function Test-TargetResource $currentState = Get-TargetResource -DnsServer $DnsServer - $null = $PSBoundParameters.Remove('DnsServer') + $params = @{ + CurrentValues = $currentState + DesiredValues = $PSBoundParameters + ExcludeProperties = @('DnsServer') + TurnOffTypeChecking = $true + } - $result = Test-DscDnsParameterState -CurrentValues $currentState -DesiredValues $PSBoundParameters -TurnOffTypeChecking -Verbose:$VerbosePreference + $result = Test-DscParameterState @params return $result } diff --git a/source/DSCResources/DSC_DnsServerRootHint/DSC_DnsServerRootHint.psm1 b/source/DSCResources/DSC_DnsServerRootHint/DSC_DnsServerRootHint.psm1 index 440ac470..b1add66c 100644 --- a/source/DSCResources/DSC_DnsServerRootHint/DSC_DnsServerRootHint.psm1 +++ b/source/DSCResources/DSC_DnsServerRootHint/DSC_DnsServerRootHint.psm1 @@ -118,7 +118,14 @@ function Test-TargetResource $entry.Value = $entry.Value -replace ' ', '' } - $result = Test-DscDnsParameterState -CurrentValues $currentState -DesiredValues $desiredState -TurnOffTypeChecking -ReverseCheck + $params = @{ + CurrentValues = $currentState + DesiredValues = $desiredState + TurnOffTypeChecking = $true + ReverseCheck = $true + } + + $result = Test-DscParameterState @params $result } diff --git a/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psd1 b/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psd1 index a6e918d5..b21b497c 100644 --- a/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psd1 +++ b/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psd1 @@ -24,7 +24,6 @@ FunctionsToExport = @( 'ConvertTo-FollowRfc1034' 'Convert-RootHintsToHashtable' - 'Test-DscDnsParameterState' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psm1 b/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psm1 index d3e32322..14e79277 100644 --- a/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psm1 +++ b/source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psm1 @@ -84,333 +84,3 @@ function Convert-RootHintsToHashtable return $r } - -<# - .SYNOPSIS - Tests the status of DSC resource parameters. - - .DESCRIPTION - This function tests the parameter status of DSC resource parameters against the current values present on the system. - - .PARAMETER CurrentValues - A hashtable with the current values on the system, obtained by e.g. Get-TargetResource. - - .PARAMETER DesiredValues - The hashtable of desired values. - - .PARAMETER ValuesToCheck - The values to check if not all values should be checked. - - .PARAMETER TurnOffTypeChecking - Indicates that the type of the parameter should not be checked. - - .PARAMETER ReverseCheck - Indicates that a reverse check should be done. The current and desired state are swapped for another test. - - .PARAMETER SortArrayValues - If the sorting of array values does not matter, values are sorted internally before doing the comparison. - - .NOTES - This function is enhanced with additional parameters compared to the function - Test-DscParameterState that is available in the module DscResource.Common. - These enhancements should be merged into DscResource.Common; - https://github.com/dsccommunity/DscResource.Common/blob/master/source/Public/Test-DscParameterState.ps1. -#> -function Test-DscDnsParameterState -{ - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [System.Object] - $CurrentValues, - - [Parameter(Mandatory = $true)] - [System.Object] - $DesiredValues, - - [Parameter()] - [System.String[]] - $ValuesToCheck, - - [Parameter()] - [switch] - $TurnOffTypeChecking, - - [Parameter()] - [switch] - $ReverseCheck, - - [Parameter()] - [switch] - $SortArrayValues - ) - - $returnValue = $true - - if ($CurrentValues -is [Microsoft.Management.Infrastructure.CimInstance] -or - $CurrentValues -is [Microsoft.Management.Infrastructure.CimInstance[]]) - { - $CurrentValues = ConvertTo-HashTable -CimInstance $CurrentValues - } - - if ($DesiredValues -is [Microsoft.Management.Infrastructure.CimInstance] -or - $DesiredValues -is [Microsoft.Management.Infrastructure.CimInstance[]]) - { - $DesiredValues = ConvertTo-HashTable -CimInstance $DesiredValues - } - - $types = 'System.Management.Automation.PSBoundParametersDictionary', 'System.Collections.Hashtable', 'Microsoft.Management.Infrastructure.CimInstance' - - if ($DesiredValues.GetType().FullName -notin $types) - { - New-InvalidArgumentException ` - -Message ($script:localizedData.InvalidDesiredValuesError -f $DesiredValues.GetType().FullName) ` - -ArgumentName 'DesiredValues' - } - - if ($CurrentValues.GetType().FullName -notin $types) - { - New-InvalidArgumentException ` - -Message ($script:localizedData.InvalidCurrentValuesError -f $CurrentValues.GetType().FullName) ` - -ArgumentName 'CurrentValues' - } - - if ($DesiredValues -is [Microsoft.Management.Infrastructure.CimInstance] -and -not $ValuesToCheck) - { - New-InvalidArgumentException ` - -Message $script:localizedData.InvalidValuesToCheckError ` - -ArgumentName 'ValuesToCheck' - } - - $desiredValuesClean = Remove-CommonParameter -Hashtable $DesiredValues - - if (-not $ValuesToCheck) - { - $keyList = $desiredValuesClean.Keys - } - else - { - $keyList = $ValuesToCheck - } - - foreach ($key in $keyList) - { - $desiredValue = $desiredValuesClean.$key - $currentValue = $CurrentValues.$key - - if ($desiredValue -is [Microsoft.Management.Infrastructure.CimInstance] -or - $desiredValue -is [Microsoft.Management.Infrastructure.CimInstance[]]) - { - $desiredValue = ConvertTo-HashTable -CimInstance $desiredValue - } - if ($currentValue -is [Microsoft.Management.Infrastructure.CimInstance] -or - $currentValue -is [Microsoft.Management.Infrastructure.CimInstance[]]) - { - $currentValue = ConvertTo-HashTable -CimInstance $currentValue - } - - if ($null -ne $desiredValue) - { - $desiredType = $desiredValue.GetType() - } - else - { - $desiredType = @{ - Name = 'Unknown' - } - } - - if ($null -ne $currentValue) - { - $currentType = $currentValue.GetType() - } - else - { - $currentType = @{ - Name = 'Unknown' - } - } - - if ($currentType.Name -ne 'Unknown' -and $desiredType.Name -eq 'PSCredential') - { - # This is a credential object. Compare only the user name - if ($currentType.Name -eq 'PSCredential' -and $currentValue.UserName -eq $desiredValue.UserName) - { - Write-Verbose -Message ($script:localizedData.MatchPsCredentialUsernameMessage -f $currentValue.UserName, $desiredValue.UserName) - continue - } - else - { - Write-Verbose -Message ($script:localizedData.NoMatchPsCredentialUsernameMessage -f $currentValue.UserName, $desiredValue.UserName) - $returnValue = $false - } - - # Assume the string is our username when the matching desired value is actually a credential - if ($currentType.Name -eq 'string' -and $currentValue -eq $desiredValue.UserName) - { - Write-Verbose -Message ($script:localizedData.MatchPsCredentialUsernameMessage -f $currentValue, $desiredValue.UserName) - continue - } - else - { - Write-Verbose -Message ($script:localizedData.NoMatchPsCredentialUsernameMessage -f $currentValue, $desiredValue.UserName) - $returnValue = $false - } - } - - if (-not $TurnOffTypeChecking) - { - if (($desiredType.Name -ne 'Unknown' -and $currentType.Name -ne 'Unknown') -and - $desiredType.FullName -ne $currentType.FullName) - { - Write-Verbose -Message ($script:localizedData.NoMatchTypeMismatchMessage -f $key, $currentType.Name, $desiredType.Name) - $returnValue = $false - continue - } - } - - if ($currentValue -eq $desiredValue -and -not $desiredType.IsArray) - { - Write-Verbose -Message ($script:localizedData.MatchValueMessage -f $desiredType.Name, $key, $currentValue, $desiredValue) - continue - } - - if ($desiredValuesClean.GetType().Name -in 'HashTable', 'PSBoundParametersDictionary') - { - $checkDesiredValue = $desiredValuesClean.ContainsKey($key) - } - else - { - $checkDesiredValue = Test-DscObjectHasProperty -Object $desiredValuesClean -PropertyName $key - } - - if (-not $checkDesiredValue) - { - Write-Verbose -Message ($script:localizedData.MatchValueMessage -f $desiredType.Name, $key, $currentValue, $desiredValue) - continue - } - - if ($desiredType.IsArray) - { - Write-Verbose -Message ($script:localizedData.TestDscParameterCompareMessage -f $key) - - if (-not $currentValue) - { - Write-Verbose -Message ($script:localizedData.NoMatchValueMessage -f $desiredType.Name, $key, $currentValue, $desiredValue) - $returnValue = $false - continue - } - elseif ($currentValue.Count -ne $desiredValue.Count) - { - Write-Verbose -Message ($script:localizedData.NoMatchValueDifferentCountMessage -f $desiredType.Name, $key, $currentValue.Count, $desiredValue.Count) - $returnValue = $false - continue - } - else - { - $desiredArrayValues = $desiredValue - $currentArrayValues = $currentValue - - if ($SortArrayValues) - { - $desiredArrayValues = $desiredArrayValues | Sort-Object - $currentArrayValues = $currentArrayValues | Sort-Object - } - - for ($i = 0; $i -lt $desiredArrayValues.Count; $i++) - { - if ($null -ne $desiredArrayValues[$i]) - { - $desiredType = $desiredArrayValues[$i].GetType() - } - else - { - $desiredType = @{ - Name = 'Unknown' - } - } - - if ($null -ne $currentArrayValues[$i]) - { - $currentType = $currentArrayValues[$i].GetType() - } - else - { - $currentType = @{ - Name = 'Unknown' - } - } - - if (-not $TurnOffTypeChecking) - { - if (($desiredType.Name -ne 'Unknown' -and $currentType.Name -ne 'Unknown') -and - $desiredType.FullName -ne $currentType.FullName) - { - Write-Verbose -Message ($script:localizedData.NoMatchElementTypeMismatchMessage -f $key, $i, $currentType.Name, $desiredType.Name) - $returnValue = $false - continue - } - } - - if ($desiredArrayValues[$i] -ne $currentArrayValues[$i]) - { - Write-Verbose -Message ($script:localizedData.NoMatchElementValueMismatchMessage -f $i, $desiredType.Name, $key, $currentArrayValues[$i], $desiredArrayValues[$i]) - $returnValue = $false - continue - } - else - { - Write-Verbose -Message ($script:localizedData.MatchElementValueMessage -f $i, $desiredType.Name, $key, $currentArrayValues[$i], $desiredArrayValues[$i]) - continue - } - } - - } - } - elseif ($desiredType -eq [System.Collections.Hashtable] -and $currentType -eq [System.Collections.Hashtable]) - { - $param = $PSBoundParameters - $param.CurrentValues = $currentValue - $param.DesiredValues = $desiredValue - [void]$param.Remove('ValuesToCheck') - if ($returnValue) - { - $returnValue = Test-DscDnsParameterState @param - } - else - { - Test-DscDnsParameterState @param | Out-Null - } - continue - } - else - { - if ($desiredValue -ne $currentValue) - { - Write-Verbose -Message ($script:localizedData.NoMatchValueMessage -f $desiredType.Name, $key, $currentValue, $desiredValue) - $returnValue = $false - } - } - } - - if ($ReverseCheck) - { - Write-Verbose -Message $script:localizedData.StartingReverseCheck - $reverseCheckParameters = $PSBoundParameters - $reverseCheckParameters.CurrentValues = $DesiredValues - $reverseCheckParameters.DesiredValues = $CurrentValues - [void] $reverseCheckParameters.Remove('ReverseCheck') - if ($returnValue) - { - $returnValue = Test-DscDnsParameterState @reverseCheckParameters - } - else - { - Test-DscDnsParameterState @reverseCheckParameters | Out-Null - } - } - - Write-Verbose -Message ($script:localizedData.TestDscParameterResultMessage -f $returnValue) - return $returnValue -} diff --git a/source/Modules/DnsServerDsc.Common/en-US/DnsServerDsc.Common.strings.psd1 b/source/Modules/DnsServerDsc.Common/en-US/DnsServerDsc.Common.strings.psd1 index 81f9d668..3b35d661 100644 --- a/source/Modules/DnsServerDsc.Common/en-US/DnsServerDsc.Common.strings.psd1 +++ b/source/Modules/DnsServerDsc.Common/en-US/DnsServerDsc.Common.strings.psd1 @@ -1,17 +1,2 @@ ConvertFrom-StringData @' - InvalidCurrentValuesError = Property 'CurrentValues' in Test-DscDnsParameterState must be either a Hashtable, CimInstance or CimIntance[]. Type detected was '{0}'. - InvalidDesiredValuesError = Property 'DesiredValues' in Test-DscDnsParameterState must be either a Hashtable or CimInstance. Type detected was '{0}'. - InvalidValuesToCheckError = If 'DesiredValues' is a CimInstance then property 'ValuesToCheck' must contain a value. - TestDscParameterCompareMessage = Comparing values in property '{0}'. - MatchPsCredentialUsernameMessage = MATCH: PSCredential username match. Current state is '{0}' and desired state is '{1}'. - NoMatchPsCredentialUsernameMessage = NOTMATCH: PSCredential username mismatch. Current state is '{0}' and desired state is '{1}'. - NoMatchTypeMismatchMessage = NOTMATCH: Type mismatch for property '{0}' Current state type is '{1}' and desired type is '{2}'. - MatchValueMessage = MATCH: Value (type '{0}') for property '{1}' does match. Current state is '{2}' and desired state is '{3}'. - NoMatchValueMessage = NOTMATCH: Value (type '{0}') for property '{1}' does not match. Current state is '{2}' and desired state is '{3}'. - NoMatchValueDifferentCountMessage = NOTMATCH: Value (type '{0}') for property '{1}' does have a different count. Current state count is '{2}' and desired state count is '{3}'. - NoMatchElementTypeMismatchMessage = NOTMATCH: Type mismatch for property '{0}' Current state type of element [{1}] is '{2}' and desired type is '{3}'. - NoMatchElementValueMismatchMessage = NOTMATCH: Value [{0}] (type '{1}') for property '{2}' does match. Current state is '{3}' and desired state is '{4}'. - MatchElementValueMessage = MATCH: Value [{0}] (type '{1}') for property '{2}' does match. Current state is '{3}' and desired state is '{4}'. - TestDscParameterResultMessage = Test-DscParameter result is '{0}'. - StartingReverseCheck = Starting with a reverse check. '@ diff --git a/tests/Unit/DSC_DnsServerRootHint.Tests.ps1 b/tests/Unit/DSC_DnsServerRootHint.Tests.ps1 index d4454ad3..204d53f3 100644 --- a/tests/Unit/DSC_DnsServerRootHint.Tests.ps1 +++ b/tests/Unit/DSC_DnsServerRootHint.Tests.ps1 @@ -139,7 +139,7 @@ Describe 'DSC_DnsServerRootHint\Get-TargetResource' { } $targetResource = Get-TargetResource @params - Test-DscDnsParameterState -CurrentValues $targetResource.NameServer -DesiredValues $rootHintsHashtable | Should -BeTrue + Test-DscParameterState -CurrentValues $targetResource.NameServer -DesiredValues $rootHintsHashtable | Should -BeTrue } } }