Skip to content

Commit

Permalink
[Sql] Overhaul failover group cmdlets to use current API and support …
Browse files Browse the repository at this point in the history
…multiple secondaries (Azure#23898)

* rework failover group cmdlets to use current sdk instead of legacy sdk; update get to accomodate multiple secondary failover groups

* default to manual failover policy

* update changelog

* remove commented out import statement

* Update src/Sql/Sql/ChangeLog.md

Co-authored-by: Yunchi Wang <[email protected]>

* Update src/Sql/Sql/Failover Group/Cmdlet/NewAzureSqlFailoverGroup.cs

Co-authored-by: Yunchi Wang <[email protected]>

* Update src/Sql/Sql/Failover Group/Cmdlet/SetAzureSqlFailoverGroup.cs

Co-authored-by: Yunchi Wang <[email protected]>

* modify changelog lines to be in upcoming release instead of previously released version

* update help files with updated default param values

---------

Co-authored-by: Yunchi Wang <[email protected]>
  • Loading branch information
alecbain and wyunchi-ms authored Apr 30, 2024
1 parent 71c6293 commit 5536fd0
Show file tree
Hide file tree
Showing 32 changed files with 47,008 additions and 168,475 deletions.
22 changes: 16 additions & 6 deletions src/Sql/Sql.Test/ScenarioTests/FailoverGroupTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@ function Handle-FailoverGroupTestWithFailoverGroup($scriptBlock, $failoverPolicy
}.GetNewClosure()
}

