Skip to content

Commit fd1001f

Browse files
Adding backup cmdlet support and Get-SqlAzureDatabaseRestorePoints
1 parent ecb546b commit fd1001f

File tree

6 files changed

+368
-5
lines changed

6 files changed

+368
-5
lines changed

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
<Prefer32Bit>false</Prefer32Bit>
5252
</PropertyGroup>
5353
<ItemGroup>
54-
<Compile Include="Backup\Cmdlet\AzureSqlDatabaseRestorePointCmdletBase.cs" />
55-
<Compile Include="Backup\Cmdlet\GetAzureSqlDatabaseRestorePoints.cs" />
56-
<Compile Include="Backup\Model\AzureSqlDatabaseRestorePointModel.cs" />
57-
<Compile Include="Backup\Services\AzureSqlDatabaseBackupAdapter.cs" />
58-
<Compile Include="Backup\Services\AzureSqlDatabaseBackupCommunicator.cs" />
54+
<Compile Include="Database Backup\Cmdlet\AzureSqlDatabaseRestorePointCmdletBase.cs" />
55+
<Compile Include="Database Backup\Cmdlet\GetAzureSqlDatabaseRestorePoints.cs" />
56+
<Compile Include="Database Backup\Model\AzureSqlDatabaseRestorePointModel.cs" />
57+
<Compile Include="Database Backup\Services\AzureSqlDatabaseBackupAdapter.cs" />
58+
<Compile Include="Database Backup\Services\AzureSqlDatabaseBackupCommunicator.cs" />
5959
<Compile Include="Database Activation\Cmdlet\AzureSqlDatabaseActivationCmdletBase.cs" />
6060
<Compile Include="Database Activation\Cmdlet\ResumeAzureSqlDatabase.cs" />
6161
<Compile Include="Database Activation\Cmdlet\SuspendAzureSqlDatabase.cs" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/// Initializes the adapter
40+
/// </summary>
41+
/// <param name="subscription"></param>
42+
/// <returns></returns>
43+
protected override AzureSqlDatabaseBackupAdapter InitModelAdapter(Azure.Common.Authentication.Models.AzureSubscription subscription)
44+
{
45+
return new AzureSqlDatabaseBackupAdapter(Profile, subscription);
46+
}
47+
}
48+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/// Gets or sets the name of the database to use.
29+
/// </summary>
30+
[Parameter(Mandatory = true,
31+
ValueFromPipelineByPropertyName = true,
32+
Position = 2,
33+
HelpMessage = "The name of the Azure SQL Database to retrieve restore points from.")]
34+
[ValidateNotNullOrEmpty]
35+
public string DatabaseName { get; set; }
36+
37+
/// <summary>
38+
/// Get the entities from the service
39+
/// </summary>
40+
/// <returns>The list of entities</returns>
41+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> GetEntity()
42+
{
43+
IEnumerable<AzureSqlDatabaseRestorePointModel> results;
44+
results = ModelAdapter.ListRestorePoints(this.ResourceGroupName, this.ServerName, this.DatabaseName);
45+
return results;
46+
}
47+
48+
/// <summary>
49+
/// No user input to apply to model
50+
/// </summary>
51+
/// <param name="model">Model retrieved from service</param>
52+
/// <returns>The model that was passed in</returns>
53+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseRestorePointModel> model)
54+
{
55+
return model;
56+
}
57+
58+
/// <summary>
59+
/// No changes to persist to server
60+
/// </summary>
61+
/// <param name="entity">The output of apply user input to model</param>
62+
/// <returns>The input entity</returns>
63+
protected override IEnumerable<AzureSqlDatabaseRestorePointModel> PersistChanges(IEnumerable<AzureSqlDatabaseRestorePointModel> entity)
64+
{
65+
return entity;
66+
}
67+
}
68+
}
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+
/// Backup size in blob storage of the restore point.
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 activation 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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 activation 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 Databases
50+
/// </summary>
51+
/// <param name="profile"></param>
52+
/// <param name="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+
/// Gets all the restore points for a given Azure Sql Database.
65+
/// </summary>
66+
public IList<Management.Sql.Models.RestorePoint> ListRestorePoints(string resourceGroupName, string serverName, string databaseName, string clientRequestId)
67+
{
68+
return GetCurrentSqlClient(clientRequestId).Backup.ListRestorePoints(resourceGroupName, serverName, databaseName).RestorePoints;
69+
}
70+
71+
/// <summary>
72+
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
73+
/// id tracing headers for the current cmdlet invocation.
74+
/// </summary>
75+
/// <returns>The SQL Management client for the currently selected subscription.</returns>
76+
private SqlManagementClient GetCurrentSqlClient(String clientRequestId)
77+
{
78+
// Get the SQL management client for the current subscription
79+
if (SqlClient == null)
80+
{
81+
SqlClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(Profile, Subscription, AzureEnvironment.Endpoint.ResourceManager);
82+
}
83+
SqlClient.HttpClient.DefaultRequestHeaders.Remove(Constants.ClientRequestIdHeaderName);
84+
SqlClient.HttpClient.DefaultRequestHeaders.Add(Constants.ClientRequestIdHeaderName, clientRequestId);
85+
return SqlClient;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)