diff --git a/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/AzureSqlDatabaseRestorePointCmdletBase.cs b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/AzureSqlDatabaseRestorePointCmdletBase.cs new file mode 100644 index 000000000000..31845874cf41 --- /dev/null +++ b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/AzureSqlDatabaseRestorePointCmdletBase.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------- +// +// 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; +using Microsoft.Azure.Commands.Sql.Backup.Services; +using Microsoft.Azure.Commands.Sql.Common; +using Microsoft.Azure.Commands.Sql.Database.Model; +using Microsoft.Azure.Commands.Sql.Database.Services; + +namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet +{ + public abstract class AzureSqlDatabaseRestorePointCmdletBase + : AzureSqlCmdletBase, AzureSqlDatabaseBackupAdapter> + { + /// + /// Gets or sets the name of the database server to use. + /// + [Parameter(Mandatory = true, + ValueFromPipelineByPropertyName = true, + Position = 1, + HelpMessage = "The name of the Azure SQL Database Server the database is in.")] + [ValidateNotNullOrEmpty] + public string ServerName { get; set; } + + /// + /// Gets or sets the name of the database to use. + /// + [Parameter(Mandatory = true, + ValueFromPipelineByPropertyName = true, + Position = 2, + HelpMessage = "The name of the Azure SQL Database to retrieve restore points from.")] + [ValidateNotNullOrEmpty] + public string DatabaseName { get; set; } + + /// + /// Initializes the adapter + /// + /// + /// + protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(Azure.Common.Authentication.Models.AzureSubscription subscription) + { + return new AzureSqlDatabaseBackupAdapter(Profile, subscription); + } + } +} diff --git a/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/GetAzureSqlDatabaseRestorePoints.cs b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/GetAzureSqlDatabaseRestorePoints.cs new file mode 100644 index 000000000000..0cdaa64a81fc --- /dev/null +++ b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Cmdlet/GetAzureSqlDatabaseRestorePoints.cs @@ -0,0 +1,56 @@ +// ---------------------------------------------------------------------------------- +// +// 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; +using Microsoft.Azure.Commands.Sql.Database.Model; + +namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet +{ + [Cmdlet(VerbsCommon.Get, "AzureSqlDatabaseRestorePoints", + ConfirmImpact = ConfirmImpact.None)] + public class GetAzureSqlDatabaseRestorePoints : AzureSqlDatabaseRestorePointCmdletBase + { + /// + /// Get the entities from the service + /// + /// The list of entities + protected override IEnumerable GetEntity() + { + return ModelAdapter.ListRestorePoints(this.ResourceGroupName, this.ServerName, this.DatabaseName); + } + + /// + /// No user input to apply to model + /// + /// Model retrieved from service + /// The model that was passed in + protected override IEnumerable ApplyUserInputToModel(IEnumerable model) + { + return model; + } + + /// + /// No changes to persist to server + /// + /// The output of apply user input to model + /// The input entity + protected override IEnumerable PersistChanges(IEnumerable entity) + { + return entity; + } + } +} diff --git a/src/ResourceManager/Sql/Commands.Sql/Database Backup/Model/AzureSqlDatabaseRestorePointModel.cs b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Model/AzureSqlDatabaseRestorePointModel.cs new file mode 100644 index 000000000000..226b2d740b6d --- /dev/null +++ b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Model/AzureSqlDatabaseRestorePointModel.cs @@ -0,0 +1,67 @@ +// ---------------------------------------------------------------------------------- +// +// 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; + +namespace Microsoft.Azure.Commands.Sql.Backup.Model +{ + /// + /// Represents an Azure Sql Database restore point + /// + public class AzureSqlDatabaseRestorePointModel + { + /// + /// Gets or sets the name of the resource group + /// + public string ResourceGroupName { get; set; } + + /// + /// Gets or sets the name of the server + /// + public string ServerName { get; set; } + + /// + /// Gets or sets the name of the database + /// + public string DatabaseName { get; set; } + + /// + /// Gets or sets the location of the database + /// + public string Location { get; set; } + + /// + /// Gets the restore point type of the Azure SQL Database restore point. Possible values are: DISCRETE and CONTINUOUS. + /// + public string RestorePointType { get; set; } + + /// + /// Earliest restore time. Populated when restorePointType = CONTINUOUS. Null otherwise. + /// + public DateTime? RestorePointCreationDate { get; set; } + + /// + /// Earliest restore time. Populated when restorePointType = DISCRETE. Null otherwise. + /// + public DateTime? EarliestRestoreDate { get; set; } + + /// + /// Size of the backup in blob storage. + /// For discrete restore point, the value is the database snap size; for continuous restore point, the value is the + /// total backup storage usage of the database. + /// + public int SizeBytes { get; set; } + } +} diff --git a/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupAdapter.cs b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupAdapter.cs new file mode 100644 index 000000000000..f6a9cf3adc68 --- /dev/null +++ b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupAdapter.cs @@ -0,0 +1,92 @@ +// ---------------------------------------------------------------------------------- +// +// 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 System.Globalization; +using System.Linq; + +using Microsoft.Azure.Commands.Sql.Backup.Model; +using Microsoft.Azure.Commands.Sql.Common; +using Microsoft.Azure.Commands.Sql.Database.Model; +using Microsoft.Azure.Commands.Sql.Database.Services; +using Microsoft.Azure.Commands.Sql.ElasticPool.Services; +using Microsoft.Azure.Commands.Sql.Properties; +using Microsoft.Azure.Commands.Sql.Server.Adapter; +using Microsoft.Azure.Commands.Sql.Services; +using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.Azure.Management.Sql; +using Microsoft.Azure.Management.Sql.Models; + +namespace Microsoft.Azure.Commands.Sql.Backup.Services +{ + /// + /// Adapter for database backup operations + /// + public class AzureSqlDatabaseBackupAdapter + { + /// + /// Gets or sets the AzureSqlDatabaseBackupCommunicator which has all the needed management clients + /// + private AzureSqlDatabaseBackupCommunicator Communicator { get; set; } + + /// + /// Gets or sets the Azure profile + /// + public AzureProfile Profile { get; set; } + + /// + /// Gets or sets the Azure Subscription + /// + private AzureSubscription _subscription { get; set; } + + /// + /// Constructs a database backup adapter + /// + /// The current azure profile + /// The current azure subscription + public AzureSqlDatabaseBackupAdapter(AzureProfile Profile, AzureSubscription subscription) + { + this.Profile = Profile; + this._subscription = subscription; + Communicator = new AzureSqlDatabaseBackupCommunicator(Profile, subscription); + } + + /// + /// Lists the restore points for a given Sql Azure Database. + /// + /// The name of the resource group + /// The name of the Azure SQL Server + /// The name of the Azure SQL database + /// List of restore points + internal IEnumerable ListRestorePoints(string resourceGroup, string serverName, string databaseName) + { + var resp = Communicator.ListRestorePoints(resourceGroup, serverName, databaseName, Util.GenerateTracingId()); + return resp.Select((restorePoint) => + { + return new AzureSqlDatabaseRestorePointModel() + { + ResourceGroupName = resourceGroup, + ServerName = serverName, + DatabaseName = databaseName, + Location = restorePoint.Location, + RestorePointType = restorePoint.Properties.RestorePointType, + RestorePointCreationDate = restorePoint.Properties.RestorePointCreationDate, + EarliestRestoreDate = restorePoint.Properties.EarliestRestoreDate, + SizeBytes = restorePoint.Properties.SizeBytes + }; + }).ToList(); + } + } +} diff --git a/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupCommunicator.cs b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupCommunicator.cs new file mode 100644 index 000000000000..a56dd2597220 --- /dev/null +++ b/src/ResourceManager/Sql/Commands.Sql/Database Backup/Services/AzureSqlDatabaseBackupCommunicator.cs @@ -0,0 +1,92 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Common.Authentication; +using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.Azure.Management.Resources; +using Microsoft.Azure.Management.Sql; +using Microsoft.Azure.Management.Sql.Models; +using Microsoft.Azure.Commands.Sql.Common; +using Microsoft.WindowsAzure.Management.Storage; + +namespace Microsoft.Azure.Commands.Sql.Backup.Services +{ + /// + /// This class is responsible for all the REST communication with the database backup REST endpoints. + /// + public class AzureSqlDatabaseBackupCommunicator + { + /// + /// The Sql client to be used by this end points communicator + /// + private static SqlManagementClient SqlClient { get; set; } + + /// + /// Gets or set the Azure subscription + /// + private static AzureSubscription Subscription { get; set; } + + /// + /// Gets or sets the Azure profile + /// + public AzureProfile Profile { get; set; } + + /// + /// Creates a communicator for Azure Sql Database backup REST endpoints. + /// + /// Azure profile + /// Associated subscription + public AzureSqlDatabaseBackupCommunicator(AzureProfile profile, AzureSubscription subscription) + { + Profile = profile; + if (subscription != Subscription) + { + Subscription = subscription; + SqlClient = null; + } + } + + /// + /// Lists the restore points for a given Sql Azure Database. + /// + /// The name of the resource group + /// The name of the Azure SQL Server + /// The name of the Azure SQL database + /// List of restore points + public IList ListRestorePoints(string resourceGroupName, string serverName, string databaseName, string clientRequestId) + { + return GetCurrentSqlClient(clientRequestId).Backup.ListRestorePoints(resourceGroupName, serverName, databaseName).RestorePoints; + } + + /// + /// Retrieve the SQL Management client for the currently selected subscription, adding the session and request + /// id tracing headers for the current cmdlet invocation. + /// + /// The SQL Management client for the currently selected subscription. + private SqlManagementClient GetCurrentSqlClient(String clientRequestId) + { + // Get the SQL management client for the current subscription + if (SqlClient == null) + { + SqlClient = AzureSession.ClientFactory.CreateClient(Profile, Subscription, AzureEnvironment.Endpoint.ResourceManager); + } + SqlClient.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName); + SqlClient.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, clientRequestId); + return SqlClient; + } + } +} \ No newline at end of file