1
1
// ----------------------------------------------------------------------------------
2
- //
2
+ //
3
3
// Copyright Microsoft Corporation
4
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
5
// you may not use this file except in compliance with the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using System ;
16
+ using System . Management . Automation ;
16
17
using System . Runtime . Serialization ;
17
18
using System . Text ;
18
19
using System . Threading ;
19
20
using System . Xml ;
20
- using Microsoft . Azure . Commands . Common . Authentication ;
21
+ using Microsoft . Azure . Commands . RecoveryServices . SiteRecovery . Properties ;
21
22
using Microsoft . Azure . Commands . ResourceManager . Common ;
22
- using Microsoft . Azure . Management . RecoveryServices . SiteRecovery . Models ;
23
23
using Microsoft . Rest . Azure ;
24
+ using Microsoft . Rest . Serialization ;
24
25
using Newtonsoft . Json ;
26
+ using Job = Microsoft . Azure . Management . RecoveryServices . SiteRecovery . Models . Job ;
25
27
26
28
namespace Microsoft . Azure . Commands . RecoveryServices . SiteRecovery
27
29
{
28
30
/// <summary>
29
- /// The base class for all Windows Azure Recovery Services commands
31
+ /// The base class for all Windows Azure Recovery Services commands
30
32
/// </summary>
31
33
public abstract class SiteRecoveryCmdletBase : AzureRMCmdlet
32
34
{
33
35
/// <summary>
34
- /// Recovery Services client.
35
- /// </summary>
36
- private PSRecoveryServicesClient recoveryServicesClient ;
37
-
38
- /// <summary>
39
- /// Gets or sets a value indicating whether stop processing has been triggered.
36
+ /// Gets or sets a value indicating whether stop processing has been triggered.
40
37
/// </summary>
41
38
internal bool StopProcessingFlag { get ; set ; }
42
39
43
40
/// <summary>
44
- /// Gets Recovery Services client.
41
+ /// Gets Recovery Services client.
45
42
/// </summary>
46
43
internal PSRecoveryServicesClient RecoveryServicesClient
47
44
{
48
45
get
49
46
{
50
- if ( this . recoveryServicesClient == null )
47
+ if ( recoveryServicesClient == null )
51
48
{
52
- this . recoveryServicesClient = new PSRecoveryServicesClient ( DefaultProfile ) ;
49
+ recoveryServicesClient = new PSRecoveryServicesClient ( DefaultProfile ) ;
53
50
}
54
51
55
- return this . recoveryServicesClient ;
52
+ return recoveryServicesClient ;
56
53
}
57
54
}
58
55
59
56
/// <summary>
60
- /// Virtual method to be implemented by Site Recovery cmdlets.
57
+ /// Recovery Services client.
58
+ /// </summary>
59
+ private PSRecoveryServicesClient recoveryServicesClient ;
60
+
61
+ /// <summary>
62
+ /// Virtual method to be implemented by Site Recovery cmdlets.
61
63
/// </summary>
62
64
public virtual void ExecuteSiteRecoveryCmdlet ( )
63
65
{
64
66
SiteRecoveryAutoMapperProfile . Initialize ( ) ;
67
+
65
68
// Do Nothing
66
69
}
67
70
68
71
/// <summary>
69
- /// Overriding base implementation go execute cmdlet.
72
+ /// Overriding base implementation go execute cmdlet.
70
73
/// </summary>
71
74
public override void ExecuteCmdlet ( )
72
75
{
@@ -77,47 +80,57 @@ public override void ExecuteCmdlet()
77
80
}
78
81
catch ( Exception ex )
79
82
{
80
- this . HandleException ( ex ) ;
83
+ HandleException ( ex ) ;
81
84
}
82
85
}
83
86
84
87
/// <summary>
85
- /// Exception handler.
88
+ /// Exception handler.
86
89
/// </summary>
87
90
/// <param name="ex">Exception to handle.</param>
88
91
public void HandleException ( Exception ex )
89
92
{
90
- string clientRequestIdMsg = string . Empty ;
91
- if ( this . recoveryServicesClient != null )
93
+ var clientRequestIdMsg = string . Empty ;
94
+ if ( recoveryServicesClient != null )
92
95
{
93
- clientRequestIdMsg = "ClientRequestId: " + this . recoveryServicesClient . ClientRequestId + "\n " ;
96
+ clientRequestIdMsg = "ClientRequestId: " +
97
+ recoveryServicesClient . ClientRequestId +
98
+ "\n " ;
94
99
}
95
100
96
- CloudException cloudException = ex as CloudException ;
97
- if ( cloudException != null && cloudException . Body != null && cloudException . Response != null )
98
- {
101
+ var cloudException = ex as CloudException ;
102
+ if ( cloudException != null &&
103
+ cloudException . Body != null &&
104
+ cloudException . Response != null )
105
+ {
99
106
try
100
107
{
101
108
if ( cloudException . Message != null )
102
109
{
103
- ARMError error = Rest . Serialization . SafeJsonConvert . DeserializeObject < ARMError > ( cloudException . Response . Content ) ; ;
104
- StringBuilder exceptionMessage = new StringBuilder ( ) ;
105
- exceptionMessage . Append ( Properties . Resources . CloudExceptionDetails ) ;
110
+ var error =
111
+ SafeJsonConvert . DeserializeObject < ARMError > ( cloudException . Response
112
+ . Content ) ;
113
+ ;
114
+ var exceptionMessage = new StringBuilder ( ) ;
115
+ exceptionMessage . Append ( Resources . CloudExceptionDetails ) ;
106
116
107
117
if ( error . Error . Details != null )
108
118
{
109
- foreach ( ARMExceptionDetails detail in error . Error . Details )
119
+ foreach ( var detail in error . Error . Details )
110
120
{
111
121
if ( ! string . IsNullOrEmpty ( detail . ErrorCode ) )
112
122
exceptionMessage . AppendLine ( "ErrorCode: " + detail . ErrorCode ) ;
113
123
if ( ! string . IsNullOrEmpty ( detail . Message ) )
114
124
exceptionMessage . AppendLine ( "Message: " + detail . Message ) ;
115
125
if ( ! string . IsNullOrEmpty ( detail . PossibleCauses ) )
116
- exceptionMessage . AppendLine ( "Possible Causes: " + detail . PossibleCauses ) ;
126
+ exceptionMessage . AppendLine (
127
+ "Possible Causes: " + detail . PossibleCauses ) ;
117
128
if ( ! string . IsNullOrEmpty ( detail . RecommendedAction ) )
118
- exceptionMessage . AppendLine ( "Recommended Action: " + detail . RecommendedAction ) ;
129
+ exceptionMessage . AppendLine (
130
+ "Recommended Action: " + detail . RecommendedAction ) ;
119
131
if ( ! string . IsNullOrEmpty ( detail . ClientRequestId ) )
120
- exceptionMessage . AppendLine ( "ClientRequestId: " + detail . ClientRequestId ) ;
132
+ exceptionMessage . AppendLine (
133
+ "ClientRequestId: " + detail . ClientRequestId ) ;
121
134
if ( ! string . IsNullOrEmpty ( detail . ActivityId ) )
122
135
exceptionMessage . AppendLine ( "ActivityId: " + detail . ActivityId ) ;
123
136
@@ -134,52 +147,44 @@ public void HandleException(Exception ex)
134
147
135
148
throw new InvalidOperationException ( exceptionMessage . ToString ( ) ) ;
136
149
}
137
- else
138
- {
139
- throw new Exception (
140
- string . Format (
141
- Properties . Resources . InvalidCloudExceptionErrorMessage ,
150
+
151
+ throw new Exception ( string . Format ( Resources . InvalidCloudExceptionErrorMessage ,
142
152
clientRequestIdMsg + ex . Message ) ,
143
- ex ) ;
144
- }
153
+ ex ) ;
145
154
}
146
155
catch ( XmlException )
147
156
{
148
157
throw new XmlException (
149
- string . Format (
150
- Properties . Resources . InvalidCloudExceptionErrorMessage ,
151
- cloudException . Message ) ,
158
+ string . Format ( Resources . InvalidCloudExceptionErrorMessage ,
159
+ cloudException . Message ) ,
152
160
cloudException ) ;
153
161
}
154
162
catch ( SerializationException )
155
163
{
156
- throw new SerializationException (
157
- string . Format (
158
- Properties . Resources . InvalidCloudExceptionErrorMessage ,
159
- clientRequestIdMsg + cloudException . Message ) ,
164
+ throw new SerializationException ( string . Format (
165
+ Resources . InvalidCloudExceptionErrorMessage ,
166
+ clientRequestIdMsg + cloudException . Message ) ,
160
167
cloudException ) ;
161
168
}
162
169
catch ( JsonReaderException )
163
170
{
164
171
throw new JsonReaderException (
165
- string . Format (
166
- Properties . Resources . InvalidCloudExceptionErrorMessage ,
167
- clientRequestIdMsg + cloudException . Message ) ,
172
+ string . Format ( Resources . InvalidCloudExceptionErrorMessage ,
173
+ clientRequestIdMsg + cloudException . Message ) ,
168
174
cloudException ) ;
169
175
}
170
176
}
171
- else if ( ex . Message != null )
177
+
178
+ if ( ex . Message != null )
172
179
{
173
- throw new Exception (
174
- string . Format (
175
- Properties . Resources . InvalidCloudExceptionErrorMessage ,
176
- clientRequestIdMsg + ex . Message ) ,
180
+ throw new Exception ( string . Format ( Resources . InvalidCloudExceptionErrorMessage ,
181
+ clientRequestIdMsg + ex . Message ) ,
177
182
ex ) ;
178
183
}
179
184
}
180
185
181
186
/// <summary>
182
- /// Waits for the job to complete.
187
+ /// Waits for the job to complete.
183
188
/// </summary>
184
189
/// <param name="jobId">Id of the job to wait for.</param>
185
190
/// <returns>Final job response</returns>
@@ -189,53 +194,47 @@ public Job WaitForJobCompletion(string jobId)
189
194
do
190
195
{
191
196
Thread . Sleep ( PSRecoveryServicesClient . TimeToSleepBeforeFetchingJobDetailsAgain ) ;
192
- job = this . RecoveryServicesClient . GetAzureSiteRecoveryJobDetails ( jobId ) ;
193
- this . WriteProgress (
194
- new System . Management . Automation . ProgressRecord (
195
- 0 ,
196
- Properties . Resources . WaitingForCompletion ,
197
- job . Properties . State ) ) ;
198
- }
199
- while ( ! ( job . Properties . State == JobStatus . Cancelled ||
200
- job . Properties . State == JobStatus . Failed ||
201
- job . Properties . State == JobStatus . Suspended ||
202
- job . Properties . State == JobStatus . Succeeded ||
203
- this . StopProcessingFlag ) ) ;
197
+ job = RecoveryServicesClient . GetAzureSiteRecoveryJobDetails ( jobId ) ;
198
+ WriteProgress ( new ProgressRecord ( 0 ,
199
+ Resources . WaitingForCompletion ,
200
+ job . Properties . State ) ) ;
201
+ } while ( ! ( job . Properties . State == TaskStatus . Cancelled ||
202
+ job . Properties . State == TaskStatus . Failed ||
203
+ job . Properties . State == TaskStatus . Suspended ||
204
+ job . Properties . State == TaskStatus . Succeeded ||
205
+ StopProcessingFlag ) ) ;
206
+
204
207
return job ;
205
208
}
206
209
207
-
208
210
/// <summary>
209
- /// Handles interrupts.
211
+ /// Handles interrupts.
210
212
/// </summary>
211
213
protected override void StopProcessing ( )
212
214
{
213
215
// Ctrl + C and etc
214
216
base . StopProcessing ( ) ;
215
- this . StopProcessingFlag = true ;
217
+ StopProcessingFlag = true ;
216
218
}
217
219
218
220
/// <summary>
219
- /// Validates if the usage by ID is allowed or not.
221
+ /// Validates if the usage by ID is allowed or not.
220
222
/// </summary>
221
223
/// <param name="replicationProvider">Replication provider.</param>
222
224
/// <param name="paramName">Parameter name.</param>
223
- protected void ValidateUsageById ( string replicationProvider , string paramName )
225
+ protected void ValidateUsageById ( string replicationProvider ,
226
+ string paramName )
224
227
{
225
228
if ( replicationProvider != Constants . HyperVReplica2012 )
226
229
{
227
- throw new Exception (
228
- string . Format (
230
+ throw new Exception ( string . Format (
229
231
"Call using ID based parameter {0} is not supported for this provider. Please use its corresponding full object parameter instead" ,
230
232
paramName ) ) ;
231
233
}
232
- else
233
- {
234
- this . WriteWarningWithTimestamp (
235
- string . Format (
236
- Properties . Resources . IDBasedParamUsageNotSupportedFromNextRelease ,
234
+
235
+ WriteWarningWithTimestamp (
236
+ string . Format ( Resources . IDBasedParamUsageNotSupportedFromNextRelease ,
237
237
paramName ) ) ;
238
- }
239
238
}
240
239
}
241
- }
240
+ }
0 commit comments