Skip to content

Commit

Permalink
ADReplicationSiteLink: Refactor Test-TargetResource Function (#601)
Browse files Browse the repository at this point in the history
This PR refactors the Test-TargetResource function of the ADReplicationSiteLink resource.
  • Loading branch information
X-Guardian authored May 2, 2020
1 parent 2b5590a commit fa3aee9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- ADDomainTrust
- Move `Get-ActiveDirectoryDomain` and `Get-ActiveDirectoryForest` functions
into the `ActiveDirectoryDsc.Common` module.
- ADReplicationSiteLink
- Refactor Test-TargetResource Function.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,53 +444,99 @@ function Test-TargetResource
$Ensure = 'Present'
)

$isCompliant = $true
$currentSiteLink = Get-TargetResource -Name $Name
$parameters = @{} + $PSBoundParameters
$parameters.Remove('Ensure')
$parameters.Remove('Verbose')
$parameters.Remove('Debug')

# Test for Ensure.
if ($Ensure -ne $currentSiteLink.Ensure)
{
return $false
}
elseif ($Ensure -eq 'Absent')
{
return $true
}
# Add parameters with default values as they may not be explicitly passed
$parameters['OptionChangeNotification'] = $OptionChangeNotification
$parameters['OptionTwoWaySync'] = $OptionTwoWaySync
$parameters['OptionDisableCompression'] = $OptionDisableCompression

$targetResource = Get-TargetResource -Name $Name

# Test for SitesIncluded.
foreach ($desiredIncludedSite in $SitesIncluded)
$inDesiredState = $true

if ($targetResource.Ensure -eq 'Present')
{
if ($desiredIncludedSite -notin $currentSiteLink.SitesIncluded)
# Resource is Present
if ($Ensure -eq 'Present')
{
Write-Verbose -Message ($script:localizedData.SiteNotFound -f $desiredIncludedSite, $($currentSiteLink.SitesIncluded -join ', '))
$isCompliant = $false
}
}
# Resource Should be Present
foreach ($parameter in $parameters.Keys)
{
if ($parameter -eq 'SitesIncluded')
{
foreach ($desiredIncludedSite in $SitesIncluded)
{
if ($desiredIncludedSite -notin $targetResource.SitesIncluded)
{
Write-Verbose -Message ($script:localizedData.SiteNotFound -f
$desiredIncludedSite, $($targetResource.SitesIncluded -join ', '))
$inDesiredState = $false
}
}
}
elseif ($parameter -eq 'SitesExcluded')
{
foreach ($desiredExcludedSite in $SitesExcluded)
{
if ($desiredExcludedSite -in $targetResource.SitesIncluded)
{
Write-Verbose -Message ($script:localizedData.SiteFoundInExcluded -f
$desiredExcludedSite, $($targetResource.SitesIncluded -join ', '))
$inDesiredState = $false
}
}
}
elseif ($parameters[$parameter] -ne $targetResource[$parameter])
{
Write-Verbose -Message ($script:localizedData.PropertyNotInDesiredState -f
$parameter, $targetResource[$parameter], $parameters[$parameter])
$inDesiredState = $false
}
}

# Test for SitesExcluded.
foreach ($desiredExcludedSite in $SitesExcluded)
{
if ($desiredExcludedSite -in $currentSiteLink.SitesIncluded)
if ($inDesiredState)
{
# Resource is in desired state
Write-Verbose -Message ($script:localizedData.ADSiteInDesiredState -f $Name)
}
else
{
# Resource is not in the desired state
Write-Verbose -Message ($script:localizedData.ADSiteNotInDesiredState -f $Name)
}
}
else
{
Write-Verbose -Message ($script:localizedData.SiteFoundInExcluded -f $desiredExcludedSite, $($currentSiteLink.SitesIncluded -join ', '))
$isCompliant = $false
# Resource Should be Absent
Write-Verbose -Message ($script:localizedData.ADSiteIsPresentButShouldBeAbsent -f $Name)

$inDesiredState = $false
}
}

foreach ($parameter in $PSBoundParameters.Keys)
else
{
# Test for Description|ReplicationFrequencyInMinutes|Cost.
if ($parameter -match 'Description|ReplicationFrequencyInMinutes|Cost|OptionChangeNotification|OptionTwoWaySync|OptionDisableCompression')
# Resource is Absent
if ($Ensure -eq 'Present')
{
if ($PSBoundParameters[$parameter] -ne $currentSiteLink[$parameter])
{
Write-Verbose -Message ($script:localizedData.PropertyNotInDesiredState -f $parameter, $($currentSiteLink[$parameter]), $($PSBoundParameters[$parameter]))
$isCompliant = $false
}
# Resource Should be Present
Write-Verbose -Message ($script:localizedData.ADSiteIsAbsentButShouldBePresent -f $Name)

$inDesiredState = $false
}
else
{
# Resource should be Absent
Write-Verbose ($script:localizedData.ADSiteInDesiredState -f $Name)

$inDesiredState = $true
}
}

return $isCompliant
return $inDesiredState
}

<#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
ConvertFrom-StringData @'
SiteNotFound = Site: '{0}' not found in SitesIncluded. Current SitesIncluded: '{1}'. (ADRSL0001)
SiteFoundInExcluded = Excluded '{0}' site found in SitesIncluded. Current SitesIncluded: '{1}'. (ADRSL0002)
PropertyNotInDesiredState = '{0}' is not in desired state Current: '{1}' Desired: '{2}'. (ADRSL0003)
SettingProperty = Setting property '{0}' to '{1}' on site link '{2}'. (ADRSL0004)
RemovingSites = Removing sites '{0}' from site link '{1}'. (ADRSL0005)
AddingSites = Adding sites '{0}' to site link '{1}'. (ADRSL0006)
NewSiteLink = Creating AD Site Link '{0}'. (ADRSL0007)
RemoveSiteLink = Removing AD Site Link '{0}'. (ADRSL0008)
SiteLinkNotFound = Could not find '{0}' site link. (ADRSL0009)
GetSiteLinkUnexpectedError = Unexpected error getting site link '{0}'. (ADRSL0010)
SiteNotFound = Site: '{0}' not found in SitesIncluded. Current SitesIncluded: '{1}'. (ADRSL0001)
SiteFoundInExcluded = Excluded '{0}' site found in SitesIncluded. Current SitesIncluded: '{1}'. (ADRSL0002)
PropertyNotInDesiredState = '{0}' is not in desired state Current: '{1}' Desired: '{2}'. (ADRSL0003)
SettingProperty = Setting property '{0}' to '{1}' on site link '{2}'. (ADRSL0004)
RemovingSites = Removing sites '{0}' from site link '{1}'. (ADRSL0005)
AddingSites = Adding sites '{0}' to site link '{1}'. (ADRSL0006)
NewSiteLink = Creating AD Site Link '{0}'. (ADRSL0007)
RemoveSiteLink = Removing AD Site Link '{0}'. (ADRSL0008)
SiteLinkNotFound = Could not find '{0}' site link. (ADRSL0009)
GetSiteLinkUnexpectedError = Unexpected error getting site link '{0}'. (ADRSL0010)
ADSiteInDesiredState = '{0}' is in the desired state. (ADRSL0011)
ADSiteNotInDesiredState = '{0}' is not in the desired state. (ADRSL0012)
ADSiteIsPresentButShouldBeAbsent = '{0}' is present but should be absent. (ADRSL0013)
ADSiteIsAbsentButShouldBePresent = '{0}' is absent but should be present. (ADRSL0014)
'@

0 comments on commit fa3aee9

Please sign in to comment.