Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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-AzSqlInstance` to support retention
- Added `EnableSoftDeleteRetention` parameter to `New-AzSqlInstance` to enable the creation of instances with retention period enabled
- Added `SoftDeleteRetentionInDays` parameter to `New-AzSqlInstance` to enable the creation of instances with custom retention period
* Updated `Set-AzSqlInstance` to support retention
- Added `EnableSoftDeleteRetention` parameter to `Set-AzSqlInstance` to enable/disable retention period on existing instances
- Added `SoftDeleteRetentionInDays` parameter to `Set-AzSqlInstance` to enable the update of retention period on existing instances
* Added `Restore-AzSqlServer` cmdlet to restore a deleted Azure Sql servers 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
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,27 @@ 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>
/// Soft-delete retention days for the server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specifies whether or not soft-delete retention is enabled for the server.")]
public bool EnableSoftDeleteRetention { get; set; } = false;

/// <summary>
/// Soft-delete retention days for the server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specifies the soft-delete retention days for the server.")]
public int? SoftDeleteRetentionDays { get; set; }

/// <summary>
/// Soft-delete retention days for the server
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Specifies the create mode for the server, valid values for this parameter are \"Normal\" and \"Restore\".")]
public string CreateMode { get; set; } = "Normal";

/// <summary>
/// Overriding to add warning message
/// </summary>
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
}
},
RetentionDays = (this.EnableSoftDeleteRetention && !this.SoftDeleteRetentionDays.HasValue) ? 7 : this.SoftDeleteRetentionDays

Choose a reason for hiding this comment

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

Are we still covered for the scenario of SoftDeleteRetentionDays being null and EnableSoftDeleteRetention not being passed (i.e. using default value of false)?

});
return newEntity;
}
Expand Down
161 changes: 161 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/RestoreAzureSqlServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// ----------------------------------------------------------------------------------
//
// 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>
/// Overriding to add warning message
/// </summary>
public override void ExecuteCmdlet()
{
if (this.ServerName == "")
{
throw new PSArgumentException("Missing ServerName");
}

base.ExecuteCmdlet();
}

/// <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("No deleted server named '{0}' found in resource group '{1}' that can be restored.",
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("No deleted server named '{0}' found in resource group '{1}' that can be restored.",
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())
};
}
}
}
17 changes: 17 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
[PSArgumentCompleter("SystemAssigned", "UserAssigned", "\"SystemAssigned,UserAssigned\"", "None")]
public string IdentityType { get; set; }

/// <summary>
/// Value for soft-delete retention days for the server.
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Boolean value for whether or not to enable soft-delete for the server such that the server can be restored for a default of 7 days after dropping. If you want to specify a different retention period, use the RetentionDays parameter.")]
[PSArgumentCompleter("true", "false")]
public bool EnableSoftDeleteRetention { get; set; } = false;

/// <summary>
/// Value for soft-delete retention days for the server.
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "Value for soft-delete retention days for the server such that the server can be restored for the specified number of days after dropping. Only valid values are from 0 to 35. If set to 0, soft-delete retention is disabled.")]
[PSArgumentCompleter("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")]
public int? RetentionDays { get; set; }

/// <summary>
/// Defines whether it is ok to skip the requesting of rule removal confirmation
/// </summary>
Expand Down Expand Up @@ -169,6 +185,7 @@ 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;
updateData[0].RetentionDays = this.EnableSoftDeleteRetention ? (this.RetentionDays ?? 7) : (int?)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? RetentionDays { get; set; }

/// <summary>
/// Sets a value for the create mode for the server
/// </summary>
public string CreateMode { get; set; } = "Normal";
}
}
16 changes: 15 additions & 1 deletion src/Sql/Sql/Server/Services/AzureSqlServerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public AzureSqlServerModel UpsertServer(AzureSqlServerModel model)
Administrators = GetActiveDirectoryInformation(model.Administrators),
PrimaryUserAssignedIdentityId = model.PrimaryUserAssignedIdentityId,
KeyId = model.KeyId,
FederatedClientId = model.FederatedClientId
FederatedClientId = model.FederatedClientId,
RetentionDays = model.RetentionDays,
CreateMode = model.CreateMode,
});

return CreateServerModelFromResponse(resp);
Expand Down Expand Up @@ -193,6 +195,7 @@ private static AzureSqlServerModel CreateServerModelFromResponse(Management.Sql.
server.PrimaryUserAssignedIdentityId = resp.PrimaryUserAssignedIdentityId;
server.KeyId = resp.KeyId;
server.FederatedClientId = resp.FederatedClientId;
server.RetentionDays = resp.RetentionDays;

return server;
}
Expand Down Expand Up @@ -409,5 +412,16 @@ protected Guid GetTenantId()

return tenantIdGuid;
}
/// <summary>
/// Gets a deleted server in a resource group
/// </summary>
/// <param name="resourceGroupName">The name of the resource group</param>
/// <param name="serverName">The name of the deleted server</param>
/// <param name="subscriptionId">The subscription id of the server.</param>
/// <returns>The deleted server information</returns>
public Management.Sql.Models.DeletedServer GetDeletedServer(string resourceGroupName, string serverName, string subscriptionId = null)
{
return Communicator.GetDeleted(resourceGroupName, serverName, subscriptionId);
}
}
}
8 changes: 8 additions & 0 deletions src/Sql/Sql/Server/Services/AzureSqlServerCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public void Remove(string resourceGroupName, string serverName)
GetCurrentSqlClient().Servers.Delete(resourceGroupName, serverName);
}

/// <summary>
/// Gets the deleted Azure Sql Database Server
/// </summary>
public Management.Sql.Models.DeletedServer GetDeleted(string resourceGroupName, string serverName, string subscriptionId = null)
{
return GetCurrentSqlClient(subscriptionId).DeletedServers.Get(resourceGroupName, serverName);
}

/// <summary>
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
/// id tracing headers for the current cmdlet invocation.
Expand Down
Loading