Skip to content

Commit

Permalink
Adding Get-AzureSqlDatabaseRestorePoints powershell support
Browse files Browse the repository at this point in the history
  • Loading branch information
caln-microsoft committed Jun 19, 2015
1 parent a054ea6 commit 72858ce
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<AzureSqlDatabaseRestorePointModel>, AzureSqlDatabaseBackupAdapter>
{
/// <summary>
/// Gets or sets the name of the database server to use.
/// </summary>
[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; }

/// <summary>
/// Gets or sets the name of the database to use.
/// </summary>
[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; }

/// <summary>
/// Initializes the adapter
/// </summary>
/// <param name="subscription"></param>
/// <returns></returns>
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(Azure.Common.Authentication.Models.AzureSubscription subscription)
{
return new AzureSqlDatabaseBackupAdapter(Profile, subscription);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Get the entities from the service
/// </summary>
/// <returns>The list of entities</returns>
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> GetEntity()
{
return ModelAdapter.ListRestorePoints(this.ResourceGroupName, this.ServerName, this.DatabaseName);
}

/// <summary>
/// No user input to apply to model
/// </summary>
/// <param name="model">Model retrieved from service</param>
/// <returns>The model that was passed in</returns>
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseRestorePointModel> model)
{
return model;
}

/// <summary>
/// No changes to persist to server
/// </summary>
/// <param name="entity">The output of apply user input to model</param>
/// <returns>The input entity</returns>
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> PersistChanges(IEnumerable<AzureSqlDatabaseRestorePointModel> entity)
{
return entity;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Represents an Azure Sql Database restore point
/// </summary>
public class AzureSqlDatabaseRestorePointModel
{
/// <summary>
/// Gets or sets the name of the resource group
/// </summary>
public string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets the name of the server
/// </summary>
public string ServerName { get; set; }

/// <summary>
/// Gets or sets the name of the database
/// </summary>
public string DatabaseName { get; set; }

/// <summary>
/// Gets or sets the location of the database
/// </summary>
public string Location { get; set; }

/// <summary>
/// Gets the restore point type of the Azure SQL Database restore point. Possible values are: DISCRETE and CONTINUOUS.
/// </summary>
public string RestorePointType { get; set; }

/// <summary>
/// Earliest restore time. Populated when restorePointType = CONTINUOUS. Null otherwise.
/// </summary>
public DateTime? RestorePointCreationDate { get; set; }

/// <summary>
/// Earliest restore time. Populated when restorePointType = DISCRETE. Null otherwise.
/// </summary>
public DateTime? EarliestRestoreDate { get; set; }

/// <summary>
/// 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.
/// </summary>
public int SizeBytes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Adapter for database backup operations
/// </summary>
public class AzureSqlDatabaseBackupAdapter
{
/// <summary>
/// Gets or sets the AzureSqlDatabaseBackupCommunicator which has all the needed management clients
/// </summary>
private AzureSqlDatabaseBackupCommunicator Communicator { get; set; }

/// <summary>
/// Gets or sets the Azure profile
/// </summary>
public AzureProfile Profile { get; set; }

/// <summary>
/// Gets or sets the Azure Subscription
/// </summary>
private AzureSubscription _subscription { get; set; }

/// <summary>
/// Constructs a database backup adapter
/// </summary>
/// <param name="profile">The current azure profile</param>
/// <param name="subscription">The current azure subscription</param>
public AzureSqlDatabaseBackupAdapter(AzureProfile Profile, AzureSubscription subscription)
{
this.Profile = Profile;
this._subscription = subscription;
Communicator = new AzureSqlDatabaseBackupCommunicator(Profile, subscription);
}

/// <summary>
/// Lists the restore points for a given Sql Azure Database.
/// </summary>
/// <param name="resourceGroup">The name of the resource group</param>
/// <param name="serverName">The name of the Azure SQL Server</param>
/// <param name="databaseName">The name of the Azure SQL database</param>
/// <returns>List of restore points</returns>
internal IEnumerable<AzureSqlDatabaseRestorePointModel> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This class is responsible for all the REST communication with the database backup REST endpoints.
/// </summary>
public class AzureSqlDatabaseBackupCommunicator
{
/// <summary>
/// The Sql client to be used by this end points communicator
/// </summary>
private static SqlManagementClient SqlClient { get; set; }

/// <summary>
/// Gets or set the Azure subscription
/// </summary>
private static AzureSubscription Subscription { get; set; }

/// <summary>
/// Gets or sets the Azure profile
/// </summary>
public AzureProfile Profile { get; set; }

/// <summary>
/// Creates a communicator for Azure Sql Database backup REST endpoints.
/// </summary>
/// <param name="profile">Azure profile</param>
/// <param name="subscription">Associated subscription</param>
public AzureSqlDatabaseBackupCommunicator(AzureProfile profile, AzureSubscription subscription)
{
Profile = profile;
if (subscription != Subscription)
{
Subscription = subscription;
SqlClient = null;
}
}

/// <summary>
/// Lists the restore points for a given Sql Azure Database.
/// </summary>
/// <param name="resourceGroup">The name of the resource group</param>
/// <param name="serverName">The name of the Azure SQL Server</param>
/// <param name="databaseName">The name of the Azure SQL database</param>
/// <returns>List of restore points</returns>
public IList<Management.Sql.Models.RestorePoint> ListRestorePoints(string resourceGroupName, string serverName, string databaseName, string clientRequestId)
{
return GetCurrentSqlClient(clientRequestId).Backup.ListRestorePoints(resourceGroupName, serverName, databaseName).RestorePoints;
}

/// <summary>
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
/// id tracing headers for the current cmdlet invocation.
/// </summary>
/// <returns>The SQL Management client for the currently selected subscription.</returns>
private SqlManagementClient GetCurrentSqlClient(String clientRequestId)
{
// Get the SQL management client for the current subscription
if (SqlClient == null)
{
SqlClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(Profile, Subscription, AzureEnvironment.Endpoint.ResourceManager);
}
SqlClient.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName);
SqlClient.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, clientRequestId);
return SqlClient;
}
}
}

0 comments on commit 72858ce

Please sign in to comment.