diff --git a/CHANGELOG.md b/CHANGELOG.md index 24009aa239..0aa6a88987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 b/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 index 35df748942..32a509e95d 100644 --- a/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 +++ b/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 @@ -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. @@ -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 { @@ -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 @{ @@ -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 @@ -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 @@ -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 } } @@ -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 diff --git a/DSCResources/MSFT_SqlServerEndpointState/en-US/MSFT_SqlServerEndpointState.strings.psd1 b/DSCResources/MSFT_SqlServerEndpointState/en-US/MSFT_SqlServerEndpointState.strings.psd1 new file mode 100644 index 0000000000..1b3882f5ce --- /dev/null +++ b/DSCResources/MSFT_SqlServerEndpointState/en-US/MSFT_SqlServerEndpointState.strings.psd1 @@ -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}'. +'@ diff --git a/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 b/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 index 7f90c553ef..cb006d4e95 100644 --- a/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 @@ -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 } @@ -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 } @@ -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 }