Skip to content

Commit c65bf87

Browse files
committed
Merge pull request #242 from MabOneSdk/mkheranidev1
Mkheranidev1
2 parents 766a257 + 078b554 commit c65bf87

File tree

12 files changed

+317
-62
lines changed

12 files changed

+317
-62
lines changed

src/ResourceManager/RecoveryServices.Backup/CmdletParameterHelpMessages.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ internal static class RecoveryPoint
8787
internal static class RestoreDisk
8888
{
8989
public const string RecoveryPoint = "Recovery point objected to be restored";
90-
public const string StorageAccountName = "Storage account name where the disk need to be recovered";
90+
public const string StorageAccountName = "Storage account name where the disk need to be recovered";
91+
public const string StorageAccountResourceGroupName = "Resource group name of Storage account name where the disk need to be recovered";
9192
}
9293
}
9394
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Management.Automation;
2121
using System.Text;
2222
using System.Threading.Tasks;
23+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2324

2425
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2526
{
@@ -59,16 +60,21 @@ public override void ExecuteCmdlet()
5960
if(this.ParameterSetName == DateTimeFilterParameterSet)
6061
{
6162
//User want list of RPs between given time range
63+
WriteDebug(String.Format("ParameterSet = DateTimeFilterParameterSet. \n" +
64+
"StartDate = {0} EndDate = {1}, Item.Name = {2}, Item.ContainerName = {3}",
65+
StartDate, EndDate, Item.Name, Item.ContainerName));
6266
if (StartDate >= EndDate)
6367
{
64-
throw new Exception("End date should be greated than start date"); //tbd: Correct nsg and exception type
68+
throw new ArgumentException(Resources.RecoveryPointEndDateShouldBeGreater);
6569
}
6670

6771
parameter.Add(GetRecoveryPointParams.StartDate, StartDate);
6872
parameter.Add(GetRecoveryPointParams.EndDate, EndDate);
6973
PsBackupProviderManager providerManager = new PsBackupProviderManager(parameter, HydraAdapter);
7074
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(Item.ContainerType, Item.BackupManagementType);
7175
var rpList = psBackupProvider.ListRecoveryPoints();
76+
77+
WriteDebug(String.Format("RPCount in Response = {0}", rpList.Count));
7278
if (rpList.Count == 1)
7379
{
7480
WriteObject(rpList[0]);
@@ -81,14 +87,18 @@ public override void ExecuteCmdlet()
8187
else if (this.ParameterSetName == RecoveryPointIdParameterSet)
8288
{
8389
//User want details of a particular recovery point
90+
WriteDebug(String.Format("ParameterSet = DateTimeFilterParameterSet. \n" +
91+
"StartDate = {0} EndDate = {1}, RPId = {2}",
92+
StartDate, EndDate, RecoveryPointId));
93+
8494
parameter.Add(GetRecoveryPointParams.RecoveryPointId, RecoveryPointId);
8595
PsBackupProviderManager providerManager = new PsBackupProviderManager(parameter, HydraAdapter);
8696
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(Item.ContainerType, Item.BackupManagementType);
8797
WriteObject(psBackupProvider.GetRecoveryPointDetails());
8898
}
8999
else
90100
{
91-
throw new Exception("Unsupported Parameter set");
101+
throw new Exception(Resources.RecoveryPointUnsupportedParamet);
92102
}
93103
});
94104
}

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

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
using System.Threading;
2525
using System.Threading.Tasks;
2626
using ResourcesNS = Microsoft.Azure.Management.Resources;
27+
using Newtonsoft.Json.Linq;
28+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2729

2830
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2931
{
3032
[Cmdlet(VerbsData.Restore, "AzureRMRecoveryServicesBackupItem"), OutputType(typeof(AzureRmRecoveryServicesJobBase))]
31-
class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBase
33+
public class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBase
3234
{
3335
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, HelpMessage = ParamHelpMsg.RestoreDisk.RecoveryPoint)]
3436
[ValidateNotNullOrEmpty]
@@ -38,56 +40,119 @@ class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBas
3840
[ValidateNotNullOrEmpty]
3941
public string StorageAccountName { get; set; }
4042

