diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cbcccb92d..89dc56944a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ - Updated tests to cover Revoke(). - Changes to xSQLServerHelper - The missing helper function ('Test-SPDSCObjectHasProperty'), that was referenced in the helper function Test-SQLDscParameterState, is now incorporated into Test-SQLDscParameterState (issue #589). +- Changes to xSQLServer + - BREAKING CHANGE: Remove deprecated resource xSQLAOGroupJoin. ## 7.0.0.0 diff --git a/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.psm1 b/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.psm1 deleted file mode 100644 index a9693ab030..0000000000 --- a/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.psm1 +++ /dev/null @@ -1,227 +0,0 @@ -$script:currentPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent -Import-Module -Name (Join-Path -Path (Split-Path -Path (Split-Path -Path $script:currentPath -Parent) -Parent) -ChildPath 'xSQLServerHelper.psm1') - -<# - .SYNOPSIS - Returns the current joined state of the Availability Group. - - .PARAMETER Ensure - If the replica should be joined ('Present') to the Availability Group or not joined ('Absent') to the Availability Group. - - .PARAMETER AvailabilityGroupName - The name Availability Group to join. - - .PARAMETER SQLServer - Name of the SQL server to be configured. - - .PARAMETER SQLInstanceName - Name of the SQL instance to be configured. - - .PARAMETER SetupCredential - Credential to be used to Grant Permissions in SQL. -#> -function Get-TargetResource -{ - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet('Present','Absent')] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $AvailabilityGroupName, - - [Parameter()] - [System.String] - $SQLServer = $env:COMPUTERNAME, - - [Parameter()] - [System.String] - $SQLInstanceName= 'MSSQLSERVER', - - [Parameter(Mandatory = $true)] - [System.Management.Automation.PSCredential] - $SetupCredential - ) - - $sql = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -SetupCredential $SetupCredential - - if (Test-TargetResource @PSBoundParameters) - { - $ensure = 'Present' - } - else - { - $ensure = 'Absent' - } - - return @{ - Ensure = $ensure - AvailabilityGroupName = $sql.AvailabilityGroups[$AvailabilityGroupName].Name - AvailabilityGroupNameListener = $sql.AvailabilityGroups[$AvailabilityGroupName].AvailabilityGroupListeners.Name - AvailabilityGroupNameIP = $sql.AvailabilityGroups[$AvailabilityGroupName].AvailabilityGroupListeners.AvailabilityGroupListenerIPAddresses.IPAddress - AvailabilityGroupSubMask = $sql.AvailabilityGroups[$AvailabilityGroupName].AvailabilityGroupListeners.AvailabilityGroupListenerIPAddresses.SubnetMask - AvailabilityGroupPort = $sql.AvailabilityGroups[$AvailabilityGroupName].AvailabilityGroupListeners.PortNumber - AvailabilityGroupNameDatabase = $sql.AvailabilityGroups[$AvailabilityGroupName].AvailabilityDatabases.Name - BackupDirectory = "" - SQLServer = $SQLServer - SQLInstanceName = $SQLInstanceName - } -} - -<# - .SYNOPSIS - Join the node to the the Availability Group. - - .PARAMETER Ensure - If the replica should be joined ('Present') to the Availability Group or not joined ('Absent') to the Availability Group. - - .PARAMETER AvailabilityGroupName - The name Availability Group to join. - - .PARAMETER SQLServer - Name of the SQL server to be configured. - - .PARAMETER SQLInstanceName - Name of the SQL instance to be configured. - - .PARAMETER SetupCredential - Credential to be used to Grant Permissions in SQL. -#> -function Set-TargetResource -{ - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet('Present','Absent')] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $AvailabilityGroupName, - - [Parameter()] - [System.String] - $SQLServer = $env:COMPUTERNAME, - - [Parameter()] - [System.String] - $SQLInstanceName= 'MSSQLSERVER', - - [Parameter(Mandatory = $true)] - [System.Management.Automation.PSCredential] - $SetupCredential - ) - - Initialize-SqlServerAssemblies - - $sql = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -SetupCredential $SetupCredential - Grant-ServerPerms -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -AuthorizedUser "NT AUTHORITY\SYSTEM" -SetupCredential $SetupCredential - - try - { - $sql.JoinAvailabilityGroup($AvailabilityGroupName) - New-VerboseMessage -Message "Joined $SQLServer\$SQLInstanceName to $AvailabilityGroupName" - } - catch - { - throw "Unable to Join $AvailabilityGroupName on $SQLServer\$SQLInstanceName" - } -} - -<# - .SYNOPSIS - Test if the node is joined to the Availability Group. - - .PARAMETER Ensure - If the replica should be joined ('Present') to the Availability Group or not joined ('Absent') to the Availability Group. - - .PARAMETER AvailabilityGroupName - The name Availability Group to join. - - .PARAMETER SQLServer - Name of the SQL server to be configured. - - .PARAMETER SQLInstanceName - Name of the SQL instance to be configured. - - .PARAMETER SetupCredential - Credential to be used to Grant Permissions in SQL. -#> -function Test-TargetResource -{ - [CmdletBinding()] - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateSet('Present','Absent')] - [System.String] - $Ensure, - - [Parameter(Mandatory = $true)] - [System.String] - $AvailabilityGroupName, - - [Parameter()] - [System.String] - $SQLServer = $env:COMPUTERNAME, - - [Parameter()] - [System.String] - $SQLInstanceName= 'MSSQLSERVER', - - [Parameter(Mandatory = $true)] - [System.Management.Automation.PSCredential] - $SetupCredential - ) - - $sql = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -SetupCredential $SetupCredential - - $returnValue = $false - - switch ($Ensure) - { - 'Present' - { - $availabilityGroupPresent = $sql.AvailabilityGroups.Contains($AvailabilityGroupName) - - if ($availabilityGroupPresent) - { - $returnValue = $true - } - } - - "Absent" - { - $availabilityGroupPresent = $sql.AvailabilityGroups.Contains($AvailabilityGroupName) - - if (!$availabilityGroupPresent) - { - $returnValue = $true - } - } - } - - return $returnValue -} - -<# - .SYNOPSIS - Loads the needed assemblies for the resource to be able to use methods. -#> -function Initialize-SqlServerAssemblies -{ - param () - - $null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') - $null = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") -} - -Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.schema.mof b/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.schema.mof deleted file mode 100644 index c3a19b5e14..0000000000 --- a/DSCResources/MSFT_xSQLAOGroupJoin/MSFT_xSQLAOGroupJoin.schema.mof +++ /dev/null @@ -1,10 +0,0 @@ -[ClassVersion("1.0.0.0"), FriendlyName("xSQLAOGroupJoin")] -class MSFT_xSQLAOGroupJoin : OMI_BaseResource -{ - [Key, Description("If the replica should be joined ('Present') to the Availability Group or not joined ('Absent') to the Availability Group."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; - [Key, Description("The name Availability Group to join.")] String AvailabilityGroupName; - [Write, Description("Name of the SQL server to be configured.")] String SQLServer; - [Write, Description("Name of the SQL instance to be configured.")] String SQLInstanceName; - [Required, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to Grant Permissions in SQL.")] String SetupCredential; -}; - diff --git a/README.md b/README.md index 960cb8e8fa..c927ddec90 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,6 @@ A full list of changes in each version can be found in the [change log](CHANGELO ## Resources * **[Deprecated]** ~~[**xSQLAOGroupEnsure**](#xsqlaogroupensure) resource to ensure availability group is present or absent.~~ Please use [xSQLServerAlwaysOnAvailabilityGroup](https://github.com/PowerShell/xSQLServer#xsqlserveralwaysonavailabilitygroup) and [xSQLServerAlwaysOnAvailabilityGroupReplica](https://github.com/PowerShell/xSQLServer#xsqlserveralwaysonavailabilitygroupreplica) instead. -* **[Deprecated]** ~~[**xSQLAOGroupJoin**](#xsqlaogroupjoin) resource to join a replica to an existing availability group.~~ Please use [xSQLServerAlwaysOnAvailabilityGroup](https://github.com/PowerShell/xSQLServer#xsqlserveralwaysonavailabilitygroup) and [xSQLServerAlwaysOnAvailabilityGroupReplica](https://github.com/PowerShell/xSQLServer#xsqlserveralwaysonavailabilitygroupreplica) instead. * [**xSQLServerAlias**](#xsqlserveralias) resource to manage SQL Server client Aliases. * [**xSQLServerAlwaysOnAvailabilityGroup**](#xsqlserveralwaysonavailabilitygroup) resource to ensure an availability group is present or absent. * [**xSQLServerAlwaysOnAvailabilityGroupReplica**](#xsqlserveralwaysonavailabilitygroupreplica) resource to ensure an availability group replica is present or absent. @@ -139,29 +138,6 @@ No description. None. -### xSQLAOGroupJoin **[Deprecated]** - -No description. - -**This resource is deprecated.** The functionality of this resource has been replaced with [**xSQLServerAlwaysOnAvailabilityGroupReplica**](#xsqlserveralwaysonavailabilitygroupreplica). Please do not use this resource for new deployment or development efforts. - -#### Requirements - -* Target machine must be running Windows Server 2008 R2 or later. -* Target machine must be running SQL Server Database Engine2012 or later. - -#### Parameters - -* **[String] Ensure** _(Key)_: If the replica should be joined ('Present') to the Availability Group or not joined ('Absent') to the Availability Group. { Present | Absent }. -* **[String] AvailabilityGroupName** _(Key)_: The name Availability Group to join. -* **[String] SQLServer** _(Write)_: Name of the SQL server to be configured. -* **[String] SQLInstanceName** _(Write)_: Name of the SQL instance to be configured. -* **[PSCredential] SetupCredential** _(Required)_: Credential to be used to Grant Permissions in SQL. - -#### Examples - -None. - ### xSQLServerAlias No description. diff --git a/Tests/Unit/MSFT_xSQLAOGroupJoin.Tests.ps1 b/Tests/Unit/MSFT_xSQLAOGroupJoin.Tests.ps1 deleted file mode 100644 index f967853e97..0000000000 --- a/Tests/Unit/MSFT_xSQLAOGroupJoin.Tests.ps1 +++ /dev/null @@ -1,299 +0,0 @@ -# Suppressing this rule because PlainText is required for one of the functions used in this test -[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] -param() - -$script:DSCModuleName = 'xSQLServer' -$script:DSCResourceName = 'MSFT_xSQLAOGroupJoin' - -#region HEADER - -# Unit Test Template Version: 1.2.0 -$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) -if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` - (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) -{ - & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) -} - -Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force - -$TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName $script:DSCModuleName ` - -DSCResourceName $script:DSCResourceName ` - -TestType Unit - -#endregion HEADER - -function Invoke-TestSetup { -} - -function Invoke-TestCleanup { - Restore-TestEnvironment -TestEnvironment $TestEnvironment -} - -# Begin Testing -try -{ - Invoke-TestSetup - - InModuleScope $script:DSCResourceName { - $mockSQLServerName = 'localhost' - $mockSQLServerInstanceName = 'TEST' - - $mockUnknonwAvailabilityGroupName = 'UnkownGroup' - - $mockAvailabilityGroupName = 'TestGroup' - $mockAvailabilityGroupListenerName = 'ListnerName' - $mockAvailabilityGroupListenerPortNumber = 5022 - $mockAvailabilityGroupListenerIPAddress = '192.168.0.1' - $mockAvailabilityGroupListenerSubnetMask = '255.255.255.0' - $mockAvailabilityDatabasesName = 'MyDatabase' - - $mockmockSetupCredentialUserName = "COMPANY\sqladmin" - $mockmockSetupCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force - $mockSetupCredential = New-Object System.Management.Automation.PSCredential( $mockmockSetupCredentialUserName, $mockmockSetupCredentialPassword ) - - #region Function mocks - - $mockConnectSQL = { - return @( - ( - New-Object Object | - Add-Member NoteProperty -Name AvailabilityGroups -Value @{ - "$mockAvailabilityGroupName" = @( - New-Object Object | - Add-Member NoteProperty -Name 'Name' -Value $mockAvailabilityGroupName -PassThru | - Add-Member ScriptProperty AvailabilityGroupListeners { - return @( ( New-Object Object | - Add-Member -MemberType NoteProperty -Name 'Name' -Value $mockAvailabilityGroupListenerName -PassThru | - Add-Member -MemberType NoteProperty -Name 'PortNumber' -Value $mockAvailabilityGroupListenerPortNumber -PassThru | - Add-Member ScriptProperty AvailabilityGroupListenerIPAddresses { - return @( ( New-Object Object | - Add-Member -MemberType NoteProperty -Name 'IPAddress' -Value $mockAvailabilityGroupListenerIPAddress -PassThru | - Add-Member -MemberType NoteProperty -Name 'SubnetMask' -Value $mockAvailabilityGroupListenerSubnetMask -PassThru -Force - ) ) - } -PassThru -Force - ) ) - } -PassThru | - Add-Member ScriptProperty AvailabilityDatabases { - return @( ( New-Object Object | - Add-Member -MemberType NoteProperty -Name 'Name' -Value $mockAvailabilityDatabasesName -PassThru -Force - ) ) - } -PassThru -Force - ) - } -PassThru | - Add-Member ScriptMethod JoinAvailabilityGroup { - param - ( - [Parameter()] - [String] - $AvailabilityGroupName - ) - - if( $AvailabilityGroupName -eq $mockUnknonwAvailabilityGroupName ) - { - throw 'Mock of method JoinAvailabilityGroup() throw error' - } - - return - } -PassThru -Force - ) - ) - } - - #endregion Function mocks - - # Default parameters that are used for the It-blocks - $mockDefaultParameters = @{ - SQLServer = $mockSQLServerName - SQLInstanceName = $mockSQLServerInstanceName - SetupCredential = $mockSetupCredential - } - - Describe 'MSFT_xSQLAOGroupJoin\Get-TargetResource' -Tag 'Get' { - BeforeEach { - # General mocks - Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable - } - - Context "When the system is not in the desired state" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Present' - AvailabilityGroupName = $mockUnknonwAvailabilityGroupName - } - } - - It 'Should return the same values as passed as parameters' { - $result = Get-TargetResource @testParameters - $result.SQLServer | Should Be $testParameters.SQLServer - $result.SQLInstanceName | Should Be $testParameters.SQLInstanceName - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 2 -Scope It - } - - It 'Should return the correct values in the hash table' { - $result = Get-TargetResource @testParameters - $result.Ensure | Should Be 'Absent' - $result.AvailabilityGroupName | Should BeNullOrEmpty - $result.AvailabilityGroupNameListener | Should BeNullOrEmpty - $result.AvailabilityGroupNameIP | Should BeNullOrEmpty - $result.AvailabilityGroupSubMask | Should BeNullOrEmpty - $result.AvailabilityGroupPort | Should BeNullOrEmpty - $result.AvailabilityGroupNameDatabase | Should BeNullOrEmpty - } - } - - Context "When the system is in the desired state" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Present' - AvailabilityGroupName = $mockAvailabilityGroupName - } - } - - It 'Should return the same values as passed as parameters' { - $result = Get-TargetResource @testParameters - $result.SQLServer | Should Be $testParameters.SQLServer - $result.SQLInstanceName | Should Be $testParameters.SQLInstanceName - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 2 -Scope It - } - - It 'Should return the correct values in the hash table' { - $result = Get-TargetResource @testParameters - $result.Ensure | Should Be 'Present' - $result.AvailabilityGroupName | Should Be $mockAvailabilityGroupName - $result.AvailabilityGroupNameListener | Should Be $mockAvailabilityGroupListenerName - $result.AvailabilityGroupNameIP | Should Be $mockAvailabilityGroupListenerIPAddress - $result.AvailabilityGroupSubMask | Should Be $mockAvailabilityGroupListenerSubnetMask - $result.AvailabilityGroupPort | Should Be $mockAvailabilityGroupListenerPortNumber - $result.AvailabilityGroupNameDatabase | Should Be $mockAvailabilityDatabasesName - } - } - - Assert-VerifiableMocks - } - - Describe 'MSFT_xSQLAOGroupJoin\Test-TargetResource' -Tag 'Test' { - BeforeEach { - # General mocks - Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable - } - - Context "When the system is not in the desired state and Availability Group should be present" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Present' - AvailabilityGroupName = $mockUnknonwAvailabilityGroupName - } - } - - It 'Should return the state as absent ($false)' { - $result = Test-TargetResource @testParameters - $result | Should Be $false - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - } - } - - Context "When the system is not in the desired state and Availability Group should be absent" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Absent' - AvailabilityGroupName = $mockAvailabilityGroupName - } - } - - It 'Should return the state as absent ($false)' { - $result = Test-TargetResource @testParameters - $result | Should Be $false - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - } - } - - Context "When the system is in the desired state with Availibilty Group already present" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Present' - AvailabilityGroupName = $mockAvailabilityGroupName - } - } - - It 'Should return the state as present ($true)' { - $result = Test-TargetResource @testParameters - $result | Should Be $true - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - } - } - - Context "When the system is in the desired state with Availibilty Group already absent" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Absent' - AvailabilityGroupName = $mockUnknonwAvailabilityGroupName - } - } - - It 'Should return the state as present ($true)' { - $result = Test-TargetResource @testParameters - $result | Should Be $true - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - } - } - - Assert-VerifiableMocks - } - - Describe 'MSFT_xSQLAOGroupJoin\Set-TargetResource' -Tag 'Set' { - BeforeEach { - # General mocks - Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable - Mock -CommandName Initialize-SqlServerAssemblies -MockWith {} -Verifiable - Mock -CommandName Grant-ServerPerms -MockWith {} -Verifiable - } - - Context "When the system is not in the desired state" { - BeforeEach { - $testParameters = $mockDefaultParameters - $testParameters += @{ - Ensure = 'Present' - AvailabilityGroupName = $mockAvailabilityGroupName - } - } - - It 'Should not throw when joining mocked availability group' { - { Set-TargetResource @testParameters } | Should Not Throw - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Initialize-SqlServerAssemblies -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Grant-ServerPerms -Exactly -Times 1 -Scope It - } - - It 'Should throw with the right error message when failing to join mocked availability group' { - $testParameters.AvailabilityGroupName = $mockUnknonwAvailabilityGroupName - { Set-TargetResource @testParameters } | Should Throw "Unable to Join $mockUnknonwAvailabilityGroupName on localhost\TEST" - - Assert-MockCalled -CommandName Connect-SQL -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Initialize-SqlServerAssemblies -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Grant-ServerPerms -Exactly -Times 1 -Scope It - } - } - - Assert-VerifiableMocks - } - } -} -finally -{ - Invoke-TestCleanup -}