Skip to content

Commit 7b359a1

Browse files
committed
GeoDR Failover Cmdlets
GeoDR Failover Cmdlets
1 parent 3399089 commit 7b359a1

File tree

10 files changed

+175
-7
lines changed

10 files changed

+175
-7
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/Commands.Sql.Test.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
<SpecificVersion>False</SpecificVersion>
6969
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
7070
</Reference>
71-
<Reference Include="Microsoft.Azure.Management.Sql">
72-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.36.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
71+
<Reference Include="Microsoft.Azure.Management.Sql, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
72+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.37.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
73+
<Private>True</Private>
7374
</Reference>
7475
<Reference Include="Microsoft.Azure.Management.Storage">
7576
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>

src/ResourceManager/Sql/Commands.Sql.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Management.Authorization" version="0.19.2-preview" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
10-
<package id="Microsoft.Azure.Management.Sql" version="0.36.0-prerelease" targetFramework="net45" />
10+
<package id="Microsoft.Azure.Management.Sql" version="0.37.0-prerelease" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Test.Framework" version="1.0.5687.28567-prerelease" targetFramework="net45" />
1313
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5687.28567-prerelease" targetFramework="net45" />

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
<Compile Include="Replication\Cmdlet\NewAzureSqlDatabaseCopy.cs" />
161161
<Compile Include="Replication\Cmdlet\NewAzureSqlDatabaseSecondary.cs" />
162162
<Compile Include="Replication\Cmdlet\RemoveAzureSqlDatabaseSecondary.cs" />
163+
<Compile Include="Replication\Cmdlet\SetAzureSqlDatabaseSecondary.cs" />
163164
<Compile Include="Replication\Model\AllowConnections.cs" />
164165
<Compile Include="Replication\Model\AzureReplicationLinkModel.cs" />
165166
<Compile Include="Replication\Model\AzureSqlDatabaseCopyModel.cs" />
@@ -244,7 +245,7 @@
244245
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
245246
</Reference>
246247
<Reference Include="Microsoft.Azure.Management.Sql, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
247-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.36.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
248+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.37.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
248249
<Private>True</Private>
249250
</Reference>
250251
<Reference Include="Microsoft.Azure.Management.Storage">

src/ResourceManager/Sql/Commands.Sql/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Sql/Commands.Sql/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,7 @@
255255
<data name="InvalidGraphEndpoint" xml:space="preserve">
256256
<value>Please use Set-AzureEnvironment to set a valid GraphEndpoint for the current AzureEnvironment.</value>
257257
</data>
258+
<data name="SetSecondaryNoOptionProvided" xml:space="preserve">
259+
<value>Must provide a Secondary Option to Set.</value>
260+
</data>
258261
</root>

