Skip to content

Commit

Permalink
Changes to xWaitForCluster
Browse files Browse the repository at this point in the history
- Enabled localization for all strings (issue dsccommunity#88).
  • Loading branch information
johlju committed Jul 27, 2017
1 parent 9879aa5 commit 513aa43
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- Changed the code to be more aligned with the style guideline.
- Updated parameter description in the schema.mof.
- Resolved Script Analyzer warnings ([issue #54](https://github.com/PowerShell/xFailOverCluster/issues/54)).
- Enabled localization for all strings ([issue #88](https://github.com/PowerShell/xFailOverCluster/issues/88)).
- Changes to xClusterQuorum
- Refactored the unit test for this resource to use stubs and increase coverage
([issue #77](https://github.com/PowerShell/xFailOverCluster/issues/77)).
Expand Down
32 changes: 20 additions & 12 deletions DSCResources/MSFT_xWaitForCluster/MSFT_xWaitForCluster.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xWaitForCluster'

<#
.SYNOPSIS
Get the values for which failover cluster and for how long to wait for the
Expand Down Expand Up @@ -31,6 +36,8 @@ function Get-TargetResource
$RetryCount = 50
)

Write-Verbose -Message $script:localizedData.ReturnParameterValues

@{
Name = $Name
RetryIntervalSec = $RetryIntervalSec
Expand Down Expand Up @@ -71,7 +78,7 @@ function Set-TargetResource
)

$clusterFound = $false
Write-Verbose -Message "Checking for the existance of failover cluster $Name."
Write-Verbose -Message ($script:localizedData.CheckClusterPresent -f $Name)

for ($count = 0; $count -lt $RetryCount; $count++)
{
Expand All @@ -80,31 +87,32 @@ function Set-TargetResource
$computerObject = Get-CimInstance -ClassName Win32_ComputerSystem
if ($null -eq $computerObject -or $null -eq $computerObject.Domain)
{
Write-Verbose -Message "Can't find machine's domain name"
Write-Verbose -Message $script:localizedData.TargetNodeDomainMissing
break
}

$cluster = Get-Cluster -Name $Name -Domain $computerObject.Domain

if ($null -ne $cluster)
{
Write-Verbose -Message "Found failover cluster $Name"
Write-Verbose -Message ($script:localizedData.ClusterPresent -f $Name)
$clusterFound = $true
break
}
}
catch
{
Write-Verbose -Message "Failover cluster $Name not found. Will retry again after $RetryIntervalSec sec"
Write-Verbose -Message ($script:localizedData.ClusterAbsent -f $Name, $RetryIntervalSec)
}

Write-Verbose -Message "Failover cluster $Name not found. Will retry again after $RetryIntervalSec sec"
Write-Verbose -Message ($script:localizedData.ClusterAbsent -f $Name, $RetryIntervalSec)
Start-Sleep -Seconds $RetryIntervalSec
}

if (! $clusterFound)
if (-not $clusterFound)
{
throw "Failover cluster $Name not found after $count attempts with $RetryIntervalSec sec interval"
$errorMessage = $script:localizedData.ClusterAbsentAfterTimeOut -f $Name, $count, $RetryIntervalSec
New-InvalidOperationException -Message $errorMessage
}
}

Expand Down Expand Up @@ -140,7 +148,7 @@ function Test-TargetResource
$RetryCount = 50
)

Write-Verbose -Message "Checking for Cluster $Name ..."
Write-Verbose -Message ($script:localizedData.EvaluatingClusterPresent -f $Name)

$testTargetResourceReturnValue = $false

Expand All @@ -149,25 +157,25 @@ function Test-TargetResource
$computerObject = Get-CimInstance -ClassName Win32_ComputerSystem
if ($null -eq $computerObject -or $null -eq $computerObject.Domain)
{
Write-Verbose -Message "Can't find machine's domain name"
Write-Verbose -Message $script:localizedData.TargetNodeDomainMissing
}
else
{
$cluster = Get-Cluster -Name $Name -Domain $computerObject.Domain
if ($null -eq $cluster)
{
Write-Verbose -Message "Cluster $Name not found in domain $computerObject.Domain"
Write-Verbose -Message ($script:localizedData.ClusterAbsentWithDomain -f $Name, $computerObject.Domain)
}
else
{
Write-Verbose -Message "Found cluster $Name"
Write-Verbose -Message ($script:localizedData.ClusterPresent -f $Name)
$testTargetResourceReturnValue = $true
}
}
}
catch
{
Write-Verbose -Message "Cluster $Name not found"
Write-Verbose -Message ($script:localizedData.ClusterAbsentWithError -f $Name, $_.Message)
}

$testTargetResourceReturnValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Localized resources for xWaitForCluster

ConvertFrom-StringData @'
ReturnParameterValues = Returning the values passed as parameters.
CheckClusterPresent = Checking if cluster {0} is present.
TargetNodeDomainMissing = Can't find the target node's domain name.
ClusterPresent = Cluster {0} is present.
ClusterAbsent = Cluster {0} is NOT present. Will retry again after {1} seconds.
ClusterAbsentWithDomain = Cluster {0} is NOT present in Active Directory domain '{1}'.
ClusterAbsentWithError = Cluster {0} is NOT present with error: {1}
ClusterAbsentAfterTimeOut = Failover cluster {0} was not found after {1} attempts with {2} seconds interval.
EvaluatingClusterPresent = Evaluating if cluster {0} is present.
'@
6 changes: 4 additions & 2 deletions Tests/Unit/MSFT_xWaitForCluster.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ try
It 'Should throw the correct error message' {
Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockCimInstance_ParameterFilter -Verifiable

{ Set-TargetResource @mockDefaultParameters } | Should Throw ('Failover cluster {0} not found after {1} attempts with {2} sec interval' -f $mockClusterName, ($mockRetryCount-1), $mockRetryIntervalSec)
$mockCorrectErrorRecord = Get-InvalidOperationRecord -Message ($script:localizedData.ClusterAbsentAfterTimeOut -f $mockClusterName, ($mockRetryCount-1), $mockRetryIntervalSec)
{ Set-TargetResource @mockDefaultParameters } | Should Throw $mockCorrectErrorRecord
}
}

Expand All @@ -114,7 +115,8 @@ try
$mockDynamicDomainName = $mockDomainName

It 'Should throw the correct error message' {
{ Set-TargetResource @mockDefaultParameters } | Should Throw ('Failover cluster {0} not found after {1} attempts with {2} sec interval' -f $mockClusterName, $mockRetryCount, $mockRetryIntervalSec)
$mockCorrectErrorRecord = Get-InvalidOperationRecord -Message ($script:localizedData.ClusterAbsentAfterTimeOut -f $mockClusterName, $mockRetryCount, $mockRetryIntervalSec)
{ Set-TargetResource @mockDefaultParameters } | Should Throw $mockCorrectErrorRecord
}

Assert-VerifiableMocks
Expand Down

0 comments on commit 513aa43

Please sign in to comment.