20
20
using System . Management . Automation ;
21
21
using Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets . Models ;
22
22
using Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets . ProviderModel ;
23
+ using Microsoft . Azure . Commands . RecoveryServices . Backup . Properties ;
23
24
using Microsoft . Azure . Commands . RecoveryServices . Backup . Helpers ;
25
+ using HydraModel = Microsoft . Azure . Management . RecoveryServices . Backup . Models ;
26
+
24
27
25
28
namespace Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets
26
29
{
@@ -30,8 +33,8 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
30
33
[ Cmdlet ( VerbsLifecycle . Enable , "AzureRmRecoveryServicesProtection" , DefaultParameterSetName = ModifyProtectionParameterSet ) , OutputType ( typeof ( AzureRmRecoveryServicesJobBase ) ) ]
31
34
public class EnableAzureRmRecoveryServicesProtection : RecoveryServicesBackupCmdletBase
32
35
{
33
- internal const string AzureVMClassicComputeParameterSet = "AzureVMClassicCompute " ;
34
- internal const string AzureVMComputeParameterSet = "AzureVMCompute " ;
36
+ internal const string AzureVMClassicComputeParameterSet = "AzureVMClassicComputeEnableProtection " ;
37
+ internal const string AzureVMComputeParameterSet = "AzureVMComputeEnableProtection " ;
35
38
internal const string ModifyProtectionParameterSet = "ModifyProtection" ;
36
39
37
40
[ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = AzureVMClassicComputeParameterSet , HelpMessage = ParamHelpMsg . Item . AzureVMName ) ]
@@ -44,15 +47,15 @@ public class EnableAzureRmRecoveryServicesProtection : RecoveryServicesBackupCmd
44
47
[ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = AzureVMComputeParameterSet , HelpMessage = ParamHelpMsg . Item . AzureVMResourceGroupName ) ]
45
48
public string ResourceGroupName { get ; set ; }
46
49
47
- [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = AzureVMClassicComputeParameterSet , HelpMessage = ParamHelpMsg . Common . WorkloadType ) ]
48
- [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = AzureVMComputeParameterSet , HelpMessage = ParamHelpMsg . Common . WorkloadType ) ]
50
+ [ Parameter ( Mandatory = true , ParameterSetName = AzureVMClassicComputeParameterSet , HelpMessage = ParamHelpMsg . Common . WorkloadType ) ]
51
+ [ Parameter ( Mandatory = true , ParameterSetName = AzureVMComputeParameterSet , HelpMessage = ParamHelpMsg . Common . WorkloadType ) ]
49
52
public WorkloadType WorkLoadType { get ; set ; }
50
53
51
54
[ Parameter ( Mandatory = true , HelpMessage = ParamHelpMsg . Policy . ProtectionPolicy ) ]
52
55
[ ValidateNotNullOrEmpty ]
53
56
public AzureRmRecoveryServicesPolicyBase Policy { get ; set ; }
54
57
55
- [ Parameter ( Mandatory = false , HelpMessage = ParamHelpMsg . Item . ProtectedItem , ValueFromPipeline = true ) ]
58
+ [ Parameter ( Mandatory = true , ParameterSetName = ModifyProtectionParameterSet , HelpMessage = ParamHelpMsg . Item . ProtectedItem , ValueFromPipeline = true ) ]
56
59
[ ValidateNotNullOrEmpty ]
57
60
public AzureRmRecoveryServicesItemBase Item { get ; set ; }
58
61
@@ -81,19 +84,30 @@ public override void ExecuteCmdlet()
81
84
// Track Response and display job details
82
85
// -- TBD to move it to common helper and remove hard-coded vaules
83
86
84
- var response = HydraAdapter . GetProtectedItemOperationStatusByURL ( jobResponse . AzureAsyncOperation ) ;
85
- while ( response . OperationStatus . Status == "InProgress" )
86
- {
87
- response = HydraAdapter . GetProtectedItemOperationStatusByURL ( jobResponse . AzureAsyncOperation ) ;
88
- System . Threading . Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
89
- }
87
+ var response = OperationStatusHelper . TrackOperationStatus ( jobResponse , HydraAdapter ) ;
90
88
91
- if ( response . OperationStatus . Status == "Completed" )
89
+ if ( response . OperationStatus . Status == HydraModel . OperationStatusValues . Succeeded )
92
90
{
93
- // TBD -- Hydra change to add jobId in OperationStatusExtendedInfo
94
- string jobId = "" ; //response.OperationStatus.Properties.jobId ;
91
+ var jobStatusResponse = ( HydraModel . OperationStatusJobExtendedInfo ) response . OperationStatus . Properties ;
92
+ string jobId = jobStatusResponse . JobId ;
95
93
var job = HydraAdapter . GetJob ( jobId ) ;
96
- //WriteObject(ConversionHelpers.GetJobModel(job));
94
+ WriteObject ( JobConversions . GetPSJob ( job ) ) ;
95
+ }
96
+ else if ( response . OperationStatus . Status == HydraModel . OperationStatusValues . Failed )
97
+ {
98
+ var jobStatusResponse = ( HydraModel . OperationStatusJobExtendedInfo ) response . OperationStatus . Properties ;
99
+ if ( jobStatusResponse != null || ! string . IsNullOrEmpty ( jobStatusResponse . JobId ) )
100
+ {
101
+ string jobId = jobStatusResponse . JobId ;
102
+ var job = HydraAdapter . GetJob ( jobId ) ;
103
+ WriteObject ( JobConversions . GetPSJob ( job ) ) ;
104
+ }
105
+
106
+ var errorMessage = string . Format ( Resources . EnableProtectionOperationFailed ,
107
+ response . OperationStatus . OperationStatusError . Code ,
108
+ response . OperationStatus . OperationStatusError . Message ) ;
109
+
110
+ throw new Exception ( errorMessage ) ;
97
111
}
98
112
} ) ;
99
113
}
0 commit comments