From d2c3a3682ecaaa56a6841b7c11e313fc87854e5e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 29 Apr 2019 13:47:35 +0200 Subject: [PATCH] SqlDatabase: Added localization (#1345) - Changes to SqlDatabase - Added en-US localization (issue #606). --- CHANGELOG.md | 2 + .../MSFT_SqlDatabase/MSFT_SqlDatabase.psm1 | 89 ++++++++++++------- .../en-US/MSFT_SqlDatabase.strings.psd1 | 17 ++++ .../en-US/DscResource.Common.strings.psd1 | 12 --- .../sv-SE/DscResource.Common.strings.psd1 | 10 --- Tests/Unit/MSFT_SqlDatabase.Tests.ps1 | 42 +++++---- 6 files changed, 100 insertions(+), 72 deletions(-) create mode 100644 DSCResources/MSFT_SqlDatabase/en-US/MSFT_SqlDatabase.strings.psd1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5296829ae..461489c9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,8 @@ - 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)). +- Changes to SqlDatabase + - Added en-US localization ([issue #606](https://github.com/PowerShell/SqlServerDsc/issues/606)). ## 12.4.0.0 diff --git a/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.psm1 b/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.psm1 index afa5a205b..f57a17233 100644 --- a/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.psm1 +++ b/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.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_SqlDatabase' + <# .SYNOPSIS This function gets the sql database. @@ -62,25 +64,34 @@ function Get-TargetResource $Collation ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + Write-Verbose -Message ( + $script:localizedData.GetDatabase -f $Name, $InstanceName + ) + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName if ($sqlServerObject) { $sqlDatabaseCollation = $sqlServerObject.Collation - Write-Verbose -Message 'Getting SQL Databases' + # Check database exists $sqlDatabaseObject = $sqlServerObject.Databases[$Name] if ($sqlDatabaseObject) { - Write-Verbose -Message "SQL Database name $Name is present" $Ensure = 'Present' $sqlDatabaseCollation = $sqlDatabaseObject.Collation + + Write-Verbose -Message ( + $script:localizedData.DatabasePresent -f $Name, $sqlDatabaseCollation + ) } else { - Write-Verbose -Message "SQL Database name $Name is absent" $Ensure = 'Absent' + + Write-Verbose -Message ( + $script:localizedData.DatabaseAbsent -f $Name + ) } } @@ -149,7 +160,6 @@ function Set-TargetResource ) $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName - if ($sqlServerObject) { if ($Ensure -eq 'Present') @@ -160,49 +170,51 @@ function Set-TargetResource } elseif ($Collation -notin $sqlServerObject.EnumCollations().Name) { - throw New-TerminatingError -ErrorType InvalidCollationError ` - -FormatArgs @($ServerName, $InstanceName, $Name, $Collation) ` - -ErrorCategory InvalidOperation + $errorMessage = $script:localizedData.InvalidCollation -f $Collation, $InstanceName + New-ObjectNotFoundException -Message $errorMessage } $sqlDatabaseObject = $sqlServerObject.Databases[$Name] - if ($sqlDatabaseObject) { + Write-Verbose -Message ( + $script:localizedData.SetDatabase -f $Name, $InstanceName + ) + try { - Write-Verbose -Message "Updating the database $Name with specified settings." + Write-Verbose -Message ( + $script:localizedData.UpdatingCollation -f $Collation + ) + $sqlDatabaseObject.Collation = $Collation $sqlDatabaseObject.Alter() - New-VerboseMessage -Message "Updated Database $Name." } catch { - throw New-TerminatingError -ErrorType UpdateDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToUpdateDatabase -f $Name + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } else { try { - $sqlDatabaseObjectToCreate = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -ArgumentList $sqlServerObject, $Name + $sqlDatabaseObjectToCreate = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Database' -ArgumentList $sqlServerObject, $Name if ($sqlDatabaseObjectToCreate) { - Write-Verbose -Message "Adding to SQL the database $Name." + Write-Verbose -Message ( + $script:localizedData.CreateDatabase -f $Name + ) + $sqlDatabaseObjectToCreate.Collation = $Collation $sqlDatabaseObjectToCreate.Create() - New-VerboseMessage -Message "Created Database $Name." } } catch { - throw New-TerminatingError -ErrorType CreateDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToCreateDatabase -f $Name + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -213,17 +225,17 @@ function Set-TargetResource $sqlDatabaseObjectToDrop = $sqlServerObject.Databases[$Name] if ($sqlDatabaseObjectToDrop) { - Write-Verbose -Message "Deleting to SQL the database $Name." + Write-Verbose -Message ( + $script:localizedData.DropDatabase -f $Name + ) + $sqlDatabaseObjectToDrop.Drop() - New-VerboseMessage -Message "Dropped Database $Name." } } catch { - throw New-TerminatingError -ErrorType DropDatabaseSetError ` - -FormatArgs @($ServerName, $InstanceName, $Name) ` - -ErrorCategory InvalidOperation ` - -InnerException $_.Exception + $errorMessage = $script:localizedData.FailedToDropDatabase -f $Name + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } } @@ -283,7 +295,9 @@ function Test-TargetResource $Collation ) - Write-Verbose -Message "Checking if database named $Name is present or absent" + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration -f $Name, $InstanceName + ) $getTargetResourceResult = Get-TargetResource @PSBoundParameters $isDatabaseInDesiredState = $true @@ -299,7 +313,10 @@ function Test-TargetResource { if ($getTargetResourceResult.Ensure -ne 'Absent') { - New-VerboseMessage -Message "Ensure is set to Absent. The database $Name should be dropped" + Write-Verbose -Message ( + $script:localizedData.NotInDesiredStateAbsent -f $Name + ) + $isDatabaseInDesiredState = $false } } @@ -308,18 +325,24 @@ function Test-TargetResource { if ($getTargetResourceResult.Ensure -ne 'Present') { - New-VerboseMessage -Message "Ensure is set to Present. The database $Name should be created" + Write-Verbose -Message ( + $script:localizedData.NotInDesiredStatePresent -f $Name + ) + $isDatabaseInDesiredState = $false } elseif ($getTargetResourceResult.Collation -ne $Collation) { - New-VerboseMessage -Message 'Database exist but has the wrong collation.' + Write-Verbose -Message ( + $script:localizedData.CollationWrong -f $Name, $getTargetResourceResult.Collation, $Collation + ) + $isDatabaseInDesiredState = $false } } } - $isDatabaseInDesiredState + return $isDatabaseInDesiredState } Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/MSFT_SqlDatabase/en-US/MSFT_SqlDatabase.strings.psd1 b/DSCResources/MSFT_SqlDatabase/en-US/MSFT_SqlDatabase.strings.psd1 new file mode 100644 index 000000000..2212ea442 --- /dev/null +++ b/DSCResources/MSFT_SqlDatabase/en-US/MSFT_SqlDatabase.strings.psd1 @@ -0,0 +1,17 @@ +ConvertFrom-StringData @' + GetDatabase = Get the state of the database '{0}' on the instance '{1}'. + DatabasePresent = There is a database '{0}' present, and has the collation '{1}'. + DatabaseAbsent = It does not exist a database named '{0}'. + InvalidCollation = The specified collation '{0}' is not a valid collation for the instance '{1}'. + SetDatabase = Changing properties of the database '{0}' on the instance '{1}'. + UpdatingCollation = Changing the database collation to '{0}'. + FailedToUpdateDatabase = Failed to update database {0} with specified changes. + CreateDatabase = Creating the database '{0}'. + DropDatabase = Removing the database '{0}'. + FailedToCreateDatabase = Failed to create the database '{0}'. + FailedToDropDatabase = Failed to remove the database '{0}' + TestingConfiguration = Determines the state of the database '{0}' on the instance '{1}'. + NotInDesiredStateAbsent = Expected the database '{0}' to absent, but it was present. + NotInDesiredStatePresent = Expected the database '{0}' to present, but it was absent + CollationWrong = The database '{0}' exist and has the collation '{1}', but expected it to have the collation '{2}'. +'@ diff --git a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 index 3a15497e9..2cf36852e 100644 --- a/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1 @@ -81,9 +81,6 @@ ConvertFrom-StringData @' LoginNotFound = Login '{0}' does not exist on SQL server '{1}\\{2}'." FailedLogin = Creating a login of type 'SqlLogin' requires LoginCredential - # Database Role - AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}. - # AvailabilityGroupListener AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist. AvailabilityGroupListenerErrorVerifyExist = Unexpected result when trying to verify existence of listener '{0}'. @@ -105,13 +102,4 @@ ConvertFrom-StringData @' JoinAvailabilityGroupFailed = Failed to join the availability group replica '{0}'. RemoveAvailabilityGroupReplicaFailed = Failed to remove the availability group replica '{0}'. ReplicaNotFound = Unable to find the availability group replica '{0}' on the instance '{1}'. - - # SQLServerDatabase - CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. - DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. - 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}. - - # SQLServerNetwork - UnableToUseBothDynamicAndStaticPort = Unable to set both TCP dynamic port and TCP static port. Only one can be set. '@ diff --git a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 index 07871cc40..42b5a6f95 100644 --- a/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 +++ b/Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1 @@ -70,9 +70,6 @@ ConvertFrom-StringData @' LoginNotFound = Login '{0}' does not exist on SQL server '{1}\\{2}'." FailedLogin = Creating a login of type 'SqlLogin' requires LoginCredential - # Database Role - AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}. - # AvailabilityGroupListener AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist. AvailabilityGroupListenerErrorVerifyExist = Unexpected result when trying to verify existence of listener '{0}'. @@ -99,11 +96,4 @@ ConvertFrom-StringData @' JoinAvailabilityGroupFailed = Failed to join the availability group replica '{0}'. RemoveAvailabilityGroupReplicaFailed = Failed to remove the availability group replica '{0}'. ReplicaNotFound = Unable to find the availability group replica '{0}' on the instance '{1}'. - - # SQLServerDatabase - CreateDatabaseSetError = Failed to create the database named {2} on {0}\\{1}. - DropDatabaseSetError = Failed to drop the database named {2} on {0}\\{1}. - - # SQLServerNetwork - UnableToUseBothDynamicAndStaticPort = Unable to set both TCP dynamic port and TCP static port. Only one can be set. '@ diff --git a/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 index feac8d33a..54293a4c6 100644 --- a/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 @@ -379,10 +379,26 @@ try Ensure = 'Present' } - $throwInvalidOperation = ('InnerException: Exception calling "Create" ' + ` - 'with "0" argument(s): "Mock Create Method was called with invalid operation."') + $errorMessage = $script:localizedData.FailedToCreateDatabase -f $testParameters.Name - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + { Set-TargetResource @testParameters } | Should -Throw $errorMessage + + Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter { + $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Database' + } -Scope It + } + + It 'Should throw the correct error when Alter() method was called with invalid operation' { + $testParameters = $mockDefaultParameters + $testParameters += @{ + Name = $mockSqlDatabaseName + Ensure = 'Present' + Collation = 'SQL_Latin1_General_Pref_CP850_CI_AS' + } + + $errorMessage = $script:localizedData.FailedToUpdateDatabase -f $testParameters.Name + + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should throw the correct error when invalid collation is specified' { @@ -393,19 +409,11 @@ try Collation = 'InvalidCollation' } - $throwInvalidOperation = ("The specified collation '{3}' is not a valid collation for database {2} on {0}\{1}." -f $mockServerName, $mockInstanceName, $testParameters.Name, $testParameters.Collation) + $errorMessage = $script:localizedData.InvalidCollation -f $testParameters.Collation, $testParameters.InstanceName - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation - } - - It 'Should call the mock function Connect-SQL' { - Assert-MockCalled Connect-SQL -Exactly -Times 2 -Scope Context - } + { Set-TargetResource @testParameters } | Should -Throw $errorMessage - It 'Should call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.Database' { - Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter { - $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Database' - } -Scope Context + Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It } } @@ -421,10 +429,10 @@ try } It 'Should throw the correct error when Drop() method was called with invalid operation' { - $throwInvalidOperation = ('InnerException: Exception calling "Drop" ' + ` - 'with "0" argument(s): "Mock Drop Method was called with invalid operation."') - { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation + $errorMessage = $script:localizedData.FailedToDropDatabase -f $testParameters.Name + + { Set-TargetResource @testParameters } | Should -Throw $errorMessage } It 'Should call the mock function Connect-SQL' {