src/ResourceManager/Sql/Commands.Sql/Replication/Cmdlet/RemoveAzureSqlDatabaseSecondary.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace Microsoft.Azure.Commands.Sql.Replication.Cmdlet
2323
{
2424
[Cmdlet(VerbsCommon.Remove, "AzureSqlDatabaseSecondary",
25-
SupportsShouldProcess = true,
2625
ConfirmImpact = ConfirmImpact.High)]
2726
public class RemoveAzureSqlDatabaseSecondary : AzureSqlDatabaseSecondaryCmdletBase
2827
{
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Sql.Properties;
16+
using Microsoft.Azure.Commands.Sql.Replication.Model;
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Globalization;
20+
using System.Management.Automation;
21+
22+
namespace Microsoft.Azure.Commands.Sql.Replication.Cmdlet
23+
{
24+
/// <summary>
25+
/// Cmdlet to fail over Azure SQL Database Replication Link to the secondary database
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Set, "AzureSqlDatabaseSecondary",
28+
ConfirmImpact = ConfirmImpact.Medium)]
29+
public class SetAzureSqlDatabaseSecondary : AzureSqlDatabaseSecondaryCmdletBase
30+
{
31+
/// <summary>
32+
/// Gets or sets the name of the primary Azure SQL Database with the replication link to remove.
33+
/// </summary>
34+
[Parameter(Mandatory = true,
35+
ValueFromPipelineByPropertyName = true,
36+
Position = 2,
37+
HelpMessage = "The name of the Azure SQL Database to failover.")]
38+
[ValidateNotNullOrEmpty]
39+
public string DatabaseName { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets the name of the partner resource group.
43+
/// </summary>
44+
[Parameter(Mandatory = true,
45+
ValueFromPipelineByPropertyName = true,
46+
HelpMessage = "The name of the Azure Resource Group of the partner Azure SQL Database.")]
47+
[ValidateNotNullOrEmpty]
48+
public string PartnerResourceGroupName { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets a value indicating whether this is a failover.
52+
/// </summary>
53+
/// <returns></returns>
54+
[Parameter(Mandatory = false,
55+
ValueFromPipelineByPropertyName = false,
56+
HelpMessage = "Whether this operation is a failover.")]
57+
public SwitchParameter Failover { get; set; }
58+
59+
/// <summary>
60+
/// Gets or sets a value indicating whether to make this failover will allow data loss.
61+
/// </summary>
62+
/// <returns></returns>
63+
[Parameter(Mandatory = false,
64+
ValueFromPipelineByPropertyName = false,
65+
HelpMessage = "Whether this failover operation will allow data loss.")]
66+
public SwitchParameter AllowDataLoss { get; set; }
67+
68+
/// <summary>
69+
/// Get the entities from the service
70+
/// </summary>
71+
/// <returns>The list of entities</returns>
72+
protected override IEnumerable<AzureReplicationLinkModel> GetEntity()
73+
{
74+
return ModelAdapter.ListLinks(this.ResourceGroupName, this.ServerName, this.DatabaseName, this.PartnerResourceGroupName);
75+
}
76+
77+
/// <summary>
78+
/// No user input to apply to model
79+
/// </summary>
80+
/// <param name="model">Model retrieved from service</param>
81+
/// <returns>The model that was passed in</returns>
82+
protected override IEnumerable<AzureReplicationLinkModel> ApplyUserInputToModel(IEnumerable<AzureReplicationLinkModel> model)
83+
{
84+
return model;
85+
}
86+
87+
/// <summary>
88+
/// No changes to persist to Azure SQL Server
89+
/// </summary>
90+
/// <param name="entity">The output of apply user input to model</param>
91+
/// <returns>The input entity</returns>
92+
protected override IEnumerable<AzureReplicationLinkModel> PersistChanges(IEnumerable<AzureReplicationLinkModel> entity)
93+
{
94+
if (this.MyInvocation.BoundParameters.ContainsKey("Failover"))
95+
{
96+
ModelAdapter.FailoverLink(this.ResourceGroupName,
97+
this.ServerName,
98+
this.DatabaseName,
99+
this.PartnerResourceGroupName,
100+
this.MyInvocation.BoundParameters.ContainsKey("AllowDataLoss") ? true : false);
101+
}
102+
else
103+
{
104+
// Warning user that no options were provided so no action can be taken.
105+
WriteWarning(Resources.SetSecondaryNoOptionProvided);
106+
}
107+
return entity;
108+
}
109+
}
110+
}

src/ResourceManager/Sql/Commands.Sql/Replication/Services/AzureSqlDatabaseReplicationAdapter.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,34 @@ internal void RemoveLink(string resourceGroupName, string serverName, string dat
296296

297297
ReplicationCommunicator.RemoveLink(link.ResourceGroupName, link.ServerName, link.DatabaseName, link.LinkId, Util.GenerateTracingId());
298298
}
299+
300+
/// <summary>
301+
/// Finds and removes the Secondary Link by the secondary resource group and Azure SQL Server
302+
/// </summary>
303+
/// <param name="resourceGroupName">The resource group name of primary database</param>
304+
/// <param name="serverName">The Azure SQL Server name of primary database</param>
305+
/// <param name="databaseName">The name of primary database</param>
306+
/// <param name="partnerResourceGroupName">The resource group name of secondary database</param>
307+
/// <param name="partnerServerName">The Azure SQL Server name of secondary database</param>
308+
/// <param name="allowDataLoss">Whether the failover operation will allow data loss</param>
309+
/// <returns>The Azure SQL Database ReplicationLink object</returns>
310+
internal AzureReplicationLinkModel FailoverLink(string resourceGroupName, string serverName, string databaseName, string partnerResourceGroupName, bool allowDataLoss)
311+
{
312+
IList<AzureReplicationLinkModel> links = ListLinks(resourceGroupName, serverName, databaseName, partnerResourceGroupName).ToList();
313+
314+
// Resource Management executes in context of the Secondary
315+
AzureReplicationLinkModel link = links.FirstOrDefault();
316+
317+
if(allowDataLoss)
318+
{
319+
ReplicationCommunicator.FailoverLinkAllowDataLoss(link.ResourceGroupName, link.ServerName, link.DatabaseName, link.LinkId, Util.GenerateTracingId());
320+
}
321+
else
322+
{
323+
ReplicationCommunicator.FailoverLink(link.ResourceGroupName, link.ServerName, link.DatabaseName, link.LinkId, Util.GenerateTracingId());
324+
}
325+
326+
return GetLink(link.PartnerResourceGroupName, link.PartnerServerName, link.DatabaseName, link.PartnerResourceGroupName, link.PartnerServerName);
327+
}
299328
}
300329
}

src/ResourceManager/Sql/Commands.Sql/Replication/Services/AzureSqlDatabaseReplicationCommunicator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ public void RemoveLink(string resourceGroupName, string serverName, string datab
9191
GetCurrentSqlClient(clientRequestId).DatabaseReplicationLinks.Delete(resourceGroupName, serverName, databaseName, linkId.ToString());
9292
}
9393

94+
/// <summary>
95+
/// Fails over a Replication Link without data loss
96+
/// </summary>
97+
public void FailoverLink(string resourceGroupName, string serverName, string databaseName, Guid linkId, string clientRequestId)
98+
{
99+
GetCurrentSqlClient(clientRequestId).DatabaseReplicationLinks.Failover(resourceGroupName, serverName, databaseName, linkId.ToString());
100+
}
101+
102+
/// <summary>
103+
/// Fails over a Replication Link with data loss
104+
/// </summary>
105+
public void FailoverLinkAllowDataLoss(string resourceGroupName, string serverName, string databaseName, Guid linkId, string clientRequestId)
106+
{
107+
GetCurrentSqlClient(clientRequestId).DatabaseReplicationLinks.FailoverAllowDataLoss(resourceGroupName, serverName, databaseName, linkId.ToString());
108+
}
109+
94110
/// <summary>
95111
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
96112
/// id tracing headers for the current cmdlet invocation.

src/ResourceManager/Sql/Commands.Sql/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="Microsoft.Azure.Common.Authentication" version="1.1.3-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
8-
<package id="Microsoft.Azure.Management.Sql" version="0.36.0-prerelease" targetFramework="net45" />
8+
<package id="Microsoft.Azure.Management.Sql" version="0.37.0-prerelease" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
1010
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
1111
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />

0 commit comments

Comments
 (0)