Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Support for Contained Availability Groups #9345

Merged
merged 14 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion public/New-DbaAvailabilityGroup.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function New-DbaAvailabilityGroup {
function New-DbaAvailabilityGroup {
<#
.SYNOPSIS
Automates the creation of availability groups.
Expand Down Expand Up @@ -56,6 +56,14 @@ function New-DbaAvailabilityGroup {
.PARAMETER Name
The name of the Availability Group.

.PARAMETER IsContained
Builds the Availability Group as contained. Only supported in SQL Server 2022 or higher.

https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/contained-availability-groups-overview?view=sql-server-ver16

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would exclude the "?view=sql-server-ver16" to always point to the latest version.

.PARAMETER ReuseSystemDatabases
Used when rebuilding a cluster where system databases already exist for the contained availability group.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use the term "cluster" here. Quote from the documentation: "This option is only valid for contained AGs, and specifies that the newly created AG should reuse existing contained system databases for a previous contained AG of the same name. For example, if you had a contained AG with the name MyContainedAG, and wanted to drop and recreate it, you could use this option to reuse the contents of the original contained system databases."

.PARAMETER DtcSupport
Indicates whether the DtcSupport is enabled

Expand Down Expand Up @@ -219,6 +227,11 @@ function New-DbaAvailabilityGroup {

Creates a basic availability group named BAG1 on sql2016std and does not confirm when setting up

.EXAMPLE
PS C:\> New-DbaAvailabilityGroup -Primary sql2022n01 -Secondary sql2022n02 -Name AgContained -IsContained

Creates a contained availability group named AgContained on sql2022

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "sql2022" (which is your cluster name?) we should name the nodes: "sql2022n01 and sql2022n02".

.EXAMPLE
PS C:\> New-DbaAvailabilityGroup -Primary sql2016b -Name AG1 -Dhcp -Database db1 -UseLastBackup

Expand Down Expand Up @@ -268,6 +281,8 @@ function New-DbaAvailabilityGroup {

[parameter(Mandatory)]
[string]$Name,
[switch]$IsContained,
[switch]$ReuseSystemDatabases,
[switch]$DtcSupport,
[ValidateSet('Wsfc', 'External', 'None')]
[string]$ClusterType = (Get-DbatoolsConfigValue -FullName 'AvailabilityGroups.Default.ClusterType' -Fallback 'Wsfc'),
Expand Down Expand Up @@ -366,6 +381,14 @@ function New-DbaAvailabilityGroup {
return
}

if ($IsContained -and $server.VersionMajor -lt 16) {
Stop-Function -Level Warning -Message "Contained availability groups are only supported in SQL Server 2022 and above"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aa you can see some lines above, we need an additional "return" in case Stop-Function only issues a warning and not an exception (this depends on -EnableException). And please remove the -Level Warning. But you should add the -Target $Primary so that all the tests are written the same way.


if ($ReuseSystemDatabases -and $IsContained -eq $false) {
Write-Message -Level Warning -Message "Reuse system databases is only applicable in contained availability groups test "
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "test" mean? And there is a space at the end of the string.


Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Checking requirements"
$requirementsFailed = $false

Expand Down Expand Up @@ -488,6 +511,7 @@ function New-DbaAvailabilityGroup {
$ag.AutomatedBackupPreference = [Microsoft.SqlServer.Management.Smo.AvailabilityGroupAutomatedBackupPreference]::$AutomatedBackupPreference
$ag.FailureConditionLevel = [Microsoft.SqlServer.Management.Smo.AvailabilityGroupFailureConditionLevel]::$FailureConditionLevel
$ag.HealthCheckTimeout = $HealthCheckTimeout
$ag.ReuseSystemDatabases = $ReuseSystemDatabases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is saver (and better to read) if we move this line after the new line 527 ($ag.IsContained = $IsContained). This way, the two new parameters are used only on 2022 and newer. This way, the code does not change for older versions of sql server.

if ($server.VersionMajor -ge 13) {
$ag.BasicAvailabilityGroup = $Basic
Expand All @@ -499,6 +523,10 @@ function New-DbaAvailabilityGroup {
$ag.ClusterType = $ClusterType
}

if ($server.VersionMajor -ge 16) {
$ag.IsContained = $IsContained
}

if ($PassThru) {
$defaults = 'LocalReplicaRole', 'Name as AvailabilityGroup', 'PrimaryReplicaServerName as PrimaryReplica', 'AutomatedBackupPreference', 'AvailabilityReplicas', 'AvailabilityDatabases', 'AvailabilityGroupListeners'
return (Select-DefaultView -InputObject $ag -Property $defaults)
Expand Down
2 changes: 1 addition & 1 deletion tests/New-DbaAvailabilityGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
Describe "$commandname Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'Primary', 'PrimarySqlCredential', 'Secondary', 'SecondarySqlCredential', 'Name', 'DtcSupport', 'ClusterType', 'AutomatedBackupPreference', 'FailureConditionLevel', 'HealthCheckTimeout', 'Basic', 'DatabaseHealthTrigger', 'Passthru', 'Database', 'SharedPath', 'UseLastBackup', 'Force', 'AvailabilityMode', 'FailoverMode', 'BackupPriority', 'ConnectionModeInPrimaryRole', 'ConnectionModeInSecondaryRole', 'SeedingMode', 'Endpoint', 'EndpointUrl', 'Certificate', 'ConfigureXESession', 'IPAddress', 'SubnetMask', 'Port', 'Dhcp', 'EnableException'
[object[]]$knownParameters = 'Primary', 'PrimarySqlCredential', 'Secondary', 'SecondarySqlCredential', 'Name', 'DtcSupport', 'ClusterType', 'AutomatedBackupPreference', 'FailureConditionLevel', 'HealthCheckTimeout', 'Basic', 'DatabaseHealthTrigger', 'Passthru', 'Database', 'SharedPath', 'UseLastBackup', 'Force', 'AvailabilityMode', 'FailoverMode', 'BackupPriority', 'ConnectionModeInPrimaryRole', 'ConnectionModeInSecondaryRole', 'SeedingMode', 'Endpoint', 'EndpointUrl', 'Certificate', 'ConfigureXESession', 'IPAddress', 'SubnetMask', 'Port', 'Dhcp', 'EnableException', 'ReuseSystemDatabases', 'IsContained'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have the order of parameters in this list in the order of the param block. So after "name" in this case.

$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
Expand Down
Loading