Skip to content
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
2 changes: 2 additions & 0 deletions src/Accounts/Accounts/AzureRmAlias/Mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,8 @@
"New-AzSqlDatabaseImport": "New-AzureRmSqlDatabaseImport",
"Get-AzSqlDatabaseGeoBackupPolicy": "Get-AzureRmSqlDatabaseGeoBackupPolicy",
"Set-AzSqlDatabaseGeoBackupPolicy": "Set-AzureRmSqlDatabaseGeoBackupPolicy",
"Get-AzSqlDatabaseBackupShortTermRetentionPolicy": "Get-AzureRmSqlDatabaseBackupShortTermRetentionPolicy",
"Set-AzSqlDatabaseBackupShortTermRetentionPolicy": "Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy",
"Get-AzSqlDatabaseBackupLongTermRetentionPolicy": "Get-AzureRmSqlDatabaseBackupLongTermRetentionPolicy",
"Set-AzSqlDatabaseBackupLongTermRetentionPolicy": "Set-AzureRmSqlDatabaseBackupLongTermRetentionPolicy",
"Get-AzSqlDatabaseLongTermRetentionBackup": "Get-AzureRmSqlDatabaseLongTermRetentionBackup",
Expand Down
7 changes: 7 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/DatabaseBackupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,12 @@ public void TestRemoveDatabaseRestorePoint()
RunPowerShellTest("Test-RemoveDatabaseRestorePoint");
}
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestShortTermRetentionPolicy()
{
RunPowerShellTest("Test-ShortTermRetentionPolicy");
}
}
}
68 changes: 68 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/DatabaseBackupTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,72 @@ function Test-RemoveDatabaseRestorePoint
{
Remove-ResourceGroupForTest $rg
}
}

