diff --git a/CHANGELOG.md b/CHANGELOG.md index 461489c9a..e19bbc3a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/DSCResources/MSFT_SqlAGListener/MSFT_SqlAGListener.psm1 b/DSCResources/MSFT_SqlAGListener/MSFT_SqlAGListener.psm1 index 2ca4e66df..5fcbc5617 100644 --- a/DSCResources/MSFT_SqlAGListener/MSFT_SqlAGListener.psm1 +++ b/DSCResources/MSFT_SqlAGListener/MSFT_SqlAGListener.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_SqlAGListener' + <# .SYNOPSIS Returns the current state of the Availability Group listener. @@ -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 ) @@ -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 @@ -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 @{ @@ -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 @@ -190,7 +203,10 @@ function Set-TargetResource if ($Port) { - New-VerboseMessage -Message "Listener port set to $Port" + Write-Verbose -Message ( + $script:localizedData.SetAvailabilityGroupListenerPort -f $Port + ) + $newListenerParams += @{ Port = $Port } @@ -198,33 +214,42 @@ function Set-TargetResource 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 @@ -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 @@ -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 @@ -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 @@ -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 } } @@ -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 } } @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/DSCResources/MSFT_SqlAGListener/en-US/MSFT_SqlAGListener.strings.psd1 b/DSCResources/MSFT_SqlAGListener/en-US/MSFT_SqlAGListener.strings.psd1 new file mode 100644 index 000000000..a6ab5755e --- /dev/null +++ b/DSCResources/MSFT_SqlAGListener/en-US/MSFT_SqlAGListener.strings.psd1 @@ -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}'. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index 2cf36852e..e55e2d117 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -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}'. @@ -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}'. diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 42b5a6f95..bfeb47952 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -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}'. @@ -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. diff --git a/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 b/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 index 8033eae08..160da4867 100644 --- a/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 @@ -236,7 +236,7 @@ try $mockDynamicAvailabilityGroup = $mockUnknownAvailabilityGroup It 'Should throw the correct error' { - { Get-TargetResource @testParameters } | Should -Throw 'Trying to make a change to a listener that does not exist. InnerException: Unable to locate the availability group ''AG01'' on the instance ''MSSQLSERVER''.' + { Get-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupListenerNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } } @@ -521,7 +521,7 @@ try return $null } - { Test-TargetResource @testParameters } | Should -Throw 'Got unexpected result from Get-TargetResource. No change is made.' + { Test-TargetResource @testParameters } | Should -Throw $script:localizedData.UnexpectedErrorFromGet } } @@ -685,7 +685,7 @@ try } } - { Set-TargetResource @testParameters } | Should -Throw 'Unable to locate the availability group ''AG01'' on the instance ''MSSQLSERVER''.' + { Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } It 'Should throw the correct error when availability group is not found and Ensure is set to ''Absent''' { @@ -697,7 +697,7 @@ try } } - { Set-TargetResource @testParameters } | Should -Throw 'Unable to locate the availability group ''AG01'' on the instance ''MSSQLSERVER''.' + { Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } $mockDynamicAvailabilityGroup = $mockKnownAvailabilityGroup @@ -706,7 +706,7 @@ try It 'Should throw the correct error when listener is not found and Ensure is set to ''Absent''' { $testParameters['Ensure'] = 'Absent' - { Set-TargetResource @testParameters } | Should -Throw 'Trying to make a change to a listener that does not exist.' + { Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupListenerNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } It 'Should throw the correct error when listener is not found and Ensure is set to ''Present''' { @@ -726,7 +726,7 @@ try } } - { Set-TargetResource @testParameters } | Should -Throw 'Trying to make a change to a listener that does not exist.' + { Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupListenerNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } $mockDynamicAvailabilityGroup = $mockUnknownAvailabilityGroup @@ -749,7 +749,7 @@ try } } - { Set-TargetResource @testParameters } | Should -Throw 'Unable to locate the availability group ''AG01'' on the instance ''MSSQLSERVER''.' + { Set-TargetResource @testParameters } | Should -Throw ($script:localizedData.AvailabilityGroupNotFound -f $testParameters.AvailabilityGroup, $testParameters.InstanceName) } } @@ -810,7 +810,7 @@ try return $null } - { Set-TargetResource @testParameters } | Should -Throw 'Got unexpected result from Get-TargetResource. No change is made.' + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.UnexpectedErrorFromGet } }