43+
[Parameter(Mandatory = true, Position = 2, HelpMessage = ParamHelpMsg.RestoreDisk.StorageAccountResourceGroupName)]
44+
[ValidateNotNullOrEmpty]
45+
public string StorageAccountResourceGroupName { get; set; }
46+
4147
public override void ExecuteCmdlet()
4248
{
4349
ExecutionBlock(() =>
4450
{
4551
base.ExecuteCmdlet();
46-
52+
StorageAccountName = StorageAccountName.ToLower();
53+
WriteDebug("InsideRestore. going to create ResourceManager Client");
4754
ResourcesNS.ResourceManagementClient rmClient = AzureSession.ClientFactory.CreateClient<ResourcesNS.ResourceManagementClient>(DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
55+
WriteDebug("Client Created successfully");
4856
ResourceIdentity identity = new ResourceIdentity();
4957
identity.ResourceName = StorageAccountName;
50-
identity.ResourceType = "Microsoft.ClassicStorage";
58+
identity.ResourceProviderNamespace = "Microsoft.ClassicStorage/storageAccounts";
59+
identity.ResourceProviderApiVersion = "2015-12-01";
60+
identity.ResourceType = string.Empty;
5161

52-
ResourcesNS.Models.ResourceGetResult resource = rmClient.Resources.GetAsync(StorageAccountName, identity, CancellationToken.None).Result;
53-
if(resource == null)
62+
ResourcesNS.Models.ResourceGetResult resource = null;
63+
try
5464
{
55-
identity.ResourceType = "Microsoft.Storage";
56-
resource = rmClient.Resources.GetAsync(StorageAccountName, identity, CancellationToken.None).Result;
65+
WriteDebug(String.Format("Query Microsoft.ClassicStorage with name = {0}", StorageAccountName));
66+
resource = rmClient.Resources.GetAsync(StorageAccountResourceGroupName, identity, CancellationToken.None).Result;
5767
}
58-
if(resource == null)
68+
catch (Exception)
5969
{
60-
throw new ArgumentException("Storage account doesnt exists");
70+
identity.ResourceProviderNamespace = "Microsoft.Storage/storageAccounts";
71+
identity.ResourceProviderApiVersion = "2016-01-01";
72+
resource = rmClient.Resources.GetAsync(StorageAccountResourceGroupName, identity, CancellationToken.None).Result;
6173
}
74+
75+
string storageAccountId = resource.Resource.Id;
76+
string storageAccountlocation = resource.Resource.Location;
77+
string storageAccountType = resource.Resource.Type;
6278

63-
string storageId = resource.Resource.Id;
79+
//GetStorageResource(StorageAccountName, out storageAccountId, out storageAccountlocation, out storageAccountType);
6480

65-
storageId = StorageAccountName; //TBD: once service will migrate to storageID we will remove this line;
81+
WriteDebug(String.Format("StorageId = {0}", storageAccountId));
82+
83+
storageAccountId = StorageAccountName; //TBD: once service will migrate to storageID we will remove this line;
6684

6785
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
6886
{
6987
{RestoreBackupItemParams.RecoveryPoint, RecoveryPoint},
70-
{RestoreBackupItemParams.StorageAccountId, storageId}
88+
{RestoreBackupItemParams.StorageAccountId, storageAccountId},
89+
{RestoreBackupItemParams.StorageAccountLocation, storageAccountlocation},
90+
{RestoreBackupItemParams.StorageAccountType, storageAccountType}
7191
}, HydraAdapter);
7292

7393
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType);
7494
var jobResponse = psBackupProvider.TriggerRestore();
7595

96+
WriteDebug(String.Format("Restore submitted"));
7697
var response = HydraAdapter.GetProtectedItemOperationStatusByURL(jobResponse.AzureAsyncOperation);
7798
while (response.OperationStatus.Status == "InProgress")
7899
{
100+
WriteDebug(String.Format("Restore inProgress"));
79101
response = HydraAdapter.GetProtectedItemOperationStatusByURL(jobResponse.AzureAsyncOperation);
80102
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
81103
}
82104

83105
if (response.OperationStatus.Status == "Completed")
84106
{
85107
// TBD -- Hydra change to add jobId in OperationStatusExtendedInfo
108+
WriteDebug(String.Format("Restore Completed"));
86109
string jobId = ""; //response.OperationStatus.Properties.jobId;
87110
var job = HydraAdapter.GetJob(jobId);
88111
//WriteObject(ConversionHelpers.GetJobModel(job));
89112
}
90113
});
91114
}
115+
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>
123+
internal void GetStorageResource(string storageAccountName, out string id, out string location, out string resourceType)
124+
{
125+
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
126+
{
127+
//Get-AzureRmResource | Where-Object { ($_.Name -eq "<StorageAccName>") -and ($_.ResourceType -eq "Microsoft.Storage/storageAccounts" -or $_.ResourceType -eq "Microsoft.ClassicStorage/storageAccounts")}
128+
ps.AddCommand("Get-AzureRmResource");
129+
ps.AddCommand("where-object");
130+
string filterString = String.Format(@"($_.Name -eq ""{0}"") -and ($_.ResourceType -eq ""Microsoft.Storage/storageAccounts"" -or $_.ResourceType -eq ""Microsoft.ClassicStorage/storageAccounts"")");
131+
ScriptBlock filter = ScriptBlock.Create(filterString);
132+
133+
ps.AddParameter("FilterScript", filter);
134+
var result = ps.Invoke();
135+
136+
if (ps.HadErrors)
137+
{
138+
WriteVerbose(string.Format("Error in Get-AzureRmResource"));
139+
throw new Exception(ps.HadErrors.ToString());
140+
}
141+
142+
if(result.Count == 0)
143+
{
144+
WriteVerbose(string.Format("Storage Account not fount"));
145+
throw new ArgumentException(Resources.RestoreAzureStorageNotFound);
146+
}
147+
else if (result.Count > 1)
148+
{
149+
WriteVerbose(string.Format("Found more than one StorageAccount with same name. Some thing went wrong"));
150+
throw new Exception(Resources.RestoreDiskMoreThanOneAccFound);
151+
}
152+
id = result[0].Members["ResourceId"].Value.ToString();
153+
location = result[0].Members["Location"].Value.ToString();
154+
resourceType = result[0].Members["ResourceType"].Value.ToString();
155+
}
156+
}
92157
}
93158
}

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

