From 2fd584864333c52b2b56708330b92642b685e8c4 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Wed, 24 Apr 2019 11:21:29 +0200 Subject: [PATCH] Changes to SqlServerMaxDop - Added en-US localization (issue #616). --- CHANGELOG.md | 2 + .../MSFT_SqlServerMaxDop.psm1 | 94 +++++++++++-------- .../en-US/MSFT_SqlServerMaxDop.strings.psd1 | 12 +++ .../en-US/DscResource.Common.strings.psd1 | 4 - .../sv-SE/DscResource.Common.strings.psd1 | 4 - Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 | 12 +-- 6 files changed, 75 insertions(+), 53 deletions(-) create mode 100644 DSCResources/MSFT_SqlServerMaxDop/en-US/MSFT_SqlServerMaxDop.strings.psd1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1a20b40..15b0d3cef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ ([issue #1329](https://github.com/PowerShell/SqlServerDsc/issues/1329)). - Refactored unit tests to simplify them add add slightly more code coverage. +- Changes to SqlServerMaxDop + - Added en-US localization ([issue #616](https://github.com/PowerShell/SqlServerDsc/issues/616)). ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlServerMaxDop/MSFT_SqlServerMaxDop.psm1 b/DSCResources/MSFT_SqlServerMaxDop/MSFT_SqlServerMaxDop.psm1 index e7f84fcc9..cad9eed78 100644 --- a/DSCResources/MSFT_SqlServerMaxDop/MSFT_SqlServerMaxDop.psm1 +++ b/DSCResources/MSFT_SqlServerMaxDop/MSFT_SqlServerMaxDop.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_SqlServerMaxDop' + <# .SYNOPSIS This function gets the max degree of parallelism server configuration option. @@ -34,14 +36,16 @@ function Get-TargetResource $ServerName = $env:COMPUTERNAME ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - - # Is this node actively hosting the SQL instance? - $isActiveNode = Test-ActiveNode -ServerObject $sqlServerObject + Write-Verbose -Message ( + $script:localizedData.GetConfiguration -f $InstanceName + ) + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName if ($sqlServerObject) { - Write-Verbose -Message 'Getting the max degree of parallelism server configuration option' + # Is this node actively hosting the SQL instance? + $isActiveNode = Test-ActiveNode -ServerObject $sqlServerObject + $currentMaxDop = $sqlServerObject.Configuration.MaxDegreeOfParallelism.ConfigValue } @@ -115,10 +119,12 @@ function Set-TargetResource ) $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - if ($sqlServerObject) { - Write-Verbose -Message 'Setting the max degree of parallelism server configuration option' + Write-Verbose -Message ( + $script:localizedData.SetConfiguration -f $InstanceName + ) + switch ($Ensure) { 'Present' @@ -127,13 +133,15 @@ function Set-TargetResource { if ($MaxDop) { - throw New-TerminatingError -ErrorType MaxDopParamMustBeNull ` - -FormatArgs @( $ServerName, $InstanceName ) ` - -ErrorCategory InvalidArgument + $errorMessage = $script:localizedData.MaxDopParamMustBeNull + New-InvalidArgumentException -ArgumentName 'MaxDop' -Message $errorMessage } $targetMaxDop = Get-SqlDscDynamicMaxDop -SqlServerObject $sqlServerObject - New-VerboseMessage -Message "Dynamic MaxDop is $targetMaxDop." + + Write-Verbose -Message ( + $script:localizedData.DynamicMaxDop -f $targetMaxDop + ) } else { @@ -144,7 +152,10 @@ function Set-TargetResource 'Absent' { $targetMaxDop = 0 - New-VerboseMessage -Message 'Desired state should be absent - MAXDOP is reset to the default value.' + + Write-Verbose -Message ( + $script:localizedData.SettingDefaultValue -f $targetMaxDop + ) } } @@ -152,14 +163,15 @@ function Set-TargetResource { $sqlServerObject.Configuration.MaxDegreeOfParallelism.ConfigValue = $targetMaxDop $sqlServerObject.Alter() - New-VerboseMessage -Message "Setting MAXDOP value to $targetMaxDop." + + Write-Verbose -Message ( + $script:localizedData.ChangeValue -f $targetMaxDop + ) } catch { - throw New-TerminatingError -ErrorType MaxDopSetError ` - -FormatArgs @($ServerName, $InstanceName, $targetMaxDop) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.MaxDopSetError + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -223,7 +235,9 @@ function Test-TargetResource $ProcessOnlyOnActiveNode ) - Write-Verbose -Message 'Testing the max degree of parallelism server configuration option' + Write-Verbose -Message ( + $script:localizedData.EvaluationConfiguration -f $targetMaxDop + ) $parameters = @{ InstanceName = $InstanceName @@ -241,7 +255,10 @@ function Test-TargetResource #> if ( $ProcessOnlyOnActiveNode -and -not $getTargetResourceResult.IsActiveNode ) { - New-VerboseMessage -Message ( 'The node "{0}" is not actively hosting the instance "{1}". Exiting the test.' -f $env:COMPUTERNAME, $InstanceName ) + Write-Verbose -Message ( + $script:localizedData.NotActiveNode -f $env:COMPUTERNAME, $InstanceName + ) + return $isMaxDopInDesiredState } @@ -249,39 +266,42 @@ function Test-TargetResource { 'Absent' { - if ($getMaxDop -ne 0) + $defaultMaxDopValue = 0 + + if ($getMaxDop -ne $defaultMaxDopValue) { - New-VerboseMessage -Message "Current MaxDop is $getMaxDop should be updated to 0" + Write-Verbose -Message ( + $script:localizedData.WrongMaxDop -f $getMaxDop, $defaultMaxDopValue + ) + $isMaxDopInDesiredState = $false } } + 'Present' { if ($DynamicAlloc) { if ($MaxDop) { - throw New-TerminatingError -ErrorType MaxDopParamMustBeNull ` - -FormatArgs @( $ServerName, $InstanceName ) ` - -ErrorCategory InvalidArgument + $errorMessage = $script:localizedData.MaxDopParamMustBeNull + New-InvalidArgumentException -ArgumentName 'MaxDop' -Message $errorMessage } - $dynamicMaxDop = Get-SqlDscDynamicMaxDop - New-VerboseMessage -Message "Dynamic MaxDop is $dynamicMaxDop." + $MaxDop = Get-SqlDscDynamicMaxDop - if ($getMaxDop -ne $dynamicMaxDop) - { - New-VerboseMessage -Message "Current MaxDop is $getMaxDop should be updated to $dynamicMaxDop" - $isMaxDopInDesiredState = $false - } + Write-Verbose -Message ( + $script:localizedData.DynamicMaxDop -f $MaxDop + ) } - else + + if ($getMaxDop -ne $MaxDop) { - if ($getMaxDop -ne $MaxDop) - { - New-VerboseMessage -Message "Current MaxDop is $getMaxDop should be updated to $MaxDop" - $isMaxDopInDesiredState = $false - } + Write-Verbose -Message ( + $script:localizedData.WrongMaxDop -f $getMaxDop, $MaxDop + ) + + $isMaxDopInDesiredState = $false } } } diff --git a/DSCResources/MSFT_SqlServerMaxDop/en-US/MSFT_SqlServerMaxDop.strings.psd1 b/DSCResources/MSFT_SqlServerMaxDop/en-US/MSFT_SqlServerMaxDop.strings.psd1 new file mode 100644 index 000000000..b1c096d17 --- /dev/null +++ b/DSCResources/MSFT_SqlServerMaxDop/en-US/MSFT_SqlServerMaxDop.strings.psd1 @@ -0,0 +1,12 @@ +ConvertFrom-StringData @' + GetConfiguration = Getting the max degree of parallelism server configuration option for instance '{0}'. + SetConfiguration = Setting the max degree of parallelism server configuration option for instance '{0}'. + DynamicMaxDop = The dynamically calculated value for max degree of parallelism is '{0}'. + MaxDopParamMustBeNull = The parameter max degree of parallelism must be set to $null or not assigned if the parameter DynamicAlloc is set to $true. + MaxDopSetError = Unexpected result when trying to configure the max degree of parallelism server configuration option. + SettingDefaultValue = Desired state should be absent, so max degree of parallelism will be reset to the default value '{0}'. + ChangeValue = Changed the value for max degree of parallelism to '{0}'. + EvaluationConfiguration = Determines the current value for the max degree of parallelism server configuration option. + NotActiveNode = The node '{0}' is not actively hosting the instance '{1}'. Will always return success for this resource on this node, until this node is actively hosting the instance. + WrongMaxDop = The current value for max degree of parallelism is '{0}', but expected '{1}'. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index 8dc996df7..4e304ff8c 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -128,10 +128,6 @@ ConvertFrom-StringData @' RemoveAvailabilityGroupReplicaFailed = Failed to remove the availability group replica '{0}'. ReplicaNotFound = Unable to find the availability group replica '{0}' on the instance '{1}'. - # Max degree of parallelism - MaxDopSetError = Unexpected result when trying to configure the max degree of parallelism server configuration option. - MaxDopParamMustBeNull = MaxDop parameter must be set to $null or not assigned if DynamicAlloc parameter is set to $true. - # SQLServerDatabase CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 24ddc6293..4c6a7cd5f 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -122,10 +122,6 @@ ConvertFrom-StringData @' RemoveAvailabilityGroupReplicaFailed = Failed to remove the availability group replica '{0}'. ReplicaNotFound = Unable to find the availability group replica '{0}' on the instance '{1}'. - # Max degree of parallelism - MaxDopSetError = Unexpected result when trying to configure the max degree of parallelism server configuration option. - MaxDopParamMustBeNull = MaxDop parameter must be set to $null or not assigned if DynamicAlloc parameter is set to $true. - # SQLServerDatabase CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. diff --git a/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 b/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 index bdc09f063..fa416db03 100644 --- a/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 @@ -372,7 +372,7 @@ try } It 'Should throw the correct error' { - { Test-TargetResource @testParameters } | Should -Throw 'MaxDop parameter must be set to $null or not assigned if DynamicAlloc parameter is set to $true.' + { Test-TargetResource @testParameters } | Should -Throw $script:localizedData.MaxDopParamMustBeNull } It 'Should call the mock function Connect-SQL' { @@ -418,7 +418,7 @@ try } It 'Should throw the correct error' { - { Set-TargetResource @testParameters } | Should -Throw 'MaxDop parameter must be set to $null or not assigned if DynamicAlloc parameter is set to $true.' + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.MaxDopParamMustBeNull } It 'Should call the mock function Connect-SQL' { @@ -501,12 +501,8 @@ try Ensure = 'Present' } - It 'Shoud throw the correct error when Alter() method was called with invalid operation' { - $throwInvalidOperation = ('Unexpected result when trying to configure the max degree of parallelism ' + ` - 'server configuration option. InnerException: Exception calling "Alter" ' + ` - 'with "0" argument(s): "Mock Alter Method was called with invalid operation."') - - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + It 'Should throw the correct error when Alter() method was called with invalid operation' { + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.MaxDopSetError } It 'Should call the mock function Connect-SQL' {