Skip to content

Commit dcf396a

Browse files
committed
Adding support for following cmdlets
1) Get Storage classification (Get-AzureRmSiteRecoveryStorageClassification) 2) Pair storages (Start-AzureRmSiteRecoveryStorageClassificationMappingJob) 3) Unpair Storages (Start-AzureRmSiteRecoveryStorageClassificationUnmappingJob)
1 parent e586593 commit dcf396a

12 files changed

+974
-3
lines changed

src/ResourceManager/SiteRecovery/Commands.SiteRecovery/Commands.SiteRecovery.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
5353
</Reference>
5454
<Reference Include="Microsoft.Azure.Management.SiteRecovery, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
55-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.SiteRecovery.1.0.2-preview\lib\net40\Microsoft.Azure.Management.SiteRecovery.dll</HintPath>
56-
<Private>True</Private>
55+
<SpecificVersion>False</SpecificVersion>
56+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.SiteRecovery.1.0.3-preview\lib\net40\Microsoft.Azure.Management.SiteRecovery.dll</HintPath>
5757
</Reference>
5858
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.18.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5959
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.18.206251556\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
@@ -125,6 +125,7 @@
125125
<Compile Include="Common\PSSiteRecoveryPolicyClient.cs" />
126126
<Compile Include="Common\PSSiteRecoveryRecoveryServicesProviderClient.cs" />
127127
<Compile Include="Common\PSSiteRecoveryFabricClient.cs" />
128+
<Compile Include="Common\PSSiteRecoveryStorageClassificationClient.cs" />
128129
<Compile Include="Common\PSSiteRecoveryVaultClient.cs" />
129130
<Compile Include="Common\PSSiteRecoveryVaultExtendedInfoClient.cs" />
130131
<Compile Include="Common\PSSiteRecoveryVMClient.cs" />
@@ -166,6 +167,9 @@
166167
<Compile Include="Policy\StartAzureSiteRecoveryPolicyAssociationJob.cs" />
167168
<Compile Include="Policy\StartAzureSiteRecoveryPolicyDissociationJob.cs" />
168169
<Compile Include="Server\GetAzureSiteRecoveryServer.cs" />
170+
<Compile Include="Storage\Classification\GetAzureSiteRecoveryStorageClassification.cs" />
171+
<Compile Include="Storage\Classification\StartAzureSiteRecoveryStorageClassificationMappingJob.cs" />
172+
<Compile Include="Storage\Classification\StartAzureSiteRecoveryStorageClassificationUnmappingJob.cs" />
169173
<Compile Include="Utilities\CertUtils.cs" />
170174
<Compile Include="Utilities\Utilities.cs" />
171175
<Compile Include="Vault\RemoveAzureSiteRecoveryVault.cs" />