function Test-ShortTermRetentionPolicy
{
# Setup
$location = Get-Location "Microsoft.Sql" "servers" "West US 2"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

# Not divisible by 7, client should error
$invalidRetention = 20

try
{
# Create db with default values
$databaseName = Get-DatabaseName
$db = New-AzureRmSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName

# Test default parameter set
$retention = 28
$policy = Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -RetentionDays $retention
Assert-AreEqual $policy.Count 1
Assert-AreEqual $retention $policy[0].RetentionDays
$policy = Get-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName
Assert-AreEqual $policy.Count 1
Assert-AreEqual $retention $policy[0].RetentionDays

# Test InputObject
$retention = 21
$policy = Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -AzureSqlDatabase $db -RetentionDays $retention
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays
$policy = Get-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -AzureSqlDatabase $db
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays

# Test ResourceId
$retention = 14
$resourceId = $db.ResourceId + "/backupShortTermRetentionPolicies/default"
$policy = Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -ResourceId $resourceId -RetentionDays $retention
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays
$policy = Get-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -ResourceId $resourceId
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays

# Test Piping
$retention = 7
$policy = $db | Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -RetentionDays $retention
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays
$policy = $db | Get-AzureRmSqlDatabaseBackupShortTermRetentionPolicy
Assert-AreEqual 1 $policy.Count
Assert-AreEqual $retention $policy[0].RetentionDays

# Test client-side error handling
try {
$db | Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -RetentionDays $invalidRetention
}
catch [System.Management.Automation.PSArgumentException] {
# We expect an error here
Assert-AreEqual $_.Count 1
}
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Sql/Sql/Az.Sql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
'Get-AzSqlDatabaseImportExportStatus', 'New-AzSqlDatabaseExport',
'New-AzSqlDatabaseImport', 'Get-AzSqlDatabaseGeoBackupPolicy',
'Set-AzSqlDatabaseGeoBackupPolicy',
'Get-AzSqlDatabaseBackupShortTermRetentionPolicy',
'Set-AzSqlDatabaseBackupShortTermRetentionPolicy',
'Get-AzSqlDatabaseBackupLongTermRetentionPolicy',
'Set-AzSqlDatabaseBackupLongTermRetentionPolicy',
'Get-AzSqlDatabaseLongTermRetentionBackup',
Expand Down
1 change: 1 addition & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Add Get/Set AzSqlDatabaseBackupShortTermRetentionPolicy

## Version 1.1.0
* Update incorrect online help URLs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// ----------------------------------------------------------------------------------
//
// 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 Microsoft.Azure.Commands.Sql.Backup.Services;
using Microsoft.Azure.Commands.Sql.Common;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Sql.Backup.Model;
using Microsoft.Azure.Commands.Sql.Database.Model;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;

namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
{
public abstract class AzureSqlDatabaseBackupShortTermRetentionPolicyCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlDatabaseBackupShortTermRetentionPolicyModel>, AzureSqlDatabaseBackupAdapter>
{
/// <summary>
/// The expected number of segments in a short term retention policy resource id.
/// </summary>
private const int BackupShortTermRetentionPolicyResourceIdSegmentsLength = 12;

/// <summary>
/// Parameter set with ResourceGroup name, Server name and Database name.
/// </summary>
protected const string PolicyByResourceServerDatabaseSet = "PolicyByResourceServerDatabaseSet";

/// <summary>
/// Parameter set for using a Database Input Object.
/// </summary>
private const string PolicyByInputObjectSet = "PolicyByInputObjectSet";

/// <summary>
/// Parameter set for using a resource Id.
/// </summary>
private const string PolicyByResourceIdSet = "PolicyByResourceIdSet";

/// <summary>
/// Gets or sets the Database object to get the policy for.
/// </summary>
[Parameter(
ParameterSetName = PolicyByInputObjectSet,
Mandatory = true,
ValueFromPipeline = true,
HelpMessage = "The database object to get the policy for.")]
[ValidateNotNullOrEmpty]
[Alias("AzureSqlDatabase")]
public AzureSqlDatabaseModel AzureSqlDatabaseObject { get; set; }

/// <summary>
/// Gets or sets the Database object to get the policy for.
/// </summary>
[Parameter(
ParameterSetName = PolicyByResourceIdSet,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The short term retention policy resource Id.")]
[ValidateNotNullOrEmpty]
public string ResourceId { get; set; }

/// <summary>
/// Gets or sets the name of the resource group to use.
/// </summary>
[Parameter(
ParameterSetName = PolicyByResourceServerDatabaseSet,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
Position = 0,
HelpMessage = "The name of the resource group.")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public override string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets the name of the database server to use.
/// </summary>
[Parameter(ParameterSetName = PolicyByResourceServerDatabaseSet,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
Position = 1,
HelpMessage = "The name of the Azure SQL Server the database is in.")]
[ValidateNotNullOrEmpty]
public string ServerName { get; set; }

/// <summary>
/// Gets or sets the name of the database to use.
/// </summary>
[Parameter(
ParameterSetName = PolicyByResourceServerDatabaseSet,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
Position = 2,
HelpMessage = "The name of the Azure SQL Database to use.")]
[ValidateNotNullOrEmpty]
public string DatabaseName { get; set; }

/// <summary>
/// Initializes the adapter
/// </summary>
/// <param name="subscription">The subscription to operate on</param>
/// <returns></returns>
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(IAzureSubscription subscription)
{
return new AzureSqlDatabaseBackupAdapter(DefaultProfile.DefaultContext);
}

public override void ExecuteCmdlet()
{
if (AzureSqlDatabaseObject != null)
{
this.ResourceGroupName = AzureSqlDatabaseObject.ResourceGroupName;
this.ServerName = AzureSqlDatabaseObject.ServerName;
this.DatabaseName = AzureSqlDatabaseObject.DatabaseName;
}
else if (!string.IsNullOrEmpty(ResourceId))
{
ParseResourceId();
}

base.ExecuteCmdlet();
}

/// <summary>
/// Helper method to parse resourceGroupName, serverName, databaseName from resourceId
/// Ideally this would utilize Microsoft.Azure.Management.Internal.Resources.Utilities.Models.ResourceIdentifier
/// However, that class is not setup to handle resources that have an ancestry higher than just parent level.
/// That class could recursively create a parent ResourceIdentifier on a __get__ ParentResource,
/// rather than just a string, so a consumer could work all the way to the root resource easily.
/// Leave as a TODO for now, as many other cmdlets consume that class, and I will work on it in separate change.
/// </summary>
private void ParseResourceId()
{
string[] tokens = ResourceId.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

// "/subscriptions/<subId>/resourceGroups/<resourceGroup>/providers/Microsoft.Sql/servers/<server>/databases/<database>/backupShortTermRetentionPolicies/default"
if (tokens.Length != BackupShortTermRetentionPolicyResourceIdSegmentsLength)
{
throw new ArgumentException("Invalid format of the resource identifier.", "ResourceId");
}

// Convert tokens into TYPE:NAME key value pairs, ignoring case
Dictionary<string, string> segments = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
int i = 0;
while (i < tokens.Length)
{
string type = tokens[i++];
string name = tokens[i++];
segments[type] = name;
}

try
{
this.ResourceGroupName = segments["resourceGroups"];
this.ServerName = segments["servers"];
this.DatabaseName = segments["databases"];
}
catch (KeyNotFoundException)
{
throw new ArgumentException(
"Invalid format of the resource identifier. ResourceID should follow format /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Sql/servers/<serverName>/databases/<databaseName>/backupShortTermRetentionPolicies/default");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// ----------------------------------------------------------------------------------
//
// 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.Sql.Backup.Model;

namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
{
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlDatabaseBackupShortTermRetentionPolicy",
DefaultParameterSetName = PolicyByResourceServerDatabaseSet),
OutputType(typeof(AzureSqlDatabaseBackupShortTermRetentionPolicyModel))]
public class GetAzureRmSqlDatabaseBackupShortTermRetentionPolicy : AzureSqlDatabaseBackupShortTermRetentionPolicyCmdletBase
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> GetEntity()
{
ICollection<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> results = new List<AzureSqlDatabaseBackupShortTermRetentionPolicyModel>();

results.Add(ModelAdapter.GetDatabaseBackupShortTermRetentionPolicy(
ResourceGroupName,
ServerName,
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<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> ApplyUserInputToModel(
IEnumerable<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> 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<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> PersistChanges(
IEnumerable<AzureSqlDatabaseBackupShortTermRetentionPolicyModel> entity)
{
return entity;
}
}
}
Loading