2424using Microsoft . Azure . Management . BackupServices . Models ;
2525using MBS = Microsoft . Azure . Management . BackupServices ;
2626using Microsoft . Azure . Commands . Compute . Models ;
27+ using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
2728
2829namespace Microsoft . Azure . Commands . AzureBackup . Cmdlets
2930{
3031 /// <summary>
3132 /// Get list of containers
3233 /// </summary>
33- [ Cmdlet ( VerbsLifecycle . Register , "AzureBackupContainer" ) , OutputType ( typeof ( Guid ) ) ]
34+ [ Cmdlet ( VerbsLifecycle . Register , "AzureBackupContainer" , DefaultParameterSetName = V1VMParameterSet ) , OutputType ( typeof ( Guid ) ) ]
3435 public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
3536 {
36- //[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
37- //[ValidateNotNullOrEmpty]
38- //public PSVirtualMachineInstanceView VirtualMachine { get; set; }
39- [ Parameter ( Position = 2 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
40- [ ValidateNotNullOrEmpty ]
41- public string VirtualMachineName { get ; set ; }
37+ internal const string V1VMParameterSet = "V1VM" ;
38+ internal const string V2VMParameterSet = "V2VM" ;
4239
43- [ Parameter ( Position = 3 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
44- [ ValidateNotNullOrEmpty ]
45- public string VirtualMachineRGName { get ; set ; }
40+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
41+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
42+ public string Name { get ; set ; }
4643
44+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V1VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
45+ public string ServiceName { get ; set ; }
46+
47+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , ParameterSetName = V2VMParameterSet , HelpMessage = AzureBackupCmdletHelpMessage . VirtualMachine ) ]
48+ public string VMResourceGroupName { get ; set ; }
49+
50+
4751 public override void ExecuteCmdlet ( )
4852 {
4953 base . ExecuteCmdlet ( ) ;
5054
5155 ExecutionBlock ( ( ) =>
5256 {
53- //string vmName = VirtualMachine.Name;
54- //string rgName = VirtualMachine.ResourceGroupName;
55- string vmName = VirtualMachineName ;
56- string rgName = VirtualMachineRGName ;
57+ string vmName = String . Empty ;
58+ string rgName = String . Empty ;
59+ string ServiceOrRG = String . Empty ;
60+
61+ if ( this . ParameterSetName == V1VMParameterSet )
62+ {
63+ vmName = Name ;
64+ rgName = ServiceName ;
65+ WriteDebug ( String . Format ( "Registering ARM-V1 VM, VMName: {0}, CloudServiceName: {1}" , vmName , rgName ) ) ;
66+ ServiceOrRG = "CloudServiceName" ;
67+ }
68+ else if ( this . ParameterSetName == V2VMParameterSet )
69+ {
70+ vmName = Name ;
71+ rgName = VMResourceGroupName ;
72+ WriteDebug ( String . Format ( "Registering ARM-V2 VM, VMName: {0}, ResourceGroupName: {1}" , vmName , rgName ) ) ;
73+ ServiceOrRG = "ResourceGroupName" ;
74+ }
75+
76+ else
77+ {
78+ throw new PSArgumentException ( "Please make sure you have pass right set of parameters" ) ; //TODO: PM scrub needed
79+ }
80+
5781 Guid jobId = Guid . Empty ;
5882 bool isDiscoveryNeed = false ;
5983 MBS . OperationResponse operationResponse ;
@@ -62,16 +86,20 @@ public override void ExecuteCmdlet()
6286 isDiscoveryNeed = IsDiscoveryNeeded ( vmName , rgName , out container ) ;
6387 if ( isDiscoveryNeed )
6488 {
89+ WriteDebug ( String . Format ( "VM {0} is not yet discovered. Triggering Discovery" , vmName ) ) ;
6590 RefreshContainer ( ) ;
6691 isDiscoveryNeed = IsDiscoveryNeeded ( vmName , rgName , out container ) ;
6792 if ( ( isDiscoveryNeed == true ) || ( container == null ) )
6893 {
6994 //Container is not discovered. Throw exception
70- throw new NotImplementedException ( ) ;
95+ 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+ WriteDebug ( errMsg ) ;
97+ throw new Exception ( errMsg ) ; //TODO: Sync with piyush and srub error msg
7198 }
7299 }
73100
74101 //Container is discovered. Register the container
102+ WriteDebug ( String . Format ( "Going to register VM {0}" , vmName ) ) ;
75103 List < string > containerNameList = new List < string > ( ) ;
76104 containerNameList . Add ( container . Name ) ;
77105 RegisterContainerRequestInput registrationRequest = new RegisterContainerRequestInput ( containerNameList , AzureBackupContainerType . IaasVMContainer . ToString ( ) ) ;
@@ -108,16 +136,14 @@ private void RefreshContainer()
108136 private bool WaitForDiscoveryToCOmplete ( string operationId , out bool isDiscoverySuccessful )
109137 {
110138 bool isRetryNeeded = false ;
111-
112-
113- BMSOperationStatusResponse status = new BMSOperationStatusResponse ( )
139+ AzureBackupOperationStatusResponse status = new AzureBackupOperationStatusResponse ( )
114140 {
115141 OperationStatus = AzureBackupOperationStatus . InProgress . ToString ( )
116142 } ;
117-
118143 while ( status . OperationStatus != AzureBackupOperationStatus . Completed . ToString ( ) )
119144 {
120145 status = AzureBackupClient . OperationStatus . GetAsync ( operationId , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
146+ WriteDebug ( String . Format ( "Status of DiscoveryOperation: {0}" , status . OperationStatus ) ) ;
121147 System . Threading . Thread . Sleep ( TimeSpan . FromSeconds ( 15 ) ) ;
122148 }
123149
@@ -126,11 +152,13 @@ private bool WaitForDiscoveryToCOmplete(string operationId, out bool isDiscovery
126152 if ( status . OperationResult != AzureBackupOperationResult . Succeeded . ToString ( ) )
127153 {
128154 isDiscoverySuccessful = false ;
155+ WriteDebug ( String . Format ( "DiscoveryOperation failed wit ErrorCOde: {0}" , status . ErrorCode ) ) ;
129156 if ( ( status . ErrorCode == AzureBackupOperationErrorCode . DiscoveryInProgress . ToString ( ) ||
130157 ( status . ErrorCode == AzureBackupOperationErrorCode . BMSUserErrorObjectLocked . ToString ( ) ) ) )
131158 {
132159 //Need to retry for this errors
133160 isRetryNeeded = true ;
161+ WriteDebug ( String . Format ( "Going to retry Discovery if retry count is not exceeded" ) ) ;
134162 }
135163 }
136164 return isRetryNeeded ;
@@ -148,22 +176,24 @@ private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo c
148176
149177 ListContainerResponse containers = AzureBackupClient . Container . ListAsync ( queryString ,
150178 GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
179+ WriteDebug ( String . Format ( "Container count returned from service: {0}." , containers . Objects . Count ( ) ) ) ;
151180 if ( containers . Objects . Count ( ) == 0 )
152181 {
153182 //Container is not discover
154- WriteVerbose ( "Container is not discovered" ) ;
183+ WriteDebug ( "Container is not discovered" ) ;
155184 container = null ;
156185 isDiscoveryNeed = true ;
157186 }
158187
159188 else
160189 {
161190 //We can have multiple container with same friendly name.
162- //Look for resourceGroup name in the container unoque name
191+ //Look for resourceGroup name in the container unoque name
163192 container = containers . Objects . Where ( c => c . ParentContainerFriendlyName . ToLower ( ) . Equals ( rgName . ToLower ( ) ) ) . FirstOrDefault ( ) ;
164193 if ( container == null )
165194 {
166195 //Container is not in list of registered container
196+ WriteDebug ( String . Format ( "Desired Container is not found. Returning with isDiscoveryNeed = true" ) ) ;
167197 isDiscoveryNeed = true ;
168198 }
169199 }
0 commit comments