From a84aaec8ea38ec561a09ef9c018372656e53b780 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 May 2023 00:40:50 +0200 Subject: [PATCH 1/4] Add draft for issue https://github.com/dsccommunity/SqlServerDsc/issues/1939 --- .../DSC_SqlProtocolTcpIp.psm1 | 77 +++++++++++++++++++ .../en-US/DSC_SqlProtocolTcpIp.strings.psd1 | 2 + .../4-ConfigureIPAddressGroupByDetecting.ps1 | 31 ++++++++ 3 files changed, 110 insertions(+) create mode 100644 source/Examples/Resources/SqlProtocolTcpIp/4-ConfigureIPAddressGroupByDetecting.ps1 diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 index 7cb73650a..f595d7bbd 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 @@ -73,6 +73,8 @@ function Get-TargetResource $RestartTimeout = 120 ) + $IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName + $IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup $returnValue = @{ @@ -271,6 +273,8 @@ function Set-TargetResource $RestartTimeout = 120 ) + $IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName + $IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup <# @@ -536,6 +540,10 @@ function Test-TargetResource $RestartTimeout = 120 ) + $IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName + + $IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup + Write-Verbose -Message ( $script:localizedData.TestDesiredState -f $IpAddressGroup, $InstanceName, $ServerName ) @@ -750,3 +758,72 @@ function Convert-IpAdressGroupCasing return ($IpAddressGroup.ToUpper() -replace 'IPALL', 'IPAll') } + + +<# + .SYNOPSIS + Find's an IP address group by the specified IP. + + .PARAMETER IpAddress + The IP address already stored in an IP address group. +#> +function Find-IpAddressGroup +{ + + [CmdletBinding()] + [OutputType([System.String])] + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $InstanceName, + + [Parameter(Mandatory = $true)] + [AllowEmptyString()] + [System.String] + $IpAddressGroup + ) + + if ($IpAddressGroup -like '*.*.*.*' -or $IpAddressGroup -like '*:*') + { + # Getting the server protocol properties by using the computer name. + $computerName = Get-ComputerName + + Write-Verbose -Message ( + $script:localizedData.GetIpAddressGroupByIpAddress -f $IpAddressGroup + ) + + Import-SqlDscPreferredModule + + <# + Must connect to the local machine name because $ServerName can + point to a cluster instance or availability group listener. + #> + $getServerProtocolObjectParameters = @{ + ServerName = $computerName + Instance = $InstanceName + ProtocolName = 'TcpIp' + } + + $serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters + + if ($serverProtocolProperties) + { + foreach ($ipAddressGroupObject in $serverProtocolProperties.IPAddresses) + { + if ($ipAddressGroupObject.IPAddress.IPAddressToString -eq $IpAddressGroup) + { + return $ipAddressGroupObject.Name + } + } + } + + throw ( + $script:localizedData.IpAddressGroupNotFoundError -f $IpAddressGroup + ) + } + else + { + return $IpAddressGroup + } +} diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/en-US/DSC_SqlProtocolTcpIp.strings.psd1 b/source/DSCResources/DSC_SqlProtocolTcpIp/en-US/DSC_SqlProtocolTcpIp.strings.psd1 index 5e4dc7abb..07db08928 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/en-US/DSC_SqlProtocolTcpIp.strings.psd1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/en-US/DSC_SqlProtocolTcpIp.strings.psd1 @@ -14,4 +14,6 @@ ConvertFrom-StringData @' GroupIsInDesiredState = The TCP/IP address group '{0}' on the instance '{1}' is already in desired state. (SSPTI0013) RestartSuppressed = The restart was suppressed. The configuration will not be active until the node is manually restart. (SSPTI0014) FailedToGetSqlServerProtocol = Failed to get the settings for the SQL Server Database Engine server protocol TCP/IP. (SSPTI0015) + GetIpAddressGroupByIpAddress = Detect the TCP/IP address group by using the TCP/IP address '{0}'. (SSPTI0016) + IpAddressGroupNotFoundError = The TCP/IP address group was not detected because the TCP/IP address '{0}' is not assigned to any TCP/IP address group. (SSPTI0017) '@ diff --git a/source/Examples/Resources/SqlProtocolTcpIp/4-ConfigureIPAddressGroupByDetecting.ps1 b/source/Examples/Resources/SqlProtocolTcpIp/4-ConfigureIPAddressGroupByDetecting.ps1 new file mode 100644 index 000000000..0dd6f2cbc --- /dev/null +++ b/source/Examples/Resources/SqlProtocolTcpIp/4-ConfigureIPAddressGroupByDetecting.ps1 @@ -0,0 +1,31 @@ +<# + .DESCRIPTION + This example will set the TCP/IP address group by detecting the group name. Not required to specify the group name. + + The resource will be run as the account provided in $SystemAdministratorAccount. +#> +Configuration Example +{ + param + ( + [Parameter(Mandatory = $true)] + [System.Management.Automation.PSCredential] + $SystemAdministratorAccount + ) + + Import-DscResource -ModuleName 'SqlServerDsc' + + node localhost + { + SqlProtocolTcpIP 'ChangeIP' + { + InstanceName = 'MSSQLSERVER' + IpAddressGroup = 'fe80::7894:a6b6:59dd:c8fe%9' + Enabled = $true + IpAddress = 'fe80::7894:a6b6:59dd:c8fe%9' + TcpPort = '1433,1500,1501' + + PsDscRunAsCredential = $SystemAdministratorAccount + } + } +} From 2982d512246b2b25019e76703cddbded7462a7cc Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 May 2023 00:44:27 +0200 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20e96836d..f0b4609f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added new public command: - `Get-SqlDscConfigurationOption` - Returns the available configuration options that can be used with the DSC resource _SqlConfiguration_. +- SqlProtocolTcpIp + - Auto-detect the TCP/IP address group name by IP address ### Changed From 9fbe372d4cc795444ef50b64466b20c043043032 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 May 2023 01:16:03 +0200 Subject: [PATCH 3/4] Lower the threshold for ip address group not found to warning --- .../DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 index f595d7bbd..060304ae5 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 @@ -818,7 +818,7 @@ function Find-IpAddressGroup } } - throw ( + Write-Warning ( $script:localizedData.IpAddressGroupNotFoundError -f $IpAddressGroup ) } From 360e02fd0cdc1d0294582241b50bbb953df3fd87 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 May 2023 01:17:09 +0200 Subject: [PATCH 4/4] Fix code sytle --- .../DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 | 1 - 1 file changed, 1 deletion(-) diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 index 060304ae5..e29e46c99 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 @@ -769,7 +769,6 @@ function Convert-IpAdressGroupCasing #> function Find-IpAddressGroup { - [CmdletBinding()] [OutputType([System.String])] param