-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SqlServerDsc: Add public command
Get-SqlDscConfigurationOption
(#1935)
- SqlServerDsc - Added new public command: - `Get-SqlDscConfigurationOption` - Returns the available configuration options that can be used with the DSC resource _SqlConfiguration_.
- Loading branch information
Showing
8 changed files
with
348 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<# | ||
.SYNOPSIS | ||
Get server configuration option. | ||
.DESCRIPTION | ||
This command gets the available configuration options from a SQL Server Database Engine instance. | ||
.PARAMETER ServerObject | ||
Specifies current server connection object. | ||
.PARAMETER Name | ||
Specifies the name of the configuration option to get. | ||
.PARAMETER Refresh | ||
Specifies that the **ServerObject**'s configuration property should be | ||
refreshed before trying get the available configuration options. This is | ||
helpful when run values or configuration values have been modified outside | ||
of the specified **ServerObject**. | ||
.EXAMPLE | ||
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
$sqlServerObject | Get-SqlDscConfigurationOption | ||
Get all the available configuration options. | ||
.EXAMPLE | ||
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
$sqlServerObject | Get-SqlDscConfigurationOption -Name '*threshold*' | ||
Get the configuration options that contains the word **threshold**. | ||
.OUTPUTS | ||
`[Microsoft.SqlServer.Management.Smo.ConfigProperty[]]` | ||
#> | ||
function Get-SqlDscConfigurationOption | ||
{ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Because the rule does not understands that the command returns [System.String[]] when using , (comma) in the return statement')] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] | ||
[OutputType([Microsoft.SqlServer.Management.Smo.ConfigProperty[]])] | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[Microsoft.SqlServer.Management.Smo.Server] | ||
$ServerObject, | ||
|
||
[Parameter()] | ||
[System.String] | ||
$Name, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$Refresh | ||
) | ||
|
||
process | ||
{ | ||
if ($Refresh.IsPresent) | ||
{ | ||
# Make sure the configuration option values are up-to-date. | ||
$serverObject.Configuration.Refresh() | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey('Name')) | ||
{ | ||
$configurationOption = $serverObject.Configuration.Properties | | ||
Where-Object -FilterScript { | ||
$_.DisplayName -like $Name | ||
} | ||
|
||
if (-not $configurationOption) | ||
{ | ||
$missingConfigurationOptionMessage = $script:localizedData.ConfigurationOption_Get_Missing -f $Name | ||
|
||
$writeErrorParameters = @{ | ||
Message = $missingConfigurationOptionMessage | ||
Category = 'InvalidOperation' | ||
ErrorId = 'GSDCO0001' # cspell: disable-line | ||
TargetObject = $Name | ||
} | ||
|
||
Write-Error @writeErrorParameters | ||
} | ||
} | ||
else | ||
{ | ||
$configurationOption = $serverObject.Configuration.Properties.ForEach({ $_ }) | ||
} | ||
|
||
return , [Microsoft.SqlServer.Management.Smo.ConfigProperty[]] ( | ||
$configurationOption | | ||
Sort-Object -Property 'DisplayName' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
tests/Unit/Public/Get-SqlDscConfigurationOption.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] | ||
param () | ||
|
||
BeforeDiscovery { | ||
try | ||
{ | ||
if (-not (Get-Module -Name 'DscResource.Test')) | ||
{ | ||
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. | ||
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) | ||
{ | ||
# Redirect all streams to $null, except the error stream (stream 2) | ||
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null | ||
} | ||
|
||
# If the dependencies has not been resolved, this will throw an error. | ||
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' | ||
} | ||
} | ||
catch [System.IO.FileNotFoundException] | ||
{ | ||
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' | ||
} | ||
} | ||
|
||
BeforeAll { | ||
$script:dscModuleName = 'SqlServerDsc' | ||
|
||
$env:SqlServerDscCI = $true | ||
|
||
Import-Module -Name $script:dscModuleName | ||
|
||
# Loading mocked classes | ||
Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs') | ||
|
||
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName | ||
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName | ||
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName | ||
} | ||
|
||
AfterAll { | ||
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName') | ||
$PSDefaultParameterValues.Remove('Mock:ModuleName') | ||
$PSDefaultParameterValues.Remove('Should:ModuleName') | ||
|
||
# Unload the module being tested so that it doesn't impact any other tests. | ||
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force | ||
|
||
Remove-Item -Path 'env:SqlServerDscCI' | ||
} | ||
|
||
Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' { | ||
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @( | ||
@{ | ||
MockParameterSetName = '__AllParameterSets' | ||
MockExpectedParameters = '[-ServerObject] <Server> [[-Name] <string>] [-Refresh] [<CommonParameters>]' | ||
} | ||
) { | ||
$result = (Get-Command -Name 'Get-SqlDscConfigurationOption').ParameterSets | | ||
Where-Object -FilterScript { | ||
$_.Name -eq $mockParameterSetName | ||
} | | ||
Select-Object -Property @( | ||
@{ | ||
Name = 'ParameterSetName' | ||
Expression = { $_.Name } | ||
}, | ||
@{ | ||
Name = 'ParameterListAsString' | ||
Expression = { $_.ToString() } | ||
} | ||
) | ||
|
||
$result.ParameterSetName | Should -Be $MockParameterSetName | ||
$result.ParameterListAsString | Should -Be $MockExpectedParameters | ||
} | ||
|
||
Context 'When the specified configuration option exist' { | ||
BeforeAll { | ||
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | | ||
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { | ||
return @{ | ||
Properties = @() | ||
} | ||
} -PassThru -Force | ||
|
||
$mockDefaultParameters = @{ | ||
ServerObject = $mockServerObject | ||
Name = 'Unknown Option Name' | ||
} | ||
} | ||
|
||
Context 'When specifying to throw on error' { | ||
BeforeAll { | ||
$mockErrorMessage = InModuleScope -ScriptBlock { | ||
$script:localizedData.ConfigurationOption_Get_Missing | ||
} | ||
} | ||
|
||
It 'Should throw the correct error' { | ||
{ Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'Stop' } | | ||
Should -Throw -ExpectedMessage ($mockErrorMessage -f 'Unknown Option Name') | ||
} | ||
} | ||
|
||
Context 'When ignoring the error' { | ||
It 'Should not throw an exception and return $null' { | ||
Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'SilentlyContinue' | | ||
Should -BeNullOrEmpty | ||
} | ||
} | ||
} | ||
|
||
Context 'When getting a specific configuration option' { | ||
BeforeAll { | ||
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | | ||
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { | ||
$configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() | ||
$configOption1.DisplayName = 'blocked process threshold (s)' | ||
|
||
return @{ | ||
Properties = @($configOption1) | ||
} | ||
} -PassThru -Force | ||
} | ||
|
||
It 'Should return the correct values' { | ||
$mockDefaultParameters = @{ | ||
ServerObject = $mockServerObject | ||
Name = 'blocked process threshold (s)' | ||
} | ||
|
||
$result = Get-SqlDscConfigurationOption @mockDefaultParameters | ||
|
||
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' | ||
|
||
$result.DisplayName | Should -Be 'blocked process threshold (s)' | ||
} | ||
|
||
Context 'When passing parameter ServerObject over the pipeline' { | ||
It 'Should return the correct values' { | ||
$result = $mockServerObject | Get-SqlDscConfigurationOption -Name 'blocked process threshold (s)' | ||
|
||
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' | ||
|
||
$result.DisplayName | Should -Be 'blocked process threshold (s)' | ||
} | ||
} | ||
} | ||
|
||
Context 'When getting all available configuration options' { | ||
BeforeAll { | ||
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | | ||
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value { | ||
$configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() | ||
$configOption1.DisplayName = 'blocked process threshold (s)' | ||
|
||
$configOption2 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance() | ||
$configOption2.DisplayName = 'show advanced options' | ||
|
||
return @{ | ||
Properties = @($configOption1, $configOption2) | ||
} | ||
} -PassThru -Force | ||
} | ||
|
||
It 'Should return the correct values' { | ||
$result = Get-SqlDscConfigurationOption -ServerObject $mockServerObject | ||
|
||
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' | ||
|
||
$result.DisplayName | Should -Contain 'show advanced options' | ||
$result.DisplayName | Should -Contain 'blocked process threshold (s)' | ||
} | ||
|
||
Context 'When passing parameter ServerObject over the pipeline' { | ||
It 'Should return the correct values' { | ||
$result = $mockServerObject | Get-SqlDscConfigurationOption | ||
|
||
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty' | ||
|
||
$result.DisplayName | Should -Contain 'show advanced options' | ||
$result.DisplayName | Should -Contain 'blocked process threshold (s)' | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.