From 874c8aa993c32d6c96af0ad8d74598ede8ab8206 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 26 Apr 2019 15:35:11 +0200 Subject: [PATCH] SqlWindowsFirewall: Added localization (#1336) - Changes to SqlSetup - Made the error message more descriptive when the Set-TargetResource function calls the Test-TargetResource function to verify the desired state. - Changes to SqlWindowsFirewall - Added en-US localization (issue #614). - Changes to SqlRS - Fixed one of the error handling to use localization, and made the error message more descriptive when the Set-TargetResource function calls the Test-TargetResource function to verify the desired state. --- CHANGELOG.md | 10 +++ DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1 | 3 +- .../MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1 | 3 +- .../en-US/MSFT_SqlRSSetup.strings.psd1 | 2 - .../en-US/MSFT_SqlSetup.strings.psd1 | 2 +- .../sv-SE/MSFT_SqlSetup.strings.psd1 | 2 +- .../MSFT_SqlWindowsFirewall.psm1 | 78 ++++++++++++++++--- .../MSFT_SqlWindowsFirewall.strings.psd1 | 11 +++ .../en-US/DscResource.Common.strings.psd1 | 1 - .../sv-SE/DscResource.Common.strings.psd1 | 1 - Tests/Unit/MSFT_SqlRS.Tests.ps1 | 2 +- Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 | 2 +- 12 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 DSCResources/MSFT_SqlWindowsFirewall/en-US/MSFT_SqlWindowsFirewall.strings.psd1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 9114424a8..0a94afd89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ - Added additional unit tests for code coverage. - Changes to SqlSetup - Concatenated Robocopy localization strings ([issue #694](https://github.com/PowerShell/SqlServerDsc/issues/694)). + - Made the error message more descriptive when the Set-TargetResource + function calls the Test-TargetResource function to verify the desired + state. - Changes to SqlWaitForAG - Added en-US localization ([issue #625](https://github.com/PowerShell/SqlServerDsc/issues/625)). - Changes to SqlServerPermission @@ -42,6 +45,13 @@ - Changes to SqlServerLogin - Added en-US localization ([issue #615](https://github.com/PowerShell/SqlServerDsc/issues/615)). - 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 SqlRS + - Fixed one of the error handling to use localization, and made the + error message more descriptive when the Set-TargetResource function + calls the Test-TargetResource function to verify the desired + state. *This was done prior to adding full en-US localization.* ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1 b/DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1 index c58e4016f..e895d44e4 100644 --- a/DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1 +++ b/DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1 @@ -703,7 +703,8 @@ function Set-TargetResource if ( -not (Test-TargetResource @PSBoundParameters) ) { - throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult + $errorMessage = $script:localizedData.TestFailedAfterSet + New-InvalidResultException -Message $errorMessage } } diff --git a/DSCResources/MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1 b/DSCResources/MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1 index ba1957932..e657f651e 100644 --- a/DSCResources/MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1 +++ b/DSCResources/MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1 @@ -1,6 +1,5 @@ -# Localized resources for SqlSetup - ConvertFrom-StringData @' Restart = Restarting Reporting Services. SuppressRestart = Suppressing restart of Reporting Services. + TestFailedAfterSet = Test-TargetResource function returned false when Set-TargetResource function verified the desired state. This indicates that the Set-TargetResource did not correctly set set the desired state, or that the function Test-TargetResource does not correctly evaluates the desired state. '@ diff --git a/DSCResources/MSFT_SqlRSSetup/en-US/MSFT_SqlRSSetup.strings.psd1 b/DSCResources/MSFT_SqlRSSetup/en-US/MSFT_SqlRSSetup.strings.psd1 index f29d8ea1b..74a389e48 100644 --- a/DSCResources/MSFT_SqlRSSetup/en-US/MSFT_SqlRSSetup.strings.psd1 +++ b/DSCResources/MSFT_SqlRSSetup/en-US/MSFT_SqlRSSetup.strings.psd1 @@ -1,5 +1,3 @@ -# Localized resources for SqlSetup - ConvertFrom-StringData @' TestingConfiguration = Determines if the Microsoft SQL Server Reporting Service instance is installed. FoundInstance = Found Microsoft SQL Server Reporting Service instance named '{0}'. diff --git a/DSCResources/MSFT_SqlSetup/en-US/MSFT_SqlSetup.strings.psd1 b/DSCResources/MSFT_SqlSetup/en-US/MSFT_SqlSetup.strings.psd1 index 15912a5ba..4fcea76c2 100644 --- a/DSCResources/MSFT_SqlSetup/en-US/MSFT_SqlSetup.strings.psd1 +++ b/DSCResources/MSFT_SqlSetup/en-US/MSFT_SqlSetup.strings.psd1 @@ -55,7 +55,7 @@ ConvertFrom-StringData @' SetupFailed = Please see the 'Summary.txt' log file in the 'Setup Bootstrap\\Log' folder. Reboot = Rebooting target node. SuppressReboot = Suppressing reboot of target node. - TestFailedAfterSet = Test-TargetResource returned false after calling Set-TargetResource. + TestFailedAfterSet = Test-TargetResource function returned false when Set-TargetResource function verified the desired state. This indicates that the Set-TargetResource did not correctly set set the desired state, or that the function Test-TargetResource does not correctly evaluates the desired state. FeaturesFound = Found features already installed: {0} NoFeaturesFound = No features are installed. UnableToFindFeature = Unable to find feature '{0}' among the installed features: '{1}'. diff --git a/DSCResources/MSFT_SqlSetup/sv-SE/MSFT_SqlSetup.strings.psd1 b/DSCResources/MSFT_SqlSetup/sv-SE/MSFT_SqlSetup.strings.psd1 index f2ebc2da5..c688ef106 100644 --- a/DSCResources/MSFT_SqlSetup/sv-SE/MSFT_SqlSetup.strings.psd1 +++ b/DSCResources/MSFT_SqlSetup/sv-SE/MSFT_SqlSetup.strings.psd1 @@ -55,7 +55,7 @@ ConvertFrom-StringData @' SetupFailed = Vänligen titta i loggfilen 'Summary.txt' i sökvägen 'Setup Bootstrap\\Log'. Reboot = Startar om målnod. SuppressReboot = Förhindrar omstart av målnod. - TestFailedAfterSet = Test-TargetResource retunerade falskt efter anropet till Set-TargetResource. + TestFailedAfterSet = Funktionen Test-TargetResource returnerade falskt när funktionen Set-TargetResource verifierade önskad konfiguration. Detta indikerar att funktionen Set-TargetResource inte på ett korrekt sätt kunde sätta önskad konfiguration, eller att funktionen Test-TargetResource inte utvärderar önskad konfiguration på korrekt sätt. FeaturesFound = Funktioner funna: {0} UnableToFindFeature = Kunde inte hitta funktion '{0}' bland som installerade funktionerna: '{1}'. EvaluatingClusterParameters = Klustrad installation, kontrollerar parametrar. diff --git a/DSCResources/MSFT_SqlWindowsFirewall/MSFT_SqlWindowsFirewall.psm1 b/DSCResources/MSFT_SqlWindowsFirewall/MSFT_SqlWindowsFirewall.psm1 index 513fe8e8d..a74de05b0 100644 --- a/DSCResources/MSFT_SqlWindowsFirewall/MSFT_SqlWindowsFirewall.psm1 +++ b/DSCResources/MSFT_SqlWindowsFirewall/MSFT_SqlWindowsFirewall.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_SqlWindowsFirewall' + <# .SYNOPSIS Returns the current state of the firewall rules. @@ -48,13 +50,23 @@ function Get-TargetResource $InstanceName = $InstanceName.ToUpper() + Write-Verbose -Message ( + $script:localizedData.EnumeratingFirewallRules -f $InstanceName + ) + $SourcePath = [Environment]::ExpandEnvironmentVariables($SourcePath) if ($SourceCredential) { + $userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + + Write-Verbose -Message ( + $script:localizedData.ConnectUsingCredential -f $SourcePath, $userName + ) + $newSmbMappingParameters = @{ RemotePath = $SourcePath - UserName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + UserName = $userName Password = $($SourceCredential.GetNetworkCredential().Password) } @@ -63,10 +75,16 @@ function Get-TargetResource $pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe' - New-VerboseMessage -Message "Using path: $pathToSetupExecutable" + Write-Verbose -Message ( + $script:localizedData.UsingPath -f $pathToSetupExecutable + ) $sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable + Write-Verbose -Message ( + $script:localizedData.MajorVersion -f $sqlVersion + ) + if ($SourceCredential) { Remove-SmbMapping -RemotePath $SourcePath -Force @@ -352,24 +370,40 @@ function Set-TargetResource $InstanceName = $InstanceName.ToUpper() + Write-Verbose -Message ( + $script:localizedData.ModifyFirewallRules -f $InstanceName + ) + $SourcePath = [Environment]::ExpandEnvironmentVariables($SourcePath) if ($SourceCredential) { + $userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + + Write-Verbose -Message ( + $script:localizedData.ConnectUsingCredential -f $SourcePath, $userName + ) + $newSmbMappingParameters = @{ RemotePath = $SourcePath - UserName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + UserName = $userName Password = $($SourceCredential.GetNetworkCredential().Password) } $null = New-SmbMapping @newSmbMappingParameters } - $path = Join-Path -Path $SourcePath -ChildPath 'setup.exe' + $pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe' + + Write-Verbose -Message ( + $script:localizedData.UsingPath -f $pathToSetupExecutable + ) - New-VerboseMessage -Message "Using path: $path" + $sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable - $sqlVersion = Get-SqlMajorVersion -Path $path + Write-Verbose -Message ( + $script:localizedData.MajorVersion -f $sqlVersion + ) if ($SourceCredential) { @@ -558,7 +592,8 @@ function Set-TargetResource if (-not (Test-TargetResource -SourcePath $SourcePath -Features $Features -InstanceName $InstanceName)) { - throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult + $errorMessage = $script:localizedData.TestFailedAfterSet + New-InvalidResultException -Message $errorMessage } } @@ -609,9 +644,28 @@ function Test-TargetResource $SourceCredential ) + Write-Verbose -Message ( + $script:localizedData.EvaluatingFirewallRules -f $sqlVersion + ) + $getTargetResourceResult = Get-TargetResource -SourcePath $SourcePath -Features $Features -InstanceName $InstanceName - return ($getTargetResourceResult.Ensure -eq $Ensure) + $isInDesiredState = $getTargetResourceResult.Ensure -eq $Ensure + + if ($isInDesiredState) + { + Write-Verbose -Message ( + $script:localizedData.InDesiredState + ) + } + else + { + Write-Verbose -Message ( + $script:localizedData.NotInDesiredState + ) + } + + return $isInDesiredState } <# @@ -747,13 +801,13 @@ function Test-IsFirewallRuleInDesiredState $isRuleInDesiredState = $false - if ($firewallRule = Get-NetFirewallRule -DisplayName $DisplayName -ErrorAction SilentlyContinue) + if ($firewallRule = Get-NetFirewallRule -DisplayName $DisplayName -ErrorAction 'SilentlyContinue') { if (($firewallRule.Enabled -eq $Enabled) -and ($firewallRule.Profile -eq $Profile) -and ($firewallRule.Direction -eq $Direction)) { if ($PSBoundParameters.ContainsKey('Program')) { - if ($firewallApplicationFilter = Get-NetFirewallApplicationFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction SilentlyContinue) + if ($firewallApplicationFilter = Get-NetFirewallApplicationFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction 'SilentlyContinue') { if ($firewallApplicationFilter.Program -eq $Program) { @@ -764,7 +818,7 @@ function Test-IsFirewallRuleInDesiredState if ($PSBoundParameters.ContainsKey('Service')) { - if ($firewallServiceFilter = Get-NetFirewallServiceFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction SilentlyContinue) + if ($firewallServiceFilter = Get-NetFirewallServiceFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction 'SilentlyContinue') { if ($firewallServiceFilter.Service -eq $Service) { @@ -775,7 +829,7 @@ function Test-IsFirewallRuleInDesiredState if ($PSBoundParameters.ContainsKey('Protocol') -and $PSBoundParameters.ContainsKey('LocalPort')) { - if ($firewallPortFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction SilentlyContinue) + if ($firewallPortFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $firewallRule -ErrorAction 'SilentlyContinue') { if ($firewallPortFilter.Protocol -eq $Protocol -and $firewallPortFilter.LocalPort -eq $LocalPort) { diff --git a/DSCResources/MSFT_SqlWindowsFirewall/en-US/MSFT_SqlWindowsFirewall.strings.psd1 b/DSCResources/MSFT_SqlWindowsFirewall/en-US/MSFT_SqlWindowsFirewall.strings.psd1 new file mode 100644 index 000000000..1a7e6bd3b --- /dev/null +++ b/DSCResources/MSFT_SqlWindowsFirewall/en-US/MSFT_SqlWindowsFirewall.strings.psd1 @@ -0,0 +1,11 @@ +ConvertFrom-StringData @' + EnumeratingFirewallRules = Enumerating firewall rules for instance '{0}'. + ConnectUsingCredential = Connecting to the path '{0}' using the credential '{1}' through SMB. + UsingPath = Using the executable at '{0}' to determine the SQL Server major version. + MajorVersion = The SQL Server major version is '{0}'. + ModifyFirewallRules = Modifying firewall rules for instance '{0}'. + TestFailedAfterSet = Test-TargetResource function returned false when Set-TargetResource function verified the desired state. This indicates that the Set-TargetResource did not correctly set set the desired state, or that the function Test-TargetResource does not correctly evaluates the desired state. + EvaluatingFirewallRules = Determines if the firewall rules are in desired state for the instance '{0}'. + InDesiredState = The firewall rules are in desired state. + NotInDesiredState = The firewall rules are not in desired state. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index c9558d7a5..38e1cfe5a 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -65,7 +65,6 @@ ConvertFrom-StringData @' # Common NoKeyFound = No Localization key found for ErrorType: '{0}'. AbsentNotImplemented = Ensure = Absent is not implemented! - TestFailedAfterSet = Test-TargetResource returned false after calling set. 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. diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 183e4c7b2..3cb3e3ca9 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -54,7 +54,6 @@ ConvertFrom-StringData @' # Common NoKeyFound = No Localization key found for ErrorType: '{0}'. AbsentNotImplemented = Ensure = Absent is not implemented! - TestFailedAfterSet = Test-TargetResource returned false after calling set. 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. diff --git a/Tests/Unit/MSFT_SqlRS.Tests.ps1 b/Tests/Unit/MSFT_SqlRS.Tests.ps1 index 3f9493fdd..60f4a8641 100644 --- a/Tests/Unit/MSFT_SqlRS.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlRS.Tests.ps1 @@ -417,7 +417,7 @@ try } It 'Should throw the correct error message' { - { Set-TargetResource @defaultParameters } | Should -Throw 'Test-TargetResource returned false after calling set.' + { Set-TargetResource @defaultParameters } | Should -Throw $script:localizedData.TestFailedAfterSet } } diff --git a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 index 22f6f3048..0254b0f7d 100644 --- a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 @@ -1047,7 +1047,7 @@ try } It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' { - { Set-TargetResource @testParameters } | Should -Throw TestFailedAfterSet + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet Assert-MockCalled -CommandName New-SmbMapping -Exactly -Times 1 -Scope It Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It