Skip to content

Commit 325b10e

Browse files
committed
Network mapping operation (E2A) + few minor fixes
1 parent f5530c2 commit 325b10e

13 files changed

+377
-89
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesNetworkClient.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,5 @@ public NetworkListResponse GetAzureSiteRecoveryNetworks(string serverId)
3333
{
3434
return this.GetSiteRecoveryClient().Networks.List(serverId, this.GetRequestHeaders());
3535
}
36-
37-
/// <summary>
38-
/// Gets Azure Site Recovery Network.
39-
/// </summary>
40-
/// <param name="serverId">Server ID</param>
41-
/// <param name="networkId">Network ID</param>
42-
/// <returns>Network response</returns>
43-
public NetworkResponse GetAzureSiteRecoveryNetwork(string serverId, string networkId)
44-
{
45-
return this.GetSiteRecoveryClient().Networks.Get(networkId, serverId, this.GetRequestHeaders());
46-
}
4736
}
4837
}

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesNetworkMappingClient.cs

Lines changed: 214 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,67 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.IO;
17+
using System.Runtime.Serialization;
18+
using System.Xml;
1619
using Microsoft.WindowsAzure;
1720
using Microsoft.WindowsAzure.Management.SiteRecovery;
21+
// using Microsoft.WindowsAzure.Management.
1822
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
23+
using Microsoft.WindowsAzure.Commands.Common.Models;
24+
using Microsoft.WindowsAzure.Commands.Common;
25+
using System.Collections.Generic;
1926

