diff --git a/CHANGELOG.md b/CHANGELOG.md index c9776520c..083fb2a57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,8 @@ - Added en-US localization ([issue #612](https://github.com/PowerShell/SqlServerDsc/issues/612)). - Changes to SqlServerEndpointState - Added en-US localization ([issue #613](https://github.com/PowerShell/SqlServerDsc/issues/613)). +- Changes to SqlDatabaseRole + - Added en-US localization ([issue #610](https://github.com/PowerShell/SqlServerDsc/issues/610)). ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlDatabaseRole/MSFT_SqlDatabaseRole.psm1 b/DSCResources/MSFT_SqlDatabaseRole/MSFT_SqlDatabaseRole.psm1 index 8ddbb884d..9dabd35cf 100644 --- a/DSCResources/MSFT_SqlDatabaseRole/MSFT_SqlDatabaseRole.psm1 +++ b/DSCResources/MSFT_SqlDatabaseRole/MSFT_SqlDatabaseRole.psm1 @@ -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_SqlDatabaseRole' + <# .SYNOPSIS Returns the current state of the user memberships in the role(s). @@ -61,18 +63,18 @@ function Get-TargetResource $Role ) - Write-Verbose -Message "Getting SQL Database role for $Name" + Write-Verbose -Message ( + $script:localizedData.GetDatabaseRole -f $Name, $Database, $InstanceName + ) $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - if ($sqlServerObject) { # Check database exists if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) ) { - throw New-TerminatingError -ErrorType NoDatabase ` - -FormatArgs @($Database, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound + $errorMessage = $script:localizedData.DatabaseNotFound -f $Database + New-ObjectNotFoundException -Message $errorMessage } # Check role exists @@ -80,18 +82,16 @@ function Get-TargetResource { if ( -not ($sqlDatabaseObject.Roles[$currentRole]) ) { - throw New-TerminatingError -ErrorType RoleNotFound ` - -FormatArgs @($currentRole, $Database, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound + $errorMessage = $script:localizedData.RoleNotFound -f $currentRole, $Database + New-ObjectNotFoundException -Message $errorMessage } } # Check login exists if ( -not ($sqlServerObject.Logins[$Name]) ) { - throw New-TerminatingError -ErrorType LoginNotFound ` - -FormatArgs @($Name, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound + $errorMessage = $script:localizedData.LoginNotFound -f $Name + New-ObjectNotFoundException -Message $errorMessage } $ensure = 'Absent' @@ -103,15 +103,17 @@ function Get-TargetResource { if ($sqlDatabaseUser.IsMember($currentRole)) { - New-VerboseMessage -Message ("The login '$Name' is a member of the role '$currentRole' on the " + ` - "database '$Database', on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + $script:localizedData.IsMember -f $Name, $currentRole, $Database + ) $grantedRole += $currentRole } else { - New-VerboseMessage -Message ("The login '$Name' is not a member of the role '$currentRole' on the " + ` - "database '$Database', on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + $script:localizedData.IsNotMember -f $Name, $currentRole, $Database + ) } } @@ -122,8 +124,9 @@ function Get-TargetResource } else { - New-VerboseMessage -Message ("The login '$Name' is not a user of the database " + ` - "'$Database' on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + $script:localizedData.LoginIsNotUser -f $Name, $Database + ) } } @@ -201,10 +204,7 @@ function Set-TargetResource $Role ) - Write-Verbose -Message "Setting SQL Database role for $Name" - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - if ($sqlServerObject) { $sqlDatabaseObject = $sqlServerObject.Databases[$Database] @@ -218,20 +218,21 @@ function Set-TargetResource { try { - New-VerboseMessage -Message ("Adding the login '$Name' as a user of the database " + ` - "'$Database', on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + '{0} {1}' -f + ($script:localizedData.LoginIsNotUser -f $Name, $Database), + $script:localizedData.AddingLoginAsUser + ) - $sqlDatabaseUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.User ` + $sqlDatabaseUser = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.User' ` -ArgumentList $sqlDatabaseObject, $Name $sqlDatabaseUser.Login = $Name $sqlDatabaseUser.Create() } catch { - throw New-TerminatingError -ErrorType AddLoginDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name, $Database) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToAddUser -f $Name, $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } @@ -240,18 +241,17 @@ function Set-TargetResource { try { - New-VerboseMessage -Message ("Adding the login '$Name' to the role '$currentRole' on the " + ` - "database '$Database', on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + $script:localizedData.AddUserToRole -f $Name, $currentRole, $Database + ) $sqlDatabaseRole = $sqlDatabaseObject.Roles[$currentRole] $sqlDatabaseRole.AddMember($Name) } catch { - throw New-TerminatingError -ErrorType AddMemberDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name, $Role, $Database) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToAddUserToRole -f $Name, $currentRole, $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -262,8 +262,9 @@ function Set-TargetResource { foreach ($currentRole in $Role) { - New-VerboseMessage -Message ("Removing the login '$Name' to the role '$currentRole' on the " + ` - "database '$Database', on the instance $ServerName\$InstanceName") + Write-Verbose -Message ( + $script:localizedData.DropUserFromRole -f $Name, $currentRole, $Database + ) $sqlDatabaseRole = $sqlDatabaseObject.Roles[$currentRole] $sqlDatabaseRole.DropMember($Name) @@ -271,10 +272,8 @@ function Set-TargetResource } catch { - throw New-TerminatingError -ErrorType DropMemberDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name, $Role, $Database) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToDropUserFromRole -f $Name, $currentRole, $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -341,7 +340,9 @@ function Test-TargetResource $Role ) - Write-Verbose -Message "Testing SQL Database role for $Name" + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration -f $Name, $Database, $InstanceName + ) $getTargetResourceParameters = @{ InstanceName = $PSBoundParameters.InstanceName @@ -361,7 +362,10 @@ function Test-TargetResource { if ($getTargetResourceResult.Ensure -ne 'Absent') { - New-VerboseMessage -Message "Ensure is set to Absent. The existing role for $Name should be dropped" + Write-Verbose -Message ( + $script:localizedData.NotInDesiredStateAbsent -f $Name, $Database + ) + $isDatabaseRoleInDesiredState = $false } } @@ -370,13 +374,23 @@ function Test-TargetResource { if ($getTargetResourceResult.Ensure -ne 'Present') { - New-VerboseMessage -Message "Ensure is set to Present. The missing role for $Name should be added" + Write-Verbose -Message ( + $script:localizedData.NotInDesiredStatePresent -f $Name, $Database + ) + $isDatabaseRoleInDesiredState = $false } } } - $isDatabaseRoleInDesiredState + if ($isDatabaseRoleInDesiredState) + { + Write-Verbose -Message ( + $script:localizedData.InDesiredState -f $Name, $Database + ) + } + + return $isDatabaseRoleInDesiredState } Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/MSFT_SqlDatabaseRole/en-US/MSFT_SqlDatabaseRole.strings.psd1 b/DSCResources/MSFT_SqlDatabaseRole/en-US/MSFT_SqlDatabaseRole.strings.psd1 new file mode 100644 index 000000000..b377825a8 --- /dev/null +++ b/DSCResources/MSFT_SqlDatabaseRole/en-US/MSFT_SqlDatabaseRole.strings.psd1 @@ -0,0 +1,19 @@ +ConvertFrom-StringData @' + GetDatabaseRole = Getting current role(s) for the user '{0}' of the database '{1}' on the instance '{2}'. + DatabaseNotFound = The database '{0}' does not exist. + RoleNotFound = The role '{0}' does not exist in the database '{1}'. + LoginNotFound = The login '{0}' does not exist on the instance. + IsMember = The login '{0}' is a member of the role '{1}' in the database '{2}'. + IsNotMember = The login '{0}' is not a member of the role '{1}' in the database '{2}'. + LoginIsNotUser = The login '{0}' is not a user in the database '{1}'. + AddingLoginAsUser = Adding the login as a user of the database. + FailedToAddUser = Failed to add the login '{0}' as a user of the database '{1}'. + AddUserToRole = Adding the user (login) '{0}' to the role '{1}' in the database '{2}'. + FailedToAddUserToRole = Failed to add the user {0} to the role {1} in the database {2}. + DropUserFromRole = Removing the user (login) '{0}' from the role '{1}' in the database '{2}'. + FailedToDropUserFromRole = Failed to remove the login {0} from the role {1} in the database {2}. + TestingConfiguration = Determines if the the user '{0}' of the database '{1}' on the instance '{2}' is a member of the desired role(s). + InDesiredState = The user '{0}' of the database '{1}' is member of the specified role(s). + NotInDesiredStateAbsent = Expected the user '{0}' to not be a member of the specified role(s) in the database '{1}', but the user was member of at least one of the roles. + NotInDesiredStatePresent = Expected the user '{0}' to be a member of the specified role(s) in the database '{1}', but the user was not a member of at least one of the roles. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index bf59ba6ec..e57b309e1 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -83,8 +83,6 @@ ConvertFrom-StringData @' # Database Role AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}. - DropMemberDatabaseSetError = Failed removing the login {2} from the role {3} on the database {4}, on the instance {0}\\{1}. - AddMemberDatabaseSetError = Failed adding the login {2} to the role {3} on the database {4}, on the instance {0}\\{1}. # AvailabilityGroupListener AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist. diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 11c18f93d..93c476da2 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -72,8 +72,6 @@ ConvertFrom-StringData @' # Database Role AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}. - DropMemberDatabaseSetError = Failed removing the login {2} from the role {3} on the database {4}, on the instance {0}\\{1}. - AddMemberDatabaseSetError = Failed adding the login {2} to the role {3} on the database {4}, on the instance {0}\\{1}. # AvailabilityGroupListener AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist. diff --git a/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 index 56c38c34f..4ff6e1c94 100644 --- a/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 @@ -94,7 +94,9 @@ try $mockSqlServerLoginOne = @(( New-Object -TypeName Object | Add-Member -MemberType ScriptMethod -Name IsMember -Value { - param( + param + ( + [Parameter()] [System.String] $mockSqlDatabaseRole ) @@ -122,7 +124,9 @@ try New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseRole -PassThru | Add-Member -MemberType ScriptMethod -Name AddMember -Value { - param( + param + ( + [Parameter()] [System.String] $mockSqlServerLogin ) @@ -137,7 +141,9 @@ try } } -PassThru | Add-Member -MemberType ScriptMethod -Name DropMember -Value { - param( + param + ( + [Parameter()] [System.String] $mockSqlServerLogin ) @@ -156,7 +162,9 @@ try New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseRoleSecond -PassThru | Add-Member -MemberType ScriptMethod -Name AddMember -Value { - param( + param + ( + [Parameter()] [System.String] $mockSqlServerLogin ) @@ -171,7 +179,9 @@ try } } -PassThru | Add-Member -MemberType ScriptMethod -Name DropMember -Value { - param( + param + ( + [Parameter()] [System.String] $mockSqlServerLogin ) @@ -234,7 +244,7 @@ try } #endregion - Describe "MSFT_SqlDatabaseRole\Get-TargetResource" -Tag 'Get' { + Describe 'MSFT_SqlDatabaseRole\Get-TargetResource' -Tag 'Get' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable } @@ -248,10 +258,9 @@ try Role = $mockSqlDatabaseRole } - $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Database - { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Get-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -268,10 +277,9 @@ try Role = 'unknownRoleName' } - $throwInvalidOperation = ("Role 'unknownRoleName' does not exist on database " + ` - "'AdventureWorks' on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.RoleNotFound -f $testParameters.Role, $testParameters.Database - { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Get-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -305,10 +313,10 @@ try Role = $mockSqlDatabaseRole } - $throwInvalidOperation = ("Login 'unknownLoginName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") - { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + $errorMessage = $script:localizedData.LoginNotFound -f $testParameters.Name + + { Get-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -604,11 +612,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ('Failed adding the login John as a user of the database AdventureWorks, on ' + ` - 'the instance localhost\MSSQLSERVER. InnerException: Exception calling "Create" ' + ` - 'with "0" argument(s): "Mock Create Method was called with invalid operation."') + $errorMessage = $script:localizedData.FailedToAddUser -f $testParameters.Name, $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -659,12 +665,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ('Failed adding the login CONTOSO\KingJulian to the role MySecondRole on ' + ` - 'the database AdventureWorks, on the instance localhost\MSSQLSERVER. ' + ` - 'InnerException: Exception calling "AddMember" with "1" argument(s): ' + ` - '"Mock AddMember Method was called with invalid operation."') + $errorMessage = $script:localizedData.FailedToAddUserToRole -f $testParameters.Name, $testParameters.Role, $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -714,12 +717,9 @@ try Ensure = 'Absent' } - $throwInvalidOperation = ('Failed removing the login CONTOSO\SQLAdmin from the role MyRole on ' + ` - 'the database AdventureWorks, on the instance localhost\MSSQLSERVER. ' + ` - 'InnerException: Exception calling "DropMember" with "1" argument(s): ' + ` - '"Mock DropMember Method was called with invalid operation."') + $errorMessage = $script:localizedData.FailedToDropUserFromRole -f $testParameters.Name, $testParameters.Role, $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' {