Skip to content

Commit

Permalink
SqlServerMemory: Localization, unit tests and fix bug (dsccommunity#1330
Browse files Browse the repository at this point in the history
)

- Changes to SqlServerMemory
  - Added en-US localization (issue dsccommunity#617).
  - No longer will the resource set the MinMemory value if it was provided
    in a configuration that also set the `Ensure` parameter to 'Absent' (issue dsccommunity#1329).
  - Refactored unit tests to simplify them add add slightly more code
    coverage.
  • Loading branch information
johlju authored Apr 25, 2019
1 parent 857cefb commit c3c4724
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 355 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
- Added en-US localization ([issue #625](https://github.com/PowerShell/SqlServerDsc/issues/625)).
- Changes to SqlServerPermission
- Added en-US localization ([issue #619](https://github.com/PowerShell/SqlServerDsc/issues/619)).
- Changes to SqlServerMemory
- Added en-US localization ([issue #617](https://github.com/PowerShell/SqlServerDsc/issues/617)).
- No longer will the resource set the MinMemory value if it was provided
in a configuration that also set the `Ensure` parameter to 'Absent'
([issue #1329](https://github.com/PowerShell/SqlServerDsc/issues/1329)).
- Refactored unit tests to simplify them add add slightly more code
coverage.

## 12.4.0.0

Expand Down
126 changes: 80 additions & 46 deletions DSCResources/MSFT_SqlServerMemory/MSFT_SqlServerMemory.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_SqlServerMemory'

<#
.SYNOPSIS
This function gets the value of the min and max memory server configuration option.
Expand Down Expand Up @@ -35,11 +37,13 @@ function Get-TargetResource
$ServerName = $env:COMPUTERNAME
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Write-Verbose -Message (
$script:localizedData.GetMemoryValues -f $InstanceName
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message 'Getting the value for minimum and maximum SQL server memory.'
$minMemory = $sqlServerObject.Configuration.MinServerMemory.ConfigValue
$maxMemory = $sqlServerObject.Configuration.MaxServerMemory.ConfigValue

Expand Down Expand Up @@ -123,11 +127,13 @@ function Set-TargetResource
$ProcessOnlyOnActiveNode
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Write-Verbose -Message (
$script:localizedData.SetNewValues -f $InstanceName
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message 'Setting the minimum and maximum memory used by the instance.'
switch ($Ensure)
{
'Present'
Expand All @@ -136,53 +142,67 @@ function Set-TargetResource
{
if ($MaxMemory)
{
throw New-TerminatingError -ErrorType MaxMemoryParamMustBeNull `
-FormatArgs @( $ServerName, $InstanceName ) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MaxMemoryParamMustBeNull
New-InvalidArgumentException -ArgumentName 'MaxMemory' -Message $errorMessage
}

$MaxMemory = Get-SqlDscDynamicMaxMemory
New-VerboseMessage -Message "Dynamic maximum memory has been calculated to $($MaxMemory)MB."

Write-Verbose -Message (
$script:localizedData.DynamicMemoryValue -f $MaxMemory
)
}
else
{
if (-not $MaxMemory)
{
throw New-TerminatingError -ErrorType MaxMemoryParamMustNotBeNull `
-FormatArgs @( $ServerName, $InstanceName ) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MaxMemoryParamMustNotBeNull
New-InvalidArgumentException -ArgumentName 'MaxMemory' -Message $errorMessage
}
}

$sqlServerObject.Configuration.MaxServerMemory.ConfigValue = $MaxMemory
New-VerboseMessage -Message "Maximum memory used by the instance has been limited to $($MaxMemory)MB."

Write-Verbose -Message (
$script:localizedData.DynamicMaxMemoryValue -f $InstanceName, $MaxMemory
)

if ($MinMemory)
{
$sqlServerObject.Configuration.MinServerMemory.ConfigValue = $MinMemory

Write-Verbose -Message (
$script:localizedData.MinimumMemoryLimited -f $InstanceName, $MinMemory
)
}
}

'Absent'
{
$sqlServerObject.Configuration.MaxServerMemory.ConfigValue = 2147483647
$sqlServerObject.Configuration.MinServerMemory.ConfigValue = 0
New-VerboseMessage -Message ('Ensure is set to absent. Minimum and maximum server memory' + `
'values used by the instance are reset to the default values.')
$defaultMaxMemory = 2147483647
$defaultMinMemory = 0

Write-Verbose -Message (
$script:localizedData.DefaultValues -f $defaultMinMemory, $defaultMaxMemory
)

$sqlServerObject.Configuration.MaxServerMemory.ConfigValue = $defaultMaxMemory
$sqlServerObject.Configuration.MinServerMemory.ConfigValue = $defaultMinMemory

Write-Verbose -Message (
$script:localizedData.ResetDefaultValues -f $InstanceName
)
}
}

try
{
if ($MinMemory)
{
$sqlServerObject.Configuration.MinServerMemory.ConfigValue = $MinMemory
New-VerboseMessage -Message "Minimum memory used by the instance is set to $($MinMemory)MB."
}

$sqlServerObject.Alter()
}
catch
{
throw New-TerminatingError -ErrorType AlterServerMemoryFailed `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.AlterServerMemoryFailed -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}
Expand Down Expand Up @@ -252,7 +272,9 @@ function Test-TargetResource
$ProcessOnlyOnActiveNode
)

Write-Verbose -Message 'Testing the values of the minimum and maximum memory server configuration option set to be used by the instance.'
Write-Verbose -Message (
$script:localizedData.EvaluatingMinAndMaxMemory -f $InstanceName
)

$getTargetResourceParameters = @{
InstanceName = $InstanceName
Expand All @@ -269,10 +291,12 @@ function Test-TargetResource
If this is supposed to process only the active node, and this is not the
active node, don't bother evaluating the test.
#>
if ( $ProcessOnlyOnActiveNode -and -not $getTargetResourceResult.IsActiveNode )
if ($ProcessOnlyOnActiveNode -and -not $getTargetResourceResult.IsActiveNode)
{
# Use localization if the resource has been converted
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 $isServerMemoryInDesiredState
}

Expand All @@ -282,13 +306,19 @@ function Test-TargetResource
{
if ($currentMaxMemory -ne 2147483647)
{
New-VerboseMessage -Message "Current maximum server memory used by the instance is $($currentMaxMemory)MB. Expected 2147483647MB."
Write-Verbose -Message (
$script:localizedData.WrongMaximumMemory -f $currentMaxMemory, '2147483647'
)

$isServerMemoryInDesiredState = $false
}

if ($currentMinMemory -ne 0)
{
New-VerboseMessage -Message "Current minimum server memory used by the instance is $($currentMinMemory)MB. Expected 0MB."
Write-Verbose -Message (
$script:localizedData.WrongMinimumMemory -f $currentMinMemory, '0'
)

$isServerMemoryInDesiredState = $false
}
}
Expand All @@ -299,37 +329,42 @@ function Test-TargetResource
{
if ($MaxMemory)
{
throw New-TerminatingError -ErrorType MaxMemoryParamMustBeNull `
-FormatArgs @( $ServerName, $InstanceName ) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MaxMemoryParamMustBeNull
New-InvalidArgumentException -ArgumentName 'MaxMemory' -Message $errorMessage
}

$MaxMemory = Get-SqlDscDynamicMaxMemory
New-VerboseMessage -Message "Dynamic maximum memory has been calculated to $($MaxMemory)MB."

Write-Verbose -Message (
$script:localizedData.DynamicMaxMemoryValue -f $MaxMemory
)
}
else
{
if (-not $MaxMemory)
{
throw New-TerminatingError -ErrorType MaxMemoryParamMustNotBeNull `
-FormatArgs @( $ServerName, $InstanceName ) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MaxMemoryParamMustNotBeNull
New-InvalidArgumentException -ArgumentName 'MaxMemory' -Message $errorMessage
}
}

if ($MaxMemory -ne $currentMaxMemory)
{
New-VerboseMessage -Message ("Current maximum server memory used by the instance " + `
"is $($currentMaxMemory)MB. Expected $($MaxMemory)MB.")
Write-Verbose -Message (
$script:localizedData.WrongMaximumMemory -f $currentMaxMemory, $MaxMemory
)

$isServerMemoryInDesiredState = $false
}

if ($MinMemory)
{
if ($MinMemory -ne $currentMinMemory)
{
New-VerboseMessage -Message ("Current minimum server memory used by the instance " + `
"is $($currentMinMemory)MB. Expected $($MinMemory)MB.")
Write-Verbose -Message (
$script:localizedData.WrongMinimumMemory -f $currentMinMemory, $MinMemory
)

$isServerMemoryInDesiredState = $false
}
}
Expand Down Expand Up @@ -392,9 +427,8 @@ function Get-SqlDscDynamicMaxMemory
}
catch
{
throw New-TerminatingError -ErrorType ErrorGetDynamicMaxMemory `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.ErrorGetDynamicMaxMemory
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

$maxMemory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ConvertFrom-StringData @'
GetMemoryValues = Getting the current values for minimum and maximum SQL server memory for instance '{0}'.
SetNewValues = Setting the minimum and maximum memory that will be used by the instance '{0}'.
MaxMemoryParamMustBeNull = The parameter MaxMemory must be null when the parameter DynamicAlloc is set to true.
MaxMemoryParamMustNotBeNull = The parameter MaxMemory must not be null when the parameter DynamicAlloc is set to false.
DynamicMaxMemoryValue = Dynamic maximum memory has been calculated to {0}MB.
MaximumMemoryLimited = Maximum memory used by the instance '{0}' has been limited to {1}MB.
MinimumMemoryLimited = Minimum memory used by the instance '{0}' has been set to {1}MB.
DefaultValues = Resetting to the default values; MinMemory = {0}, MaxMemory = {1}.
ResetDefaultValues = Minimum and maximum server memory values used by the instance {0} has been reset to the default values.
AlterServerMemoryFailed = Failed to alter the server configuration memory for {0}\\{1}.
ErrorGetDynamicMaxMemory = Failed to calculate dynamically the maximum memory.
EvaluatingMinAndMaxMemory = Determines the values of the minimum and maximum memory server configuration option for the instance '{0}'.
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.
WrongMaximumMemory = Current maximum server memory used by the instance is {0}MB, but expected {1}MB.
WrongMinimumMemory = Current minimum server memory used by the instance is {0}MB, but expected {1}MB.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ ConvertFrom-StringData @'
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.
# Server Memory
MaxMemoryParamMustBeNull = The parameter MaxMemory must be null when DynamicAlloc is set to true.
MaxMemoryParamMustNotBeNull = The parameter MaxMemory must not be null when DynamicAlloc is set to false.
AlterServerMemoryFailed = Failed to alter the server configuration memory for {0}\\{1}.
ErrorGetDynamicMaxMemory = Failed to calculate dynamically the maximum memory.
# 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 @@ -126,12 +126,6 @@ ConvertFrom-StringData @'
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.
# Server Memory
MaxMemoryParamMustBeNull = The parameter MaxMemory must be null when DynamicAlloc is set to true.
MaxMemoryParamMustNotBeNull = The parameter MaxMemory must not be null when DynamicAlloc is set to false.
AlterServerMemoryFailed = Failed to alter the server configuration memory for {0}\\{1}.
ErrorGetDynamicMaxMemory = Failed to calculate dynamically the maximum memory.
# 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
Loading

0 comments on commit c3c4724

Please sign in to comment.