2027
namespace Microsoft.Azure.Commands.RecoveryServices
2128
{
29+
/// <summary>
30+
/// Target network type.
31+
/// </summary>
32+
public enum TargetNetworkType
33+
{
34+
/// <summary>
35+
/// Server.
36+
/// </summary>
37+
Server = 0,
38+
39+
/// <summary>
40+
/// Azure.
41+
/// </summary>
42+
Azure,
43+
}
44+
45+
[DataContract(Namespace = "http://schemas.microsoft.com/windowsazure")]
46+
public class CreateNetworkMappingInput
47+
{
48+
[DataMember(Order = 1)]
49+
public string PrimaryServerId { get; set; }
50+
51+
[DataMember(Order = 2)]
52+
public string PrimaryNetworkId { get; set; }
53+
54+
[DataMember(Order = 3)]
55+
public string RecoveryServerId { get; set; }
56+
57+
[DataMember(Order = 4)]
58+
public string RecoveryNetworkId { get; set; }
59+
}
60+
61+
[DataContract(Namespace = "http://schemas.microsoft.com/windowsazure")]
62+
public class CreateAzureNetworkMappingInput
63+
{
64+
[DataMember(Order = 1)]
65+
public string PrimaryServerId { get; set; }
66+
67+
[DataMember(Order = 2)]
68+
public string PrimaryNetworkId { get; set; }
69+
70+
[DataMember(Order = 3)]
71+
public string AzureVMNetworkId { get; set; }
72+
73+
[DataMember(Order = 4)]
74+
public string AzureVMNetworkName { get; set; }
75+
}
76+
2277
/// <summary>
2378
/// Recovery services convenience client.
2479
/// </summary>
@@ -40,43 +95,99 @@ public NetworkMappingListResponse GetAzureSiteRecoveryNetworkMappings(
4095
}
4196

4297
/// <summary>
43-
/// Gets Azure Site Recovery Network mapping.
98+
/// Create Azure Site Recovery Network Mapping.
4499
/// </summary>
45-
/// <param name="networkId">Network ID</param>
46-
/// <param name="serverId">Server ID</param>
47-
/// <returns>Server response</returns>
48-
public NetworkMappingResponse GetAzureSiteRecoveryNetworkMapping(string networkId, string serverId)
100+
/// <param name="primaryServerId">Primary server Id</param>
101+
/// <param name="primaryNetworkId">Primary network Id</param>
102+
/// <param name="recoveryServerId">Recovery server Id</param>
103+
/// <param name="recoveryNetworkId">Recovery network Id</param>
104+
/// <returns>Job response</returns>
105+
public JobResponse NewAzureSiteRecoveryNetworkMapping(
106+
string primaryServerId,
107+
string primaryNetworkId,
108+
string recoveryServerId,
109+
string recoveryNetworkId)
49110
{
111+
CreateNetworkMappingInput createNetworkMappingInput =
112+
new CreateNetworkMappingInput();
113+
createNetworkMappingInput.PrimaryServerId = primaryServerId;
114+
createNetworkMappingInput.PrimaryNetworkId = primaryNetworkId;
115+
createNetworkMappingInput.RecoveryServerId = recoveryServerId;
116+
createNetworkMappingInput.RecoveryNetworkId = recoveryNetworkId;
117+
118+
NetworkMappingInput networkMappingInput = new NetworkMappingInput();
119+
networkMappingInput.TargetNetworkType = TargetNetworkType.Server.ToString();
120+
networkMappingInput.CreateNetworkMappingInput =
121+
DataContractUtils.Serialize<CreateNetworkMappingInput>(createNetworkMappingInput);
50122
return this.GetSiteRecoveryClient()
51123
.NetworkMappings
52-
.Get(networkId, serverId, this.GetRequestHeaders());
124+
.Create(networkMappingInput, this.GetRequestHeaders());
53125
}
54126

55127
/// <summary>
56-
/// Create Azure Site Recovery Network Mapping.
128+
/// Create Azure Site Recovery Azure Network Mapping.
57129
/// </summary>
58130
/// <param name="primaryServerId">Primary server Id</param>
59131
/// <param name="primaryNetworkId">Primary network Id</param>
60-
/// <param name="recoveryServerId">Recovery server Id</param>
132+
/// <param name="recoveryNetworkName">Recovery server Id</param>
61133
/// <param name="recoveryNetworkId">Recovery network Id</param>
62134
/// <returns>Job response</returns>
63-
public JobResponse NewAzureSiteRecoveryNetworkMapping(
135+
public JobResponse NewAzureSiteRecoveryAzureNetworkMapping(
64136
string primaryServerId,
65137
string primaryNetworkId,
66-
string recoveryServerId,
138+
string recoveryNetworkName,
67139
string recoveryNetworkId)
68140
{
69-
CreateNetworkMappingInput parameters = new CreateNetworkMappingInput();
70-
parameters.PrimaryServerId = primaryServerId;
71-
parameters.PrimaryNetworkId = primaryNetworkId;
72-
parameters.RecoveryServerId = recoveryServerId;
73-
parameters.RecoveryNetworkId = recoveryNetworkId;
141+
CreateAzureNetworkMappingInput createAzureNetworkMappingInput =
142+
new CreateAzureNetworkMappingInput();
143+
createAzureNetworkMappingInput.PrimaryServerId = primaryServerId;
144+
createAzureNetworkMappingInput.PrimaryNetworkId = primaryNetworkId;
145+
createAzureNetworkMappingInput.AzureVMNetworkName = recoveryNetworkName;
146+
createAzureNetworkMappingInput.AzureVMNetworkId = recoveryNetworkId;
74147

148+
NetworkMappingInput networkMappingInput = new NetworkMappingInput();
149+
networkMappingInput.TargetNetworkType = TargetNetworkType.Azure.ToString();
150+
networkMappingInput.CreateNetworkMappingInput =
151+
DataContractUtils.Serialize<CreateAzureNetworkMappingInput>(createAzureNetworkMappingInput);
75152
return this.GetSiteRecoveryClient()
76153
.NetworkMappings
77-
.Create(parameters, this.GetRequestHeaders());
154+
.Create(networkMappingInput, this.GetRequestHeaders());
78155
}
79156

157+
public void ValidateSubscriptionAccountAssociation(string azureSubscriptionId)
158+
{
159+
bool associatedSubscription = false;
160+
ProfileClient pc = new ProfileClient();
161+
List<AzureSubscription> subscriptions =
162+
pc.RefreshSubscriptions(AzureSession.CurrentContext.Environment);
163+
164+
foreach (AzureSubscription sub in subscriptions)
165+
{
166+
if (azureSubscriptionId.Equals(sub.Id.ToString(), StringComparison.OrdinalIgnoreCase))
167+
{
168+
associatedSubscription = true;
169+
break;
170+
}
171+
}
172+
173+
if (!associatedSubscription)
174+
{
175+
throw new InvalidOperationException(
176+
string.Format(
177+
Properties.Resources.SubscriptionIsNotAssociatedWithTheAccount,
178+
azureSubscriptionId));
179+
}
180+
}
181+
182+
public void ValidateVMNetworkSubscriptionAssociation(string subscriptionId, string azureNetworkId)
183+
{
184+
/*
185+
NetworkManagementClient networkClient =
186+
AzureSession.ClientFactory.CreateClient<NetworkManagementClient>(AzureSession.CurrentContext.Subscription, AzureEnvironment.Endpoint.ServiceManagement);
187+
var response = this.networkClient.Networks.List();
188+
var sites = response.VirtualNetworkSites;
189+
*/
190+
}
80191
/// <summary>
81192
/// Delete Azure Site Recovery Network Mapping.
82193
/// </summary>
@@ -89,14 +200,96 @@ public JobResponse RemoveAzureSiteRecoveryNetworkMapping(
89200
string primaryNetworkId,
90201
string recoveryServerId)
91202
{
92-
DeleteNetworkMappingInput parameters = new DeleteNetworkMappingInput();
93-
parameters.PrimaryServerId = primaryServerId;
94-
parameters.PrimaryNetworkId = primaryNetworkId;
95-
parameters.RecoveryServerId = recoveryServerId;
203+
NetworkUnMappingInput networkUnMappingInput = new NetworkUnMappingInput();
204+
networkUnMappingInput.PrimaryServerId = primaryServerId;
205+
networkUnMappingInput.PrimaryNetworkId = primaryNetworkId;
206+
networkUnMappingInput.RecoveryServerId = recoveryServerId;
96207

97208
return this.GetSiteRecoveryClient()
98209
.NetworkMappings
99-
.Delete(parameters, this.GetRequestHeaders());
210+
.Delete(networkUnMappingInput, this.GetRequestHeaders());
211+
}
212+
}
213+
214+
/// <summary>
215+
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
216+
/// DataContractUtils<T> which is the one doing the heavy lifting.
217+
/// </summary>
218+
public static class DataContractUtils
219+
{
220+
/// <summary>
221+
/// Serializes the supplied object to the string.
222+
/// </summary>
223+
/// <typeparam name="T">The object type.</typeparam>
224+
/// <param name="The object to serialize."></param>
225+
/// <returns>Serialized string.</returns>
226+
public static string Serialize<T>(T obj)
227+
{
228+
return DataContractUtils<T>.Serialize(obj);
229+
}
230+
231+
/// <summary>
232+
/// Deserialize the string to the expected object type.
233+
/// </summary>
234+
/// <param name="xmlString">Serialized string.</param>
235+
/// <param name="result">Deserialized object.</param>
236+
public static void Deserialize<T>(string xmlString, out T result)
237+
{
238+
result = DataContractUtils<T>.Deserialize(xmlString);
239+
}
240+
}
241+
242+
public static class DataContractUtils<T>
243+
{
244+
/// <summary>
245+
/// Serializes the propertyBagContainer to the string.
246+
/// </summary>
247+
/// <param name="propertyBagContainer"></param>
248+
/// <returns></returns>
249+
public static string Serialize(T propertyBagContainer)
250+
{
251+
var serializer = new DataContractSerializer(typeof(T));
252+
string xmlString;
253+
StringWriter sw = null;
254+
try
255+
{
256+
sw = new StringWriter();
257+
using (var writer = new XmlTextWriter(sw))
258+
{
259+
// Indent the XML so it's human readable.
260+
writer.Formatting = Formatting.Indented;
261+
serializer.WriteObject(writer, propertyBagContainer);
262+
writer.Flush();
263+
xmlString = sw.ToString();
264+
}
265+
}
266+
finally
267+
{
268+
if (sw != null)
269+
sw.Close();
270+
}
271+
272+
return xmlString;
273+
}
274+
275+
/// <summary>
276+
/// Deserialize the string to the propertyBagContainer.
277+
/// </summary>
278+
/// <param name="xmlString"></param>
279+
/// <returns></returns>
280+
public static T Deserialize(string xmlString)
281+
{
282+
T propertyBagContainer;
283+
using (Stream stream = new MemoryStream())
284+
{
285+
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
286+
stream.Write(data, 0, data.Length);
287+
stream.Position = 0;
288+
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
289+
propertyBagContainer = (T)deserializer.ReadObject(stream);
290+
}
291+
292+
return propertyBagContainer;
100293
}
101294
}
102295
}

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesPEClient.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public JobResponse SetProtectionOnProtectionEntity(
8484

8585
if (0 == string.Compare(EnableProtection, protection, StringComparison.OrdinalIgnoreCase))
8686
{
87-
jobResponse =
88-
this.GetSiteRecoveryClient().ProtectionEntity.EnableProtection(
87+
jobResponse = null;
88+
/* this.GetSiteRecoveryClient().ProtectionEntity.EnableProtection(
8989
protectionContainerId,
9090
virtualMachineId,
91-
requestHeaders);
91+
requestHeaders); */
9292
}
9393
else if (0 == string.Compare(DisableProtection, protection, StringComparison.OrdinalIgnoreCase))
9494
{
@@ -169,10 +169,10 @@ public JobResponse StartAzureSiteRecoveryCommitFailover(
169169
string protectionContainerId,
170170
string protectionEntityId)
171171
{
172-
return this.GetSiteRecoveryClient().ProtectionEntity.CommitFailover(
173-
protectionContainerId,
174-
protectionEntityId,
175-
this.GetRequestHeaders());
172+
return null; /* this.GetSiteRecoveryClient().ProtectionEntity.CommitFailover(
173+
protectionContainerId,
174+
protectionEntityId,
175+
this.GetRequestHeaders()); */
176176
}
177177

178178
/// <summary>

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Properties/Resources.Designer.cs

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

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,7 @@ ClientRequestId: {3}</value>
191191
<data name="RemoveRPWhatIfMessage" xml:space="preserve">
192192
<value>Removing Recovery Plan</value>
193193
</data>
194+
<data name="SubscriptionIsNotAssociatedWithTheAccount" xml:space="preserve">
195+
<value>Subscription {0} is not associated with the account</value>
196+
</data>
194197
</root>

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Service/GetAzureSiteRecoveryNetwork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GetAzureSiteRecoveryNetwork : RecoveryServicesCmdletBase
3333
/// <summary>
3434
/// Gets or sets Server object.
3535
/// </summary>
36-
[Parameter(Mandatory = true)]
36+
[Parameter(Mandatory = true, ValueFromPipeline = true)]
3737
[ValidateNotNullOrEmpty]
3838
public ASRServer Server {get; set;}
3939
#endregion Parameters

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Service/GetAzureSiteRecoveryStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GetAzureSiteRecoveryStorage : RecoveryServicesCmdletBase
3333
/// <summary>
3434
/// Gets or sets Server object.
3535
/// </summary>
36-
[Parameter(Mandatory = true)]
36+
[Parameter(Mandatory = true, ValueFromPipeline = true)]
3737
[ValidateNotNullOrEmpty]
3838
public ASRServer Server {get; set;}
3939
#endregion Parameters

0 commit comments

Comments
 (0)