Skip to content

Commit

Permalink
Changes to SqlServerEndpointState
Browse files Browse the repository at this point in the history
- Added en-US localization (issue dsccommunity#613).
  • Loading branch information
johlju committed Apr 25, 2019
1 parent 857cefb commit 0fff196
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
- Added en-US localization ([issue #625](https://github.com/PowerShell/SqlServerDsc/issues/625)).
- Changes to SqlServerPermission
- Added en-US localization ([issue #619](https://github.com/PowerShell/SqlServerDsc/issues/619)).
- Changes to SqlServerEndpointState
- Added en-US localization ([issue #613](https://github.com/PowerShell/SqlServerDsc/issues/613)).

## 12.4.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath '
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1')

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

<#
.SYNOPSIS
Returns the current state of an endpoint.
Expand Down Expand Up @@ -39,7 +41,9 @@ function Get-TargetResource
$Name
)

New-VerboseMessage -Message "Getting state of endpoint $Name"
Write-Verbose -Message (
$script:localizedData.GetEndpointState -f $Name, $InstanceName
)

try
{
Expand All @@ -49,15 +53,21 @@ function Get-TargetResource
if ($null -ne $endpointObject)
{
$currentState = $endpointObject.EndpointState

Write-Verbose -Message (
$script:localizedData.CurrentState -f $currentState
)
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($Name) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $Name
New-ObjectNotFoundException -Message $errorMessage
}
}
catch
{
throw New-TerminatingError -ErrorType EndpointErrorVerifyExist -FormatArgs @($Name) -ErrorCategory ObjectNotFound -InnerException $_.Exception
$errorMessage = $script:localizedData.EndpointErrorVerifyExist -f $Name
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}

return @{
Expand Down Expand Up @@ -107,6 +117,10 @@ function Set-TargetResource
$State = 'Started'
)

Write-Verbose -Message (
$script:localizedData.SetEndpointState -f $Name, $InstanceName
)

$parameters = @{
InstanceName = [System.String] $InstanceName
ServerName = [System.String] $ServerName
Expand All @@ -118,7 +132,9 @@ function Set-TargetResource
{
if ($getTargetResourceResult.State -ne $State)
{
New-VerboseMessage -Message ('Changing state of endpoint ''{0}''' -f $Name)
Write-Verbose -Message (
$script:localizedData.ChangeState -f $State
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

Expand All @@ -129,16 +145,19 @@ function Set-TargetResource
State = $State
}

Set-SqlHADREndpoint @setEndpointParams -ErrorAction Stop | Out-Null
Set-SqlHADREndpoint @setEndpointParams -ErrorAction 'Stop' | Out-Null
}
else
{
New-VerboseMessage -Message ('Endpoint ''{0}'' state is already correct.' -f $Name)
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name, $State
)
}
}
else
{
throw New-TerminatingError -ErrorType UnexpectedErrorFromGet -ErrorCategory InvalidResult
$errorMessage = $script:localizedData.UnexpectedErrorFromGet
New-InvalidResultException -Message $errorMessage
}
}

Expand Down Expand Up @@ -182,27 +201,40 @@ function Test-TargetResource
$State = 'Started'
)

Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name, $InstanceName
)

$parameters = @{
InstanceName = $InstanceName
ServerName = $ServerName
Name = $Name
}

New-VerboseMessage -Message "Testing state $State on endpoint '$Name'"

$getTargetResourceResult = Get-TargetResource @parameters
if ($null -ne $getTargetResourceResult)
{
$result = $false

if ($getTargetResourceResult.State -eq $State)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name, $getTargetResourceResult.State
)

$result = $true
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState -f $Name, $getTargetResourceResult.State, $State
)

$result = $false
}
}
else
{
throw New-TerminatingError -ErrorType UnexpectedErrorFromGet -ErrorCategory InvalidResult
$errorMessage = $script:localizedData.UnexpectedErrorFromGet
New-InvalidResultException -Message $errorMessage
}

return $result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ConvertFrom-StringData @'
GetEndpointState = Getting state of the endpoint with the name '{0}' for the instance '{1}'.
EndpointNotFound = The endpoint with the name '{0}' does not exist.
EndpointErrorVerifyExist = Unexpected result when trying to verify existence of the endpoint with the name '{0}'.
UnexpectedErrorFromGet = Got unexpected result from Get-TargetResource. No change is made.
CurrentState = The current state of the endpoint is '{0}'.
SetEndpointState = Changing the state of the endpoint with the name '{0}' for the instance '{1}'.
ChangeState = Changing the state of endpoint to '{0}'.
InDesiredState = The endpoint '{0}' is the desired state, the state is '{1}'.
NotInDesiredState = The endpoint '{0}' has the state '{1}', but expected the state to be '{2}'.
TestingConfiguration = Determines the state of the endpoint with the name '{0}' for the instance '{1}'.
'@
6 changes: 3 additions & 3 deletions Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ try

Context 'When endpoint is missing' {
It 'Should throw the correct error message' {
{ Get-TargetResource @testParameters } | Should -Throw 'Unexpected result when trying to verify existence of endpoint ''DefaultEndpointMirror''. InnerException: Endpoint ''DefaultEndpointMirror'' does not exist'
{ Get-TargetResource @testParameters } | Should -Throw ($script:localizedData.EndpointErrorVerifyExist -f $testParameters.Name)

Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
}
Expand Down Expand Up @@ -267,7 +267,7 @@ try
return $null
} -Verifiable

{ Test-TargetResource @testParameters } | Should -Throw 'Got unexpected result from Get-TargetResource. No change is made.'
{ Test-TargetResource @testParameters } | Should -Throw $script:localizedData.UnexpectedErrorFromGet

Assert-MockCalled Connect-SQL -Exactly -Times 0 -Scope It
}
Expand Down Expand Up @@ -342,7 +342,7 @@ try
return $null
} -Verifiable

{ Set-TargetResource @testParameters } | Should -Throw 'Got unexpected result from Get-TargetResource. No change is made.'
{ Set-TargetResource @testParameters } | Should -Throw $script:localizedData.UnexpectedErrorFromGet

Assert-MockCalled Connect-SQL -Exactly -Times 0 -Scope It
}
Expand Down

0 comments on commit 0fff196

Please sign in to comment.