Skip to content

Commit 72858ce

Browse files
Adding Get-AzureSqlDatabaseRestorePoints powershell support
1 parent a054ea6 commit 72858ce

File tree

5 files changed

+365
-0
lines changed

5 files changed

+365
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections.Generic;
16+
using System.Management.Automation;
17+
using Microsoft.Azure.Commands.Sql.Backup.Model;
18+
using Microsoft.Azure.Commands.Sql.Backup.Services;
19+
using Microsoft.Azure.Commands.Sql.Common;
20+
using Microsoft.Azure.Commands.Sql.Database.Model;
21+
using Microsoft.Azure.Commands.Sql.Database.Services;
22+
23+
namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
24+
{
25+
public abstract class AzureSqlDatabaseRestorePointCmdletBase
26+
: AzureSqlCmdletBase<IEnumerable<AzureSqlDatabaseRestorePointModel>, AzureSqlDatabaseBackupAdapter>
27+
{
28+
/// <summary>
29+
/// Gets or sets the name of the database server to use.
30+
/// </summary>
31+
[Parameter(Mandatory = true,
32+
ValueFromPipelineByPropertyName = true,
33+
Position = 1,
34+
HelpMessage = "The name of the Azure SQL Database Server the database is in.")]
35+
[ValidateNotNullOrEmpty]
36+
public string ServerName { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets the name of the database to use.
40+
/// </summary>
41+
[Parameter(Mandatory = true,
42+
ValueFromPipelineByPropertyName = true,
43+
Position = 2,
44+
HelpMessage = "The name of the Azure SQL Database to retrieve restore points from.")]
45+
[ValidateNotNullOrEmpty]
46+
public string DatabaseName { get; set; }
47+
48+
/// <summary>
49+
/// Initializes the adapter
50+
/// </summary>
51+
/// <param name="subscription"></param>
52+
/// <returns></returns>
53+
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(Azure.Common.Authentication.Models.AzureSubscription subscription)
54+
{
55+
return new AzureSqlDatabaseBackupAdapter(Profile, subscription);
56+
}
57+
}
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections.Generic;
16+
using System.Management.Automation;
17+
18+
using Microsoft.Azure.Commands.Sql.Backup.Model;
19+
using Microsoft.Azure.Commands.Sql.Database.Model;
20+
21+
namespace Microsoft.Azure.Commands.Sql.Backup.Cmdlet
22+
{
23+
[Cmdlet(VerbsCommon.Get, "AzureSqlDatabaseRestorePoints",
24+
ConfirmImpact = ConfirmImpact.None)]
25+
public class GetAzureSqlDatabaseRestorePoints : AzureSqlDatabaseRestorePointCmdletBase
26+
{
27+
/// <summary>
28+
/// Get the entities from the service
29+
/// </summary>
30+
/// <returns>The list of entities</returns>
31+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> GetEntity()
32+
{
33+
return ModelAdapter.ListRestorePoints(this.ResourceGroupName, this.ServerName, this.DatabaseName);
34+
}
35+
36+
/// <summary>
37+
/// No user input to apply to model
38+
/// </summary>
39+
/// <param name="model">Model retrieved from service</param>
40+
/// <returns>The model that was passed in</returns>
41+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseRestorePointModel> model)
42+
{
43+
return model;
44+
}
45+
46+
/// <summary>
47+
/// No changes to persist to server
48+
/// </summary>
49+
/// <param name="entity">The output of apply user input to model</param>
50+
/// <returns>The input entity</returns>
51+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> PersistChanges(IEnumerable<AzureSqlDatabaseRestorePointModel> entity)
52+
{
53+
return entity;
54+
}
55+
}
56+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
namespace Microsoft.Azure.Commands.Sql.Backup.Model
19+
{
20+
/// <summary>
21+
/// Represents an Azure Sql Database restore point
22+
/// </summary>
23+
public class AzureSqlDatabaseRestorePointModel
24+
{
25+
/// <summary>
26+
/// Gets or sets the name of the resource group
27+
/// </summary>
28+
public string ResourceGroupName { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the name of the server
32+
/// </summary>
33+
public string ServerName { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the name of the database
37+
/// </summary>
38+
public string DatabaseName { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets the location of the database
42+
/// </summary>
43+
public string Location { get; set; }
44+
45+
/// <summary>
46+
/// Gets the restore point type of the Azure SQL Database restore point. Possible values are: DISCRETE and CONTINUOUS.
47+
/// </summary>
48+
public string RestorePointType { get; set; }
49+
50+
/// <summary>
51+
/// Earliest restore time. Populated when restorePointType = CONTINUOUS. Null otherwise.
52+
/// </summary>
53+
public DateTime? RestorePointCreationDate { get; set; }
54+
55+
/// <summary>
56+
/// Earliest restore time. Populated when restorePointType = DISCRETE. Null otherwise.
57+
/// </summary>
58+
public DateTime? EarliestRestoreDate { get; set; }
59+
60+
/// <summary>
61+
/// Size of the backup in blob storage.
62+
/// For discrete restore point, the value is the database snap size; for continuous restore point, the value is the
63+
/// total backup storage usage of the database.
64+
/// </summary>
65+
public int SizeBytes { get; set; }
66+
}
67+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Globalization;
18+
using System.Linq;
19+
20+
using Microsoft.Azure.Commands.Sql.Backup.Model;
21+
using Microsoft.Azure.Commands.Sql.Common;
22+
using Microsoft.Azure.Commands.Sql.Database.Model;
23+
using Microsoft.Azure.Commands.Sql.Database.Services;
24+
using Microsoft.Azure.Commands.Sql.ElasticPool.Services;
25+
using Microsoft.Azure.Commands.Sql.Properties;
26+
using Microsoft.Azure.Commands.Sql.Server.Adapter;
27+
using Microsoft.Azure.Commands.Sql.Services;
28+
using Microsoft.Azure.Common.Authentication.Models;
29+
using Microsoft.Azure.Management.Sql;
30+
using Microsoft.Azure.Management.Sql.Models;
31+
32+
namespace Microsoft.Azure.Commands.Sql.Backup.Services
33+
{
34+
/// <summary>
35+
/// Adapter for database backup operations
36+
/// </summary>
37+
public class AzureSqlDatabaseBackupAdapter
38+
{
39+
/// <summary>
40+
/// Gets or sets the AzureSqlDatabaseBackupCommunicator which has all the needed management clients
41+
/// </summary>
42+
private AzureSqlDatabaseBackupCommunicator Communicator { get; set; }
43+
44+
/// <summary>
45+
/// Gets or sets the Azure profile
46+
/// </summary>
47+
public AzureProfile Profile { get; set; }
48+
49+
/// <summary>
50+
/// Gets or sets the Azure Subscription
51+
/// </summary>
52+
private AzureSubscription _subscription { get; set; }
53+
54+
/// <summary>
55+
/// Constructs a database backup adapter
56+
/// </summary>
57+
/// <param name="profile">The current azure profile</param>
58+
/// <param name="subscription">The current azure subscription</param>
59+
public AzureSqlDatabaseBackupAdapter(AzureProfile Profile, AzureSubscription subscription)
60+
{
61+
this.Profile = Profile;
62+
this._subscription = subscription;
63+
Communicator = new AzureSqlDatabaseBackupCommunicator(Profile, subscription);
64+
}
65+
66+
/// <summary>
67+
/// Lists the restore points for a given Sql Azure Database.
68+
/// </summary>
69+
/// <param name="resourceGroup">The name of the resource group</param>
70+
/// <param name="serverName">The name of the Azure SQL Server</param>
71+
/// <param name="databaseName">The name of the Azure SQL database</param>
72+
/// <returns>List of restore points</returns>
73+
internal IEnumerable<AzureSqlDatabaseRestorePointModel> ListRestorePoints(string resourceGroup, string serverName, string databaseName)
74+
{
75+
var resp = Communicator.ListRestorePoints(resourceGroup, serverName, databaseName, Util.GenerateTracingId());
76+
return resp.Select((restorePoint) =>
77+
{
78+
return new AzureSqlDatabaseRestorePointModel()
79+
{
80+
ResourceGroupName = resourceGroup,
81+
ServerName = serverName,
82+
DatabaseName = databaseName,
83+
Location = restorePoint.Location,
84+
RestorePointType = restorePoint.Properties.RestorePointType,
85+
RestorePointCreationDate = restorePoint.Properties.RestorePointCreationDate,
86+
EarliestRestoreDate = restorePoint.Properties.EarliestRestoreDate,
87+
SizeBytes = restorePoint.Properties.SizeBytes
88+
};
89+
}).ToList();
90+
}
91+
}
92+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
using Microsoft.Azure.Common.Authentication;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.Azure.Management.Resources;
21+
using Microsoft.Azure.Management.Sql;
22+
using Microsoft.Azure.Management.Sql.Models;
23+
using Microsoft.Azure.Commands.Sql.Common;
24+
using Microsoft.WindowsAzure.Management.Storage;
25+
26+
namespace Microsoft.Azure.Commands.Sql.Backup.Services
27+
{
28+
/// <summary>
29+
/// This class is responsible for all the REST communication with the database backup REST endpoints.
30+
/// </summary>
31+
public class AzureSqlDatabaseBackupCommunicator
32+
{
33+
/// <summary>
34+
/// The Sql client to be used by this end points communicator
35+
/// </summary>
36+
private static SqlManagementClient SqlClient { get; set; }
37+
38+
/// <summary>
39+
/// Gets or set the Azure subscription
40+
/// </summary>
41+
private static AzureSubscription Subscription { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the Azure profile
45+
/// </summary>
46+
public AzureProfile Profile { get; set; }
47+
48+
/// <summary>
49+
/// Creates a communicator for Azure Sql Database backup REST endpoints.
50+
/// </summary>
51+
/// <param name="profile">Azure profile</param>
52+
/// <param name="subscription">Associated subscription</param>
53+
public AzureSqlDatabaseBackupCommunicator(AzureProfile profile, AzureSubscription subscription)
54+
{
55+
Profile = profile;
56+
if (subscription != Subscription)
57+
{
58+
Subscription = subscription;
59+
SqlClient = null;
60+
}
61+
}
62+
63+
/// <summary>
64+
/// Lists the restore points for a given Sql Azure Database.
65+
/// </summary>
66+
/// <param name="resourceGroup">The name of the resource group</param>
67+
/// <param name="serverName">The name of the Azure SQL Server</param>
68+
/// <param name="databaseName">The name of the Azure SQL database</param>
69+
/// <returns>List of restore points</returns>
70+
public IList<Management.Sql.Models.RestorePoint> ListRestorePoints(string resourceGroupName, string serverName, string databaseName, string clientRequestId)
71+
{
72+
return GetCurrentSqlClient(clientRequestId).Backup.ListRestorePoints(resourceGroupName, serverName, databaseName).RestorePoints;
73+
}
74+
75+
/// <summary>
76+
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
77+
/// id tracing headers for the current cmdlet invocation.
78+
/// </summary>
79+
/// <returns>The SQL Management client for the currently selected subscription.</returns>
80+
private SqlManagementClient GetCurrentSqlClient(String clientRequestId)
81+
{
82+
// Get the SQL management client for the current subscription
83+
if (SqlClient == null)
84+
{
85+
SqlClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(Profile, Subscription, AzureEnvironment.Endpoint.ResourceManager);
86+
}
87+
SqlClient.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName);
88+
SqlClient.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, clientRequestId);
89+
return SqlClient;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)