-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Introduced changes to cmdlets to support self-server restore of sql logical server #28589
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
Changes from 2 commits
caf31a9
bdc66f7
caf1888
423f32c
c819b27
4a14bfd
7c09029
481f6be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/// <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> | ||
|
@@ -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) | ||
|
||
{ | ||
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteRetention, "EnableSoftDeleteRetention"); | ||
} | ||
|
||
base.ExecuteCmdlet(); | ||
} | ||
|
||
|
@@ -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 | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
return newEntity; | ||
} | ||
|
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) && | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
string.Equals(s.ServerName, this.ServerName, StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (serverExists) | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
// 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); | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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>(); | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
newEntity.Add(new Model.AzureSqlServerModel() | ||
{ | ||
Location = this.Location, | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Assert that |
||
ModelAdapter.UpsertServer(entity.First()) | ||
}; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/// <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) | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteRetention, "EnableSoftDeleteRetention"); | ||
} | ||
|
||
base.ExecuteCmdlet(); | ||
} | ||
/// <summary> | ||
/// Get the server to update | ||
/// </summary> | ||
|
@@ -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) | ||
|
||
{ | ||
updateData[0].SoftDeleteRetentionDays = 0; | ||
} | ||
else | ||
{ | ||
updateData[0].SoftDeleteRetentionDays = null; | ||
} | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
return updateData; | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed