Skip to content

Commit 5f7d245

Browse files
authored
Merge pull request Azure#2811 from alazad-msft/dev
Added Powershell Cmdlets to support Enabling/Disabling GeoBackupPolicy for SQL Azure DataWarehouses
2 parents 1a5105d + d86397f commit 5f7d245

File tree

12 files changed

+1212
-0
lines changed

12 files changed

+1212
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@
509509
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseBackupTests\TestDatabaseBackupLongTermRetentionPolicy.json">
510510
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
511511
</None>
512+
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseBackupTests\TestDatabaseGeoBackupPolicy.json">
513+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
514+
</None>
512515
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseCrudStretchTests\TestStretchDatabaseCreate.json">
513516
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
514517
</None>

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DatabaseBackupTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,11 @@ public void TestRestoreLongTermRetentionBackup()
6969
{
7070
RunPowerShellTest("Test-RestoreLongTermRetentionBackup");
7171
}
72+
[Fact]
73+
[Trait(Category.AcceptanceType, Category.CheckIn)]
74+
public void TestDatabaseGeoBackupPolicy()
75+
{
76+
RunPowerShellTest("Test-DatabaseGeoBackupPolicy");
77+
}
7278
}
7379
}

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DatabaseBackupTests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,21 @@ function Test-RestoreLongTermRetentionBackup
136136
$recoveryPointResourceId = "/subscriptions/e5e8af86-2d93-4ebd-8eb5-3b0184daa9de/resourceGroups/hchung/providers/Microsoft.RecoveryServices/vaults/hchung-testvault/backupFabrics/Azure/protectionContainers/AzureSqlContainer;Sql;hchung;hchung-testsvr/protectedItems/AzureSqlDb;dsName;hchung-testdb;fbf5641f-77f8-43b7-8fd7-5338ec293213/recoveryPoints/1731556986347"
137137

138138
Restore-AzureRmSqlDatabase -FromLongTermRetentionBackup -ResourceId $recoveryPointResourceId -TargetDatabaseName $restoredDbName -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName
139+
}
140+
141+
function Test-DatabaseGeoBackupPolicy
142+
{
143+
$rg = Get-AzureRmResourceGroup -ResourceGroupName alazad-rg
144+
$server = Get-AzureRmSqlServer -ServerName testsvr-alazad -ResourceGroupName $rg.ResourceGroupName
145+
$db = Get-AzureRmSqlDatabase -ServerName $server.ServerName -DatabaseName testdwdb -ResourceGroupName $rg.ResourceGroupName
146+
147+
# Enable and verify
148+
Set-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName -State Enabled
149+
$result = Get-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName
150+
Assert-True { $result.State -eq "Enabled" }
151+
152+
# Disable and verify
153+
Set-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName -State Disabled
154+
$result = Get-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName
155+
Assert-True { $result.State -eq "Disabled" }
139156
}

src/ResourceManager/Sql/Commands.Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseBackupTests/TestDatabaseGeoBackupPolicy.json

