Skip to content

Commit ef1a46a

Browse files
committed
RestoreDisk completed. Added test cases as well
1 parent 410fbfb commit ef1a46a

File tree

11 files changed

+186
-50
lines changed

11 files changed

+186
-50
lines changed

src/ResourceManager/RecoveryServices.Backup/Cmdlets/RecoveryPoint/GetAzureRMRecoveryServicesRecoveryPoint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1616
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel;
17-
using Microsoft.Azure.Commands.ResourceManager.Common.Properties;
1817
using System;
1918
using System.Collections.Generic;
2019
using System.Linq;
2120
using System.Management.Automation;
2221
using System.Text;
2322
using System.Threading.Tasks;
23+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2424

2525
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2626
{
@@ -65,7 +65,7 @@ public override void ExecuteCmdlet()
6565
StartDate, EndDate, Item.Name, Item.ContainerName));
6666
if (StartDate >= EndDate)
6767
{
68-
throw new Exception("End date should be greater than start date"); //tbd: Correct nsg and exception type
68+
throw new ArgumentException(Resources.RecoveryPointEndDateShouldBeGreater);
6969
}
7070

7171
parameter.Add(GetRecoveryPointParams.StartDate, StartDate);
@@ -98,7 +98,7 @@ public override void ExecuteCmdlet()
9898
}
9999
else
100100
{
101-
throw new Exception("Unsupported Parameter set");
101+
throw new Exception(Resources.RecoveryPointUnsupportedParamet);
102102
}
103103
});
104104
}

src/ResourceManager/RecoveryServices.Backup/Cmdlets/Restore/RestoreAzureRMRecoveryServicesBackupItem.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Threading.Tasks;
2626
using ResourcesNS = Microsoft.Azure.Management.Resources;
2727
using Newtonsoft.Json.Linq;
28+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2829

2930
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
3031
{
@@ -48,6 +49,7 @@ public override void ExecuteCmdlet()
4849
ExecutionBlock(() =>
4950
{
5051
base.ExecuteCmdlet();
52+
StorageAccountName = StorageAccountName.ToLower();
5153
WriteDebug("InsideRestore. going to create ResourceManager Client");
5254
ResourcesNS.ResourceManagementClient rmClient = AzureSession.ClientFactory.CreateClient<ResourcesNS.ResourceManagementClient>(DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
5355
WriteDebug("Client Created successfully");
@@ -63,18 +65,12 @@ public override void ExecuteCmdlet()
6365
WriteDebug(String.Format("Query Microsoft.ClassicStorage with name = {0}", StorageAccountName));
6466
resource = rmClient.Resources.GetAsync(StorageAccountResourceGroupName, identity, CancellationToken.None).Result;
6567
}
66-
catch (Hyak.Common.CloudException exp)
68+
catch (Exception)
6769
{
68-
string expType = exp.GetType().ToString();
6970
identity.ResourceProviderNamespace = "Microsoft.Storage/storageAccounts";
7071
identity.ResourceProviderApiVersion = "2016-01-01";
7172
resource = rmClient.Resources.GetAsync(StorageAccountResourceGroupName, identity, CancellationToken.None).Result;
7273
}
73-
catch(Exception e)
74-
{
75-
WriteDebug(e.Message);
76-
throw;
77-
}
7874

7975
string storageAccountId = resource.Resource.Id;
8076
string storageAccountlocation = resource.Resource.Location;
@@ -89,7 +85,9 @@ public override void ExecuteCmdlet()
8985
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
9086
{
9187
{RestoreBackupItemParams.RecoveryPoint, RecoveryPoint},
92-
{RestoreBackupItemParams.StorageAccountId, storageAccountId}
88+
{RestoreBackupItemParams.StorageAccountId, storageAccountId},
89+
{RestoreBackupItemParams.StorageAccountLocation, storageAccountlocation},
90+
{RestoreBackupItemParams.StorageAccountType, storageAccountType}
9391
}, HydraAdapter);
9492

9593
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType);
@@ -115,6 +113,13 @@ public override void ExecuteCmdlet()
115113
});
116114
}
117115

