Skip to content

Commit

Permalink
SqlServerEndpoint*: Added localization (dsccommunity#1337)
Browse files Browse the repository at this point in the history
- Changes to SqlServerEndpoint
  - Added en-US localization (issue dsccommunity#611).
- Changes to SqlServerEndpointPermission
  - Added en-US localization (issue dsccommunity#612).
- Changes to SqlServerEndpointState
  - Added en-US localization (issue dsccommunity#613).
  • Loading branch information
johlju authored Apr 27, 2019
1 parent 874c8aa commit 1fd9a8a
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 69 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@
- Reporting Services are restarted after changing settings, unless
`$SuppressRestart` parameter is set ([issue #1331](https://github.com/PowerShell/SqlServerDsc/issues/1331)).
`$SuppressRestart` will also prevent Reporting Services restart after initialization.
- Fixed one of the error handling to use localization, and made the
error message more descriptive when the Set-TargetResource function
calls the Test-TargetResource function to verify the desired
state. *This was done prior to adding full en-US localization.*
- Changes to SqlServerLogin
- Added en-US localization ([issue #615](https://github.com/PowerShell/SqlServerDsc/issues/615)).
- Added unit tests to improved code coverage.
- Changes to SqlWindowsFirewall
- Added en-US localization ([issue #614](https://github.com/PowerShell/SqlServerDsc/issues/614)).
- Changes to SqlRS
- Fixed one of the error handling to use localization, and made the
error message more descriptive when the Set-TargetResource function
calls the Test-TargetResource function to verify the desired
state. *This was done prior to adding full en-US localization.*
- Changes to SqlServerEndpoint
- Added en-US localization ([issue #611](https://github.com/PowerShell/SqlServerDsc/issues/611)).
- Changes to SqlServerEndpointPermission
- Added en-US localization ([issue #612](https://github.com/PowerShell/SqlServerDsc/issues/612)).
- Changes to SqlServerEndpointState
- Added en-US localization ([issue #613](https://github.com/PowerShell/SqlServerDsc/issues/613)).

## 12.4.0.0

Expand Down
77 changes: 59 additions & 18 deletions DSCResources/MSFT_SqlServerEndpoint/MSFT_SqlServerEndpoint.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_SqlServerEndpoint'

<#
.SYNOPSIS
Returns the current state of the endpoint.
Expand Down Expand Up @@ -39,6 +41,10 @@ function Get-TargetResource
$InstanceName
)

Write-Verbose -Message (
$script:localizedData.GetEndpoint -f $EndpointName, $InstanceName
)

$getTargetResourceReturnValues = @{
ServerName = $ServerName
InstanceName = $InstanceName
Expand All @@ -52,16 +58,17 @@ function Get-TargetResource
$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
if ($sqlServerObject)
{
Write-Verbose -Message ('Connected to {0}\{1}' -f $ServerName, $InstanceName)
Write-Verbose -Message (
$script:localizedData.ConnectedToInstance -f $ServerName, $InstanceName
)

$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject.Name -eq $EndpointName)
{
if ($sqlServerObject.Endpoints[$EndPointName].EndpointType -ne 'DatabaseMirroring')
{
throw New-TerminatingError -ErrorType EndpointFoundButWrongType `
-FormatArgs @($EndpointName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.EndpointFoundButWrongType -f $EndpointName
New-InvalidOperationException -Message $errorMessage
}

$getTargetResourceReturnValues.Ensure = 'Present'
Expand All @@ -81,9 +88,8 @@ function Get-TargetResource
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.NotConnectedToInstance -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage
}

return $getTargetResourceReturnValues
Expand Down Expand Up @@ -156,7 +162,9 @@ function Set-TargetResource
{
if ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Absent')
{
Write-Verbose -Message ('Creating endpoint {0}.' -f $EndpointName)
Write-Verbose -Message (
$script:localizedData.CreateEndpoint -f $EndpointName, $InstanceName
)

$endpointObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Endpoint -ArgumentList $sqlServerObject, $EndpointName
$endpointObject.EndpointType = [Microsoft.SqlServer.Management.Smo.EndpointType]::DatabaseMirroring
Expand All @@ -177,56 +185,72 @@ function Set-TargetResource
}
elseif ($Ensure -eq 'Present' -and $getTargetResourceResult.Ensure -eq 'Present')
{
Write-Verbose -Message (
$script:localizedData.SetEndpoint -f $EndpointName, $InstanceName
)

# The endpoint already exist, verifying supported endpoint properties so they are in desired state.
$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
if ($endpointObject.Protocol.Tcp.ListenerIPAddress -ne $IpAddress)
{
Write-Verbose -Message ('Updating endpoint {0} IP address to {1}.' -f $EndpointName, $IpAddress)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointIPAddress -f $IpAddress
)

$endpointObject.Protocol.Tcp.ListenerIPAddress = $IpAddress
$endpointObject.Alter()
}

if ($endpointObject.Protocol.Tcp.ListenerPort -ne $Port)
{
Write-Verbose -Message ('Updating endpoint {0} port to {1}.' -f $EndpointName, $Port)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointPort -f $Port
)

$endpointObject.Protocol.Tcp.ListenerPort = $Port
$endpointObject.Alter()
}

if ($endpointObject.Owner -ne $Owner)
{
Write-Verbose -Message ('Updating endpoint {0} Owner to {1}.' -f $EndpointName, $Owner)
Write-Verbose -Message (
$script:localizedData.UpdatingEndpointOwner -f $Owner
)

$endpointObject.Owner = $Owner
$endpointObject.Alter()
}
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $EndpointName
New-ObjectNotFoundException -Message $errorMessage
}
}
elseif ($Ensure -eq 'Absent' -and $getTargetResourceResult.Ensure -eq 'Present')
{
Write-Verbose -Message ('Dropping endpoint {0}.' -f $EndpointName)

$endpointObject = $sqlServerObject.Endpoints[$EndpointName]
if ($endpointObject)
{
Write-Verbose -Message (
$script:localizedData.DropEndpoint -f $EndpointName, $InstanceName
)

$endpointObject.Drop()
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($EndpointName) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $EndpointName
New-ObjectNotFoundException -Message $errorMessage
}
}
}
else
{
throw New-TerminatingError -ErrorType NotConnectedToInstance `
-FormatArgs @($ServerName, $InstanceName) `
-ErrorCategory InvalidOperation
$errorMessage = $script:localizedData.NotConnectedToInstance -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage
}
}

Expand Down Expand Up @@ -291,6 +315,10 @@ function Test-TargetResource
$Owner
)

Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $EndpointName, $InstanceName
)

$getTargetResourceResult = Get-TargetResource -EndpointName $EndpointName -ServerName $ServerName -InstanceName $InstanceName
if ($getTargetResourceResult.Ensure -eq $Ensure)
{
Expand All @@ -317,6 +345,19 @@ function Test-TargetResource
$result = $false
}

if ($result)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $EndpointName
)
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState -f $EndpointName
)
}

return $result
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ConvertFrom-StringData @'
GetEndpoint = Getting the current values of the endpoint with the name '{0}' for the instance '{1}'.
EndpointFoundButWrongType = Endpoint '{0}' does exist, but it is not of type 'DatabaseMirroring'.
ConnectedToInstance = Connect to the instance '{0}\\{1}'.
NotConnectedToInstance = Was unable to connect to the instance '{0}\\{1}'.
SetEndpoint = Changing the values of the endpoint with the name '{0}' for the instance '{1}'.
CreateEndpoint = Creating the endpoint '{0}' on the instance '{1}'.
UpdatingEndpointIPAddress = Updating the endpoint IP address to '{0}'.
UpdatingEndpointPort = Updating the endpoint port to '{0}'.
UpdatingEndpointOwner = Updating the endpoint owner to '{0}'.
EndpointNotFound = The endpoint with the name '{0}' does not exist.
DropEndpoint = Removing the endpoint '{0}' on the instance '{1}'.
TestingConfiguration = Determines if the endpoint with the name '{0}' for the instance '{1}' is in desired state.
InDesiredState = The endpoint '{0}' is the desired state.
NotInDesiredState = The endpoint '{0}' is not in the desired state.
'@
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_SqlServerEndpointPermission'

<#
.SYNOPSIS
Returns the current state of the permissions for the principal (login).
Expand Down Expand Up @@ -46,18 +48,25 @@ function Get-TargetResource
$Principal
)

Write-Verbose -Message (
$script:localizedData.GetEndpointPermission -f $EndpointName, $InstanceName
)

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

$endpointObject = $sqlServerObject.Endpoints[$Name]
if ( $null -ne $endpointObject )
{
New-VerboseMessage -Message "Enumerating permissions for endpoint $Name"
$permissionSet = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ObjectPermissionSet' -Property @{
Connect = $true
}

$permissionSet = New-Object -Property @{ Connect = $true } -TypeName Microsoft.SqlServer.Management.Smo.ObjectPermissionSet
$endpointPermission = $endpointObject.EnumObjectPermissions($permissionSet) | Where-Object -FilterScript {
$_.PermissionState -eq 'Grant' -and $_.Grantee -eq $Principal
}

$endpointPermission = $endpointObject.EnumObjectPermissions( $permissionSet ) | Where-Object { $_.PermissionState -eq "Grant" -and $_.Grantee -eq $Principal }
if ($endpointPermission.Count -ne 0)
{
$Ensure = 'Present'
Expand All @@ -71,12 +80,14 @@ function Get-TargetResource
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($Name) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $Name
New-ObjectNotFoundException -Message $errorMessage
}
}
catch
{
throw New-TerminatingError -ErrorType UnexpectedErrorFromGet -FormatArgs @($Name) -ErrorCategory ObjectNotFound -InnerException $_.Exception
$errorMessage = $script:localizedData.UnexpectedErrorFromGet -f $Name
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}

return @{
Expand Down Expand Up @@ -153,33 +164,47 @@ function Set-TargetResource
$getTargetResourceResult = Get-TargetResource @parameters
if ($getTargetResourceResult.Ensure -ne $Ensure)
{
Write-Verbose -Message (
$script:localizedData.SetEndpointPermission -f $EndpointName, $InstanceName
)

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

$endpointObject = $sqlServerObject.Endpoints[$Name]
if ($null -ne $endpointObject)
{
$permissionSet = New-Object -Property @{ Connect = $true } -TypeName Microsoft.SqlServer.Management.Smo.ObjectPermissionSet
$permissionSet = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ObjectPermissionSet' -Property @{
Connect = $true
}

if ($Ensure -eq 'Present')
{
New-VerboseMessage -Message "Grant permission to $Principal on endpoint $Name"
Write-Verbose -Message (
$script:localizedData.GrantPermission -f $Principal
)

$endpointObject.Grant($permissionSet, $Principal)
}
else
{
New-VerboseMessage -Message "Revoke permission to $Principal on endpoint $Name"
Write-Verbose -Message (
$script:localizedData.RevokePermission -f $Principal
)

$endpointObject.Revoke($permissionSet, $Principal)
}
}
else
{
throw New-TerminatingError -ErrorType EndpointNotFound -FormatArgs @($Name) -ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.EndpointNotFound -f $Name
New-ObjectNotFoundException -Message $errorMessage
}
}
else
{
New-VerboseMessage -Message "State is already $Ensure"
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name
)
}
}

Expand Down Expand Up @@ -245,11 +270,28 @@ function Test-TargetResource
Principal = [System.String] $Principal
}

New-VerboseMessage -Message "Testing state of endpoint permission for $Principal"
Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name, $InstanceName
)

$getTargetResourceResult = Get-TargetResource @parameters

return $getTargetResourceResult.Ensure -eq $Ensure
$isInDesiredState = $getTargetResourceResult.Ensure -eq $Ensure

if ($isInDesiredState)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name
)
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState -f $Name
)
}

return $isInDesiredState
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ConvertFrom-StringData @'
GetEndpointPermission = Enumerating the current permissions for the endpoint with the name '{0}' for the instance '{1}'.
EndpointNotFound = The endpoint with the name '{0}' does not exist.
UnexpectedErrorFromGet = Got unexpected result from Get-TargetResource. No change is made.
SetEndpointPermission = Changing the permissions of the endpoint with the name '{0}' for the instance '{1}'.
GrantPermission = Grant permission to '{0}'.
RevokePermission = Revoke permission for '{0}'.
InDesiredState = The endpoint '{0}' is in the desired state.
NotInDesiredState = The endpoint '{0}' is not in the desired state.
TestingConfiguration = Determines the state of the endpoint with the name '{0}' for the instance '{1}'.
'@
Loading

0 comments on commit 1fd9a8a

Please sign in to comment.