Lines changed: 508 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@
9797
<Compile Include="Auditing\Model\BaseBlobAuditingPolicyModel.cs" />
9898
<Compile Include="Auditing\Model\BaseTableAuditingPolicyModel.cs" />
9999
<Compile Include="Auditing\Model\AuditingPolicyModel.cs" />
100+
<Compile Include="Database Backup\Cmdlet\AzureSqlDatabaseGeoBackupPolicyCmdletBase.cs" />
100101
<Compile Include="Database Backup\Cmdlet\AzureSqlDatabaseBackupLongTermRetentionPolicyCmdletBase.cs" />
102+
<Compile Include="Database Backup\Cmdlet\GetAzureSqlDatabaseGeoBackupPolicy.cs" />
103+
<Compile Include="Database Backup\Cmdlet\SetAzureSqlDatabaseGeoBackupPolicy.cs" />
104+
<Compile Include="Database Backup\Model\AzureSqlDatabaseGeoBackupPolicyModel.cs" />
101105
<Compile Include="ImportExport\Cmdlet\GetAzureSqlDatabaseImportExportStatus.cs" />
102106
<Compile Include="ImportExport\Model\AzureSqlDatabaseImportExportStatusModel.cs" />
103107
<Compile Include="ImportExport\Cmdlet\ImportExportCmdletBase.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 System.Collections.Generic;
16+
using System.Management.Automation;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using Microsoft.Azure.Commands.Sql.Backup.Model;
19+
using Microsoft.Azure.Commands.Sql.Backup.Services;
20+
using Microsoft.Azure.Commands.Sql.Common;
21+
using Microsoft.Azure.Commands.Sql.Database.Model;
22+
using Microsoft.Azure.Commands.Sql.Database.Services;
23+
24+
namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
25+
{
26+
public abstract class AzureSqlDatabaseGeoBackupPolicyCmdletBase :
27+
AzureSqlDatabaseCmdletBase<IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel>, AzureSqlDatabaseBackupAdapter>
28+
{
29+
/// <summary>
30+
/// Initializes the adapter
31+
/// </summary>
32+
/// <param name="subscription">The subscription to operate on</param>
33+
/// <returns></returns>
34+
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(AzureSubscription subscription)
35+
{
36+
return new AzureSqlDatabaseBackupAdapter(DefaultProfile.Context);
37+
}
38+
}
39+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 System.Linq;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Sql.Backup.Model;
19+
using Microsoft.Azure.Commands.Sql.Database.Model;
20+
21+
namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
22+
{
23+
[Cmdlet(VerbsCommon.Get, "AzureRmSqlDatabaseGeoBackupPolicy")]
24+
public class GetAzureSqlDatabaseGeoBackupPolicy : AzureSqlDatabaseGeoBackupPolicyCmdletBase
25+
{
26+
/// <summary>
27+
/// Get the entities from the service
28+
/// </summary>
29+
/// <returns>The list of entities</returns>
30+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> GetEntity()
31+
{
32+
ICollection<AzureSqlDatabaseGeoBackupPolicyModel> results;
33+
34+
results = new List<AzureSqlDatabaseGeoBackupPolicyModel>();
35+
results.Add(ModelAdapter.GetDatabaseGeoBackupPolicy(
36+
this.ResourceGroupName,
37+
this.ServerName,
38+
this.DatabaseName));
39+
40+
return results;
41+
}
42+
43+
/// <summary>
44+
/// No user input to apply to model
45+
/// </summary>
46+
/// <param name="model">Model retrieved from service</param>
47+
/// <returns>The model that was passed in</returns>
48+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> ApplyUserInputToModel(
49+
IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> model)
50+
{
51+
return model;
52+
}
53+
54+
/// <summary>
55+
/// No changes to persist to server
56+
/// </summary>
57+
/// <param name="entity">The output of apply user input to model</param>
58+
/// <returns>The input entity</returns>
59+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> PersistChanges(
60+
IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> entity)
61+
{
62+
return entity;
63+
}
64+
}
65+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.Sql.Backup.Model;
20+
using Microsoft.Azure.Commands.Sql.Database.Model;
21+
22+
namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
23+
{
24+
/// <summary>
25+
/// Cmdlet to create or update a new Azure Sql Database geo backup policy
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Set, "AzureRmSqlDatabaseGeoBackupPolicy",
28+
SupportsShouldProcess = true,
29+
ConfirmImpact = ConfirmImpact.Medium)]
30+
public class SetAzureSqlDatabaseGeoBackupPolicy : AzureSqlDatabaseGeoBackupPolicyCmdletBase
31+
{
32+
/// <summary>
33+
/// Gets or sets the geo backup policy state
34+
/// </summary>
35+
[Parameter(Mandatory = true,
36+
HelpMessage = "The state of the geo backup policy, 'Enabled' or 'Disabled'")]
37+
[ValidateNotNullOrEmpty]
38+
public AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState State { get; set; }
39+
40+
/// <summary>
41+
/// Get the entities from the service
42+
/// </summary>
43+
/// <returns>The list of entities</returns>
44+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> GetEntity()
45+
{
46+
return new List<AzureSqlDatabaseGeoBackupPolicyModel>() {
47+
ModelAdapter.GetDatabaseGeoBackupPolicy(this.ResourceGroupName, this.ServerName, this.DatabaseName)
48+
};
49+
}
50+
51+
/// <summary>
52+
/// Create the model from user input
53+
/// </summary>
54+
/// <param name="model">Model retrieved from service</param>
55+
/// <returns>The model that was passed in</returns>
56+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> model)
57+
{
58+
List<Model.AzureSqlDatabaseGeoBackupPolicyModel> newEntity =
59+
new List<AzureSqlDatabaseGeoBackupPolicyModel>();
60+
newEntity.Add(new AzureSqlDatabaseGeoBackupPolicyModel()
61+
{
62+
Location = model.FirstOrDefault().Location,
63+
ResourceGroupName = ResourceGroupName,
64+
ServerName = ServerName,
65+
DatabaseName = DatabaseName,
66+
State = State,
67+
});
68+
return newEntity;
69+
}
70+
71+
/// <summary>
72+
/// Update the entity
73+
/// </summary>
74+
/// <param name="entity">The output of apply user input to model</param>
75+
/// <returns>The input entity</returns>
76+
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> PersistChanges(IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> entity)
77+
{
78+
if (ShouldProcess(DatabaseName))
79+
{
80+
return new List<AzureSqlDatabaseGeoBackupPolicyModel>() {
81+
ModelAdapter.SetDatabaseGeoBackupPolicy(this.ResourceGroupName, this.ServerName, this.DatabaseName, entity.First())
82+
};
83+
}
84+
else
85+
{
86+
return null;
87+
}
88+
}
89+
}
90+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Microsoft.Azure.Commands.Sql.Backup.Model
5+
{
6+
public class AzureSqlDatabaseGeoBackupPolicyModel
7+
{
8+
public enum GeoBackupPolicyState
9+
{
10+
Disabled,
11+
Enabled
12+
};
13+
14+
/// <summary>
15+
/// Gets or sets the location
16+
/// </summary>
17+
public string Location { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the name of the resource group
21+
/// </summary>
22+
public string ResourceGroupName { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the name of the server
26+
/// </summary>
27+
public string ServerName { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets the name of the database
31+
/// </summary>
32+
public string DatabaseName { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the geo backup policy state
36+
/// </summary>
37+
public GeoBackupPolicyState State { get; set; }
38+
}
39+
}

src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupAdapter.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,76 @@ internal AzureSqlDatabaseBackupLongTermRetentionPolicyModel SetDatabaseBackupLon
305305
};
306306
}
307307

308+
/// <summary>
309+
/// Get a geo backup policy for a Azure SQL Database
310+
/// </summary>
311+
/// <param name="resourceGroup">The name of the resource group</param>
312+
/// <param name="serverName">The name of the Azure SQL Server</param>
313+
/// <param name="databaseName">The name of the Azure SQL Database</param>
314+
/// <returns>A geo backup policy</returns>
315+
internal AzureSqlDatabaseGeoBackupPolicyModel GetDatabaseGeoBackupPolicy(
316+
string resourceGroup,
317+
string serverName,
318+
string databaseName)
319+
{
320+
var geoBackupPolicy = Communicator.GetDatabaseGeoBackupPolicy(
321+
resourceGroup,
322+
serverName,
323+
databaseName,
324+
"Default",
325+
Util.GenerateTracingId());
326+
return new AzureSqlDatabaseGeoBackupPolicyModel()
327+
{
328+
Location = geoBackupPolicy.Location,
329+
ResourceGroupName = resourceGroup,
330+
ServerName = serverName,
331+
DatabaseName = databaseName,
332+
State = (AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState) Enum.Parse(
333+
typeof(AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState),
334+
geoBackupPolicy.Properties.State),
335+
};
336+
}
337+
338+
/// <summary>
339+
/// Create or update a geo backup policy for a Azure SQL Database
340+
/// </summary>
341+
/// <param name="resourceGroup">The name of the resource group</param>
342+
/// <param name="serverName">The name of the Azure SQL Server</param>
343+
/// <param name="databaseName">The name of the Azure SQL Database</param>
344+
/// <returns>A geo backup policy</returns>
345+
internal AzureSqlDatabaseGeoBackupPolicyModel SetDatabaseGeoBackupPolicy(
346+
string resourceGroup,
347+
string serverName,
348+
string databaseName,
349+
AzureSqlDatabaseGeoBackupPolicyModel model)
350+
{
351+
var geoBackupPolicy = Communicator.SetDatabaseGeoBackupPolicy(
352+
resourceGroup,
353+
serverName,
354+
databaseName,
355+
"Default",
356+
Util.GenerateTracingId(),
357+
new GeoBackupPolicyCreateOrUpdateParameters()
358+
{
359+
Location = model.Location,
360+
Properties = new GeoBackupPolicyProperties()
361+
{
362+
State = model.State.ToString(),
363+
}
364+
});
365+
366+
return new AzureSqlDatabaseGeoBackupPolicyModel()
367+
{
368+
Location = geoBackupPolicy.Location,
369+
ResourceGroupName = resourceGroup,
370+
ServerName = serverName,
371+
DatabaseName = databaseName,
372+
State = (AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState) Enum.Parse(
373+
typeof(AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState),
374+
geoBackupPolicy.Properties.State),
375+
};
376+
}
377+
308378
/// <summary>
309379
/// Restore a given Sql Azure Database
310380
/// </summary>

0 commit comments

Comments
 (0)