Skip to content

Commit

Permalink
SqlAlias: Added localization (#1348)
Browse files Browse the repository at this point in the history
- Changes to SqlAlias
  - Added en-US localization (issue #602).
  - Removed ShouldProcess for the code, since it has no purpose in a DSC
    resource (issue #242).
  • Loading branch information
johlju authored Apr 30, 2019
1 parent 874c790 commit ef60c85
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
- Added en-US localization ([issue #604](https://github.com/PowerShell/SqlServerDsc/issues/604)).
- Changes to SqlAlwaysOnService
- Added en-US localization ([issue #603](https://github.com/PowerShell/SqlServerDsc/issues/608)).
- Changes to SqlAlias
- Added en-US localization ([issue #602](https://github.com/PowerShell/SqlServerDsc/issues/602)).
- Removed ShouldProcess for the code, since it has no purpose in a DSC
resource ([issue #242](https://github.com/PowerShell/SqlServerDsc/issues/242)).

## 12.4.0.0

Expand Down
87 changes: 52 additions & 35 deletions DSCResources/MSFT_SqlAlias/MSFT_SqlAlias.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_SqlAlias'

function Get-TargetResource
{
[CmdletBinding()]
Expand Down Expand Up @@ -37,14 +39,19 @@ function Get-TargetResource
$protocolTcp = 'DBMSSOCN'
$protocolNamedPipes = 'DBNMPNTW'

Write-Verbose -Message "Getting the SQL Server Client Alias $Name"
Write-Verbose -Message (
$script:localizedData.GetClientAlias -f $Name
)

$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' `
-Name $Name `
-ErrorAction SilentlyContinue

if (((Get-CimInstance -ClassName win32_OperatingSystem).OSArchitecture) -eq '64-bit')
{
Write-Verbose -Message "64-bit Operating System. Also get the client alias $Name from Wow6432Node"
Write-Verbose -Message (
$script:localizedData.OSArchitecture64Bit -f $Name
)

$isWow6432Node = $true
$itemValueWow6432Node = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' `
Expand Down Expand Up @@ -97,7 +104,7 @@ function Get-TargetResource

function Set-TargetResource
{
[CmdletBinding(SupportsShouldProcess)]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
Expand Down Expand Up @@ -129,8 +136,6 @@ function Set-TargetResource
$Ensure = 'Present'
)

Write-Verbose -Message "Setting the SQL Server Client Alias $Name"

if ($Protocol -eq 'NP')
{
$itemValue = "DBNMPNTW,\\$ServerName\PIPE\sql\query"
Expand All @@ -139,7 +144,7 @@ function Set-TargetResource
if ($Protocol -eq 'TCP')
{
$itemValue = "DBMSSOCN,$ServerName"
if (!$UseDynamicTcpPort)
if (-not $UseDynamicTcpPort)
{
$itemValue += ",$TcpPort"
}
Expand All @@ -150,53 +155,53 @@ function Set-TargetResource

if ($Ensure -eq 'Present')
{
Write-Verbose -Message "Adding the SQL Server Client Alias $Name"
Write-Verbose -Message (
$script:localizedData.AddClientAlias64Bit -f $Name
)

if ($PSCmdlet.ShouldProcess($Name, 'Setting the client alias'))
if (-not (Test-Path -Path $registryPath))
{
if (!(Test-Path -Path $registryPath))
{
New-Item -Path $registryPath -Force | Out-Null
}

Set-ItemProperty -Path $registryPath -Name $Name -Value $itemValue | Out-Null
New-Item -Path $registryPath -Force | Out-Null
}

Set-ItemProperty -Path $registryPath -Name $Name -Value $itemValue | Out-Null

# If this is a 64-bit OS then also update Wow6432Node
if (((Get-CimInstance -ClassName win32_OperatingSystem).OSArchitecture) -eq '64-bit')
{
if ($PSCmdlet.ShouldProcess($Name, 'Setting the client alias (32-bit)'))
{
if (!(Test-Path -Path $registryPathWow6432Node))
{
New-Item -Path $registryPathWow6432Node -Force | Out-Null
}
Write-Verbose -Message (
$script:localizedData.AddClientAlias32Bit -f $Name
)

Set-ItemProperty -Path $registryPathWow6432Node -Name $Name -Value $itemValue | Out-Null
if (-not (Test-Path -Path $registryPathWow6432Node))
{
New-Item -Path $registryPathWow6432Node -Force | Out-Null
}

Set-ItemProperty -Path $registryPathWow6432Node -Name $Name -Value $itemValue | Out-Null
}
}

if ($Ensure -eq 'Absent')
{
Write-Verbose -Message "Removing the SQL Server Client Alias $Name"
Write-Verbose -Message (
$script:localizedData.RemoveClientAlias64Bit -f $Name
)

if ($PSCmdlet.ShouldProcess($Name, 'Remove the client alias'))
if (Test-Path -Path $registryPath)
{
if (Test-Path -Path $registryPath)
{
Remove-ItemProperty -Path $registryPath -Name $Name
}
Remove-ItemProperty -Path $registryPath -Name $Name
}

# If this is a 64-bit OS then also remove from Wow6432Node
if (((Get-CimInstance -ClassName win32_OperatingSystem).OSArchitecture) -eq '64-bit' `
-and (Test-Path -Path $registryPathWow6432Node))
{
if ($PSCmdlet.ShouldProcess($Name, 'Remove the client alias (32-bit)'))
{
Remove-ItemProperty -Path $registryPathWow6432Node -Name $Name
}
Write-Verbose -Message (
$script:localizedData.RemoveClientAlias32Bit -f $Name
)

Remove-ItemProperty -Path $registryPathWow6432Node -Name $Name
}
}
}
Expand Down Expand Up @@ -236,7 +241,9 @@ function Test-TargetResource
$Ensure = 'Present'
)

Write-Verbose -Message "Testing the SQL Server Client Alias $Name"
Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name
)

$result = $false

Expand All @@ -251,11 +258,17 @@ function Test-TargetResource
{
if( $Ensure -eq 'Absent' )
{
Write-Verbose -Message (
$script:localizedData.ClientAliasMissing -f $Name
)

$result = $true
}
else
{
Write-Verbose -Message 'Ensure is in the desired state. Verifying values.'
Write-Verbose -Message (
$script:localizedData.ClientAliasPresent -f $Name
)

if ($Protocol -eq $currentValues.Protocol)
{
Expand Down Expand Up @@ -283,11 +296,15 @@ function Test-TargetResource

if ($result)
{
Write-Verbose -Message 'In the desired state'
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name
)
}
else
{
Write-Verbose -Message 'Not in the desired state'
Write-Verbose -Message (
$script:localizedData.NotInDesiredState -f $Name
)
}

return $result
Expand Down
13 changes: 13 additions & 0 deletions DSCResources/MSFT_SqlAlias/en-US/MSFT_SqlAlias.strings.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ConvertFrom-StringData @'
GetClientAlias = Getting the SQL Server Client Alias '{0}'.
OSArchitecture64Bit = Current node is a 64-bit operating system, so will get the SQL Server Client Alias '{0}' from Wow6432Node too.
AddClientAlias64Bit = Adding the SQL Server Client Alias '{0}' (64-bit).
AddClientAlias32Bit = Adding the SQL Server Client Alias '{0}' (32-bit).
RemoveClientAlias64Bit = Removing the SQL Server Client Alias '{0}' (64-bit).
RemoveClientAlias32Bit = Removing the SQL Server Client Alias '{0}' (32-bit).
TestingConfiguration = Determines if the SQL Server Client Alias is in desired state.
ClientAliasMissing = The SQL Server Client Alias '{0}' does not exist.
ClientAliasPresent = The SQL Server Client Alias '{0}' exist, verifying values.
InDesiredState = The SQL Server Client Alias '{0}' is in desired state.
NotInDesiredState = The SQL Server Client Alias '{0}' is not in desired state.
'@
22 changes: 22 additions & 0 deletions Tests/Unit/MSFT_SqlAlias.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,28 @@ try
Add-Member -MemberType NoteProperty -Name OSArchitecture -Value '64-bit' -PassThru -Force
} -ParameterFilter { $ClassName -eq 'win32_OperatingSystem' } -Verifiable

Context 'When the system is in the desired state (Absent)' {
BeforeAll {
Mock -CommandName Get-TargetResource -MockWith {
return @{
Ensure = 'Absent'
}
}

$testParameters = @{
Ensure = 'Absent'
Name = $name
ServerName = $serverNameTcp
}
}

It "Should return true from the test method" {
Test-TargetResource @testParameters | Should -Be $true

Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
}
}

Context 'When the system is in the desired state (when using TCP)' {
$testParameters = @{
Name = $name
Expand Down

0 comments on commit ef60c85

Please sign in to comment.