Skip to content

Commit

Permalink
Changes to SqlServerEndpoint
Browse files Browse the repository at this point in the history
- Added en-US localization (issue dsccommunity#611).
  • Loading branch information
johlju committed Apr 26, 2019
1 parent 54c7797 commit 2b2ce7f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
- Added unit tests to improved code coverage.
- Changes to SqlWindowsFirewall
- Added en-US localization ([issue #614](https://github.com/PowerShell/SqlServerDsc/issues/614)).
- Changes to SqlServerEndpoint
- Added en-US localization ([issue #611](https://github.com/PowerShell/SqlServerDsc/issues/611)).
- Changes to SqlServerEndpointState
- Added en-US localization ([issue #613](https://github.com/PowerShell/SqlServerDsc/issues/613)).

Expand Down
75 changes: 59 additions & 16 deletions DSCResources/MSFT_SqlServerEndpoint/MSFT_SqlServerEndpoint.psm1
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_SqlServerEndpoint'

<#
.SYNOPSIS
Returns the current state of the endpoint.
Expand Down Expand Up @@ -39,6 +41,10 @@ function Get-TargetResource
$InstanceName
)

Write-Verbose -Message (
$script:localizedData.GetEndpoint -f $EndpointName, $InstanceName
)

$getTargetResourceReturnValues = @{
ServerName = $ServerName
InstanceName = $InstanceName
Expand All @@ -52,16 +58,17 @@ function Get-TargetResource
$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message ('Connected to {0}\{1}' -f $ServerName, $InstanceName)
Write-Verbose -Message (
$script:localizedData.ConnectedToInstance -f $ServerName, $InstanceName
)

$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject.Name -eq $EndpointName)
{
if ($sqlServerObject.Endpoints[$EndPointName].EndpointType -ne 'DatabaseMirroring')
{
throw New-TerminatingError -ErrorType EndpointFoundButWrongType `
-FormatArgs @($EndpointName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.EndpointFoundButWrongType -f $EndpointName
New-InvalidOperationException -Message $errorMessage
}

$getTargetResourceReturnValues.Ensure = 'Present'
Expand All @@ -81,9 +88,8 @@ function Get-TargetResource
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.NotConnectedToInstance -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage
}

return $getTargetResourceReturnValues
Expand Down Expand Up @@ -156,7 +162,9 @@ function Set-TargetResource
{
if ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Absent')
{
Write-Verbose -Message ('Creating endpoint {0}.' -f $EndpointName)
Write-Verbose -Message (
$script:localizedData.CreateEndpoint -f $EndpointName, $InstanceName
)

$endpointObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Endpoint -ArgumentList $sqlServerObject, $EndpointName
$endpointObject.EndpointType = [Microsoft.SqlServer.Management.Smo.EndpointType]::DatabaseMirroring
Expand All @@ -177,34 +185,48 @@ function Set-TargetResource
}
elseif ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Present')
{
Write-Verbose -Message (
$script:localizedData.SetEndpoint -f $EndpointName, $InstanceName
)

# The endpoint already exist, verifying supported endpoint properties so they are in desired state.
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
if ($endpointObject.Protocol.Tcp.ListenerIPAddress -ne $IpAddress)
{
Write-Verbose -Message ('Updating endpoint {0} IP address to {1}.' -f $EndpointName, $IpAddress)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointIPAddress -f $IpAddress
)

$endpointObject.Protocol.Tcp.ListenerIPAddress = $IpAddress
$endpointObject.Alter()
}

if ($endpointObject.Protocol.Tcp.ListenerPort -ne $Port)
{
Write-Verbose -Message ('Updating endpoint {0} port to {1}.' -f $EndpointName, $Port)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointPort -f $Port
)

$endpointObject.Protocol.Tcp.ListenerPort = $Port
$endpointObject.Alter()
}

if ($endpointObject.Owner -ne $Owner)
{
Write-Verbose -Message ('Updating endpoint {0} Owner to {1}.' -f $EndpointName, $Owner)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointOwner -f $Owner
)

$endpointObject.Owner = $Owner
$endpointObject.Alter()
}
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $EndpointName
New-ObjectNotFoundException -Message $errorMessage
}
}
elseif ($Ensure -eq 'Absent' -and $getTargetResourceResult.Ensure -eq 'Present')
Expand All @@ -214,19 +236,23 @@ function Set-TargetResource
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
Write-Verbose -Message (
$script:localizedData.DropEndpoint -f $EndpointName, $InstanceName
)

$endpointObject.Drop()
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $EndpointName
New-ObjectNotFoundException -Message $errorMessage
}
}
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.NotConnectedToInstance -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage
}
}

Expand Down Expand Up @@ -291,6 +317,10 @@ function Test-TargetResource
$Owner
)

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

$getTargetResourceResult = Get-TargetResource -EndpointName $EndpointName -ServerName $ServerName -InstanceName $InstanceName
if ($getTargetResourceResult.Ensure -eq $Ensure)
{
Expand All @@ -317,6 +347,19 @@ function Test-TargetResource
$result = $false
}

if ($result)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $EndpointName
)
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState -f $EndpointName
)
}

return $result
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ConvertFrom-StringData @'
GetEndpoint = Getting the current values of the endpoint with the name '{0}' for the instance '{1}'.
EndpointFoundButWrongType = Endpoint '{0}' does exist, but it is not of type 'DatabaseMirroring'.
ConnectedToInstance = Connect to the instance '{0}\\{1}'.
NotConnectedToInstance = Was unable to connect to the instance '{0}\\{1}'.
SetEndpoint = Changing the values of the endpoint with the name '{0}' for the instance '{1}'.
CreateEndpoint = Creating the endpoint '{0}' on the instance '{1}'.
UpdatingEndpointIPAddress = Updating the endpoint IP address to '{0}'.
UpdatingEndpointPort = Updating the endpoint port to '{0}'.
UpdatingEndpointOwner = Updating the endpoint owner to '{0}'.
EndpointNotFound = The endpoint with the name '{0}' does not exist.
DropEndpoint = Removing the endpoint '{0}' on the instance '{1}'.
TestingConfiguration = Determines if the endpoint with the name '{0}' for the instance '{1}' is in desired state.
InDesiredState = The endpoint '{0}' is the desired state.
NotInDesiredState = The endpoint '{0}' is not in the desired state.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ ConvertFrom-StringData @'
RemoteConnectionFailed = Remote PowerShell connection to Server '{0}' failed.
TODO = ToDo. Work not implemented at this time.
UnexpectedErrorFromGet = Got unexpected result from Get-TargetResource. No change is made.
NotConnectedToInstance = Was unable to connect to the instance '{0}\\{1}'
AlterAvailabilityGroupFailed = Failed to alter the availability group '{0}'.
HadrNotEnabled = HADR is not enabled.
AvailabilityGroupNotFound = Unable to locate the availability group '{0}' on the instance '{1}'.
Expand Down Expand Up @@ -96,7 +95,6 @@ ConvertFrom-StringData @'
# Endpoint
EndpointNotFound = Endpoint '{0}' does not exist
EndpointErrorVerifyExist = Unexpected result when trying to verify existence of endpoint '{0}'.
EndpointFoundButWrongType = Endpoint '{0}' does exist, but it is not of type 'DatabaseMirroring'.
# AlwaysOnService
AlterAlwaysOnServiceFailed = Failed to ensure Always On is {0} on the instance '{1}'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ ConvertFrom-StringData @'
RemoteConnectionFailed = Remote PowerShell connection to Server '{0}' failed.
TODO = ToDo. Work not implemented at this time.
UnexpectedErrorFromGet = Got unexpected result from Get-TargetResource. No change is made.
NotConnectedToInstance = Was unable to connect to the instance '{0}\\{1}'
AlterAvailabilityGroupFailed = Failed to alter the availability group '{0}'.
HadrNotEnabled = HADR is not enabled.
AvailabilityGroupNotFound = Unable to locate the availability group '{0}' on the instance '{1}'.
Expand Down Expand Up @@ -85,7 +84,6 @@ ConvertFrom-StringData @'
# Endpoint
EndpointNotFound = Endpoint '{0}' does not exist
EndpointErrorVerifyExist = Unexpected result when trying to verify existence of endpoint '{0}'.
EndpointFoundButWrongType = Endpoint '{0}' does exist, but it is not of type 'DatabaseMirroring'.
# Configuration
ConfigurationOptionNotFound = Specified option '{0}' could not be found.
Expand Down
10 changes: 5 additions & 5 deletions Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ try

Context 'When endpoint exist but with wrong endpoint type' {
It 'Should throw the correct error' {
{ Get-TargetResource @testParameters } | Should -Throw 'Endpoint ''DefaultEndpointMirror'' does exist, but it is not of type ''DatabaseMirroring''.'
{ Get-TargetResource @testParameters } | Should -Throw ($script:localizedData.EndpointFoundButWrongType -f $testParameters.EndpointName)
}
}

Expand All @@ -241,7 +241,7 @@ try
return $null
}

{ Get-TargetResource @testParameters } | Should -Throw 'Was unable to connect to the instance ''localhost\INSTANCE1'''
{ Get-TargetResource @testParameters } | Should -Throw ($script:localizedData.NotConnectedToInstance -f $testParameters.ServerName, $testParameters.InstanceName)
}
}

Expand Down Expand Up @@ -583,7 +583,7 @@ try
}
} -Verifiable

{ Set-TargetResource @testParameters } | Should -Throw 'Endpoint ''DefaultEndpointMirror'' does not exist'
{ Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.EndpointNotFound -f $testParameters.EndpointName)
}
}

Expand All @@ -600,7 +600,7 @@ try

$testParameters.Add('Ensure', 'Absent')

{ Set-TargetResource @testParameters } | Should -Throw 'Endpoint ''DefaultEndpointMirror'' does not exist'
{ Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.EndpointNotFound -f $testParameters.EndpointName)
}
}

Expand All @@ -611,7 +611,7 @@ try
return $null
}

{ Set-TargetResource @testParameters } | Should -Throw 'Was unable to connect to the instance ''localhost\INSTANCE1'''
{ Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.NotConnectedToInstance -f $testParameters.ServerName, $testParameters.InstanceName)
}
}
}
Expand Down

0 comments on commit 2b2ce7f

Please sign in to comment.