Skip to content

Commit

Permalink
Changes to SqlAGReplica
Browse files Browse the repository at this point in the history
  - BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed
    to ServerName and InstanceName respectivly (issue dsccommunity#308).
  • Loading branch information
johlju committed Dec 1, 2017
1 parent 1baf0cd commit c7a8630
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 63 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
the resource will only determine if a change is needed if the target node
is the active host of the SQL Server instance ([issue #869](https://github.com/PowerShell/xSQLServer/issues/869)).
- Changes to xSQLServerAlwaysOnAvailabilityGroupReplica
- BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed
to ServerName and InstanceName respectivly ([issue #308](https://github.com/PowerShell/xSQLServer/issues/)).
- Made the resource cluster aware. When ProcessOnlyOnActiveNode is specified,
the resource will only determine if a change is needed if the target node is
the active host of the SQL Server instance ([issue #870](https://github.com/PowerShell/xSQLServer/issues/870)).
Expand Down
50 changes: 25 additions & 25 deletions DSCResources/MSFT_SqlAGReplica/MSFT_SqlAGReplica.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Pare
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance to be configued.
#>
function Get-TargetResource
Expand All @@ -34,15 +34,15 @@ function Get-TargetResource

[Parameter(Mandatory = $true)]
[String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName
$InstanceName
)

# Connect to the instance
$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
$serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

# Is this node actively hosting the SQL instance?
$isActiveNode = Test-ActiveNode -ServerObject $serverObject
Expand All @@ -68,8 +68,8 @@ function Get-TargetResource
IsActiveNode = $isActiveNode
ReadOnlyRoutingConnectionUrl = ''
ReadOnlyRoutingList = @()
SQLServer = $SQLServer
SQLInstanceName = $SQLInstanceName
ServerName = $ServerName
InstanceName = $InstanceName
EndpointPort = $endpointPort
SQLServerNetName = $serverObject.NetName
}
Expand Down Expand Up @@ -114,10 +114,10 @@ function Get-TargetResource
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance to be configued.
.PARAMETER PrimaryReplicaSQLServer
Expand Down Expand Up @@ -172,11 +172,11 @@ function Set-TargetResource

[Parameter(Mandatory = $true)]
[String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName,
$InstanceName,

[Parameter()]
[String]
Expand Down Expand Up @@ -236,12 +236,12 @@ function Set-TargetResource
Import-SQLPSModule

# Connect to the instance
$serverObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName
$serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

# Determine if HADR is enabled on the instance. If not, throw an error
if ( -not $serverObject.IsHadrEnabled )
{
throw New-TerminatingError -ErrorType HadrNotEnabled -FormatArgs $Ensure, $SQLInstanceName -ErrorCategory NotImplemented
throw New-TerminatingError -ErrorType HadrNotEnabled -FormatArgs $Ensure, $InstanceName -ErrorCategory NotImplemented
}

# Get the Availability Group if it exists
Expand Down Expand Up @@ -288,7 +288,7 @@ function Set-TargetResource
$endpoint = $serverObject.Endpoints | Where-Object { $_.EndpointType -eq 'DatabaseMirroring' }
if ( -not $endpoint )
{
throw New-TerminatingError -ErrorType DatabaseMirroringEndpointNotFound -FormatArgs $SQLServer, $SQLInstanceName -ErrorCategory ObjectNotFound
throw New-TerminatingError -ErrorType DatabaseMirroringEndpointNotFound -FormatArgs $ServerName, $InstanceName -ErrorCategory ObjectNotFound
}

# If a hostname for the endpoint was not specified, define it now.
Expand Down Expand Up @@ -377,7 +377,7 @@ function Set-TargetResource
}
else
{
throw New-TerminatingError -ErrorType ReplicaNotFound -FormatArgs $Name, $SQLInstanceName -ErrorCategory ResourceUnavailable
throw New-TerminatingError -ErrorType ReplicaNotFound -FormatArgs $Name, $InstanceName -ErrorCategory ResourceUnavailable
}
}
else
Expand Down Expand Up @@ -437,7 +437,7 @@ function Set-TargetResource
}
catch
{
throw New-TerminatingError -ErrorType CreateAvailabilityGroupReplicaFailed -FormatArgs $Name, $SQLInstanceName -ErrorCategory OperationStopped -InnerException $_.Exception
throw New-TerminatingError -ErrorType CreateAvailabilityGroupReplicaFailed -FormatArgs $Name, $InstanceName -ErrorCategory OperationStopped -InnerException $_.Exception
}

# Join the Availability Group Replica to the Availability Group
Expand Down Expand Up @@ -470,10 +470,10 @@ function Set-TargetResource
.PARAMETER AvailabilityGroupName
The name of the availability group.
.PARAMETER SQLServer
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER SQLInstanceName
.PARAMETER InstanceName
Name of the SQL instance to be configued.
.PARAMETER PrimaryReplicaSQLServer
Expand Down Expand Up @@ -528,11 +528,11 @@ function Test-TargetResource

[Parameter(Mandatory = $true)]
[String]
$SQLServer,
$ServerName,

[Parameter(Mandatory = $true)]
[String]
$SQLInstanceName,
$InstanceName,

[Parameter()]
[String]
Expand Down Expand Up @@ -590,8 +590,8 @@ function Test-TargetResource
)

$getTargetResourceParameters = @{
SQLInstanceName = $SQLInstanceName
SQLServer = $SQLServer
InstanceName = $InstanceName
ServerName = $ServerName
Name = $Name
AvailabilityGroupName = $AvailabilityGroupName
}
Expand All @@ -608,7 +608,7 @@ function Test-TargetResource
if ( $ProcessOnlyOnActiveNode -and -not $getTargetResourceResult.IsActiveNode )
{
# Use localization if the resource has been converted
New-VerboseMessage -Message ( 'The node "{0}" is not actively hosting the instance "{1}". Exiting the test.' -f $env:COMPUTERNAME,$SQLInstanceName )
New-VerboseMessage -Message ( 'The node "{0}" is not actively hosting the instance "{1}". Exiting the test.' -f $env:COMPUTERNAME, $InstanceName )
return $result
}

Expand All @@ -631,8 +631,8 @@ function Test-TargetResource
$parametersToCheck = @(
'Name',
'AvailabilityGroupName',
'SQLServer',
'SQLInstanceName',
'ServerName',
'InstanceName',
'Ensure',
'AvailabilityMode',
'BackupPriority',
Expand Down
4 changes: 2 additions & 2 deletions DSCResources/MSFT_SqlAGReplica/MSFT_SqlAGReplica.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class MSFT_SqlAGReplica : OMI_BaseResource
{
[Key, Description("The name of the availability group replica. For named instances this must be in the following format SQLServer\\InstanceName.")] String Name;
[Key, Description("The name of the availability group.")] String AvailabilityGroupName;
[Required, Description("Hostname of the SQL Server to be configured.")] String SQLServer;
[Key, Description("Name of the SQL instance to be configured.")] String SQLInstanceName;
[Required, Description("Hostname of the SQL Server to be configured.")] String ServerName;
[Key, Description("Name of the SQL instance to be configured.")] String InstanceName;
[Write, Description("Hostname of the SQL Server where the primary replica is expected to be active. If the primary replica is not found here, the resource will attempt to find the host that holds the primary replica and connect to it.")] String PrimaryReplicaSQLServer;
[Write, Description("Name of the SQL instance where the primary replica lives.")] String PrimaryReplicaSQLInstanceName;
[Write, Description("Specifies if the availability group replica should be present or absent. Default is Present."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Configuration Example
Ensure = 'Present'
Name = $Node.NodeName
AvailabilityGroupName = $Node.AvailabilityGroupName
SQLServer = $Node.NodeName
SQLInstanceName = $Node.SQLInstanceName
ServerName = $Node.NodeName
InstanceName = $Node.SQLInstanceName
PrimaryReplicaSQLServer = ( $AllNodes | Where-Object { $_.Role -eq 'PrimaryReplica' } ).NodeName
PrimaryReplicaSQLInstanceName = ( $AllNodes | Where-Object { $_.Role -eq 'PrimaryReplica' } ).SQLInstanceName
ProcessOnlyOnActiveNode = $Node.ProcessOnlyOnActiveNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ Configuration Example
Ensure = 'Absent'
Name = $Node.NodeName
AvailabilityGroupName = $Node.AvailabilityGroupName
SQLServer = $Node.NodeName
SQLInstanceName = $Node.SQLInstanceName
ServerName = $Node.NodeName
InstanceName = $Node.SQLInstanceName
PrimaryReplicaSQLServer = ( $AllNodes | Where-Object { $_.Role -eq 'PrimaryReplica' } ).NodeName
PrimaryReplicaSQLInstanceName = ( $AllNodes | Where-Object { $_.Role -eq 'PrimaryReplica' } ).SQLInstanceName
}
Expand Down
64 changes: 32 additions & 32 deletions Tests/Unit/MSFT_SqlAGReplica.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ try

#region parameter mocks

$mockSqlServer = 'Server1'
$mockSqlInstanceName = 'MSSQLSERVER'
$mockServerName = 'Server1'
$mockInstanceName = 'MSSQLSERVER'
$mockPrimaryReplicaSQLServer = 'Server2'
$mockPrimaryReplicaSQLInstanceName = 'MSSQLSERVER'
$mockAvailabilityGroupName = 'AG_AllServers'
$mockAvailabilityGroupReplicaName = $mockSqlServer
$mockAvailabilityGroupReplicaName = $mockServerName
$mockEnsure = 'Present'
$mockAvailabilityMode = 'AsynchronousCommit'
$mockBackupPriority = 50
$mockConnectionModeInPrimaryRole = 'AllowAllConnections'
$mockConnectionModeInSecondaryRole = 'AllowNoConnections'
$mockEndpointHostName = $mockSqlServer
$mockEndpointHostName = $mockServerName
$mockFailoverMode = 'Manual'
$mockReadOnlyRoutingConnectionUrl = "TCP://$($mockSqlServer).domain.com:1433"
$mockReadOnlyRoutingList = @($mockSqlServer)
$mockReadOnlyRoutingConnectionUrl = "TCP://$($mockServerName).domain.com:1433"
$mockReadOnlyRoutingList = @($mockServerName)
$mockProcessOnlyOnActiveNode = $false

#endregion
Expand Down Expand Up @@ -85,16 +85,16 @@ try
$mockAllLoginsAbsent = @{}

$mockNtServiceClusSvcPresent = @{
$mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtServiceClusSvcName) )
$mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockServerName, $mockNtServiceClusSvcName) )
}

$mockNtAuthoritySystemPresent = @{
$mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtAuthoritySystemName) )
$mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockServerName, $mockNtAuthoritySystemName) )
}

