diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b5deeddc..5296829ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ - 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)). +- Changes to SqlDatabaseOwner + - Added en-US localization ([issue #607](https://github.com/PowerShell/SqlServerDsc/issues/607)). ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 index c1005ebce..0be438038 100644 --- a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 +++ b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.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_SqlDatabaseOwner' + <# .SYNOPSIS This function gets the owner of the desired sql database. @@ -50,31 +52,34 @@ function Get-TargetResource $InstanceName ) - Write-Verbose -Message "Getting owner of database $Database" - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + Write-Verbose -Message ( + $script:localizedData.GetCurrentDatabaseOwner -f $Database, $InstanceName + ) - if ($sqlServerObject) + try { - # Check database exists - if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) ) + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + if ($sqlServerObject) { - throw New-TerminatingError -ErrorType NoDatabase ` - -FormatArgs @($Database, $ServerName, $InstanceName) ` - -ErrorCategory ObjectNotFound - } + # Check database exists + if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) ) + { + $errorMessage = $script:localizedData.DatabaseNotFound -f $Database + New-ObjectNotFoundException -Message $errorMessage + } - try - { $sqlDatabaseOwner = $sqlDatabaseObject.Owner - New-VerboseMessage -Message "Owner for SQL Database name $Database is $sqlDatabaseOwner" - } - catch - { - throw New-TerminatingError -ErrorType FailedToGetOwnerDatabase ` - -FormatArgs @($Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidOperation + + Write-Verbose -Message ( + $script:localizedData.CurrentDatabaseOwner -f $Database, $sqlDatabaseOwner + ) } } + catch + { + $errorMessage = $script:localizedData.FailedToGetOwnerDatabase -f $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ + } $returnValue = @{ Database = $Database @@ -128,34 +133,39 @@ function Set-TargetResource $InstanceName ) - Write-Verbose -Message "Setting owner $Name of database $Database" $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 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 } + Write-Verbose -Message ( + $script:localizedData.SetDatabaseOwner -f $Database, $InstanceName + ) + try { $sqlDatabaseObject.SetOwner($Name) - New-VerboseMessage -Message "Owner of SQL Database name $Database is now $Name" + + Write-Verbose -Message ( + $script:localizedData.NewOwner -f $Name + ) } catch { - throw New-TerminatingError -ErrorType FailedToSetOwnerDatabase ` - -FormatArgs @($Name, $Database, $ServerName, $InstanceName) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToSetOwnerDatabase -f $Database + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -203,7 +213,9 @@ function Test-TargetResource $InstanceName ) - Write-Verbose -Message "Testing owner $Name of database $Database" + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration -f $Database, $InstanceName + ) $currentValues = Get-TargetResource @PSBoundParameters return Test-DscParameterState -CurrentValues $CurrentValues ` diff --git a/DSCResources/MSFT_SqlDatabaseOwner/en-US/MSFT_SqlDatabaseOwner.strings.psd1 b/DSCResources/MSFT_SqlDatabaseOwner/en-US/MSFT_SqlDatabaseOwner.strings.psd1 new file mode 100644 index 000000000..2c7b458ec --- /dev/null +++ b/DSCResources/MSFT_SqlDatabaseOwner/en-US/MSFT_SqlDatabaseOwner.strings.psd1 @@ -0,0 +1,11 @@ +ConvertFrom-StringData @' + GetCurrentDatabaseOwner = Get the current owner of the database '{0}' on the instance '{1}'. + DatabaseNotFound = The database '{0}' does not exist. + FailedToGetOwnerDatabase = Failed to get owner of the database '{0}'. + CurrentDatabaseOwner = The current owner of the database '{0}' is the login '{1}'. + LoginNotFound = The login '{0}' does not exist on the instance. + SetDatabaseOwner = Setting a new owner of the database '{0}' on the instance '{1}'. + NewOwner = The owner was change to '{0}'. + FailedToSetOwnerDatabase = Failed to set the new owner of the database '{0}'. + TestingConfiguration = Determines if the correct login owns the database '{0}' in the instance '{1}'. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index b8cd2ab1d..3a15497e9 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -109,8 +109,6 @@ ConvertFrom-StringData @' # SQLServerDatabase CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. 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}. 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 adeb7f1bb..07871cc40 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -103,8 +103,6 @@ ConvertFrom-StringData @' # SQLServerDatabase CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. 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}. # SQLServerNetwork UnableToUseBothDynamicAndStaticPort = Unable to set both TCP dynamic port and TCP static port. Only one can be set. diff --git a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 index b847d4311..5392278b1 100644 --- a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 @@ -120,10 +120,9 @@ try Name = $mockSqlServerLogin } - $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' { @@ -157,6 +156,27 @@ try } } + Context 'When the Connect-Sql fails with an error' { + BeforeEach { + Mock -CommandName Connect-Sql -MockWith { + throw 'mocked error' + } + } + + It 'Should throw the correct error when the method SetOwner() set the wrong login' { + $testParameters = $mockDefaultParameters + $testParameters += @{ + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin + } + + $errorMessage = $script:localizedData.FailedToGetOwnerDatabase -f $testParameters.Database + + { Get-TargetResource @testParameters } | Should -Throw $errorMessage + } + } + + Assert-VerifiableMock } @@ -217,10 +237,10 @@ try Name = $mockSqlServerLogin } - $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + $errorMessage = $script:localizedData.DatabaseNotFound -f $testParameters.Database + + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -236,10 +256,9 @@ try Name = 'John' } - $throwInvalidOperation = ("Login 'John' does not exist on " + ` - "SQL server 'localhost\MSSQLSERVER'.") + $errorMessage = $script:localizedData.LoginNotFound -f $testParameters.Name - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -274,13 +293,9 @@ try Name = $mockSqlServerLogin } - $throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + ` - 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` - 'Exception calling "SetOwner" with "1" argument(s): "Called mocked ' + ` - 'SetOwner() method without setting the right login. ' + ` - "Expected 'Zebes\SamusAran'. But was 'Elysia\Chozo'.") + $errorMessage = $script:localizedData.FailedToSetOwnerDatabase -f $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' { @@ -298,12 +313,9 @@ try Name = $mockSqlServerLogin } - $throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + ` - 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` - 'Exception calling "SetOwner" with "1" argument(s): "Mock ' + ` - 'of method SetOwner() was called with invalid operation.') + $errorMessage = $script:localizedData.FailedToSetOwnerDatabase -f $testParameters.Database - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' {