Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/Sql/Sql.Management.Sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use-extension:

###
``` yaml
commit: 37d29762e68000fc58e03dcefada999f3876f0af
commit: 51eb4a8df996ebb94c835bf93e08a7001e5d5459
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pipeline validation tool detected the SDK does not match the commit in this README.md, please either regenerate or make sure what generated in your local was uploaded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

input-file:
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/stable/2014-04-01/metrics.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/stable/2014-04-01-legacy/sql.core_legacy.json
Expand Down Expand Up @@ -107,6 +107,8 @@ input-file:
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/RecoverableDatabases.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/RestorableDroppedDatabases.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-05-01-preview/InstanceFailoverGroups.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2024-11-01-preview/Servers.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2024-11-01-preview/DeletedServers.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/ServerConfigurationOptions.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/ManagedLedgerDigestUploads.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/ManagedDatabases.json
Expand All @@ -117,7 +119,6 @@ input-file:
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2022-08-01-preview/StartStopManagedInstanceSchedules.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2024-05-01-preview/DatabaseSchemas.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2024-05-01-preview/Databases.json
- https://github.com/Azure/azure-rest-api-specs/blob/$(commit)/specification/sql/resource-manager/Microsoft.Sql/preview/2024-05-01-preview/Servers.json

output-folder: Generated

Expand Down
3 changes: 2 additions & 1 deletion src/Sql/Sql/Az.Sql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ CmdletsToExport = 'Add-AzSqlDatabaseToFailoverGroup', 'Add-AzSqlElasticJobStep',
'Remove-AzSqlServerVirtualNetworkRule', 'Remove-AzSqlSyncAgent',
'Remove-AzSqlSyncGroup', 'Remove-AzSqlSyncMember',
'Remove-AzSqlVirtualCluster', 'Restore-AzSqlDatabase',
'Restore-AzSqlInstanceDatabase', 'Resume-AzSqlDatabase',
'Restore-AzSqlInstanceDatabase',
'Restore-AzSqlServer', 'Resume-AzSqlDatabase',
'Set-AzSqlDatabase', 'Set-AzSqlDatabaseAdvisorAutoExecuteStatus',
'Set-AzSqlDatabaseAudit',
'Set-AzSqlDatabaseBackupLongTermRetentionPolicy',
Expand Down
7 changes: 7 additions & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
- Additional information about change #1
-->
## Upcoming Release
* Updated `New-AzSqlServer` to support soft-delete retention
- Added `EnableSoftDeleteRetention` parameter to `New-AzSqlServer` to enable creation of a server with soft-delete retention
- Added `SoftDeleteRetentionDays` parameter to `New-AzSqlServer` to set the soft-delete retention period (in days)
* Updated `Set-AzSqlServer` to support soft-delete retention
- Added `EnableSoftDeleteRetention` parameter to `Set-AzSqlServer` to enable or disable soft-delete retention on an existing server
- Added `SoftDeleteRetentionDays` parameter to `Set-AzSqlServer` to update the soft-delete retention period (in days) on an existing server
* Added `Restore-AzSqlServer` cmdlet to restore a deleted Azure SQL server within the retention period

## Version 6.1.0
* Fixed `New-AzSqlDatabaseExport` and `New-AzSqlDatabaseImport` to use SQL Authentication with ManagedIdentity without any mandatory password
Expand Down
18 changes: 18 additions & 0 deletions src/Sql/Sql/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Sql/Sql/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@
<data name="MissingSQLAdministratorCredentials" xml:space="preserve">
<value>SQL Administrator Credentials are only optional when Azure Active Directory Only Authentication is enabled via -EnableActiveDirectoryOnlyAuthentication.</value>
</data>
<data name="MissingEnableSoftDeleteRetention" xml:space="preserve">
<value>EnableSoftDeleteRetention must be provided when SoftDeleteRetentionDays is set to a value greater than 0.</value>
</data>
<data name="RemoveAzureSqlServerOutboundFirewallRuleDescription" xml:space="preserve">
<value>Permanently removing allowed FQDN '{0}' from the list of Outbound Firewall Rules (Allowed FQDNs) for Azure Sql Database Server '{1}'.</value>
</data>
Expand Down Expand Up @@ -766,4 +769,7 @@
<data name="ForwardMigrationWithGeoDRInfo" xml:space="preserve">
<value>Changing the service tier to Hyperscale also converts the geo-secondary replica to Hyperscale. For more information, see https://go.microsoft.com/fwlink/?linkid=2314103</value>
</data>
<data name="DeletedServerNotFound" xml:space="preserve">
<value>No deleted server named '{0}' found in resource group '{1}' that can be restored.</value>
</data>
</root>
24 changes: 23 additions & 1 deletion src/Sql/Sql/Server/Cmdlet/NewAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ public class NewAzureSqlServer : AzureSqlServerCmdletBase
HelpMessage = "Specifies the Federated client ID of the server when using Cross-Tenant CMK, Do not set this value if you do not intent to use Cross-Tenant CMK")]
public Guid? FederatedClientId { get; set; }

/// <summary>
/// Boolean Value for enabling Soft Delete Retention for server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specify whether to enable soft-delete retention for the server. When enabled, a dropped server can be restored within the retention window (defaults to 7 days if not specified). To set a custom retention period use -SoftDeleteRetentionDays.")]
public bool EnableSoftDeleteRetention { get; set; } = false;

/// <summary>
/// Soft-delete retention days for the server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specifies the number of days to retain a deleted server for possible restoration. Valid values are 0-35. A value of 0 disables soft-delete retention. If EnableSoftDeleteRetention is set without an explicit value, the default retention is 7 days.")]
[ValidateRange(0, 35)]
public int? SoftDeleteRetentionDays { get; set; }

/// <summary>
/// Overriding to add warning message
/// </summary>
Expand All @@ -173,6 +188,12 @@ public override void ExecuteCmdlet()
throw new PSArgumentException(Properties.Resources.MissingSQLAdministratorCredentials, "SqlAdministratorCredentials");
}

// SoftDeleteRetentionDays depends on EnableSoftDeleteRetention; if days are provided but soft-delete is not enabled, fail early.
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDeleteRetention)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should SoftDeleteRetentionDays <= 0 be accepted if EnableSoftDeletionRetention is not supplied or set as false? I ask this purely from a command line user experience behaviour. Should the dependent parameter in absence of its parent always be rejected?

{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteRetention, "EnableSoftDeleteRetention");
}

base.ExecuteCmdlet();
}

Expand Down Expand Up @@ -238,7 +259,8 @@ public override void ExecuteCmdlet()
AzureAdOnlyAuthentication = (this.EnableActiveDirectoryOnlyAuthentication.IsPresent) ? (bool?)true : null,
Login = this.ExternalAdminName,
Sid = this.ExternalAdminSID
}
},
SoftDeleteRetentionDays = (this.EnableSoftDeleteRetention && !this.SoftDeleteRetentionDays.HasValue) ? 7 : this.SoftDeleteRetentionDays
});
return newEntity;
}
Expand Down
148 changes: 148 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/RestoreAzureSqlServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// ----------------------------------------------------------------------------------
//
// 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 Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Commands.Sql.Common;
using Microsoft.Azure.Commands.Sql.Server.Model;
using Microsoft.Rest.Azure;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Sql.Server.Cmdlet
{
/// <summary>
/// Defines the Restore-AzSqlServer cmdlet
/// </summary>
[Cmdlet("Restore", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlServer", ConfirmImpact = ConfirmImpact.Low, SupportsShouldProcess = true), OutputType(typeof(Model.AzureSqlServerModel))]
public class RestoreAzureSqlServer : AzureSqlServerCmdletBase
{
/// <summary>
/// Gets or sets the name of the database server to use.
/// </summary>
[Parameter(Mandatory = true,
HelpMessage = "SQL Database server name.")]
[Alias("Name")]
[ValidateNotNullOrEmpty]
public string ServerName { get; set; }

/// <summary>
/// The location in which to create the server
/// </summary>
[Parameter(Mandatory = true,
HelpMessage = "The location in which to create the server")]
[LocationCompleter("Microsoft.Sql/servers")]
[ValidateNotNullOrEmpty]
public string Location { get; set; }

/// <summary>
/// Check to see if the server already exists as a live server or if there's a deleted server to restore.
/// </summary>
/// <returns>Null if ready to restore. Otherwise throws exception</returns>
protected override IEnumerable<Model.AzureSqlServerModel> GetEntity()
{
// First check if a live server already exists
try
{
bool serverExists = ModelAdapter.ListServers().Any(s =>
string.Equals(s.ResourceGroupName, this.ResourceGroupName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(s.ServerName, this.ServerName, StringComparison.OrdinalIgnoreCase));

if (serverExists)
{
// If we get here, a live server exists - cannot restore
throw new PSArgumentException(
string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ServerNameExists, this.ServerName),
"ServerName");
}
}
catch (CloudException ex)
{
if (ex.Response?.StatusCode != System.Net.HttpStatusCode.NotFound)
{
// Unexpected exception encountered
throw;
}
//Continue - no live server exists, which is what we want
}

// Now check if there's a deleted server to restore
try
{
var deletedServer = ModelAdapter.GetDeletedServer(this.ResourceGroupName, this.ServerName);
if (deletedServer == null)
{
throw new PSArgumentException(
string.Format(Properties.Resources.DeletedServerNotFound,
this.ServerName, this.ResourceGroupName),
"ServerName");
}

// Deleted server exists and can be restored
return null;
}
catch (CloudException ex)
{
if (ex.Response?.StatusCode == System.Net.HttpStatusCode.NotFound)
{
throw new PSArgumentException(
string.Format(Properties.Resources.DeletedServerNotFound,
this.ServerName, this.ResourceGroupName),
"ServerName");
}

// Unexpected exception encountered
throw;
}
}

/// <summary>
/// Generates the model from user input.
/// </summary>
/// <param name="model">This is null since the server doesn't exist yet</param>
/// <returns>The generated model from user input</returns>
protected override IEnumerable<Model.AzureSqlServerModel> ApplyUserInputToModel(IEnumerable<Model.AzureSqlServerModel> model)
{
if (!Sql.Services.Util.ValidateServerName(this.ServerName))
{
throw new PSArgumentException(string.Format(Properties.Resources.ServerNameInvalid, this.ServerName), "ServerName");
}

List<Model.AzureSqlServerModel> newEntity = new List<Model.AzureSqlServerModel>();
newEntity.Add(new Model.AzureSqlServerModel()
{
Location = this.Location,
ResourceGroupName = this.ResourceGroupName,
ServerName = this.ServerName,
CreateMode = "Restore"
});
return newEntity;
}

/// <summary>
/// Sends the changes to the service -> Creates the server
/// </summary>
/// <param name="entity">The server to create</param>
/// <returns>The created server</returns>
protected override IEnumerable<Model.AzureSqlServerModel> PersistChanges(IEnumerable<Model.AzureSqlServerModel> entity)
{
return new List<Model.AzureSqlServerModel>() {
Copy link

@pranavathalye pranavathalye Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Assert that entity.Count == 1?

ModelAdapter.UpsertServer(entity.First())
};
}
}
}
47 changes: 47 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,41 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
[PSArgumentCompleter("SystemAssigned", "UserAssigned", "\"SystemAssigned,UserAssigned\"", "None")]
public string IdentityType { get; set; }

/// <summary>
/// Boolean for enabling Soft Delete Retention for server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specify whether to enable soft-delete retention for the server. When enabled, a dropped server can be restored within the retention window (defaults to 7 days if not specified). To set a custom retention period use -SoftDeleteRetentionDays.")]
[PSArgumentCompleter("true", "false")]
public bool EnableSoftDeleteRetention { get; set; } = false;

/// <summary>
/// Value for soft-delete retention days for the server.
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specifies the number of days to retain a deleted server for possible restoration. Valid values are 0-35. A value of 0 disables soft-delete retention. If EnableSoftDeleteRetention is set without an explicit value, the default retention is 7 days.")]
[ValidateRange(0, 35)]
public int? SoftDeleteRetentionDays { get; set; }

/// <summary>
/// Defines whether it is ok to skip the requesting of rule removal confirmation
/// </summary>
[Parameter(HelpMessage = "Skip confirmation message for performing the action")]
public SwitchParameter Force { get; set; }

/// <summary>
/// Overriding to add warning message
/// </summary>
public override void ExecuteCmdlet()
{
// SoftDeleteRetentionDays depends on EnableSoftDeleteRetention; if days are provided but soft-delete is not enabled, fail early.
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDeleteRetention)
{
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteRetention, "EnableSoftDeleteRetention");
}

base.ExecuteCmdlet();
}
/// <summary>
/// Get the server to update
/// </summary>
Expand Down Expand Up @@ -169,6 +198,24 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
updateData[0].PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId ?? model.FirstOrDefault().PrimaryUserAssignedIdentityId;
updateData[0].KeyId = this.KeyId ?? updateData[0].KeyId;
updateData[0].FederatedClientId = this.FederatedClientId ?? updateData[0].FederatedClientId;
if (this.EnableSoftDeleteRetention)
{
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
updateData[0].SoftDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
}
else
{
// If not enabling, only explicitly set retention to 0 when the caller provided 0.
// Otherwise, leave as null so the service preserves the existing retention setting.
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays.Value == 0)
Copy link

@pranavathalye pranavathalye Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a special case to allow user to reset SoftDeleteRetentionDays to 0, which effectively disables soft delete?

  1. Is changing of retention days via this cmdlet not allowed?
  2. If setting retention to 0 days is used as means to disallow soft deletion -
    2a. How does user enable it back?
    2b. Why not use a parameter DisableSoftDelete or DisableSoftDeleteRetention?

{
updateData[0].SoftDeleteRetentionDays = 0;
}
else
{
updateData[0].SoftDeleteRetentionDays = null;
}
}

return updateData;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Sql/Sql/Server/Model/AzureSqlServerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,15 @@ public class AzureSqlServerModel
/// Gets or sets a federated client id to use in xtcmk scenario
/// </summary>
public Guid? FederatedClientId { get; set; }

/// <summary>
/// Gets or sets a value for the soft-delete retention days for the server
/// </summary>
public int? SoftDeleteRetentionDays { get; set; }

/// <summary>
/// Sets a value for the create mode for the server
/// </summary>
public string CreateMode { get; set; } = "Normal";
}
}
Loading