Skip to content

Commit 53a4d2b

Browse files
authored
Merge pull request #7681 from MabOneSdk/afs_dev
[RecoveryServices] Adding support for restore item for azure file share
2 parents 51e918e + 0e7ebaa commit 53a4d2b

File tree

26 files changed

+677224
-12499
lines changed

26 files changed

+677224
-12499
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Helpers/HelperUtils.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
16+
using Microsoft.Rest.Azure;
1517
using System;
1618
using System.Collections.Generic;
1719
using System.Text.RegularExpressions;
18-
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
19-
using Microsoft.Rest.Azure;
2020
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
21+
using ResourceManagerModel = Microsoft.Azure.Management.Internal.Resources.Models;
2122
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2223

2324
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
@@ -327,5 +328,35 @@ public static List<T> GetPagedList<T>(
327328

328329
return resources;
329330
}
331+
332+
public static List<T> GetPagedRMList<T>(
333+
Func<IPage<T>> listResources, Func<string, IPage<T>> listNext)
334+
where T : ResourceManagerModel.Resource
335+
{
336+
var resources = new List<T>();
337+
string nextLink = null;
338+
339+
var pagedResources = listResources();
340+
341+
foreach (var pagedResource in pagedResources)
342+
{
343+
resources.Add(pagedResource);
344+
}
345+
346+
nextLink = pagedResources.NextPageLink;
347+
348+
while (!string.IsNullOrEmpty(nextLink))
349+
{
350+
pagedResources = listNext(nextLink);
351+
nextLink = pagedResources.NextPageLink;
352+
353+
foreach (var pagedResource in pagedResources)
354+
{
355+
resources.Add(pagedResource);
356+
}
357+
}
358+
359+
return resources;
360+
}
330361
}
331362
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CmdletParamEnums.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,26 @@ public enum RecoveryPointParams
5050
public enum RestoreBackupItemParams
5151
{
5252
RecoveryPoint,
53-
StorageAccountId,
54-
StorageAccountLocation,
55-
StorageAccountType,
53+
StorageAccountName,
54+
StorageAccountResourceGroupName
55+
}
56+
57+
public enum RestoreVMBackupItemParams
58+
{
5659
TargetResourceGroupName,
5760
OsaOption
5861
}
5962

63+
public enum RestoreFSBackupItemParams
64+
{
65+
ResolveConflict,
66+
SourceFilePath,
67+
SourceFileType,
68+
TargetStorageAccountName,
69+
TargetFileShareName,
70+
TargetFolder
71+
}
72+
6073
public enum PolicyParams
6174
{
6275
WorkloadType,

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CommonModels/Enums.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,22 @@ public enum ILRAction
316316
Extend,
317317
Terminate,
318318
}
319+
320+
/// <summary>
321+
/// Options to resolve conflict for a file share
322+
/// </summary>
323+
public enum RestoreFSResolveConflictOption
324+
{
325+
Overwrite,
326+
Skip
327+
}
328+
329+
/// <summary>
330+
/// Options to select the file type
331+
/// </summary>
332+
public enum SourceFileType
333+
{
334+
File,
335+
Directory
336+
}
319337
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ Please contact Microsoft for further assistance.</value>
377377
<data name="RestoreAzureStorageNotFound" xml:space="preserve">
378378
<value>Storage account not found</value>
379379
</data>
380-
<data name="RestoreDiskIncorrectRegion" xml:space="preserve">
380+
<data name="TriggerRestoreIncorrectRegion" xml:space="preserve">
381381
<value>Storage account location should be same as vault location</value>
382382
</data>
383383
<data name="RestoreDiskMoreThanOneAccFound" xml:space="preserve">

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/IPsBackupProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Collections.Generic;
1615
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1716
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
1817
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
18+
using System.Collections.Generic;
1919
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2020
using RestAzureNS = Microsoft.Rest.Azure;
21-
using System.Net.Http;
2221