src/ResourceManager/SiteRecovery/Commands.SiteRecovery/Common/PSSiteRecoveryFabricClient.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
17+
using System.Threading;
18+
using System.Threading.Tasks;
1619
using Microsoft.Azure.Management.SiteRecovery;
1720
using Microsoft.Azure.Management.SiteRecovery.Models;
1821
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
@@ -24,6 +27,31 @@ namespace Microsoft.Azure.Commands.SiteRecovery
2427
/// </summary>
2528
public partial class PSRecoveryServicesClient
2629
{
30+
/// <summary>
31+
/// Gets all fabrics associated with a vault.
32+
/// </summary>
33+
/// <param name="callback">Callback to execute on the result.</param>
34+
/// <returns>Task object tracking async operation.</returns>
35+
public Task EnumerateFabricsAsync(Action<IEnumerable<Fabric>> callback)
36+
{
37+
CancellationToken cancellationToken = new CancellationToken();
38+
39+
Task backgroundTask = new Task(new Action(() =>
40+
{
41+
Task<FabricListResponse> storageTask =
42+
this.GetSiteRecoveryClient().Fabrics.ListAsync(
43+
this.GetRequestHeaders(),
44+
cancellationToken);
45+
46+
Task.WaitAll(storageTask);
47+
48+
callback(storageTask.Result.Fabrics);
49+
}));
50+
51+
backgroundTask.Start();
52+
return backgroundTask;
53+
}
54+
2755
/// <summary>
2856
/// Gets Azure Site Recovery Fabrics.
2957
/// </summary>
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.Azure.Management.SiteRecovery;
6+
using Microsoft.Azure.Management.SiteRecovery.Models;
7+
8+
namespace Microsoft.Azure.Commands.SiteRecovery
9+
{
10+
public partial class PSRecoveryServicesClient
11+
{
12+
/// <summary>
13+
/// Gets all storage classifications associated with a vault.
14+
/// </summary>
15+
/// <param name="callback">Callback to execute on the result.</param>
16+
/// <returns>Task object tracking async operation.</returns>
17+
public Task EnumerateStorageClassificationsAsync(Action<IEnumerable<StorageClassification>> callback)
18+
{
19+
CancellationToken cancellationToken = new CancellationToken();
20+
21+
Task backgroundTask = new Task(new Action(() =>
22+
{
23+
Task<StorageClassificationListResponse> storageTask =
24+
this.GetSiteRecoveryClient().StorageClassification.ListAllAsync(
25+
this.GetRequestHeaders(),
26+
cancellationToken);
27+
28+
Task.WaitAll(storageTask);
29+
30+
callback(storageTask.Result.StorageClassifications);
31+
32+
while (!string.IsNullOrEmpty(storageTask.Result.NextLink))
33+
{
34+
storageTask =
35+
this.GetSiteRecoveryClient().StorageClassification.ListNextAsync(
36+
storageTask.Result.NextLink,
37+
this.GetRequestHeaders(),
38+
cancellationToken);
39+
40+
Task.WaitAll(storageTask);
41+
42+
callback(storageTask.Result.StorageClassifications);
43+
}
44+
}));
45+
46+
backgroundTask.Start();
47+
return backgroundTask;
48+
}
49+
50+
/// <summary>
51+
/// Gets all storage classifications associated with a vault.
52+
/// </summary>
53+
/// <param name="callback">Callback to execute on the result.</param>
54+
/// <returns>Task object tracking async operation.</returns>
55+
public Task EnumerateStorageClassificationMappingsAsync(Action<IEnumerable<StorageClassificationMapping>> callback)
56+
{
57+
CancellationToken cancellationToken = new CancellationToken();
58+
59+
Task backgroundTask = new Task(new Action(() =>
60+
{
61+
Task<StorageClassificationMappingListResponse> storageTask =
62+
this.GetSiteRecoveryClient().StorageClassificationMapping.ListAllAsync(
63+
this.GetRequestHeaders(),
64+
cancellationToken);
65+
66+
Task.WaitAll(storageTask);
67+
68+
callback(storageTask.Result.StorageClassificationMappings);
69+
70+
while (!string.IsNullOrEmpty(storageTask.Result.NextLink))
71+
{
72+
storageTask =
73+
this.GetSiteRecoveryClient().StorageClassificationMapping.ListNextAsync(
74+
storageTask.Result.NextLink,
75+
this.GetRequestHeaders(),
76+
cancellationToken);
77+
78+
Task.WaitAll(storageTask);
79+
80+
callback(storageTask.Result.StorageClassificationMappings);
81+
}
82+
}));
83+
84+
backgroundTask.Start();
85+
return backgroundTask;
86+
}
87+
88+
/// <summary>
89+
/// Starts job for unmapping storage classifications.
90+
/// </summary>
91+
/// <param name="mapping">Classification mapping.</param>
92+
/// <returns>Job object.</returns>
93+
public ASRJob UnmapStorageClassifications(StorageClassificationMapping mapping)
94+
{
95+
string[] tokens = mapping.Id.UnFormatArmId(
96+
ARMResourceIdPaths.StorageClassificationMappingResourceIdPath);
97+
LongRunningOperationResponse operationResponse =
98+
this.GetSiteRecoveryClient().StorageClassificationMapping
99+
.BeginUnpairStorageClassification(
100+
tokens[0],
101+
tokens[1],
102+
tokens[2],
103+
this.GetRequestHeaders());
104+
105+
JobResponse jobResponse =
106+
this.GetAzureSiteRecoveryJobDetails(
107+
PSRecoveryServicesClient.GetJobIdFromReponseLocation(operationResponse.Location));
108+
109+
return new ASRJob(jobResponse.Job);
110+
}
111+
112+
/// <summary>
113+
/// Starts job for mapping storage classification.
114+
/// </summary>
115+
/// <param name="primaryClassification">Primary classification.</param>
116+
/// <param name="recoveryClassification">Recovery classification.</param>
117+
/// <param name="armName">Optional. ARM name of the mapping.</param>
118+
/// <returns>Job object.</returns>
119+
public ASRJob MapStorageClassification(
120+
ASRStorageClassification primaryClassification,
121+
ASRStorageClassification recoveryClassification,
122+
string armName = null)
123+
{
124+
string[] tokens = primaryClassification.StorageClassificationId.UnFormatArmId(
125+
ARMResourceIdPaths.StorageClassificationResourceIdPath);
126+
127+
if (string.IsNullOrEmpty(armName))
128+
{
129+
armName = string.Format(
130+
"StrgMap_{0}_{1}",
131+
primaryClassification.StorageClassificationFriendlyName,
132+
recoveryClassification.StorageClassificationFriendlyName);
133+
}
134+
135+
var props = new StorageClassificationMappingInputProperties()
136+
{
137+
TargetStorageClassificationId = recoveryClassification.StorageClassificationId
138+
};
139+
140+
var input = new StorageClassificationMappingInput()
141+
{
142+
Properties = props
143+
};
144+
145+
LongRunningOperationResponse operationResponse =
146+
this.GetSiteRecoveryClient().StorageClassificationMapping
147+
.BeginPairStorageClassification(
148+
tokens[0],
149+
tokens[1],
150+
armName,
151+
input,
152+
this.GetRequestHeaders());
153+
154+
JobResponse jobResponse =
155+
this.GetAzureSiteRecoveryJobDetails(
156+
PSRecoveryServicesClient.GetJobIdFromReponseLocation(operationResponse.Location));
157+
158+
return new ASRJob(jobResponse.Job);
159+
}
160+
}
161+
162+
/// <summary>
163+
/// Extension methods for Storage classification.
164+
/// </summary>
165+
public static class StorageClassificationExtensions
166+
{
167+
/// <summary>
168+
/// Gets primary storage classification ARM Id.
169+
/// </summary>
170+
/// <param name="mapping">Storage classification mapping input.</param>
171+
/// <returns>ARM Id of the primary storage classification.</returns>
172+
public static string GetPrimaryStorageClassificationId(
173+
this StorageClassificationMapping mapping)
174+
{
175+
string[] tokens = mapping.Id.UnFormatArmId(
176+
ARMResourceIdPaths.StorageClassificationMappingResourceIdPath);
177+
178+
string vaultId = mapping.Id.GetVaultArmId();
179+
180+
return vaultId + "/" + string.Format(
181+
ARMResourceIdPaths.StorageClassificationResourceIdPath,
182+
tokens[0],
183+
tokens[1]);
184+
}
185+
186+
/// <summary>
187+
/// Gets fabric Id from classification ARM Id.
188+
/// </summary>
189+
/// <param name="classification">Storage classification.</param>
190+
/// <returns>ARM Id of the fabric.</returns>
191+
public static string GetFabricId(
192+
this StorageClassification classification)
193+
{
194+
string[] tokens = classification.Id.UnFormatArmId(
195+
ARMResourceIdPaths.StorageClassificationResourceIdPath);
196+
197+
string vaultId = classification.Id.GetVaultArmId();
198+
199+
return vaultId + "/" + string.Format(
200+
ARMResourceIdPaths.FabricResourceIdPath,
201+
tokens[0]);
202+
}
203+
204+
/// <summary>
205+
/// Gets powershell object from Storage classification object.
206+
/// </summary>
207+
/// <param name="classification">Classification to process.</param>
208+
/// <param name="classificationMap">Dictionary of all possible classifications.</param>
209+
/// <param name="fabricMap">Dictionary of list of fabrics.</param>
210+
/// <param name="mappingsDict">Dictionary of mapping objects.</param>
211+
/// <returns>Powershell representation of storage classification.</returns>
212+
public static ASRStorageClassification GetPSObject(
213+
this StorageClassification classification,
214+
Dictionary<string, StorageClassification> classificationMap,
215+
Dictionary<string, Fabric> fabricMap,
216+
Dictionary<string, List<StorageClassificationMapping>> mappingsDict)
217+
{
218+
var fabric = fabricMap[classification.GetFabricId()];
219+
List<StorageClassificationMapping> targetClassifications;
220+
221+
return new ASRStorageClassification()
222+
{
223+
FabricFriendlyName = fabric.Properties.FriendlyName,
224+
FabricId = fabric.Id,
225+
StorageClassificationFriendlyName =
226+
classification.Properties.FriendlyName,
227+
StorageClassificationId = classification.Id,
228+
TargetClassifications =
229+
mappingsDict.TryGetValue(
230+
classification.Id,
231+
out targetClassifications) ?
232+
targetClassifications.ConvertAll(item =>
233+
classificationMap[item.Properties.TargetStorageClassificationId]
234+
.GetPSObject(
235+
classificationMap,
236+
fabricMap,
237+
mappingsDict)) :
238+
new List<ASRStorageClassification>()
239+
};
240+
}
241+
}
242+
}

0 commit comments

Comments
 (0)