-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
caf31a9
bdc66f7
caf1888
423f32c
c819b27
4a14bfd
7c09029
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
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 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"; | ||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Overriding to add warning message | ||
/// </summary> | ||
|
@@ -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 | ||
|
||
}); | ||
return newEntity; | ||
} | ||
|
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"); | ||
} | ||
|
||
rambabu-yalla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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) && | ||
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("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>(); | ||
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()) | ||
}; | ||
} | ||
} | ||
} |
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