Skip to content

Commit

Permalink
SqlServerNetwork: Refactored to not load assembly from GAC (#1172)
Browse files Browse the repository at this point in the history
- Changes to SqlServerDsc
  - Updated helper function Restart-SqlService to have to new optional parameters
    `SkipClusterCheck` and `SkipWaitForOnline`. This was to support more aspects
    of the resource SqlServerNetwork.
- Changes to SqlServerNetwork
  - Refactor SqlServerNetwork to not load assembly from GAC (issue #1151).
  - The resource now supports restarting the SQL Server service when both
    enabling and disabling the protocol.
  - Added integration tests for this resource
    (issue #751).
  • Loading branch information
johlju committed Jul 6, 2018
1 parent 9b6f768 commit 4997ae4
Show file tree
Hide file tree
Showing 9 changed files with 558 additions and 424 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

## Unreleased

- Changes to SqlServerDsc
- Updated helper function Restart-SqlService to have to new optional parameters
`SkipClusterCheck` and `SkipWaitForOnline`. This was to support more aspects
of the resource SqlServerNetwork.
- Changes to SqlAlwaysOnService
- Integration tests was updated to handle new IPv6 addresses on the AppVeyor
build worker ([issue #1155](https://github.com/PowerShell/SqlServerDsc/issues/1155)).
- Changes to SqlServerNetwork
- Refactor SqlServerNetwork to not load assembly from GAC ([issue #1151](https://github.com/PowerShell/SqlServerDsc/issues/1151)).
- The resource now supports restarting the SQL Server service when both
enabling and disabling the protocol.
- Added integration tests for this resource
([issue #751](https://github.com/PowerShell/SqlServerDsc/issues/751)).

## 11.3.0.0

Expand Down
175 changes: 91 additions & 84 deletions DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,24 @@ function Get-TargetResource
$ProtocolName
)

try
{
$applicationDomainObject = Register-SqlWmiManagement -SQLInstanceName $InstanceName

$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
Import-SQLPSModule

Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]
$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer

Write-Verbose -Message $script:localizedData.ReadingNetworkProperties
$returnValue = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $tcp.IsEnabled
TcpDynamicPort = ($tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value -ge 0)
TcpPort = $tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value
}
Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]

$returnValue.Keys | ForEach-Object {
Write-Verbose -Message "$_ = $($returnValue[$_])"
}
Write-Verbose -Message $script:localizedData.ReadingNetworkProperties
$returnValue = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $tcp.IsEnabled
TcpDynamicPort = ($tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value -ge 0)
TcpPort = $tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value
}
finally
{
Unregister-SqlAssemblies -ApplicationDomain $applicationDomainObject

$returnValue.Keys | ForEach-Object {
Write-Verbose -Message "$_ = $($returnValue[$_])"
}

return $returnValue
Expand Down Expand Up @@ -145,90 +138,104 @@ function Set-TargetResource

$getTargetResourceResult = Get-TargetResource -InstanceName $InstanceName -ProtocolName $ProtocolName

try
{
$applicationDomainObject = Register-SqlWmiManagement -SQLInstanceName $InstanceName

$desiredState = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $IsEnabled
TcpDynamicPort = $TcpDynamicPort
TcpPort = $TcpPort
}
$desiredState = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $IsEnabled
TcpDynamicPort = $TcpDynamicPort
TcpPort = $TcpPort
}

$isRestartNeeded = $false
$isRestartNeeded = $false

$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
# Get-TargetResource makes the necessary calls so the type ManagedComputer is available.
$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer

Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]
Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'IsEnabled')
if ($desiredState.IsEnabled -ine $getTargetResourceResult.IsEnabled)
{
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'IsEnabled', $getTargetResourceResult.IsEnabled, $desiredState.IsEnabled)
$tcp.IsEnabled = $desiredState.IsEnabled
$tcp.Alter()
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'IsEnabled')
if ($desiredState.IsEnabled -ine $getTargetResourceResult.IsEnabled)
{
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'IsEnabled', $getTargetResourceResult.IsEnabled, $desiredState.IsEnabled)
$tcp.IsEnabled = $desiredState.IsEnabled
$tcp.Alter()

$isRestartNeeded = $true
}

$isRestartNeeded = $true
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpDynamicPort')
if ($desiredState.TcpDynamicPort -ne $getTargetResourceResult.TcpDynamicPort)
{
# Translates the current and desired state to a string for display
$dynamicPortDisplayValueTable = @{
$true = 'enabled'
$false = 'disabled'
}

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpDynamicPort')
if ($desiredState.TcpDynamicPort -ne $getTargetResourceResult.TcpDynamicPort)
{
# Translates the current and desired state to a string for display
$dynamicPortDisplayValueTable = @{
$true = 'enabled'
$false = 'disabled'
}
# Translates the desired state to a valid value
$desiredDynamicPortValue = @{
$true = '0'
$false = ''
}

# Translates the desired state to a valid value
$desiredDynamicPortValue = @{
$true = '0'
$false = ''
}
$fromTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$getTargetResourceResult.TcpDynamicPort]
$toTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$desiredState.TcpDynamicPort]

$fromTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$getTargetResourceResult.TcpDynamicPort]
$toTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$desiredState.TcpDynamicPort]
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpDynamicPorts', $fromTcpDynamicPortDisplayValue, $toTcpDynamicPortDisplayValue)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value = $desiredDynamicPortValue[$desiredState.TcpDynamicPort]
$tcp.Alter()

Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpDynamicPorts', $fromTcpDynamicPortDisplayValue, $toTcpDynamicPortDisplayValue)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value = $desiredDynamicPortValue[$desiredState.TcpDynamicPort]
$tcp.Alter()
$isRestartNeeded = $true
}

