Skip to content

Commit

Permalink
Changes to xActiveDirectory
Browse files Browse the repository at this point in the history
- 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
johlju committed Apr 26, 2019
1 parent 86745b5 commit 544b77e
Show file tree
Hide file tree
Showing 8 changed files with 974 additions and 159 deletions.
58 changes: 0 additions & 58 deletions DSCResources/CommonResourceHelper.psm1

This file was deleted.

190 changes: 190 additions & 0 deletions Modules/DscResource.Common/DscResource.Common.psm1
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 Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1
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.
'@

0 comments on commit 544b77e

Please sign in to comment.