function Validate-FailoverGroup($server, $partnerServer, $name, $role, $failoverPolicy, $gracePeriod, $readOnlyFailoverPolicy, $databases, $fg, $message="no context provided")
function Validate-FailoverGroup($server, $partnerServer, $name, $role, $failoverPolicy, $gracePeriod, $readOnlyFailoverPolicy, $databases, $fg, $message="no context provided", $isMultiServer = $false, $partnerServersList = $null)
{
Assert-NotNull $fg.Id "`$fg.Id ($message)"
Assert-NotNull $fg.PartnerServers "`$fg.PartnerServers ($message)"
Assert-AreEqual $name $fg.FailoverGroupName "`$fg.FailoverGroupName ($message)"
# Assert-AreEqual $true $partnerServer.ResourceId.Contains($fg.PartnerSubscriptionId) "`$fg.PartnerSubscriptionId ($message)"
Assert-AreEqual $server.ResourceGroupName $fg.ResourceGroupName "`$fg.ResourceGroupName ($message)"
Assert-AreEqual $partnerServer.ResourceGroupName $fg.PartnerResourceGroupName "`$fg.PartnerResourceGroupName ($message)"
Assert-AreEqual $server.ServerName $fg.ServerName "`$fg.ServerName ($message)"
Assert-AreEqual $partnerServer.ServerName $fg.PartnerServerName "`$fg.PartnerServerName ($message)"
if ($isMultiServer)
{
foreach ($server in $partnerServersList)
{
Assert-AreEqual $true $fg.PartnerServers.Contains($server) "`$fg.PartnerServers[$($server)] ($message)"
}
}
else
{
Assert-AreEqual $server.ResourceGroupName $fg.ResourceGroupName "`$fg.ResourceGroupName ($message)"
Assert-AreEqual $partnerServer.ResourceGroupName $fg.PartnerResourceGroupName "`$fg.PartnerResourceGroupName ($message)"
Assert-AreEqual $server.ServerName $fg.ServerName "`$fg.ServerName ($message)"
Assert-AreEqual $partnerServer.ServerName $fg.PartnerServerName "`$fg.PartnerServerName ($message)"
}
# Commented out because server location is a location id, but FG location is a location friendly name
# Assert-AreEqual $server.Location $fg.Location "`$fg.Location ($message)"
# Assert-AreEqual $partnerServer.Location $fg.PartnerLocation "`$fg.PartnerLocation ($message)"
Expand Down Expand Up @@ -431,6 +441,6 @@ function Test-FailoverGroupMultipleSecondaries()

$fgName = Get-FailoverGroupName
$fg = New-AzSqlDatabaseFailoverGroup -ResourceGroupName $primaryServer.ResourceGroupName -ServerName $primaryServer.ServerName -PartnerServerName $partnerServer.ServerName -FailoverGroupName $fgName -FailoverPolicy Automatic -GracePeriodWithDataLossHours 1 -AllowReadOnlyFailoverToPrimary Enabled -PartnerServerList $partnerServers -ReadOnlyEndpointTargetServer $partnerServer.ResourceId
Validate-FailoverGroup $primaryServer $server $fgName Primary Automatic 1 Enabled @() $fg
Validate-FailoverGroup $primaryServer $server $fgName Primary Automatic 1 Enabled @() $fg -isMultiServer $true -PartnerServerList $partnerServers
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

127,139 changes: 2,743 additions & 124,396 deletions ...Sql.Test.ScenarioTests.FailoverGroupTests/Test_AddRemoveDatabasesToFromFailoverGroup.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
## Upcoming Release
* Made 1.2 as default for MinimalTlsVersion when creating new Sql Server from Powershell
* Fixed an existing issue with `Set-AzSqlInstanceActiveDirectoryAdministrator`
* Added multi-secondary support for `Get-AzSqlDatabaseFailoverGroup`, `Remove-AzSqlDatabaseFromFailoverGroup` and `Add-AzSqlDatabaseFromFailoverGroup`
* Changed default FailoverPolicy value for `New-AzSqlDatabaseFailoverGroup`, `Set-AzSqlDatabaseFailoverGroup` from `Automatic` to `Manual`

## Version 4.14.0
* Added `DatabaseFormat` and `PricingModel` parameters to `New-AzSqlInstance`, `Set-AzSqlInstance`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ protected override IEnumerable<AzureSqlFailoverGroupModel> GetEntity()
/// <returns>The model that was passed in</returns>
protected override IEnumerable<AzureSqlFailoverGroupModel> ApplyUserInputToModel(IEnumerable<AzureSqlFailoverGroupModel> model)
{
string location = ModelAdapter.GetServerLocation(ResourceGroupName, ServerName);
List<AzureSqlFailoverGroupModel> newEntity = new List<AzureSqlFailoverGroupModel>();
List<string> dbs = new List<string>();
dbs.AddRange(ConvertDatabaseModelToDatabaseHelper(Database));
Expand Down
4 changes: 2 additions & 2 deletions src/Sql/Sql/Failover Group/Cmdlet/NewAzureSqlFailoverGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Hyak.Common;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Commands.Sql.FailoverGroup.Model;
using Microsoft.Azure.Management.Sql.Models;
Expand All @@ -23,6 +22,7 @@
using System.Management.Automation;
using System;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Rest.Azure;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

namespace Microsoft.Azure.Commands.Sql.FailoverGroup.Cmdlet
Expand Down Expand Up @@ -85,7 +85,7 @@ public class NewAzureSqlFailoverGroup : AzureSqlFailoverGroupCmdletBase
[Parameter(Mandatory = false,
HelpMessage = "The failover policy of the Azure SQL Database Failover Group.")]
[ValidateNotNullOrEmpty]
[PSDefaultValue(Help = "Automatic")]
[PSDefaultValue(Help = "Manual", Value = FailoverPolicy.Manual)]
public FailoverPolicy FailoverPolicy { get; set; }

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Sql/Sql/Failover Group/Cmdlet/SetAzureSqlFailoverGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class SetAzureSqlFailoverGroup : AzureSqlFailoverGroupCmdletBase
[Parameter(Mandatory = false,
HelpMessage = "The failover policy of the Azure SQL Database Failover Group.")]
[ValidateNotNullOrEmpty]
[PSDefaultValue(Help = "Automatic")]
[PSDefaultValue(Help = "Manual", Value = FailoverPolicy.Manual)]
public FailoverPolicy FailoverPolicy { get; set; }

/// <summary>
Expand Down Expand Up @@ -105,13 +105,13 @@ public class SetAzureSqlFailoverGroup : AzureSqlFailoverGroupCmdletBase
protected override IEnumerable<AzureSqlFailoverGroupModel> GetEntity()
{
bool useV2Get = MyInvocation.BoundParameters.ContainsKey("PartnerServerList");
AzureSqlFailoverGroupModel model = ModelAdapter.GetFailoverGroup(this.ResourceGroupName, this.ServerName, this.FailoverGroupName, useV2Get);
AzureSqlFailoverGroupModel model = ModelAdapter.GetFailoverGroup(this.ResourceGroupName, this.ServerName, this.FailoverGroupName);

// For cases when existing failover group is multi-secondary, but no multi-secondary properties change in the Set invocation.
if (model.PartnerServers != null && model.PartnerServers.Any() && model.PartnerServers.Count > 1)
{
useV2Get = true;
model = ModelAdapter.GetFailoverGroup(this.ResourceGroupName, this.ServerName, this.FailoverGroupName, useV2Get);
model = ModelAdapter.GetFailoverGroup(this.ResourceGroupName, this.ServerName, this.FailoverGroupName);
}
return new List<AzureSqlFailoverGroupModel>() { model };
}
Expand Down
45 changes: 20 additions & 25 deletions src/Sql/Sql/Failover Group/Services/AzureSqlFailoverGroupAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,11 @@ public AzureSqlFailoverGroupAdapter(IAzureContext context)
/// <param name="resourceGroupName">The name of the resource group</param>
/// <param name="serverName">The name of the Azure Sql Database Server</param>
/// <param name="failoverGroupName">The name of the Azure Sql Database FailoverGroup</param>
/// <param name="useV2Get">Whether to use the V2 get with current model, or the legacy get and model.</param>
/// <returns>The Azure Sql Database FailoverGroup object</returns>
internal AzureSqlFailoverGroupModel GetFailoverGroup(string resourceGroupName, string serverName, string failoverGroupName, bool useV2Get = false)
internal AzureSqlFailoverGroupModel GetFailoverGroup(string resourceGroupName, string serverName, string failoverGroupName)
{
if (useV2Get)
{
var response = Communicator.GetV2(resourceGroupName, serverName, failoverGroupName);
return CreateCurrentFailoverGroupModelFromResponse(response);
}
var resp = Communicator.Get(resourceGroupName, serverName, failoverGroupName);

return CreateFailoverGroupModelFromResponse(resp);
var response = Communicator.GetV2(resourceGroupName, serverName, failoverGroupName);
return CreateCurrentFailoverGroupModelFromResponse(response);
}

/// <summary>
Expand All @@ -90,7 +83,7 @@ internal ICollection<AzureSqlFailoverGroupModel> ListFailoverGroups(string resou

return resp.Select((db) =>
{
return CreateFailoverGroupModelFromResponse(db);
return CreateCurrentFailoverGroupModelFromResponse(db);
}).ToList();
}

Expand Down Expand Up @@ -239,16 +232,12 @@ internal ICollection<AzureSqlDatabaseModel> ListDatabasesOnServer(string resourc
/// <returns>The updated Azure Sql Database FailoverGroup</returns>
internal AzureSqlFailoverGroupModel AddOrRemoveDatabaseToFailoverGroup(string resourceGroupName, string serverName, string failoverGroupName, AzureSqlFailoverGroupModel model)
{
var resp = Communicator.PatchUpdate(resourceGroupName, serverName, failoverGroupName, new FailoverGroupPatchUpdateParameters()
var resp = Communicator.PatchUpdateV2(resourceGroupName, serverName, failoverGroupName, new FailoverGroupUpdate()
{
Location = model.Location,
Properties = new FailoverGroupPatchUpdateProperties()
{
Databases = model.Databases,
}
Databases = model.Databases,
});

return CreateFailoverGroupModelFromResponse(resp);
return CreateCurrentFailoverGroupModelFromResponse(resp);
}

/// <summary>
Expand Down Expand Up @@ -364,14 +353,20 @@ private AzureSqlFailoverGroupModel CreateCurrentFailoverGroupModelFromResponse(M
model.ResourceGroupName = GetUriSegment(failoverGroup.Id, 4);
model.ServerName = GetUriSegment(failoverGroup.Id, 8);

PartnerInfo partnerServer = failoverGroup.PartnerServers.FirstOrDefault();
if (partnerServer != null)
if (failoverGroup.PartnerServers.Count == 1)
{
model.PartnerSubscriptionId = GetUriSegment(partnerServer.Id, 2);
model.PartnerResourceGroupName = GetUriSegment(partnerServer.Id, 4);
model.PartnerServerName = GetUriSegment(partnerServer.Id, 8);
model.PartnerLocation = partnerServer.Location;
}
PartnerInfo partnerServer = failoverGroup.PartnerServers.FirstOrDefault();
if (partnerServer != null)
{
model.PartnerSubscriptionId = GetUriSegment(partnerServer.Id, 2);
model.PartnerResourceGroupName = GetUriSegment(partnerServer.Id, 4);
model.PartnerServerName = GetUriSegment(partnerServer.Id, 8);
model.PartnerLocation = partnerServer.Location;
}
}

model.PartnerServers = ConvertPartnerInfoList(failoverGroup.PartnerServers.ToList());


return model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Azure.Management.Sql.LegacySdk.Models;
using Microsoft.Azure.Management.Sql.Models;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Azure.Commands.Sql.FailoverGroup.Services
{
Expand Down Expand Up @@ -75,9 +76,9 @@ public Management.Sql.Models.FailoverGroup GetV2(string resourceGroupName, strin
/// <summary>
/// Lists Azure Sql Database Failover Groups
/// </summary>
public IList<Management.Sql.LegacySdk.Models.FailoverGroup> List(string resourceGroupName, string serverName)
public IList<Management.Sql.Models.FailoverGroup> List(string resourceGroupName, string serverName)
{
return GetLegacySqlClient().FailoverGroups.List(resourceGroupName, serverName).FailoverGroups;
return GetCurrentSqlClient().FailoverGroups.ListByServer(resourceGroupName, serverName).ToList();
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Sql/Sql/Sql.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@
<Label>ServerName</Label>
<PropertyName>ServerName</PropertyName>
</ListItem>
<ListItem>
<Label>PartnerServers</Label>
<PropertyName>PartnerServers</PropertyName>
</ListItem>
<ListItem>
<Label>PartnerLocation</Label>
<PropertyName>PartnerLocation</PropertyName>
Expand All @@ -392,6 +396,10 @@
<Label>ReadWriteFailoverPolicy</Label>
<PropertyName>ReadWriteFailoverPolicy</PropertyName>
</ListItem>
<ListItem>
<Label>ReadOnlyEndpointTarget</Label>
<PropertyName>FailoverGroupReadOnlyEndpointV2</PropertyName>
</ListItem>
<ListItem>
<Label>FailoverWithDataLossGracePeriodHours</Label>
<PropertyName>FailoverWithDataLossGracePeriodHours</PropertyName>
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/help/New-AzSqlDatabaseFailoverGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Accepted values: Automatic, Manual

Required: False
Position: Named
Default value: Automatic
Default value: Manual
Accept pipeline input: False
Accept wildcard characters: False
```
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/help/Set-AzSqlDatabaseFailoverGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Accepted values: Automatic, Manual

Required: False
Position: Named
Default value: Automatic
Default value: Manual
Accept pipeline input: False
Accept wildcard characters: False
```
Expand Down

0 comments on commit 5536fd0

Please sign in to comment.