2322
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
2423
{

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
1717
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
1818
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
19+
using Microsoft.Azure.Management.Internal.Resources.Models;
1920
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2021
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2122
using Microsoft.Rest.Azure.OData;
@@ -147,7 +148,103 @@ public RestAzureNS.AzureOperationResponse TriggerBackup()
147148

148149
public RestAzureNS.AzureOperationResponse TriggerRestore()
149150
{
150-
throw new NotImplementedException();
151+
string vaultName = (string)ProviderData[VaultParams.VaultName];
152+
string resourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName];
153+
string vaultLocation = (string)ProviderData[VaultParams.VaultLocation];
154+
CmdletModel.AzureFileShareRecoveryPoint recoveryPoint = ProviderData[RestoreBackupItemParams.RecoveryPoint]
155+
as CmdletModel.AzureFileShareRecoveryPoint;
156+
string storageAccountName = ProviderData[RestoreBackupItemParams.StorageAccountName].ToString();
157+
string storageAccountResourceGroupName = ProviderData.ContainsKey(RestoreBackupItemParams.StorageAccountResourceGroupName) ?
158+
ProviderData[RestoreBackupItemParams.StorageAccountResourceGroupName].ToString() : null;
159+
string copyOptions = (string)ProviderData[RestoreFSBackupItemParams.ResolveConflict];
160+
string sourceFilePath = ProviderData.ContainsKey(RestoreFSBackupItemParams.SourceFilePath) ?
161+
(string)ProviderData[RestoreFSBackupItemParams.SourceFilePath] : null;
162+
string sourceFileType = ProviderData.ContainsKey(RestoreFSBackupItemParams.SourceFileType) ?
163+
(string)ProviderData[RestoreFSBackupItemParams.SourceFileType] : null;
164+
string targetStorageAccountName =
165+
ProviderData.ContainsKey(RestoreFSBackupItemParams.TargetStorageAccountName) ?
166+
(string)ProviderData[RestoreFSBackupItemParams.TargetStorageAccountName] : null;
167+
string targetFileShareName = ProviderData.ContainsKey(RestoreFSBackupItemParams.TargetFileShareName) ?
168+
(string)ProviderData[RestoreFSBackupItemParams.TargetFileShareName] : null;
169+
string targetFolder = ProviderData.ContainsKey(RestoreFSBackupItemParams.TargetFolder) ?
170+
(string)ProviderData[RestoreFSBackupItemParams.TargetFolder] : null;
171+
172+
GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(storageAccountName);
173+
GenericResource targetStorageAccountResource = null;
174+
string targetStorageAccountLocation = null;
175+
if (targetStorageAccountName != null)
176+
{
177+
targetStorageAccountResource = ServiceClientAdapter.GetStorageAccountResource(targetStorageAccountName);
178+
targetStorageAccountLocation = targetStorageAccountResource.Location;
179+
}
180+
181+
List<RestoreFileSpecs> restoreFileSpecs = null;
182+
TargetAFSRestoreInfo targetDetails = null;
183+
RestoreFileSpecs restoreFileSpec = new RestoreFileSpecs();
184+
AzureFileShareRestoreRequest restoreRequest = new AzureFileShareRestoreRequest();
185+
restoreRequest.CopyOptions = copyOptions;
186+
restoreRequest.SourceResourceId = storageAccountResource.Id;
187+
if (sourceFilePath != null)
188+
{
189+
restoreFileSpec.Path = sourceFilePath;
190+
restoreFileSpec.FileSpecType = sourceFileType;
191+
restoreRequest.RestoreRequestType = RestoreRequestType.ItemLevelRestore;
192+
if (targetFolder != null)
193+
{
194+
//scenario : item level restore for alternate location
195+
restoreFileSpec.TargetFolderPath = targetFolder;
196+
targetDetails = new TargetAFSRestoreInfo();
197+
targetDetails.Name = targetFileShareName;
198+
targetDetails.TargetResourceId = targetStorageAccountResource.Id;
199+
restoreRequest.RecoveryType = RecoveryType.AlternateLocation;
200+
}
201+
else
202+
{
203+
//scenario : item level restore for original location
204+
restoreFileSpec.TargetFolderPath = null;
205+
restoreRequest.RecoveryType = RecoveryType.OriginalLocation;
206+
}
207+
208+
restoreFileSpecs = new List<RestoreFileSpecs>();
209+
restoreFileSpecs.Add(restoreFileSpec);
210+
}
211+
else
212+
{
213+
restoreRequest.RestoreRequestType = RestoreRequestType.FullShareRestore;
214+
if (targetFolder != null)
215+
{
216+
//scenario : full level restore for alternate location
217+
restoreFileSpec.Path = null;
218+
restoreFileSpec.TargetFolderPath = targetFolder;
219+
restoreFileSpec.FileSpecType = null;
220+
restoreFileSpecs = new List<RestoreFileSpecs>();
221+
restoreFileSpecs.Add(restoreFileSpec);
222+
targetDetails = new TargetAFSRestoreInfo();
223+
targetDetails.Name = targetFileShareName;
224+
targetDetails.TargetResourceId = targetStorageAccountResource.Id;
225+
restoreRequest.RecoveryType = RecoveryType.AlternateLocation;
226+
}
227+
else
228+
{
229+
//scenario : full level restore for original location
230+
restoreRequest.RecoveryType = RecoveryType.OriginalLocation;
231+
}
232+
}
233+
234+
restoreRequest.RestoreFileSpecs = restoreFileSpecs;
235+
restoreRequest.TargetDetails = targetDetails;
236+
237+
RestoreRequestResource triggerRestoreRequest = new RestoreRequestResource();
238+
triggerRestoreRequest.Properties = restoreRequest;
239+
240+
var response = ServiceClientAdapter.RestoreDisk(
241+
recoveryPoint,
242+
targetStorageAccountLocation = targetStorageAccountLocation ?? storageAccountResource.Location,
243+
triggerRestoreRequest,
244+
vaultName: vaultName,
245+
resourceGroupName: resourceGroupName,
246+
vaultLocation: vaultLocation);
247+
return response;
151248
}
152249