Lines changed: 25 additions & 15 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,18 +26,23 @@ 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

32+
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
33+
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
34+
string protectedItemName = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
35+
3136
List<AzureRmRecoveryServicesRecoveryPointBase> result = new List<AzureRmRecoveryServicesRecoveryPointBase>();
3237
foreach (RecoveryPointResource rp in rpList.RecoveryPointList.RecoveryPoints)
3338
{
3439
RecoveryPoint recPoint = rp.Properties as RecoveryPoint;
3540
AzureRmRecoveryServicesIaasVmRecoveryPoint rpBase = new AzureRmRecoveryServicesIaasVmRecoveryPoint()
3641
{
42+
Name = rp.Name,
3743
BackupManagementType = item.BackupManagementType,
38-
ItemName = item.Name,
39-
ContainerName = item.ContainerName,
44+
ItemName = protectedItemName,
45+
ContainerName = containerUri,
4046
ContainerType = item.ContainerType,
4147
RecoveryPointTime = Convert.ToDateTime(recPoint.RecoveryPointTime).ToLocalTime(),
4248
RecoveryPointType = recPoint.RecoveryPointType,
@@ -54,23 +60,27 @@ public static AzureRmRecoveryServicesRecoveryPointBase GetPSAzureRecoveryPoints(
5460
{
5561
if (rpResponse == null || rpResponse.RecPoint == null)
5662
{
57-
throw new ArgumentNullException("rpResponse is null");
63+
throw new ArgumentNullException(Resources.GetRPResponseIsNull);
5864
}
5965

6066
RecoveryPoint recPoint = rpResponse.RecPoint.Properties as RecoveryPoint;
67+
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
68+
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
69+
string protectedItemName = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
6170

6271
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-
};
72+
{
73+
Name = rpResponse.RecPoint.Name,
74+
BackupManagementType = item.BackupManagementType,
75+
ItemName = protectedItemName,
76+
ContainerName = containerUri,
77+
ContainerType = item.ContainerType,
78+
RecoveryPointTime = Convert.ToDateTime(recPoint.RecoveryPointTime).ToLocalTime(),
79+
RecoveryPointType = recPoint.RecoveryPointType,
80+
RecoveryPointId = rpResponse.RecPoint.Id,
81+
WorkloadType = item.WorkloadType,
82+
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
83+
};
7484
return result;
7585
}
7686
}

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

Lines changed: 20 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,14 +48,16 @@ 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,
4355
VirtualNetworkName = string.Empty,
4456
};
4557

4658
TriggerRestoreRequest triggerRestoreRequest = new TriggerRestoreRequest();
59+
triggerRestoreRequest.Item = new RestoreRequestResource();
60+
triggerRestoreRequest.Item.Properties = new RestoreRequest();
4761
triggerRestoreRequest.Item.Properties = restoreRequest;
4862

4963
var response = BmsAdapter.Client.Restore.TriggerRestoreAsync(resourceGroupName, resourceName, BmsAdapter.GetCustomRequestHeaders(),
@@ -52,4 +66,4 @@ public BaseRecoveryServicesJobResponse RestoreDisk(AzureRmRecoveryServicesIaasVm
5266
return response;
5367
}
5468
}
55-
}
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
@@ -166,6 +166,11 @@ public class AzureRmRecoveryServicesItemExtendedInfoBase : AzureRmRecoveryServic
166166
public class AzureRmRecoveryServicesRecoveryPointBase : AzureRmRecoveryServicesItemContext
167167
{
168168
private global::Microsoft.Azure.Management.RecoveryServices.Backup.Models.RecoveryPointResource rp;
169+
170+
/// <summary>
171+
///
172+
/// </summary>
173+
public string Name { get; set; }
169174

170175
/// <summary>
171176
///

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

0 commit comments

Comments
 (0)