Skip to content

Commit

Permalink
SqlDatabaseOwner: Added localization (dsccommunity#1342)
Browse files Browse the repository at this point in the history
- Changes to SqlDatabaseOwner
  - Added en-US localization (issue dsccommunity#607).
  • Loading branch information
johlju authored Apr 28, 2019
1 parent faa809f commit e7cee3e
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
68 changes: 40 additions & 28 deletions DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 $_
}
}
}
Expand Down Expand Up @@ -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 `
Expand Down
Original file line number Diff line number Diff line change
@@ -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}'.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 32 additions & 20 deletions Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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' {
Expand All @@ -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' {
Expand Down Expand Up @@ -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' {
Expand All @@ -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' {
Expand Down

0 comments on commit e7cee3e

Please sign in to comment.