153250
public ProtectedItemResource GetProtectedItem()
@@ -665,7 +762,7 @@ private RestAzureNS.AzureOperationResponse EnableOrModifyProtection(bool disable
665762
properties.PolicyId = policy.Id;
666763
properties.SourceResourceId = sourceResourceId;
667764
}
668-
else if(disableWithRetentionData)
765+
else if (disableWithRetentionData)
669766
{
670767
//Disable protection while retaining backup data
671768
ValidateAzureFileShareDisableProtectionRequest(itemBase);

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/AzureSqlPsBackupProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Collections.Generic;
17-
using System.Linq;
1815
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1916
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
2017
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
2118
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2219
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2320
using Microsoft.Rest.Azure.OData;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Linq;
2424
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2525
using RestAzureNS = Microsoft.Rest.Azure;
2626
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/DpmPsBackupProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Collections.Generic;
1715
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1816
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
1917
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
2018
using Microsoft.Rest.Azure.OData;
19+
using System;
20+
using System.Collections.Generic;
2121
using RestAzureNS = Microsoft.Rest.Azure;
2222
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2323

@@ -37,7 +37,8 @@ public class DpmPsBackupProvider : IPsBackupProvider
3737
/// <param name="providerData">Data from the cmdlet layer intended for the provider</param>
3838
/// <param name="serviceClientAdapter">Service client adapter for communicating with the backend service</param>
3939
public void Initialize(
40-
Dictionary<Enum, object> providerData, ServiceClientAdapter serviceClientAdapter)
40+
Dictionary<Enum, object> providerData,
41+
ServiceClientAdapter serviceClientAdapter)
4142
{
4243
ProviderData = providerData;
4344
ServiceClientAdapter = serviceClientAdapter;

0 commit comments

Comments
 (0)