$mockAllLoginsPresent = @{
$mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtServiceClusSvcName) )
$mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockSqlServer, $mockNtAuthoritySystemName) )
$mockNtServiceClusSvcName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockServerName, $mockNtServiceClusSvcName) )
$mockNtAuthoritySystemName = ( New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login($mockServerName, $mockNtAuthoritySystemName) )
}

#endregion
Expand Down Expand Up @@ -535,8 +535,8 @@ try
$getTargetResourceParameters = @{
Name = $mockAvailabilityGroupReplicaName
AvailabilityGroupName = $mockAvailabilityGroupName
SQLServer = $mockSqlServer
SQLInstanceName = $mockSqlInstanceName
ServerName = $mockServerName
InstanceName = $mockInstanceName
}

$mockDatabaseMirroringEndpoint = $true
Expand Down Expand Up @@ -565,9 +565,9 @@ try
$getTargetResourceResult.Name | Should -BeNullOrEmpty
$getTargetResourceResult.ReadOnlyRoutingConnectionUrl | Should -BeNullOrEmpty
$getTargetResourceResult.ReadOnlyRoutingList | Should -BeNullOrEmpty
$getTargetResourceResult.SQLServer | Should -Be $mockSqlServer
$getTargetResourceResult.SQLInstanceName | Should -Be $mockSqlInstanceName
$getTargetResourceResult.SQLServerNetName | Should -Be $mockSqlServer
$getTargetResourceResult.ServerName | Should -Be $mockServerName
$getTargetResourceResult.InstanceName | Should -Be $mockInstanceName
$getTargetResourceResult.SQLServerNetName | Should -Be $mockServerName

Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly
}
Expand All @@ -590,12 +590,12 @@ try
$getTargetResourceResult.EndpointPort | Should -Be $mockendpointPort
$getTargetResourceResult.Ensure | Should -Be $mockEnsure
$getTargetResourceResult.FailoverMode | Should -Be $mockFailoverMode
$getTargetResourceResult.Name | Should -Be $mockSqlServer
$getTargetResourceResult.Name | Should -Be $mockServerName
$getTargetResourceResult.ReadOnlyRoutingConnectionUrl | Should -Be $mockReadOnlyRoutingConnectionUrl
$getTargetResourceResult.ReadOnlyRoutingList | Should -Be $mockSqlServer
$getTargetResourceResult.SQLServer | Should -Be $mockSqlServer
$getTargetResourceResult.SQLInstanceName | Should -Be $mockSqlInstanceName
$getTargetResourceResult.SQLServerNetName | Should -Be $mockSqlServer
$getTargetResourceResult.ReadOnlyRoutingList | Should -Be $mockServerName
$getTargetResourceResult.ServerName | Should -Be $mockServerName
$getTargetResourceResult.InstanceName | Should -Be $mockInstanceName
$getTargetResourceResult.SQLServerNetName | Should -Be $mockServerName

Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly
}
Expand Down Expand Up @@ -649,10 +649,10 @@ try

