Skip to content

Commit

Permalink
Changes to SqlAGListener
Browse files Browse the repository at this point in the history
- Added en-US localization (issue dsccommunity#604).
  • Loading branch information
johlju committed Apr 29, 2019
1 parent d2c3a36 commit 15d0b10
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
- Added en-US localization ([issue #607](https://github.com/PowerShell/SqlServerDsc/issues/607)).
- Changes to SqlDatabase
- Added en-US localization ([issue #606](https://github.com/PowerShell/SqlServerDsc/issues/606)).
- Changes to SqlAGListener
- Added en-US localization ([issue #604](https://github.com/PowerShell/SqlServerDsc/issues/604)).

## 12.4.0.0

Expand Down
123 changes: 91 additions & 32 deletions DSCResources/MSFT_SqlAGListener/MSFT_SqlAGListener.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_SqlAGListener'

<#
.SYNOPSIS
Returns the current state of the Availability Group listener.
Expand Down Expand Up @@ -47,13 +49,19 @@ function Get-TargetResource
$AvailabilityGroup
)

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

try
{
$availabilityGroupListener = Get-SQLAlwaysOnAvailabilityGroupListener -Name $Name -AvailabilityGroup $AvailabilityGroup -ServerName $ServerName -InstanceName $InstanceName

if ($null -ne $availabilityGroupListener)
{
New-VerboseMessage -Message "Listener $Name exist."
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerIsPresent -f $Name
)

$ensure = 'Present'
$port = [uint16]( $availabilityGroupListener | Select-Object -ExpandProperty PortNumber )
Expand All @@ -69,7 +77,9 @@ function Get-TargetResource
}
else
{
New-VerboseMessage -Message "Listener $Name does not exist"
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerIsNotPresent -f $Name
)

$ensure = 'Absent'
$port = 0
Expand All @@ -79,7 +89,8 @@ function Get-TargetResource
}
catch
{
throw New-TerminatingError -ErrorType AvailabilityGroupListenerNotFound -FormatArgs @($Name) -ErrorCategory ObjectNotFound -InnerException $_.Exception
$errorMessage = $script:localizedData.AvailabilityGroupListenerNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}

return @{
Expand Down Expand Up @@ -176,7 +187,9 @@ function Set-TargetResource
{
if ($Ensure -eq 'Present')
{
New-VerboseMessage -Message "Create listener on $AvailabilityGroup"
Write-Verbose -Message (
$script:localizedData.CreateAvailabilityGroupListener -f $Name, $AvailabilityGroup, $InstanceName
)

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

Expand All @@ -190,41 +203,53 @@ function Set-TargetResource

if ($Port)
{
New-VerboseMessage -Message "Listener port set to $Port"
Write-Verbose -Message (
$script:localizedData.SetAvailabilityGroupListenerPort -f $Port
)

$newListenerParams += @{
Port = $Port
}
}

if ($DHCP -and $IpAddress.Count -gt 0)
{
New-VerboseMessage -Message "Listener set to DHCP with subnet $IpAddress"
Write-Verbose -Message (
$script:localizedData.SetAvailabilityGroupListenerDhcp -f $IpAddress
)

$newListenerParams += @{
DhcpSubnet = [System.String] $IpAddress
}
}
elseif (-not $DHCP -and $IpAddress.Count -gt 0)
{
New-VerboseMessage -Message "Listener set to static IP-address(es); $($IpAddress -join ', ')"
Write-Verbose -Message (
$script:localizedData.SetAvailabilityGroupListenerStaticIpAddress -f ($IpAddress -join ', ')
)

$newListenerParams += @{
StaticIp = $IpAddress
}
}
else
{
New-VerboseMessage -Message 'Listener using DHCP with server default subnet'
Write-Verbose -Message $script:localizedData.SetAvailabilityGroupListenerDhcpDefaultSubnet
}

New-SqlAvailabilityGroupListener @newListenerParams -ErrorAction Stop | Out-Null
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupNotFound -FormatArgs @($AvailabilityGroup, $InstanceName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}
}
else
{
New-VerboseMessage -Message "Remove listener from $AvailabilityGroup"
Write-Verbose -Message (
$script:localizedData.DropAvailabilityGroupListener -f $Name, $AvailabilityGroup, $InstanceName
)

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

Expand All @@ -238,27 +263,29 @@ function Set-TargetResource
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupListenerNotFound -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupListenerNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupNotFound -FormatArgs @($AvailabilityGroup, $InstanceName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}
}
}
else
{
if ($Ensure -ne '')
{
New-VerboseMessage -Message "State is already $Ensure"
}

if ($availabilityGroupListenerState.Ensure -eq 'Present')
{
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerIsPresent -f $Name
)

if (-not $DHCP -and $availabilityGroupListenerState.IpAddress.Count -lt $IpAddress.Count) # Only able to add a new IP-address, not change existing ones.
{
New-VerboseMessage -Message 'Found at least one new IP-address.'
Write-Verbose -Message $script:localizedData.FoundNewIpAddress

$ipAddressEqual = $false
}
else
Expand All @@ -270,13 +297,15 @@ function Set-TargetResource
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupListenerIPChangeError -FormatArgs @($($IpAddress -join ', '), $($availabilityGroupListenerState.IpAddress -join ', ')) -ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.AvailabilityGroupListenerIPChangeError -f ($IpAddress -join ', '), ($availabilityGroupListenerState.IpAddress -join ', ')
New-InvalidOperationException -Message $errorMessage
}
}

if ($($PSBoundParameters.ContainsKey('DHCP')) -and $availabilityGroupListenerState.DHCP -ne $DHCP)
{
throw New-TerminatingError -ErrorType AvailabilityGroupListenerDHCPChangeError -FormatArgs @( $DHCP, $($availabilityGroupListenerState.DHCP) ) -ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.AvailabilityGroupListenerDHCPChangeError -f $DHCP, $availabilityGroupListenerState.DHCP
New-InvalidOperationException -Message $errorMessage
}

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Expand All @@ -289,11 +318,15 @@ function Set-TargetResource
{
if ($availabilityGroupListenerState.Port -ne $Port -or -not $ipAddressEqual)
{
New-VerboseMessage -Message 'Listener differ in configuration.'
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerNotInDesiredState -f $Name, $AvailabilityGroup, $InstanceName
)

if ($availabilityGroupListenerState.Port -ne $Port)
{
New-VerboseMessage -Message 'Changing port configuration'
Write-Verbose -Message (
$script:localizedData.ChangingAvailabilityGroupListenerPort -f $Port
)

$setListenerParams = @{
InputObject = $availabilityGroupListenerObject
Expand All @@ -305,14 +338,16 @@ function Set-TargetResource

if (-not $ipAddressEqual)
{
New-VerboseMessage -Message 'Adding IP-address(es)'

$newIpAddress = @()

foreach ($currentIpAddress in $IpAddress)
{
if (-not ( $availabilityGroupListenerState.IpAddress -contains $currentIpAddress))
{
Write-Verbose -Message (
$script:localizedData.AddingAvailabilityGroupListenerIpAddress -f $currentIpAddress
)

$newIpAddress += $currentIpAddress
}
}
Expand All @@ -327,24 +362,29 @@ function Set-TargetResource
}
else
{
New-VerboseMessage -Message 'Listener configuration is already correct.'
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerInDesiredState -f $Name, $AvailabilityGroup, $InstanceName
)
}
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupListenerNotFound -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupListenerNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupNotFound -FormatArgs @($AvailabilityGroup, $InstanceName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}
}
}
}
else
{
throw New-TerminatingError -ErrorType UnexpectedErrorFromGet -ErrorCategory InvalidResult
$errorMessage = $script:localizedData.UnexpectedErrorFromGet
New-InvalidResultException -Message $errorMessage
}
}

Expand Down Expand Up @@ -424,7 +464,9 @@ function Test-TargetResource
AvailabilityGroup = [System.String] $AvailabilityGroup
}

New-VerboseMessage -Message "Testing state of listener $Name"
Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name, $AvailabilityGroup, $InstanceName
)

$availabilityGroupListenerState = Get-TargetResource @parameters
if ($null -ne $availabilityGroupListenerState)
Expand Down Expand Up @@ -459,7 +501,21 @@ function Test-TargetResource
}
else
{
throw New-TerminatingError -ErrorType UnexpectedErrorFromGet -ErrorCategory InvalidResult
$errorMessage = $script:localizedData.UnexpectedErrorFromGet
New-InvalidResultException -Message $errorMessage
}

