2323using Microsoft . Azure . Management . BackupServices . Models ;
2424using MBS = Microsoft . Azure . Management . BackupServices ;
2525using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
26+ using Microsoft . Azure . Commands . AzureBackup . Properties ;
2627using Microsoft . Azure . Commands . AzureBackup . Models ;
2728using Microsoft . Azure . Commands . AzureBackup . Helpers ;
2829using Microsoft . Azure . Management . BackupServices ;
@@ -38,15 +39,15 @@ public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
3839 internal const string V1VMParameterSet = "V1VM" ;
3940 internal const string V2VMParameterSet = "V2VM" ;
4041
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 ) ]
4344 public string Name { get ; set ; }
4445
45- [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
46+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . ServiceName ) ]
4647 public string ServiceName { get ; set ; }
4748
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 ; }
5051
5152
5253 public override void ExecuteCmdlet ( )
@@ -69,7 +70,7 @@ public override void ExecuteCmdlet()
6970 else if ( this . ParameterSetName == V2VMParameterSet )
7071 {
7172 vmName = Name ;
72- rgName = VMResourceGroupName ;
73+ rgName = ResourceGroupName ;
7374 WriteDebug ( String . Format ( "Registering ARM-V2 VM, VMName: {0}, ResourceGroupName: {1}" , vmName , rgName ) ) ;
7475 ServiceOrRG = "ResourceGroupName" ;
7576 }
@@ -94,7 +95,7 @@ public override void ExecuteCmdlet()
9495 //Container is not discovered. Throw exception
9596 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 ) ;
9697 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 ) ) ;
9899 }
99100 }
100101
@@ -112,35 +113,37 @@ public override void ExecuteCmdlet()
112113
113114 private void RefreshContainer ( )
114115 {
115- bool isRetyNeeded = true ;
116+ bool isRetryNeeded = true ;
116117 int retryCount = 1 ;
117118 bool isDiscoverySuccessful = false ;
118- while ( isRetyNeeded && retryCount <= 3 )
119+ string errorMessage = string . Empty ;
120+ while ( isRetryNeeded && retryCount <= 3 )
119121 {
120122 var operationId = AzureBackupClient . RefreshContainers ( ) ;
121123
122124 //Now wait for the operation to Complete
123- isRetyNeeded = WaitForDiscoveryToCOmplete ( operationId , out isDiscoverySuccessful ) ;
125+ isRetryNeeded = WaitForDiscoveryToComplete ( operationId , out isDiscoverySuccessful , out errorMessage ) ;
124126 retryCount ++ ;
125127 }
126128
127129 if ( ! isDiscoverySuccessful )
128130 {
129- //Discovery failed
130- throw new Exception ( ) ; //TODO:
131+ ThrowTerminatingError ( new ErrorRecord ( new Exception ( errorMessage ) , string . Empty , ErrorCategory . InvalidArgument , null ) ) ;
131132 }
132133 }
133134
134- private bool WaitForDiscoveryToCOmplete ( Guid operationId , out bool isDiscoverySuccessful )
135+ private bool WaitForDiscoveryToComplete ( Guid operationId , out bool isDiscoverySuccessful , out string errorMessage )
135136 {
136137 bool isRetryNeeded = false ;
137- var status = TrackOperation ( operationId ) ;
138+ var status = TrackOperation ( operationId ) ;
139+ errorMessage = String . Empty ;
138140
139141 isDiscoverySuccessful = true ;
140142 //If operation fails check if retry is needed or not
141143 if ( status . OperationResult != AzureBackupOperationResult . Succeeded . ToString ( ) )
142144 {
143145 isDiscoverySuccessful = false ;
146+ errorMessage = status . Message ;
144147 WriteDebug ( String . Format ( "Discovery operation failed with ErrorCode: {0}" , status . ErrorCode ) ) ;
145148 if ( ( status . ErrorCode == AzureBackupOperationErrorCode . DiscoveryInProgress . ToString ( ) ||
146149 ( status . ErrorCode == AzureBackupOperationErrorCode . BMSUserErrorObjectLocked . ToString ( ) ) ) )
0 commit comments