Skip to content

Commit

Permalink
Changes to SqlServerMaxDop
Browse files Browse the repository at this point in the history
- Added en-US localization (issue dsccommunity#616).
  • Loading branch information
johlju committed Apr 25, 2019
1 parent c3c4724 commit 2fd5848
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
94 changes: 57 additions & 37 deletions DSCResources/MSFT_SqlServerMaxDop/MSFT_SqlServerMaxDop.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_SqlServerMaxDop'

<#
.SYNOPSIS
This function gets the max degree of parallelism server configuration option.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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'
Expand All @@ -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
{
Expand All @@ -144,22 +152,26 @@ 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
)
}
}

try
{
$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 $_
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -241,47 +255,53 @@ 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
}

switch ($Ensure)
{
'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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}'.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
12 changes: 4 additions & 8 deletions Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit 2fd5848

Please sign in to comment.