23
23
using Microsoft . Azure . Management . BackupServices . Models ;
24
24
using MBS = Microsoft . Azure . Management . BackupServices ;
25
25
using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
26
+ using Microsoft . Azure . Commands . AzureBackup . Properties ;
26
27
using Microsoft . Azure . Commands . AzureBackup . Models ;
27
28
using Microsoft . Azure . Commands . AzureBackup . Helpers ;
28
29
using Microsoft . Azure . Management . BackupServices ;
@@ -38,15 +39,15 @@ public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
38
39
internal const string V1VMParameterSet = "V1VM" ;
39
40
internal const string V2VMParameterSet = "V2VM" ;
40
41
41
- [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
42
- [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
42
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VMName ) ]
43
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VMName ) ]
43
44
public string Name { get ; set ; }
44
45
45
- [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
46
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . ServiceName ) ]
46
47
public string ServiceName { get ; set ; }
47
48
48
- [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
49
- public string VMResourceGroupName { get ; set ; }
49
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . RGName ) ]
50
+ public string ResourceGroupName { get ; set ; }
50
51
51
52
52
53
public override void ExecuteCmdlet ( )
@@ -69,7 +70,7 @@ public override void ExecuteCmdlet()
69
70
else if ( this . ParameterSetName == V2VMParameterSet )
70
71
{
71
72
vmName = Name ;
72
- rgName = VMResourceGroupName ;
73
+ rgName = ResourceGroupName ;
73
74
WriteDebug ( String . Format ( "Registering ARM-V2 VM, VMName: {0}, ResourceGroupName: {1}" , vmName , rgName ) ) ;
74
75
ServiceOrRG = "ResourceGroupName" ;
75
76
}
@@ -94,7 +95,7 @@ public override void ExecuteCmdlet()
94
95
//Container is not discovered. Throw exception
95
96
string errMsg = String . Format ( "Failed to discover VM {0} under {1} {2}. Please make sure names are correct and VM is not deleted" , vmName , ServiceOrRG , rgName ) ;
96
97
WriteDebug ( errMsg ) ;
97
- throw new Exception ( errMsg ) ; //TODO: Sync with piyush and srub error msg
98
+ ThrowTerminatingError ( new ErrorRecord ( new Exception ( Resources . AzureVMNotFound ) , string . Empty , ErrorCategory . InvalidArgument , null ) ) ;
98
99
}
99
100
}
100
101
@@ -112,35 +113,37 @@ public override void ExecuteCmdlet()
112
113
113
114
private void RefreshContainer ( )
114
115
{
115
- bool isRetyNeeded = true ;
116
+ bool isRetryNeeded = true ;
116
117
int retryCount = 1 ;
117
118
bool isDiscoverySuccessful = false ;
118
- while ( isRetyNeeded && retryCount <= 3 )
119
+ string errorMessage = string . Empty ;
120
+ while ( isRetryNeeded && retryCount <= 3 )
119
121
{
120
122
var operationId = AzureBackupClient . RefreshContainers ( ) ;
121
123
122
124
//Now wait for the operation to Complete
123
- isRetyNeeded = WaitForDiscoveryToCOmplete ( operationId , out isDiscoverySuccessful ) ;
125
+ isRetryNeeded = WaitForDiscoveryToComplete ( operationId , out isDiscoverySuccessful , out errorMessage ) ;
124
126
retryCount ++ ;
125
127
}
126
128
127
129
if ( ! isDiscoverySuccessful )
128
130
{
129
- //Discovery failed
130
- throw new Exception ( ) ; //TODO:
131
+ ThrowTerminatingError ( new ErrorRecord ( new Exception ( errorMessage ) , string . Empty , ErrorCategory . InvalidArgument , null ) ) ;
131
132
}
132
133
}
133
134
134
- private bool WaitForDiscoveryToCOmplete ( Guid operationId , out bool isDiscoverySuccessful )
135
+ private bool WaitForDiscoveryToComplete ( Guid operationId , out bool isDiscoverySuccessful , out string errorMessage )
135
136
{
136
137
bool isRetryNeeded = false ;
137
- var status = TrackOperation ( operationId ) ;
138
+ var status = TrackOperation ( operationId ) ;
139
+ errorMessage = String . Empty ;
138
140
139
141
isDiscoverySuccessful = true ;
140
142
//If operation fails check if retry is needed or not
141
143
if ( status . OperationResult != AzureBackupOperationResult . Succeeded . ToString ( ) )
142
144
{
143
145
isDiscoverySuccessful = false ;
146
+ errorMessage = status . Message ;
144
147
WriteDebug ( String . Format ( "Discovery operation failed with ErrorCode: {0}" , status . ErrorCode ) ) ;
145
148
if ( ( status . ErrorCode == AzureBackupOperationErrorCode . DiscoveryInProgress . ToString ( ) ||
146
149
( status . ErrorCode == AzureBackupOperationErrorCode . BMSUserErrorObjectLocked . ToString ( ) ) ) )
0 commit comments