13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using System ;
16
+ using System . IO ;
17
+ using System . Runtime . Serialization ;
18
+ using System . Xml ;
16
19
using Microsoft . WindowsAzure ;
17
20
using Microsoft . WindowsAzure . Management . SiteRecovery ;
21
+ // using Microsoft.WindowsAzure.Management.
18
22
using Microsoft . WindowsAzure . Management . SiteRecovery . Models ;
23
+ using Microsoft . WindowsAzure . Commands . Common . Models ;
24
+ using Microsoft . WindowsAzure . Commands . Common ;
25
+ using System . Collections . Generic ;
19
26
20
27
namespace Microsoft . Azure . Commands . RecoveryServices
21
28
{
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
+
22
77
/// <summary>
23
78
/// Recovery services convenience client.
24
79
/// </summary>
@@ -40,43 +95,99 @@ public NetworkMappingListResponse GetAzureSiteRecoveryNetworkMappings(
40
95
}
41
96
42
97
/// <summary>
43
- /// Gets Azure Site Recovery Network mapping .
98
+ /// Create Azure Site Recovery Network Mapping .
44
99
/// </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 )
49
110
{
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 ) ;
50
122
return this . GetSiteRecoveryClient ( )
51
123
. NetworkMappings
52
- . Get ( networkId , serverId , this . GetRequestHeaders ( ) ) ;
124
+ . Create ( networkMappingInput , this . GetRequestHeaders ( ) ) ;
53
125
}
54
126
55
127
/// <summary>
56
- /// Create Azure Site Recovery Network Mapping.
128
+ /// Create Azure Site Recovery Azure Network Mapping.
57
129
/// </summary>
58
130
/// <param name="primaryServerId">Primary server Id</param>
59
131
/// <param name="primaryNetworkId">Primary network Id</param>
60
- /// <param name="recoveryServerId ">Recovery server Id</param>
132
+ /// <param name="recoveryNetworkName ">Recovery server Id</param>
61
133
/// <param name="recoveryNetworkId">Recovery network Id</param>
62
134
/// <returns>Job response</returns>
63
- public JobResponse NewAzureSiteRecoveryNetworkMapping (
135
+ public JobResponse NewAzureSiteRecoveryAzureNetworkMapping (
64
136
string primaryServerId ,
65
137
string primaryNetworkId ,
66
- string recoveryServerId ,
138
+ string recoveryNetworkName ,
67
139
string recoveryNetworkId )
68
140
{
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 ;
74
147
148
+ NetworkMappingInput networkMappingInput = new NetworkMappingInput ( ) ;
149
+ networkMappingInput . TargetNetworkType = TargetNetworkType . Azure . ToString ( ) ;
150
+ networkMappingInput . CreateNetworkMappingInput =
151
+ DataContractUtils . Serialize < CreateAzureNetworkMappingInput > ( createAzureNetworkMappingInput ) ;
75
152
return this . GetSiteRecoveryClient ( )
76
153
. NetworkMappings
77
- . Create ( parameters , this . GetRequestHeaders ( ) ) ;
154
+ . Create ( networkMappingInput , this . GetRequestHeaders ( ) ) ;
78
155
}
79
156
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
+ }
80
191
/// <summary>
81
192
/// Delete Azure Site Recovery Network Mapping.
82
193
/// </summary>
@@ -89,14 +200,96 @@ public JobResponse RemoveAzureSiteRecoveryNetworkMapping(
89
200
string primaryNetworkId ,
90
201
string recoveryServerId )
91
202
{
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 ;
96
207
97
208
return this . GetSiteRecoveryClient ( )
98
209
. 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 ;
100
293
}
101
294
}
102
295
}
0 commit comments