BeforeEach {
$setTargetResourceParameters = @{
Name = $mockSqlServer
Name = $mockServerName
AvailabilityGroupName = $mockAvailabilityGroupName
SQLServer = $mockSqlServer
SQLInstanceName = $mockSqlInstanceName
ServerName = $mockServerName
InstanceName = $mockInstanceName
Ensure = 'Absent'
}
}
Expand Down Expand Up @@ -733,10 +733,10 @@ try

BeforeEach {
$setTargetResourceParameters = @{
Name = $mockSqlServer
Name = $mockServerName
AvailabilityGroupName = $mockAvailabilityGroup2Name
SQLServer = $mockSqlServer
SQLInstanceName = $mockSqlInstanceName
ServerName = $mockServerName
InstanceName = $mockInstanceName
PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer
PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName
Ensure = $mockEnsure
Expand Down Expand Up @@ -1072,10 +1072,10 @@ try
$mockAlternateEndpointProtocol = $false

$setTargetResourceParameters = @{
Name = $mockSqlServer
Name = $mockServerName
AvailabilityGroupName = $mockAvailabilityGroupName
SQLServer = $mockSqlServer
SQLInstanceName = $mockSqlInstanceName
ServerName = $mockServerName
InstanceName = $mockInstanceName
PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer
PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName
Ensure = $mockEnsure
Expand Down Expand Up @@ -1274,10 +1274,10 @@ try
$mockDatabaseMirroringEndpoint = $true

$testTargetResourceParameters = @{
Name = $mockSqlServer
Name = $mockServerName
AvailabilityGroupName = $mockAvailabilityGroupName
SQLServer = $mockSqlServer
SQLInstanceName = $mockSqlInstanceName
ServerName = $mockServerName
InstanceName = $mockInstanceName
PrimaryReplicaSQLServer = $mockPrimaryReplicaSQLServer
PrimaryReplicaSQLInstanceName = $mockPrimaryReplicaSQLInstanceName
Ensure = $mockEnsure
Expand Down

0 comments on commit c7a8630

Please sign in to comment.