Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlDatabase: Added localization #1345

Merged
merged 2 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 56 additions & 33 deletions DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.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_SqlDatabase'

<#
.SYNOPSIS
This function gets the sql database.
Expand Down Expand Up @@ -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
)
}
}

Expand Down Expand Up @@ -149,7 +160,6 @@ function Set-TargetResource
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

if ($sqlServerObject)
{
if ($Ensure -eq 'Present')
Expand All @@ -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 $_
}
}
}
Expand All @@ -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 $_
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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
17 changes: 17 additions & 0 deletions DSCResources/MSFT_SqlDatabase/en-US/MSFT_SqlDatabase.strings.psd1
Original file line number Diff line number Diff line change
@@ -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}'.
'@
12 changes: 0 additions & 12 deletions Modules/DscResource.Common/en-US/DscResource.Common.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.
Expand All @@ -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.
'@
10 changes: 0 additions & 10 deletions Modules/DscResource.Common/sv-SE/DscResource.Common.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.
Expand All @@ -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.
'@
42 changes: 25 additions & 17 deletions Tests/Unit/MSFT_SqlDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand All @@ -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
}
}

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