Skip to content

Added Powershell Cmdlets to support Enabling/Disabling GeoBackupPolicy for SQL Azure DataWarehouses #2811

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

Merged
merged 7 commits into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseBackupTests\TestDatabaseBackupLongTermRetentionPolicy.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseBackupTests\TestDatabaseGeoBackupPolicy.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DatabaseCrudStretchTests\TestStretchDatabaseCreate.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ public void TestRestoreLongTermRetentionBackup()
{
RunPowerShellTest("Test-RestoreLongTermRetentionBackup");
}
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestDatabaseGeoBackupPolicy()
{
RunPowerShellTest("Test-DatabaseGeoBackupPolicy");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,21 @@ function Test-RestoreLongTermRetentionBackup
$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"

Restore-AzureRmSqlDatabase -FromLongTermRetentionBackup -ResourceId $recoveryPointResourceId -TargetDatabaseName $restoredDbName -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName
}

function Test-DatabaseGeoBackupPolicy
{
$rg = Get-AzureRmResourceGroup -ResourceGroupName alazad-rg
$server = Get-AzureRmSqlServer -ServerName testsvr-alazad -ResourceGroupName $rg.ResourceGroupName
$db = Get-AzureRmSqlDatabase -ServerName $server.ServerName -DatabaseName testdwdb -ResourceGroupName $rg.ResourceGroupName

# Enable and verify
Set-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName -State Enabled
$result = Get-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName
Assert-True { $result.State -eq "Enabled" }

# Disable and verify
Set-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName -State Disabled
$result = Get-AzureRmSqlDatabaseGeoBackupPolicy -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -DatabaseName $db.DatabaseName
Assert-True { $result.State -eq "Disabled" }
}

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@
<Compile Include="Auditing\Model\BaseBlobAuditingPolicyModel.cs" />
<Compile Include="Auditing\Model\BaseTableAuditingPolicyModel.cs" />
<Compile Include="Auditing\Model\AuditingPolicyModel.cs" />
<Compile Include="Database Backup\Cmdlet\AzureSqlDatabaseGeoBackupPolicyCmdletBase.cs" />
<Compile Include="Database Backup\Cmdlet\AzureSqlDatabaseBackupLongTermRetentionPolicyCmdletBase.cs" />
<Compile Include="Database Backup\Cmdlet\GetAzureSqlDatabaseGeoBackupPolicy.cs" />
<Compile Include="Database Backup\Cmdlet\SetAzureSqlDatabaseGeoBackupPolicy.cs" />
<Compile Include="Database Backup\Model\AzureSqlDatabaseGeoBackupPolicyModel.cs" />
<Compile Include="ImportExport\Cmdlet\GetAzureSqlDatabaseImportExportStatus.cs" />
<Compile Include="ImportExport\Model\AzureSqlDatabaseImportExportStatusModel.cs" />
<Compile Include="ImportExport\Cmdlet\ImportExportCmdletBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Sql.Backup.Model;
using Microsoft.Azure.Commands.Sql.Backup.Services;
using Microsoft.Azure.Commands.Sql.Common;
using Microsoft.Azure.Commands.Sql.Database.Model;
using Microsoft.Azure.Commands.Sql.Database.Services;

namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
{
public abstract class AzureSqlDatabaseGeoBackupPolicyCmdletBase :
AzureSqlDatabaseCmdletBase<IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel>, AzureSqlDatabaseBackupAdapter>
{
/// <summary>
/// Initializes the adapter
/// </summary>
/// <param name="subscription">The subscription to operate on</param>
/// <returns></returns>
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(AzureSubscription subscription)
{
return new AzureSqlDatabaseBackupAdapter(DefaultProfile.Context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Linq;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.Sql.Backup.Model;
using Microsoft.Azure.Commands.Sql.Database.Model;

namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
{
[Cmdlet(VerbsCommon.Get, "AzureRmSqlDatabaseGeoBackupPolicy")]
public class GetAzureSqlDatabaseGeoBackupPolicy : AzureSqlDatabaseGeoBackupPolicyCmdletBase
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> GetEntity()
{
ICollection<AzureSqlDatabaseGeoBackupPolicyModel> results;

results = new List<AzureSqlDatabaseGeoBackupPolicyModel>();
results.Add(ModelAdapter.GetDatabaseGeoBackupPolicy(
this.ResourceGroupName,
this.ServerName,
this.DatabaseName));

return results;
}

/// <summary>
/// No user input to apply to model
/// </summary>
/// <param name="model">Model retrieved from service</param>
/// <returns>The model that was passed in</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> ApplyUserInputToModel(
IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> model)
{
return model;
}

/// <summary>
/// No changes to persist to server
/// </summary>
/// <param name="entity">The output of apply user input to model</param>
/// <returns>The input entity</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> PersistChanges(
IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> entity)
{
return entity;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Sql.Backup.Model;
using Microsoft.Azure.Commands.Sql.Database.Model;

namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
{
/// <summary>
/// Cmdlet to create or update a new Azure Sql Database geo backup policy
/// </summary>
[Cmdlet(VerbsCommon.Set, "AzureRmSqlDatabaseGeoBackupPolicy",
SupportsShouldProcess = true,
ConfirmImpact = ConfirmImpact.Medium)]
public class SetAzureSqlDatabaseGeoBackupPolicy : AzureSqlDatabaseGeoBackupPolicyCmdletBase
{
/// <summary>
/// Gets or sets the geo backup policy state
/// </summary>
[Parameter(Mandatory = true,
HelpMessage = "The state of the geo backup policy, 'Enabled' or 'Disabled'")]
[ValidateNotNullOrEmpty]
public AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState State { get; set; }

/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> GetEntity()
{
return new List<AzureSqlDatabaseGeoBackupPolicyModel>() {
ModelAdapter.GetDatabaseGeoBackupPolicy(this.ResourceGroupName, this.ServerName, this.DatabaseName)
};
}

/// <summary>
/// Create the model from user input
/// </summary>
/// <param name="model">Model retrieved from service</param>
/// <returns>The model that was passed in</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> model)
{
List<Model.AzureSqlDatabaseGeoBackupPolicyModel> newEntity =
new List<AzureSqlDatabaseGeoBackupPolicyModel>();
newEntity.Add(new AzureSqlDatabaseGeoBackupPolicyModel()
{
Location = model.FirstOrDefault().Location,
ResourceGroupName = ResourceGroupName,
ServerName = ServerName,
DatabaseName = DatabaseName,
State = State,
});
return newEntity;
}

/// <summary>
/// Update the entity
/// </summary>
/// <param name="entity">The output of apply user input to model</param>
/// <returns>The input entity</returns>
protected override IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> PersistChanges(IEnumerable<AzureSqlDatabaseGeoBackupPolicyModel> entity)
{
if (ShouldProcess(DatabaseName))
{
return new List<AzureSqlDatabaseGeoBackupPolicyModel>() {
ModelAdapter.SetDatabaseGeoBackupPolicy(this.ResourceGroupName, this.ServerName, this.DatabaseName, entity.First())
};
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Sql.Backup.Model
{
public class AzureSqlDatabaseGeoBackupPolicyModel
{
public enum GeoBackupPolicyState
{
Disabled,
Enabled
};

/// <summary>
/// Gets or sets the location
/// </summary>
public string Location { get; set; }

/// <summary>
/// Gets or sets the name of the resource group
/// </summary>
public string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets the name of the server
/// </summary>
public string ServerName { get; set; }

/// <summary>
/// Gets or sets the name of the database
/// </summary>
public string DatabaseName { get; set; }

/// <summary>
/// Gets or sets the geo backup policy state
/// </summary>
public GeoBackupPolicyState State { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,76 @@ internal AzureSqlDatabaseBackupLongTermRetentionPolicyModel SetDatabaseBackupLon
};
}

/// <summary>
/// Get a geo backup policy for a Azure SQL Database
/// </summary>
/// <param name="resourceGroup">The name of the resource group</param>
/// <param name="serverName">The name of the Azure SQL Server</param>
/// <param name="databaseName">The name of the Azure SQL Database</param>
/// <returns>A geo backup policy</returns>
internal AzureSqlDatabaseGeoBackupPolicyModel GetDatabaseGeoBackupPolicy(
string resourceGroup,
string serverName,
string databaseName)
{
var geoBackupPolicy = Communicator.GetDatabaseGeoBackupPolicy(
resourceGroup,
serverName,
databaseName,
"Default",
Util.GenerateTracingId());
return new AzureSqlDatabaseGeoBackupPolicyModel()
{
Location = geoBackupPolicy.Location,
ResourceGroupName = resourceGroup,
ServerName = serverName,
DatabaseName = databaseName,
State = (AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState) Enum.Parse(
typeof(AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState),
geoBackupPolicy.Properties.State),
};
}

/// <summary>
/// Create or update a geo backup policy for a Azure SQL Database
/// </summary>
/// <param name="resourceGroup">The name of the resource group</param>
/// <param name="serverName">The name of the Azure SQL Server</param>
/// <param name="databaseName">The name of the Azure SQL Database</param>
/// <returns>A geo backup policy</returns>
internal AzureSqlDatabaseGeoBackupPolicyModel SetDatabaseGeoBackupPolicy(
string resourceGroup,
string serverName,
string databaseName,
AzureSqlDatabaseGeoBackupPolicyModel model)
{
var geoBackupPolicy = Communicator.SetDatabaseGeoBackupPolicy(
resourceGroup,
serverName,
databaseName,
"Default",
Util.GenerateTracingId(),
new GeoBackupPolicyCreateOrUpdateParameters()
{
Location = model.Location,
Properties = new GeoBackupPolicyProperties()
{
State = model.State.ToString(),
}
});

return new AzureSqlDatabaseGeoBackupPolicyModel()
{
Location = geoBackupPolicy.Location,
ResourceGroupName = resourceGroup,
ServerName = serverName,
DatabaseName = databaseName,
State = (AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState) Enum.Parse(
typeof(AzureSqlDatabaseGeoBackupPolicyModel.GeoBackupPolicyState),
geoBackupPolicy.Properties.State),
};
}

/// <summary>
/// Restore a given Sql Azure Database
/// </summary>
Expand Down
Loading