if ($result)
{
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerInDesiredState -f $Name, $AvailabilityGroup, $InstanceName
)
}
else
{
Write-Verbose -Message (
$script:localizedData.AvailabilityGroupListenerNotInDesiredState -f $Name, $AvailabilityGroup, $InstanceName
)
}

return $result
Expand Down Expand Up @@ -488,7 +544,9 @@ function Get-SQLAlwaysOnAvailabilityGroupListener
$ServerName
)

Write-Debug "Connecting to availability group $Name as $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)"
Write-Debug -Message (
$script:localizedData.DebugConnectingAvailabilityGroup -f $Name, [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
)

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

Expand All @@ -499,7 +557,8 @@ function Get-SQLAlwaysOnAvailabilityGroupListener
}
else
{
throw New-TerminatingError -ErrorType AvailabilityGroupNotFound -FormatArgs @($AvailabilityGroup, $InstanceName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.AvailabilityGroupNotFound -f $AvailabilityGroup, $InstanceName
New-ObjectNotFoundException -Message $errorMessage
}

return $availabilityGroupListener
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ConvertFrom-StringData @'
GetAvailabilityGroupListener = Get the current state of the Availability Group listener '{0}' for the Availability Group '{1}' on the instance '{2}'.
AvailabilityGroupListenerIsPresent = The Availability Group listener '{0}' exist.
AvailabilityGroupListenerIsNotPresent = The Availability Group listener '{0}' does not exist.
AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist.
CreateAvailabilityGroupListener = Create Availability Group listener '{0}' for the Availability Group '{1}' on the instance '{2}'.
SetAvailabilityGroupListenerPort = Availability Group listener port is set to '{0}'.
SetAvailabilityGroupListenerDhcp = Availability Group listener is using DHCP with the subnet '{0}'.
SetAvailabilityGroupListenerDhcpDefaultSubnet = Availability Group listener is using DHCP with the server default subnet.
SetAvailabilityGroupListenerStaticIpAddress = Availability Group listener is using static IP address(es) '{0}'.
DropAvailabilityGroupListener = Remove the Availability Group listener '{0}' from the Availability Group '{1}' on the instance '{2}'.
AvailabilityGroupNotFound = Unable to locate the Availability Group '{0}' on the instance '{1}'.
FoundNewIpAddress = Found at least one new IP-address.
AvailabilityGroupListenerIPChangeError = IP-address configuration mismatch. Expecting the IP-address(es) '{0}', but found '{1}'. Resource does not support changing IP-address. Listener needs to be removed and then created again.
AvailabilityGroupListenerDHCPChangeError = DHCP configuration mismatch. Expecting '{0}' found '{1}'. Resource does not support changing between static IP and DHCP. Listener needs to be removed and then created again.
AvailabilityGroupListenerInDesiredState = Availability Group listener '{0}' is in desired state for the Availability Group '{1}' on the instance '{2}'.
AvailabilityGroupListenerNotInDesiredState = Availability Group listener '{0}' is not in desired state for the Availability Group '{1}' on the instance '{2}'.
ChangingAvailabilityGroupListenerPort = Changing Availability Group listener port to '{0}'.
AddingAvailabilityGroupListenerIpAddress = Adding Availability Group listener IP address '{0}'.
UnexpectedErrorFromGet = Got unexpected result from Get-TargetResource. No change is made.
TestingConfiguration = Determines the current state for the Availability Group listener '{0}' for the Availability Group '{1}' on the instance '{2}'..
DebugConnectingAvailabilityGroup = Connecting to Availability Group listener '{0}' as the user '{1}'.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ ConvertFrom-StringData @'
AbsentNotImplemented = Ensure = Absent is not implemented!
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.
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 All @@ -82,10 +81,7 @@ ConvertFrom-StringData @'
FailedLogin = Creating a login of type 'SqlLogin' requires LoginCredential
# AvailabilityGroupListener
AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist.
AvailabilityGroupListenerErrorVerifyExist = Unexpected result when trying to verify existence of listener '{0}'.
AvailabilityGroupListenerIPChangeError = IP-address configuration mismatch. Expecting '{0}' found '{1}'. Resource does not support changing IP-address. Listener needs to be removed and then created again.
AvailabilityGroupListenerDHCPChangeError = IP-address configuration mismatch. Expecting '{0}' found '{1}'. Resource does not support changing between static IP and DHCP. Listener needs to be removed and then created again.
# 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 @@ -56,7 +56,6 @@ ConvertFrom-StringData @'
AbsentNotImplemented = Ensure = Absent is not implemented!
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.
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 All @@ -71,10 +70,7 @@ ConvertFrom-StringData @'
FailedLogin = Creating a login of type 'SqlLogin' requires LoginCredential
# AvailabilityGroupListener
AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist.
AvailabilityGroupListenerErrorVerifyExist = Unexpected result when trying to verify existence of listener '{0}'.
AvailabilityGroupListenerIPChangeError = IP-address configuration mismatch. Expecting '{0}' found '{1}'. Resource does not support changing IP-address. Listener needs to be removed and then created again.
AvailabilityGroupListenerDHCPChangeError = IP-address configuration mismatch. Expecting '{0}' found '{1}'. Resource does not support changing between static IP and DHCP. Listener needs to be removed and then created again.
# Configuration
ConfigurationOptionNotFound = Specified option '{0}' could not be found.
Expand Down
Loading

0 comments on commit 15d0b10

Please sign in to comment.