From b8a22ae007ff445a909af033ca0b53b8ff600ea3 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 26 Apr 2019 15:31:38 +0200 Subject: [PATCH] Changes to SqlDatabasePermission - Added en-US localization (issue #608). --- CHANGELOG.md | 2 + .../MSFT_SqlDatabasePermission.psm1 | 85 +++++++++---------- .../MSFT_SqlDatabasePermission.strings.psd1 | 14 +++ .../en-US/DscResource.Common.strings.psd1 | 2 - .../sv-SE/DscResource.Common.strings.psd1 | 2 - .../Unit/MSFT_SqlDatabasePermission.Tests.ps1 | 51 +++++------ 6 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 DSCResources/MSFT_SqlDatabasePermission/en-US/MSFT_SqlDatabasePermission.strings.psd1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8027e1349..3b5deeddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,8 @@ - Added en-US localization ([issue #610](https://github.com/PowerShell/SqlServerDsc/issues/610)). - Changes to SqlDatabaseRecoveryModel - Added en-US localization ([issue #609](https://github.com/PowerShell/SqlServerDsc/issues/609)). +- Changes to SqlDatabasePermission + - Added en-US localization ([issue #608](https://github.com/PowerShell/SqlServerDsc/issues/608)). ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlDatabasePermission/MSFT_SqlDatabasePermission.psm1 b/DSCResources/MSFT_SqlDatabasePermission/MSFT_SqlDatabasePermission.psm1 index 6904f0bb4..07aac0450 100644 --- a/DSCResources/MSFT_SqlDatabasePermission/MSFT_SqlDatabasePermission.psm1 +++ b/DSCResources/MSFT_SqlDatabasePermission/MSFT_SqlDatabasePermission.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_SqlDatabasePermission' + <# .SYNOPSIS Returns the current permissions for the user in the database @@ -66,11 +68,13 @@ function Get-TargetResource $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + Write-Verbose -Message ( + $script:localizedData.GetDatabasePermission -f $Name, $Database, $InstanceName + ) + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName if ($sqlServerObject) { - Write-Verbose -Message "Getting permissions for user $Name in database $Database" $currentEnsure = 'Absent' if ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) @@ -101,27 +105,21 @@ function Get-TargetResource } catch { - throw New-TerminatingError -ErrorType FailedToEnumDatabasePermissions ` - -FormatArgs @($Name, $Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToEnumDatabasePermissions -f $Name, $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } else { - throw New-TerminatingError -ErrorType LoginNotFound ` - -FormatArgs @($Name, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.LoginNotFound -f $Name + New-ObjectNotFoundException -Message $errorMessage } } else { - throw New-TerminatingError -ErrorType NoDatabase ` - -FormatArgs @($Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidResult ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.DatabaseNotFound -f $Database + New-ObjectNotFoundException -Message $errorMessage } if ($getSqlDatabasePermissionResult) @@ -135,7 +133,7 @@ function Get-TargetResource } } - $returnValue = @{ + return @{ Ensure = $currentEnsure Database = $Database Name = $Name @@ -144,8 +142,6 @@ function Get-TargetResource ServerName = $ServerName InstanceName = $InstanceName } - - $returnValue } <# @@ -215,10 +211,11 @@ function Set-TargetResource ) $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - if ($sqlServerObject) { - Write-Verbose -Message "Setting permissions of database $Database for login $Name" + Write-Verbose -Message ( + $script:localizedData.ChangePermissionForUser -f $Name, $Database, $InstanceName + ) if ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) { @@ -228,17 +225,20 @@ function Set-TargetResource { try { - New-VerboseMessage -Message "Adding SQL login $Name as a user of database $Database" - $sqlDatabaseUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.User -ArgumentList ($sqlDatabaseObject, $Name) + Write-Verbose -Message ( + '{0} {1}' -f + ($script:localizedData.LoginIsNotUser -f $Name, $Database), + $script:localizedData.AddingLoginAsUser + ) + + $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 $_ } } @@ -246,7 +246,7 @@ function Set-TargetResource { try { - $permissionSet = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabasePermissionSet + $permissionSet = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.DatabasePermissionSet' foreach ($permission in $permissions) { @@ -257,8 +257,9 @@ function Set-TargetResource { 'Present' { - New-VerboseMessage -Message ('{0} the permissions ''{1}'' to the database {2} on the server {3}\{4}' ` - -f $PermissionState, ($Permissions -join ','), $Database, $ServerName, $InstanceName) + Write-Verbose -Message ( + $script:localizedData.AddPermission -f $PermissionState, ($Permissions -join ','), $Database + ) switch ($PermissionState) { @@ -281,8 +282,9 @@ function Set-TargetResource 'Absent' { - New-VerboseMessage -Message ('Revoking {0} permissions {1} to the database {2} on the server {3}\{4}' ` - -f $PermissionState, ($Permissions -join ','), $Database, $ServerName, $InstanceName) + Write-Verbose -Message ( + $script:localizedData.DropPermission -f $PermissionState, ($Permissions -join ','), $Database + ) if ($PermissionState -eq 'GrantWithGrant') { @@ -297,27 +299,21 @@ function Set-TargetResource } catch { - throw New-TerminatingError -ErrorType FailedToSetPermissionDatabase ` - -FormatArgs @($Name, $Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToSetPermissionDatabase -f $Name, $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } else { - throw New-TerminatingError -ErrorType LoginNotFound ` - -FormatArgs @($Name, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.LoginNotFound -f $Name + New-ObjectNotFoundException -Message $errorMessage } } else { - throw New-TerminatingError -ErrorType NoDatabase ` - -FormatArgs @($Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidResult ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.DatabaseNotFound -f $Database + New-ObjectNotFoundException -Message $errorMessage } } } @@ -389,7 +385,10 @@ function Test-TargetResource $InstanceName = 'MSSQLSERVER' ) - Write-Verbose -Message "Testing permissions for user $Name in database $Database." + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration -f $Name, $Database, $InstanceName + ) + $getTargetResourceParameters = @{ InstanceName = $PSBoundParameters.InstanceName ServerName = $PSBoundParameters.ServerName diff --git a/DSCResources/MSFT_SqlDatabasePermission/en-US/MSFT_SqlDatabasePermission.strings.psd1 b/DSCResources/MSFT_SqlDatabasePermission/en-US/MSFT_SqlDatabasePermission.strings.psd1 new file mode 100644 index 000000000..26cbb9983 --- /dev/null +++ b/DSCResources/MSFT_SqlDatabasePermission/en-US/MSFT_SqlDatabasePermission.strings.psd1 @@ -0,0 +1,14 @@ +ConvertFrom-StringData @' + GetDatabasePermission = Get permissions for the user '{0}' in the database '{1}' on the instance '{2}'. + DatabaseNotFound = The database '{0}' does not exist. + LoginNotFound = The login '{0}' does not exist on the instance. + FailedToEnumDatabasePermissions = Failed to get the permission for the user '{0}' in the database '{1}'. + ChangePermissionForUser = Changing the permission for the user '{0}' in the database '{1}' on the instance '{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}'. + AddPermission = {0} the permissions '{1}' to the database '{2}'. + DropPermission = Revoking the {0} permissions '{1}' from the database '{2}'. + FailedToSetPermissionDatabase = Failed to set the permissions for the login '{0}' in the database '{1}'. + TestingConfiguration = Determines if the user '{0}' has the correct permissions in the database '{1}' on the instance '{2}'. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index e57b309e1..b8cd2ab1d 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -111,8 +111,6 @@ ConvertFrom-StringData @' DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. FailedToGetOwnerDatabase = Failed to get owner of the database named {0} on {1}\\{2}. FailedToSetOwnerDatabase = Failed to set owner named {0} of the database named {1} on {2}\\{3}. - FailedToSetPermissionDatabase = Failed to set permission for login named {0} of the database named {1} on {2}\\{3}. - FailedToEnumDatabasePermissions = Failed to get permission for login named {0} of the database named {1} on {2}\\{3}. UpdateDatabaseSetError = Failed to update database {1} on {0}\\{1} with specified changes. InvalidCollationError = The specified collation '{3}' is not a valid collation for database {2} on {0}\\{1}. diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 93c476da2..adeb7f1bb 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -105,8 +105,6 @@ ConvertFrom-StringData @' DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. FailedToGetOwnerDatabase = Failed to get owner of the database named {0} on {1}\\{2}. FailedToSetOwnerDatabase = Failed to set owner named {0} of the database named {1} on {2}\\{3}. - FailedToSetPermissionDatabase = Failed to set permission for login named {0} of the database named {1} on {2}\\{3}. - FailedToEnumDatabasePermissions = Failed to get permission for login named {0} of the database named {1} on {2}\\{3}. # SQLServerNetwork UnableToUseBothDynamicAndStaticPort = Unable to set both TCP dynamic port and TCP static port. Only one can be set. diff --git a/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 index aba28b934..d4edcaef3 100644 --- a/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 @@ -257,10 +257,9 @@ try Permissions = @( 'Connect', 'Update' ) } - $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 Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -276,10 +275,9 @@ try Permissions = @( 'Connect', 'Update' ) } - $throwInvalidOperation = ("Login 'unknownLoginName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.LoginNotFound -f $testParameters.Name - { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Get-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -296,10 +294,9 @@ try Permissions = @( 'Connect', 'Update' ) } - $throwInvalidOperation = ('Failed to get permission for login named Zebes\SamusAran of ' + ` - 'the database named AdventureWorks on localhost\MSSQLSERVER.') + $errorMessage = $script:localizedData.FailedToEnumDatabasePermissions -f $testParameters.Name, $testParameters.Database - { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Get-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -428,10 +425,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Database - { Test-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Test-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -448,10 +444,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ("Login 'unknownLoginName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.LoginNotFound -f $testParameters.Name - { Test-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Test-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -469,10 +464,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ('Failed to get permission for login named Zebes\SamusAran of ' + ` - 'the database named AdventureWorks on localhost\MSSQLSERVER.') + $errorMessage = $script:localizedData.FailedToEnumDatabasePermissions -f $testParameters.Name, $testParameters.Database - { Test-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Test-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -573,10 +567,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -593,10 +586,10 @@ try Ensure = 'Present' } - $throwInvalidOperation = ("Login 'unknownLoginName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + $errorMessage = $script:localizedData.LoginNotFound -f $testParameters.Name + + { Set-TargetResource @testParameters } | Should -Throw $errorMessage Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } @@ -614,11 +607,9 @@ try Ensure = 'Present' } - $throwInvalidOperation = ('Failed adding the login Elysia\Chozo ' + ` - 'as a user of the database AdventureWorks, ' + ` - 'on the instance localhost\MSSQLSERVER.') + $errorMessage = $script:localizedData.FailedToAddUser -f $testParameters.Name, $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage $script:mockMethodCreateLoginRan | Should -Be $true @@ -629,9 +620,7 @@ try Context 'When the system is not in the desired state' { Context 'When the mock methods fail (testing the test)' { BeforeAll { - $throwInvalidOperation = ('Failed to set permission for login named ' + ` - 'Zebes\SamusAran of the database named ' + ` - 'AdventureWorks on localhost\MSSQLSERVER.') + $throwInvalidOperation = $script:localizedData.FailedToSetPermissionDatabase -f 'Zebes\SamusAran', 'AdventureWorks' $mockExpectedSqlServerLogin = $mockSqlServerLoginUnknown }