Skip to content

Commit eefcd14

Browse files
authored
Test-SqlDscIsRole: New command (#2060)
Added a public command that returns whether the server role exists. Implemented new Test-SqlDscIsRole for testing whether a server principal is a Role.
1 parent 3764562 commit eefcd14

File tree

4 files changed

+150
-4
lines changed

4 files changed

+150
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ The format is based on and uses the types of changes according to [Keep a Change
44
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

66
## [Unreleased]
7-
87
### Removed
98

109
- SqlServerDsc.Common
@@ -59,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5958
- Fix style formatting in all PowerShell script files.
6059
- Update module description on GitHub, in the conceptual help, and in
6160
the module manifest.
61+
- Added Test-SqlDscIsRole to be used like Test-SqlDscIsLogin but tests for a server role as principal.
6262
- SqlSetup
6363
- Fixed issue with AddNode where cluster IP information was not being passed to
6464
setup.exe ([issue #1171](https://github.com/dsccommunity/SqlServerDsc/issues/1171)).

source/Public/Test-SqlDscIsLogin.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<#
22
.SYNOPSIS
3-
Returns whether the database principal exist.
3+
Returns whether the server principal exist and is a login.
44
55
.DESCRIPTION
6-
Returns whether the database principal exist.
6+
Returns whether the server principal exist and is a login.
77
88
.PARAMETER ServerObject
99
Specifies current server connection object.
1010
1111
.PARAMETER Name
12-
Specifies the name of the database principal.
12+
Specifies the name of the server principal.
1313
1414
.OUTPUTS
1515
[System.Boolean]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<#
2+
.SYNOPSIS
3+
Returns whether the database principal exists and is a database role.
4+
5+
.DESCRIPTION
6+
Returns whether the database principal exist and is a database role.
7+
8+
.PARAMETER ServerObject
9+
Specifies current server connection object.
10+
11+
.PARAMETER Name
12+
Specifies the name of the database principal.
13+
14+
.OUTPUTS
15+
[System.Boolean]
16+
17+
.EXAMPLE
18+
$serverInstance = Connect-SqlDscDatabaseEngine
19+
Test-SqlDscIsRole -ServerObject $serverInstance -Name 'MyPrincipal'
20+
21+
Returns $true if the principal exist as role, if not $false is returned.
22+
#>
23+
function Test-SqlDscIsRole
24+
{
25+
[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.')]
26+
[CmdletBinding()]
27+
[OutputType([System.Boolean])]
28+
param
29+
(
30+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
31+
[Microsoft.SqlServer.Management.Smo.Server]
32+
$ServerObject,
33+
34+
[Parameter(Mandatory = $true)]
35+
[System.String]
36+
$Name
37+
)
38+
39+
process
40+
{
41+
$principalExist = $false
42+
43+
if ($ServerObject.Roles[$Name])
44+
{
45+
$principalExist = $true
46+
}
47+
48+
return $principalExist
49+
}
50+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies has not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:dscModuleName = 'SqlServerDsc'
28+
29+
$env:SqlServerDscCI = $true
30+
31+
Import-Module -Name $script:dscModuleName
32+
33+
# Loading mocked classes
34+
Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs')
35+
36+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
37+
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
38+
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
39+
}
40+
41+
AfterAll {
42+
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
43+
$PSDefaultParameterValues.Remove('Mock:ModuleName')
44+
$PSDefaultParameterValues.Remove('Should:ModuleName')
45+
46+
# Unload the module being tested so that it doesn't impact any other tests.
47+
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
48+
49+
Remove-Item -Path 'env:SqlServerDscCI'
50+
}
51+
52+
Describe 'Test-SqlDscIsRole' -Tag 'Public' {
53+
Context 'When the instance does not have the specified principal' {
54+
BeforeAll {
55+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
56+
Add-Member -MemberType 'ScriptProperty' -Name 'Roles' -Value {
57+
return @{
58+
'JuniorDBA' = New-Object -TypeName Object |
59+
Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'JuniorDBA' -PassThru -Force
60+
}
61+
} -PassThru -Force
62+
}
63+
64+
It 'Should return $false' {
65+
$result = Test-SqlDscIsRole -ServerObject $mockServerObject -Name 'UnknownUser'
66+
67+
$result | Should -BeFalse
68+
}
69+
}
70+
71+
Context 'When the instance have the specified principal' {
72+
BeforeAll {
73+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
74+
Add-Member -MemberType 'ScriptProperty' -Name 'Roles' -Value {
75+
return @{
76+
'JuniorDBA' = New-Object -TypeName Object |
77+
Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'JuniorDBA' -PassThru -Force
78+
}
79+
} -PassThru -Force
80+
}
81+
82+
It 'Should return $true' {
83+
$result = Test-SqlDscIsRole -ServerObject $mockServerObject -Name 'JuniorDBA'
84+
85+
$result | Should -BeTrue
86+
}
87+
88+
Context 'When passing ServerObject over the pipeline' {
89+
It 'Should return $true' {
90+
$result = $mockServerObject | Test-SqlDscIsRole -Name 'JuniorDBA'
91+
92+
$result | Should -BeTrue
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)