$isRestartNeeded = $true
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpPort')
if ($desiredState.TcpPort -ine $getTargetResourceResult.TcpPort)
{
$fromTcpPort = $getTargetResourceResult.TcpPort
if ($fromTcpPort -eq '')
{
$fromTcpPort = 'none'
}

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpPort')
if ($desiredState.TcpPort -ine $getTargetResourceResult.TcpPort)
$toTcpPort = $desiredState.TcpPort
if ($toTcpPort -eq '')
{
$fromTcpPort = $getTargetResourceResult.TcpPort
if ($fromTcpPort -eq '')
{
$fromTcpPort = 'none'
}
$toTcpPort = 'none'
}

$toTcpPort = $desiredState.TcpPort
if ($toTcpPort -eq '')
{
$toTcpPort = 'none'
}
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpPort', $fromTcpPort, $toTcpPort)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value = $desiredState.TcpPort
$tcp.Alter()

Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpPort', $fromTcpPort, $toTcpPort)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value = $desiredState.TcpPort
$tcp.Alter()
$isRestartNeeded = $true
}

$isRestartNeeded = $true
if ($RestartService -and $isRestartNeeded)
{
$restartSqlServiceParameters = @{
SQLServer = $ServerName
SQLInstanceName = $InstanceName
Timeout = $RestartTimeout
}

if ($RestartService -and $isRestartNeeded)
if ($getTargetResourceResult.IsEnabled -eq $false -and $IsEnabled -eq $true)
{
Restart-SqlService -SQLServer $ServerName -SQLInstanceName $InstanceName -Timeout $RestartTimeout
<#
If the protocol was disabled and now being enabled, is not possible
to connect to the instance to evaluate if it is a clustered instance.
This is being tracked in issue #1174.
#>
$restartSqlServiceParameters['SkipClusterCheck'] = $true
}
}
finally
{
Unregister-SqlAssemblies -ApplicationDomain $applicationDomainObject

if ($PSBoundParameters.ContainsKey('IsEnabled') -and $IsEnabled -eq $false)
{
# If the protocol is disabled it is not possible to connect to the instance.
$restartSqlServiceParameters['SkipWaitForOnline'] = $true
}

Restart-SqlService @restartSqlServiceParameters
}
}

Expand Down
Loading

0 comments on commit 4997ae4

Please sign in to comment.