1
+ // ----------------------------------------------------------------------------------
2
+ //
3
+ // Copyright Microsoft Corporation
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ // ----------------------------------------------------------------------------------
14
+
15
+ using System ;
16
+ using System . IO ;
17
+ using System . Runtime . Serialization ;
18
+ using System . Xml ;
19
+ using Microsoft . WindowsAzure ;
20
+ using Microsoft . WindowsAzure . Management . SiteRecovery ;
21
+ // using Microsoft.WindowsAzure.Management.
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 ;
26
+
27
+ namespace Microsoft . Azure . Commands . RecoveryServices
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
+
77
+ /// <summary>
78
+ /// Recovery services convenience client.
79
+ /// </summary>
80
+ public partial class PSRecoveryServicesClient
81
+ {
82
+ /// <summary>
83
+ /// Gets Azure Site Recovery Network mappings.
84
+ /// </summary>
85
+ /// <param name="primaryServerId">Primary server ID</param>
86
+ /// <param name="recoveryServerId">Recovery server ID</param>
87
+ /// <returns>Network mapping list response</returns>
88
+ public NetworkMappingListResponse GetAzureSiteRecoveryNetworkMappings (
89
+ string primaryServerId ,
90
+ string recoveryServerId )
91
+ {
92
+ return this . GetSiteRecoveryClient ( )
93
+ . NetworkMappings
94
+ . List ( primaryServerId , recoveryServerId , this . GetRequestHeaders ( ) ) ;
95
+ }
96
+
97
+ /// <summary>
98
+ /// Create Azure Site Recovery Network Mapping.
99
+ /// </summary>
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 )
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 ) ;
122
+ return this . GetSiteRecoveryClient ( )
123
+ . NetworkMappings
124
+ . Create ( networkMappingInput , this . GetRequestHeaders ( ) ) ;
125
+ }
126
+
127
+ /// <summary>
128
+ /// Create Azure Site Recovery Azure Network Mapping.
129
+ /// </summary>
130
+ /// <param name="primaryServerId">Primary server Id</param>
131
+ /// <param name="primaryNetworkId">Primary network Id</param>
132
+ /// <param name="recoveryNetworkName">Recovery server Id</param>
133
+ /// <param name="recoveryNetworkId">Recovery network Id</param>
134
+ /// <returns>Job response</returns>
135
+ public JobResponse NewAzureSiteRecoveryAzureNetworkMapping (
136
+ string primaryServerId ,
137
+ string primaryNetworkId ,
138
+ string recoveryNetworkName ,
139
+ string recoveryNetworkId )
140
+ {
141
+ CreateAzureNetworkMappingInput createAzureNetworkMappingInput =
142
+ new CreateAzureNetworkMappingInput ( ) ;
143
+ createAzureNetworkMappingInput . PrimaryServerId = primaryServerId ;
144
+ createAzureNetworkMappingInput . PrimaryNetworkId = primaryNetworkId ;
145
+ createAzureNetworkMappingInput . AzureVMNetworkName = recoveryNetworkName ;
146
+ createAzureNetworkMappingInput . AzureVMNetworkId = recoveryNetworkId ;
147
+
148
+ NetworkMappingInput networkMappingInput = new NetworkMappingInput ( ) ;
149
+ networkMappingInput . TargetNetworkType = TargetNetworkType . Azure . ToString ( ) ;
150
+ networkMappingInput . CreateNetworkMappingInput =
151
+ DataContractUtils . Serialize < CreateAzureNetworkMappingInput > ( createAzureNetworkMappingInput ) ;
152
+ return this . GetSiteRecoveryClient ( )
153
+ . NetworkMappings
154
+ . Create ( networkMappingInput , this . GetRequestHeaders ( ) ) ;
155
+ }
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
+ }
191
+ /// <summary>
192
+ /// Delete Azure Site Recovery Network Mapping.
193
+ /// </summary>
194
+ /// <param name="primaryServerId">Primary server Id</param>
195
+ /// <param name="primaryNetworkId">Primary network Id</param>
196
+ /// <param name="recoveryServerId">Recovery server Id</param>
197
+ /// <returns>Job response</returns>
198
+ public JobResponse RemoveAzureSiteRecoveryNetworkMapping (
199
+ string primaryServerId ,
200
+ string primaryNetworkId ,
201
+ string recoveryServerId )
202
+ {
203
+ NetworkUnMappingInput networkUnMappingInput = new NetworkUnMappingInput ( ) ;
204
+ networkUnMappingInput . PrimaryServerId = primaryServerId ;
205
+ networkUnMappingInput . PrimaryNetworkId = primaryNetworkId ;
206
+ networkUnMappingInput . RecoveryServerId = recoveryServerId ;
207
+
208
+ return this . GetSiteRecoveryClient ( )
209
+ . NetworkMappings
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 ;
293
+ }
294
+ }
295
+ }
0 commit comments