Skip to content

Commit 817f8d8

Browse files
committed
Merge branch 'dev1' of https://github.com/MabOneSdk/azure-powershell into dev1
2 parents 36b67e0 + 5e918a3 commit 817f8d8

File tree

17 files changed

+332
-83
lines changed

17 files changed

+332
-83
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/Container/GetAzureRmBackupManagementServer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
3030
[Cmdlet(VerbsCommon.Get, "AzureRmBackupManagementServer"), OutputType(typeof(List<AzureRmRecoveryServicesContainerBase>), typeof(AzureRmRecoveryServicesContainerBase))]
3131
public class GetAzureRmBackupManagementServer : RecoveryServicesBackupCmdletBase
3232
{
33-
[Parameter(Mandatory = true, HelpMessage = ParamHelpMsg.Container.Name)]
33+
[Parameter(Mandatory = false, HelpMessage = ParamHelpMsg.Container.Name)]
3434
[ValidateNotNullOrEmpty]
3535
public string Name { get; set; }
3636

@@ -50,15 +50,14 @@ public override void ExecuteCmdlet()
5050
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(ContainerType.Windows, BackupManagementType.Scdpm);
5151

5252
var containerModels = psBackupProvider.ListBackupManagementServers();
53-
54-
if (containerModels.Count == 1)
55-
{
56-
WriteObject(containerModels.First());
57-
}
58-
else
53+
if (!string.IsNullOrEmpty(this.Name))
5954
{
60-
WriteObject(containerModels);
55+
if (containerModels != null)
56+
{
57+
containerModels = containerModels.Where(x => x.Name == this.Name).ToList();
58+
}
6159
}
60+
WriteObject(containerModels);
6261
});
6362
}
6463
}

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/ContainerAPIs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ public IEnumerable<ProtectionContainerResource> ListContainers(ProtectionContain
4242
/// <returns></returns>
4343
public IEnumerable<BackupEngineResource> ListBackupEngines(BackupEngineListQueryParams queryParams)
4444
{
45-
var listResponse = BmsAdapter.Client.BackupEngine.ListAsync(BmsAdapter.GetResourceGroupName(), BmsAdapter.GetResourceName(), queryParams,
46-
null, BmsAdapter.GetCustomRequestHeaders(), BmsAdapter.CmdletCancellationToken).Result;
45+
PaginationRequest paginationParam = new PaginationRequest();
46+
paginationParam.Top = "200";
47+
var listResponse = BmsAdapter.Client.BackupEngine.ListAsync(BmsAdapter.GetResourceGroupName(), BmsAdapter.GetResourceName(), queryParams, paginationParam,
48+
BmsAdapter.GetCustomRequestHeaders(), BmsAdapter.CmdletCancellationToken).Result;
4749
return listResponse.ItemList.BackupEngines;
4850
}
4951

0 commit comments

Comments
 (0)