116+
/// <summary>
117+
/// This code is not getting used. Will delete it once things will be closed.
118+
/// </summary>
119+
/// <param name="storageAccountName"></param>
120+
/// <param name="id"></param>
121+
/// <param name="location"></param>
122+
/// <param name="resourceType"></param>
118123
internal void GetStorageResource(string storageAccountName, out string id, out string location, out string resourceType)
119124
{
120125
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
@@ -137,12 +142,12 @@ internal void GetStorageResource(string storageAccountName, out string id, out s
137142
if(result.Count == 0)
138143
{
139144
WriteVerbose(string.Format("Storage Account not fount"));
140-
throw new ArgumentException("Storage account not found");
145+
throw new ArgumentException(Resources.RestoreAzureStorageNotFound);
141146
}
142147
else if (result.Count > 1)
143148
{
144149
WriteVerbose(string.Format("Found more than one StorageAccount with same name. Some thing went wrong"));
145-
throw new Exception(string.Format("Found more than one StorageAccount with same name. Some thing went wrong"));
150+
throw new Exception(Resources.RestoreDiskMoreThanOneAccFound);
146151
}
147152
id = result[0].Members["ResourceId"].Value.ToString();
148153
location = result[0].Members["Location"].Value.ToString();

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Conversions/RecoveryPointConversions.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
1717
using System;
1818
using System.Collections.Generic;
19+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
1920

2021
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
2122
{
@@ -25,7 +26,7 @@ public static List<AzureRmRecoveryServicesRecoveryPointBase> GetPSAzureRecoveryP
2526
{
2627
if (rpList == null || rpList.RecoveryPointList == null || rpList.RecoveryPointList.RecoveryPoints == null)
2728
{
28-
throw new ArgumentNullException("rpList is null");
29+
throw new ArgumentNullException("RPList");
2930
}
3031

3132
List<AzureRmRecoveryServicesRecoveryPointBase> result = new List<AzureRmRecoveryServicesRecoveryPointBase>();
@@ -34,6 +35,7 @@ public static List<AzureRmRecoveryServicesRecoveryPointBase> GetPSAzureRecoveryP
3435
RecoveryPoint recPoint = rp.Properties as RecoveryPoint;
3536
AzureRmRecoveryServicesIaasVmRecoveryPoint rpBase = new AzureRmRecoveryServicesIaasVmRecoveryPoint()
3637
{
38+
Name = rp.Name,
3739
BackupManagementType = item.BackupManagementType,
3840
ItemName = item.Name,
3941
ContainerName = item.ContainerName,
@@ -54,23 +56,24 @@ public static AzureRmRecoveryServicesRecoveryPointBase GetPSAzureRecoveryPoints(
5456
{
5557
if (rpResponse == null || rpResponse.RecPoint == null)
5658
{
57-
throw new ArgumentNullException("rpResponse is null");
59+
throw new ArgumentNullException(Resources.GetRPResponseIsNull);
5860
}
5961

6062
RecoveryPoint recPoint = rpResponse.RecPoint.Properties as RecoveryPoint;
6163

6264
AzureRmRecoveryServicesIaasVmRecoveryPoint result = new AzureRmRecoveryServicesIaasVmRecoveryPoint()
63-
{
64-
BackupManagementType = item.BackupManagementType,
65-
ItemName = item.Name,
66-
ContainerName = item.ContainerName,
67-
ContainerType = item.ContainerType,
68-
RecoveryPointTime = Convert.ToDateTime(recPoint.RecoveryPointTime).ToLocalTime(),
69-
RecoveryPointType = recPoint.RecoveryPointType,
70-
RecoveryPointId = rpResponse.RecPoint.Id,
71-
WorkloadType = item.WorkloadType,
72-
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
73-
};
65+
{
66+
Name = rpResponse.RecPoint.Name,
67+
BackupManagementType = item.BackupManagementType,
68+
ItemName = item.Name,
69+
ContainerName = item.ContainerName,
70+
ContainerType = item.ContainerType,
71+
RecoveryPointTime = Convert.ToDateTime(recPoint.RecoveryPointTime).ToLocalTime(),
72+
RecoveryPointType = recPoint.RecoveryPointType,
73+
RecoveryPointId = rpResponse.RecPoint.Id,
74+
WorkloadType = item.WorkloadType,
75+
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
76+
};
7477
return result;
7578
}
7679
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/BMSAPIs/RestoreDiskAPIs.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
89

910
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
1011
{
@@ -19,15 +20,26 @@ public partial class HydraAdapter
1920
/// <param name="protectedItemName"></param>
2021
/// <param name="recoveryPointId"></param>
2122
/// <returns></returns>
22-
public BaseRecoveryServicesJobResponse RestoreDisk(AzureRmRecoveryServicesIaasVmRecoveryPoint rp, string storageAccountId)
23+
public BaseRecoveryServicesJobResponse RestoreDisk(AzureRmRecoveryServicesIaasVmRecoveryPoint rp, string storageAccountId,
24+
string storageAccountLocation, string storageAccountType)
2325
{
2426
string resourceGroupName = BmsAdapter.GetResourceGroupName();
2527
string resourceName = BmsAdapter.GetResourceName();
26-
string location = BmsAdapter.GetResourceLocation();
27-
28+
string vaultLocation = BmsAdapter.GetResourceLocation();
2829
string containerName = rp.ContainerName;
2930
string protectedItemName = rp.ItemName;
30-
string recoveryPointId = rp.RecoveryPointId;
31+
string recoveryPointId = rp.Name;
32+
//validtion block
33+
if(storageAccountLocation != vaultLocation)
34+
{
35+
throw new Exception(Resources.RestoreDiskIncorrectRegion);
36+
}
37+
string vmType = containerName.Split(';')[1].Equals("iaasvmcontainer", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute";
38+
string strType = storageAccountType.Equals("Microsoft.ClassicStorage/StorageAccounts", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute";
39+
if(vmType != strType)
40+
{
41+
throw new Exception(String.Format(Resources.RestoreDiskStorageTypeError, vmType));
42+
}
3143

3244
IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest()
3345
{
@@ -36,7 +48,7 @@ public BaseRecoveryServicesJobResponse RestoreDisk(AzureRmRecoveryServicesIaasVm
3648
CreateNewCloudService = false,
3749
RecoveryPointId = recoveryPointId,
3850
RecoveryType = RecoveryType.RestoreDisks,
39-
Region = location,
51+
Region = vaultLocation,
4052
StorageAccountName = storageAccountId,
4153
SubnetName = string.Empty,
4254
VirtualMachineName = string.Empty,
@@ -54,4 +66,4 @@ public BaseRecoveryServicesJobResponse RestoreDisk(AzureRmRecoveryServicesIaasVm
5466
return response;
5567
}
5668
}
57-
}
69+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public class AzureRmRecoveryServicesItemExtendedInfoBase : AzureRmRecoveryServic
138138
public class AzureRmRecoveryServicesRecoveryPointBase : AzureRmRecoveryServicesItemContext
139139
{
140140
private global::Microsoft.Azure.Management.RecoveryServices.Backup.Models.RecoveryPointResource rp;
141+
142+
/// <summary>
143+
///
144+
/// </summary>
145+
public string Name { get; set; }
141146

142147
/// <summary>
143148
///

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public enum RestoreBackupItemParams
4444
{
4545
RecoveryPoint,
4646
StorageAccountId,
47+
StorageAccountLocation,
48+
StorageAccountType,
4749
}
4850

4951
public enum PolicyParams

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

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,22 @@ Please contact Microsoft for further assistant.</value>
371371
<data name="RecoveryPointItemTypeConversionError" xml:space="preserve">
372372
<value>Cant convert input Item to AzureRmRecoveryServicesItemBase</value>
373373
</data>
374+
<data name="GetRPResponseIsNull" xml:space="preserve">
375+
<value>GetRecoveryPointResponse is null</value>
376+
</data>
377+
<data name="RestoreAzureStorageNotFound" xml:space="preserve">
378+
<value>Storage account not found</value>
379+
</data>
380+
<data name="RestoreDiskIncorrectRegion" xml:space="preserve">
381+
<value>Storage account location should be same as vault location</value>
382+
</data>
383+
<data name="RestoreDiskMoreThanOneAccFound" xml:space="preserve">
384+
<value>Found more than one StorageAccount with same name. Some thing went wrong</value>
385+
</data>
386+
<data name="RestoreDiskStorageTypeError" xml:space="preserve">
387+
<value>Please provide {0} storage type to restore the vm</value>
388+
</data>
389+
<data name="RestoreDiskTimeRangeError" xml:space="preserve">
390+
<value>Time difference should not be more than 30 days</value>
391+
</data>
374392
</root>

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,11 @@ public BaseRecoveryServicesJobResponse TriggerRestore()
193193
{
194194
AzureRmRecoveryServicesIaasVmRecoveryPoint rp = ProviderData.ProviderParameters[RestoreBackupItemParams.RecoveryPoint]
195195
as AzureRmRecoveryServicesIaasVmRecoveryPoint;
196-
string storageId = ProviderData.ProviderParameters[RestoreBackupItemParams.StorageAccountId].ToString();
196+
string storageAccountId = ProviderData.ProviderParameters[RestoreBackupItemParams.StorageAccountId].ToString();
197+
string storageAccountLocation = ProviderData.ProviderParameters[RestoreBackupItemParams.StorageAccountLocation].ToString();
198+
string storageAccountType = ProviderData.ProviderParameters[RestoreBackupItemParams.StorageAccountType].ToString();
197199

198-
if(rp == null)
199-
{
200-
throw new InvalidCastException("Cant convert input to AzureRmRecoveryServicesIaasVmRecoveryPoint");
201-
}
202-
203-
var response = HydraAdapter.RestoreDisk(rp, storageId);
200+
var response = HydraAdapter.RestoreDisk(rp, storageAccountId, storageAccountLocation, storageAccountType);
204201
return response;
205202
}
206203

@@ -216,11 +213,6 @@ public AzureRmRecoveryServicesRecoveryPointBase GetRecoveryPointDetails()
216213

217214
string recoveryPointId = ProviderData.ProviderParameters[GetRecoveryPointParams.RecoveryPointId].ToString();
218215

219-
if (item == null)
220-
{
221-
throw new InvalidCastException("Cant convert input to AzureRmRecoveryServicesItemBase");
222-
}
223-
224216
string containerName = item.ContainerName;
225217
string protectedItemName = (item as AzureRmRecoveryServicesIaasVmItem).Name;
226218

@@ -235,19 +227,14 @@ public List<AzureRmRecoveryServicesRecoveryPointBase> ListRecoveryPoints()
235227
AzureRmRecoveryServicesIaasVmItem item = ProviderData.ProviderParameters[GetRecoveryPointParams.Item]
236228
as AzureRmRecoveryServicesIaasVmItem;
237229

238-
if (item == null)
239-
{
240-
throw new InvalidCastException("Cant convert input to AzureRmRecoveryServicesItemBase");
241-
}
242-
243230
string containerName = item.ContainerName;
244231
string protectedItemName = (item as AzureRmRecoveryServicesIaasVmItem).Name;
245232

246233
TimeSpan duration = endDate - startDate;
247234

248235
if (duration.TotalDays > 30)
249236
{
250-
throw new Exception("Time difference should not be more than 30 days"); //tbd: Correct nsg and exception type
237+
throw new Exception(Resource.RestoreDiskTimeRangeError); //tbd: Correct nsg and exception type
251238
}
252239

253240
//we need to fetch the list of RPs

0 commit comments

Comments
 (0)