Skip to content

Commit

Permalink
SqlWindowsFirewall: Added localization (#1336)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
johlju authored Apr 26, 2019
1 parent a15089f commit 874c8aa
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 23 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion DSCResources/MSFT_SqlRS/MSFT_SqlRS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
3 changes: 1 addition & 2 deletions DSCResources/MSFT_SqlRS/en-US/MSFT_SqlRS.strings.psd1
Original file line number Diff line number Diff line change
@@ -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.
'@
Original file line number Diff line number Diff line change
@@ -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}'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
78 changes: 66 additions & 12 deletions DSCResources/MSFT_SqlWindowsFirewall/MSFT_SqlWindowsFirewall.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_SqlWindowsFirewall'

<#
.SYNOPSIS
Returns the current state of the firewall rules.
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}

<#
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/MSFT_SqlRS.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 874c8aa

Please sign in to comment.