forked from dsccommunity/ActiveDirectoryDsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added localization module DscResource.LocalizationHelper containing the helper functions Get-LocalizedData, New-InvalidArgumentException, New-InvalidOperationException, New-ObjectNotFoundException, and New-InvalidResultException (issue dsccommunity#257). - Added common module DscResource.Common containing the helper function Test-DscParameterState. The goal is that all resource common functions are moved to this module (functions that are or can be used by more than one resource) (issue dsccommunity#257).
- Loading branch information
Showing
8 changed files
with
974 additions
and
159 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,190 @@ | ||
$script:modulesFolderPath = Split-Path -Path $PSScriptRoot -Parent | ||
|
||
$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.LocalizationHelper' | ||
Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath 'DscResource.LocalizationHelper.psm1') | ||
|
||
|
||
$script:localizedData = Get-LocalizedData -ResourceName 'DscResource.Common' -ScriptRoot $PSScriptRoot | ||
|
||
<# | ||
.SYNOPSIS | ||
This method is used to compare current and desired values for any DSC resource. | ||
.PARAMETER CurrentValues | ||
This is hash table of the current values that are applied to the resource. | ||
.PARAMETER DesiredValues | ||
This is a PSBoundParametersDictionary of the desired values for the resource. | ||
.PARAMETER ValuesToCheck | ||
This is a list of which properties in the desired values list should be checked. | ||
If this is empty then all values in DesiredValues are checked. | ||
#> | ||
function Test-DscParameterState | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.Collections.Hashtable] | ||
$CurrentValues, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.Object] | ||
$DesiredValues, | ||
|
||
[Parameter()] | ||
[System.Array] | ||
$ValuesToCheck | ||
) | ||
|
||
$returnValue = $true | ||
|
||
if (($DesiredValues.GetType().Name -ne 'HashTable') ` | ||
-and ($DesiredValues.GetType().Name -ne 'CimInstance') ` | ||
-and ($DesiredValues.GetType().Name -ne 'PSBoundParametersDictionary')) | ||
{ | ||
$errorMessage = $script:localizedData.PropertyTypeInvalidForDesiredValues -f $($DesiredValues.GetType().Name) | ||
New-InvalidArgumentException -ArgumentName 'DesiredValues' -Message $errorMessage | ||
} | ||
|
||
if (($DesiredValues.GetType().Name -eq 'CimInstance') -and ($null -eq $ValuesToCheck)) | ||
{ | ||
$errorMessage = $script:localizedData.PropertyTypeInvalidForValuesToCheck | ||
New-InvalidArgumentException -ArgumentName 'ValuesToCheck' -Message $errorMessage | ||
} | ||
|
||
if (($null -eq $ValuesToCheck) -or ($ValuesToCheck.Count -lt 1)) | ||
{ | ||
$keyList = $DesiredValues.Keys | ||
} | ||
else | ||
{ | ||
$keyList = $ValuesToCheck | ||
} | ||
|
||
$keyList | ForEach-Object -Process { | ||
if (($_ -ne 'Verbose')) | ||
{ | ||
if (($CurrentValues.ContainsKey($_) -eq $false) ` | ||
-or ($CurrentValues.$_ -ne $DesiredValues.$_) ` | ||
-or (($DesiredValues.GetType().Name -ne 'CimInstance' -and $DesiredValues.ContainsKey($_) -eq $true) -and ($null -ne $DesiredValues.$_ -and $DesiredValues.$_.GetType().IsArray))) | ||
{ | ||
if ($DesiredValues.GetType().Name -eq 'HashTable' -or ` | ||
$DesiredValues.GetType().Name -eq 'PSBoundParametersDictionary') | ||
{ | ||
$checkDesiredValue = $DesiredValues.ContainsKey($_) | ||
} | ||
else | ||
{ | ||
# If DesiredValue is a CimInstance. | ||
$checkDesiredValue = $false | ||
if (([System.Boolean]($DesiredValues.PSObject.Properties.Name -contains $_)) -eq $true) | ||
{ | ||
if ($null -ne $DesiredValues.$_) | ||
{ | ||
$checkDesiredValue = $true | ||
} | ||
} | ||
} | ||
|
||
if ($checkDesiredValue) | ||
{ | ||
$desiredType = $DesiredValues.$_.GetType() | ||
$fieldName = $_ | ||
if ($desiredType.IsArray -eq $true) | ||
{ | ||
if (($CurrentValues.ContainsKey($fieldName) -eq $false) ` | ||
-or ($null -eq $CurrentValues.$fieldName)) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.PropertyValidationError -f $fieldName) -Verbose | ||
|
||
$returnValue = $false | ||
} | ||
else | ||
{ | ||
$arrayCompare = Compare-Object -ReferenceObject $CurrentValues.$fieldName ` | ||
-DifferenceObject $DesiredValues.$fieldName | ||
if ($null -ne $arrayCompare) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.PropertiesDoesNotMatch -f $fieldName) -Verbose | ||
|
||
$arrayCompare | ForEach-Object -Process { | ||
Write-Verbose -Message ($script:localizedData.PropertyThatDoesNotMatch -f $_.InputObject, $_.SideIndicator) -Verbose | ||
} | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
switch ($desiredType.Name) | ||
{ | ||
'String' | ||
{ | ||
if (-not [System.String]::IsNullOrEmpty($CurrentValues.$fieldName) -or ` | ||
-not [System.String]::IsNullOrEmpty($DesiredValues.$fieldName)) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.ValueOfTypeDoesNotMatch ` | ||
-f $desiredType.Name, $fieldName, $($CurrentValues.$fieldName), $($DesiredValues.$fieldName)) -Verbose | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
|
||
'Int32' | ||
{ | ||
if (-not ($DesiredValues.$fieldName -eq 0) -or ` | ||
-not ($null -eq $CurrentValues.$fieldName)) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.ValueOfTypeDoesNotMatch ` | ||
-f $desiredType.Name, $fieldName, $($CurrentValues.$fieldName), $($DesiredValues.$fieldName)) -Verbose | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
|
||
{ $_ -eq 'Int16' -or $_ -eq 'UInt16' -or $_ -eq 'Single' } | ||
{ | ||
if (-not ($DesiredValues.$fieldName -eq 0) -or ` | ||
-not ($null -eq $CurrentValues.$fieldName)) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.ValueOfTypeDoesNotMatch ` | ||
-f $desiredType.Name, $fieldName, $($CurrentValues.$fieldName), $($DesiredValues.$fieldName)) -Verbose | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
|
||
'Boolean' | ||
{ | ||
if ($CurrentValues.$fieldName -ne $DesiredValues.$fieldName) | ||
{ | ||
Write-Verbose -Message ($script:localizedData.ValueOfTypeDoesNotMatch ` | ||
-f $desiredType.Name, $fieldName, $($CurrentValues.$fieldName), $($DesiredValues.$fieldName)) -Verbose | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
|
||
default | ||
{ | ||
Write-Warning -Message ($script:localizedData.UnableToCompareProperty ` | ||
-f $fieldName, $desiredType.Name) | ||
|
||
$returnValue = $false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $returnValue | ||
} | ||
|
||
Export-ModuleMember -Function @( | ||
'Test-DscParameterState' | ||
) |
11 changes: 11 additions & 0 deletions
11
Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1
This file contains 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,11 @@ | ||
# Localized resources for helper module DscResource.Common. | ||
|
||
ConvertFrom-StringData @' | ||
PropertyTypeInvalidForDesiredValues = Property 'DesiredValues' must be either a [System.Collections.Hashtable], [CimInstance] or [PSBoundParametersDictionary]. The type detected was {0}. | ||
PropertyTypeInvalidForValuesToCheck = If 'DesiredValues' is a CimInstance, then property 'ValuesToCheck' must contain a value. | ||
PropertyValidationError = Expected to find an array value for property {0} in the current values, but it was either not present or was null. This has caused the test method to return false. | ||
PropertiesDoesNotMatch = Found an array for property {0} in the current values, but this array does not match the desired state. Details of the changes are below. | ||
PropertyThatDoesNotMatch = {0} - {1} | ||
ValueOfTypeDoesNotMatch = {0} value for property {1} does not match. Current state is '{2}' and desired state is '{3}'. | ||
UnableToCompareProperty = Unable to compare property {0} as the type {1} is not handled by the Test-DscParameterState cmdlet. | ||
'@ |
Oops, something went wrong.