Skip to content

Commit

Permalink
Changes to SqlServerRole
Browse files Browse the repository at this point in the history
- Added localization ([issue dsccommunity#621](dsccommunity#621)).
- Added an outline of integration tests.
  • Loading branch information
johlju committed Jan 5, 2018
1 parent cb085f0 commit e12488a
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 121 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
- Added sysadmin account parameter usage to the examples.
- Changes to SqlServerReplication
- Fix Script Analyzer warning ([issue #263](https://github.com/PowerShell/SqlServerDsc/issues/263)).
- Changes to SqlServerRole
- Added localization ([issue #621](https://github.com/PowerShell/SqlServerDsc/issues/621)).
- Added an outline of integration tests.
- Changes to SqlServiceAccount
- Default services are now properly detected
([issue #930](https://github.com/PowerShell/SqlServerDsc/issues/930)).
Expand Down
150 changes: 98 additions & 52 deletions DSCResources/MSFT_SqlServerRole/MSFT_SqlServerRole.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDscHelper.psm1') `
-Force
Import-Module -Name (Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDscHelper.psm1')

Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlServerRole'

<#
.SYNOPSIS
This function gets the sql server role properties.
Expand Down Expand Up @@ -62,7 +67,11 @@ function Get-TargetResource

if ($sqlServerObject)
{
Write-Verbose -Message "Getting properties of SQL Server role '$ServerRoleName'."
Write-Verbose -Message (
$script:localizedData.GetProperties `
-f $ServerRoleName
)

if ($sqlServerRoleObject = $sqlServerObject.Roles[$ServerRoleName])
{
try
Expand All @@ -71,24 +80,27 @@ function Get-TargetResource
}
catch
{
throw New-TerminatingError -ErrorType EnumMemberNamesServerRoleGetError `
-FormatArgs @($ServerName, $InstanceName, $ServerRoleName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.EnumMemberNamesServerRoleGetError `
-f $ServerName, $InstanceName, $ServerRoleName

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

if ($Members)
{
if ($MembersToInclude -or $MembersToExclude)
{
throw New-TerminatingError -ErrorType MembersToIncludeAndExcludeParamMustBeNull `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MembersToIncludeAndExcludeParamMustBeNull
New-InvalidOperationException -Message $errorMessage
}

if ( $null -ne (Compare-Object -ReferenceObject $membersInRole -DifferenceObject $Members))
{
New-VerboseMessage -Message "The desired members are not present in server role $ServerRoleName"
Write-Verbose -Message (
$script:localizedData.DesiredMemberNotPresent `
-f $ServerRoleName
)

$ensure = 'Absent'
}
}
Expand All @@ -100,7 +112,11 @@ function Get-TargetResource
{
if ( -not ($membersInRole.Contains($memberToInclude)))
{
New-VerboseMessage -Message "The included members are not present in server role $ServerRoleName"
Write-Verbose -Message (
$script:localizedData.MemberNotPresent `
-f $ServerRoleName, $memberToInclude
)

$ensure = 'Absent'
}
}
Expand All @@ -112,7 +128,11 @@ function Get-TargetResource
{
if ($membersInRole.Contains($memberToExclude))
{
New-VerboseMessage -Message "The excluded members are present in server role $ServerRoleName"
Write-Verbose -Message (
$script:localizedData.MemberPresent `
-f $ServerRoleName, $memberToInclude
)

$ensure = 'Absent'
}
}
Expand Down Expand Up @@ -205,7 +225,10 @@ function Set-TargetResource

if ($sqlServerObject)
{
Write-Verbose -Message "Setting properties of SQL Server role '$ServerRoleName'."
Write-Verbose -Message (
$script:localizedData.SetProperties `
-f $ServerRoleName
)

switch ($Ensure)
{
Expand All @@ -216,17 +239,20 @@ function Set-TargetResource
$sqlServerRoleObjectToDrop = $sqlServerObject.Roles[$ServerRoleName]
if ($sqlServerRoleObjectToDrop)
{
Write-Verbose -Message "Trying to drop the SQL Server role '$ServerRoleName'."
Write-Verbose -Message (
$script:localizedData.DropRole `
-f $ServerRoleName
)

$sqlServerRoleObjectToDrop.Drop()
New-VerboseMessage -Message "Dropped the SQL Server role '$ServerRoleName'."
}
}
catch
{
throw New-TerminatingError -ErrorType DropServerRoleSetError `
-FormatArgs @($ServerName, $InstanceName, $ServerRoleName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.DropServerRoleSetError `
-f $ServerName, $InstanceName, $ServerRoleName

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

Expand All @@ -240,27 +266,29 @@ function Set-TargetResource
-ArgumentList $sqlServerObject, $ServerRoleName
if ($sqlServerRoleObjectToCreate)
{
Write-Verbose -Message "Creating the SQL Server role '$ServerRoleName'."
Write-Verbose -Message (
$script:localizedData.CreateRole `
-f $ServerRoleName
)

$sqlServerRoleObjectToCreate.Create()
New-VerboseMessage -Message "Created the SQL Server role '$ServerRoleName'."
}
}
catch
{
throw New-TerminatingError -ErrorType CreateServerRoleSetError `
-FormatArgs @($ServerName, $InstanceName, $ServerRoleName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.CreateServerRoleSetError `
-f $ServerName, $InstanceName, $ServerRoleName

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

if ($Members)
{
if ($MembersToInclude -or $MembersToExclude)
{
throw New-TerminatingError -ErrorType MembersToIncludeAndExcludeParamMustBeNull `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidArgument
$errorMessage = $script:localizedData.MembersToIncludeAndExcludeParamMustBeNull
New-InvalidOperationException -Message $errorMessage
}

$memberNamesInRoleObject = $sqlServerObject.Roles[$ServerRoleName].EnumMemberNames()
Expand Down Expand Up @@ -387,7 +415,10 @@ function Test-TargetResource
$InstanceName
)

Write-Verbose -Message "Testing SQL Server role $ServerRoleName properties."
Write-Verbose -Message (
$script:localizedData.TestProperties `
-f $ServerRoleName
)

$getTargetResourceParameters = @{
InstanceName = $PSBoundParameters.InstanceName
Expand All @@ -407,7 +438,11 @@ function Test-TargetResource
{
if ($getTargetResourceResult.Ensure -ne 'Absent')
{
New-VerboseMessage -Message "Ensure is set to Absent. The existing role $ServerRoleName should be dropped"
Write-Verbose -Message (
$script:localizedData.EnsureIsAbsent `
-f $ServerRoleName
)

$isServerRoleInDesiredState = $false
}
}
Expand All @@ -416,8 +451,11 @@ function Test-TargetResource
{
if ($getTargetResourceResult.Ensure -ne 'Present')
{
New-VerboseMessage -Message ("Ensure is set to Present. The missing role $ServerRoleName " + `
"should be added or members are not correctly configured")
Write-Verbose -Message (
$script:localizedData.EnsureIsPresent `
-f $ServerRoleName
)

$isServerRoleInDesiredState = $false
}
}
Expand Down Expand Up @@ -462,23 +500,27 @@ function Add-SqlDscServerRoleMember

if ( -not ($SqlServerObject.Logins[$LoginName]) )
{
throw New-TerminatingError -ErrorType LoginNotFound `
-FormatArgs @($LoginName, $ServerName, $InstanceName) `
-ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.LoginNotFound `
-f $LoginName, $ServerName, $InstanceName

New-ObjectNotFoundException -Message $errorMessage
}

try
{
Write-Verbose -Message "Adding SQL login $LoginName in role $ServerRoleName"
Write-Verbose -Message (
$script:localizedData.AddMemberToRole `
-f $LoginName, $ServerRoleName
)

$SqlServerObject.Roles[$ServerRoleName].AddMember($LoginName)
New-VerboseMessage -Message "SQL Role $ServerRoleName for $LoginName, successfullly added"
}
catch
{
throw New-TerminatingError -ErrorType AddMemberServerRoleSetError `
-FormatArgs @($ServerName, $InstanceName, $ServerRoleName, $LoginName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.AddMemberServerRoleSetError `
-f $ServerName, $InstanceName, $ServerRoleName, $LoginName

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

Expand Down Expand Up @@ -518,23 +560,27 @@ function Remove-SqlDscServerRoleMember

if ( -not ($SqlServerObject.Logins[$LoginName]) )
{
throw New-TerminatingError -ErrorType LoginNotFound `
-FormatArgs @($LoginName, $ServerName, $InstanceName) `
-ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.LoginNotFound `
-f $LoginName, $ServerName, $InstanceName

New-ObjectNotFoundException -Message $errorMessage
}

try
{
Write-Verbose -Message "Removing SQL login $LoginName from role $ServerRoleName"
Write-Verbose -Message (
$script:localizedData.RemoveMemberFromRole `
-f $LoginName, $ServerRoleName
)

$SqlServerObject.Roles[$ServerRoleName].DropMember($LoginName)
New-VerboseMessage -Message "SQL Role $ServerRoleName for $LoginName, successfullly dropped"
}
catch
{
throw New-TerminatingError -ErrorType DropMemberServerRoleSetError `
-FormatArgs @($ServerName, $InstanceName, $ServerRoleName, $LoginName) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.DropMemberServerRoleSetError `
-f $ServerName, $InstanceName, $ServerRoleName, $LoginName

New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Localized resources for SqlSetup

ConvertFrom-StringData @'
GetProperties = Getting properties of the SQL Server role '{0}'.
SetProperties = Setting properties of the SQL Server role '{0}'.
TestProperties = Testing properties of the SQL Server role '{0}'.
EnumMemberNamesServerRoleGetError = Failed to enumerate members of the server role named '{2}' on '{0}\\{1}'.
MembersToIncludeAndExcludeParamMustBeNull = The parameter MembersToInclude and/or MembersToExclude must not be set, or be set to $null, when parameter Members are used.
DropServerRoleSetError = Failed to drop the server role named '{2}' on '{0}\\{1}'.
CreateServerRoleSetError = Failed to create the server role named '{2}' on '{0}\\{1}'.
AddMemberServerRoleSetError = Failed to add member '{3}' to the server role named '{2}' on '{0}\\{1}'.
DropMemberServerRoleSetError = Failed to drop member '{3}' to the server role named '{2}' on '{0}\\{1}'.
DesiredMembersNotPresent = One or more of the desired members are not present in the server role '{0}'.
MemberNotPresent = The login '{1}' is not a member of the server role '{0}'.
MemberPresent = The login '{1}' should not be a member of the server role '{0}'.
DropRole = Removing the SQL Server role '{0}'.
CreateRole = Creating the SQL Server role '{0}'.
EnsureIsAbsent = Ensure is set to Absent. The existing role '{0}' should be removed.
EnsureIsPresent = Ensure is set to Present. Either the role '{0}' is missing and should be created, or members in the role is not in desired state.
LoginNotFound = Login '{0}' does not exist on SQL server '{1}\\{2}'.
AddMemberToRole = Adding login '{0}' to role '{1}'.
RemoveMemberFromRole = Removing login '{0}' from role '{1}'.
'@
Loading

0 comments on commit e12488a

Please sign in to comment.