24
24
using Microsoft . Azure . Management . BackupServices . Models ;
25
25
using MBS = Microsoft . Azure . Management . BackupServices ;
26
26
using Microsoft . Azure . Commands . Compute . Models ;
27
+ using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
27
28
28
29
namespace Microsoft . Azure . Commands . AzureBackup . Cmdlets
29
30
{
30
31
/// <summary>
31
32
/// Get list of containers
32
33
/// </summary>
33
- [ Cmdlet ( VerbsLifecycle . Register , "AzureBackupContainer" ) , OutputType ( typeof ( Guid ) ) ]
34
+ [ Cmdlet ( VerbsLifecycle . Register , "AzureBackupContainer" , DefaultParameterSetName = V1VMParameterSet ) , OutputType ( typeof ( Guid ) ) ]
34
35
public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
35
36
{
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" ;
42
39
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 ; }
46
43
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
+
47
51
public override void ExecuteCmdlet ( )
48
52
{
49
53
base . ExecuteCmdlet ( ) ;
50
54
51
55
ExecutionBlock ( ( ) =>
52
56
{
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
+
57
81
Guid jobId = Guid . Empty ;
58
82
bool isDiscoveryNeed = false ;
59
83
MBS . OperationResponse operationResponse ;
@@ -62,16 +86,20 @@ public override void ExecuteCmdlet()
62
86
isDiscoveryNeed = IsDiscoveryNeeded ( vmName , rgName , out container ) ;
63
87
if ( isDiscoveryNeed )
64
88
{
89
+ WriteDebug ( String . Format ( "VM {0} is not yet discovered. Triggering Discovery" , vmName ) ) ;
65
90
RefreshContainer ( ) ;
66
91
isDiscoveryNeed = IsDiscoveryNeeded ( vmName , rgName , out container ) ;
67
92
if ( ( isDiscoveryNeed == true ) || ( container == null ) )
68
93
{
69
94
//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
71
98
}
72
99
}
73
100
74
101
//Container is discovered. Register the container
102
+ WriteDebug ( String . Format ( "Going to register VM {0}" , vmName ) ) ;
75
103
List < string > containerNameList = new List < string > ( ) ;
76
104
containerNameList . Add ( container . Name ) ;
77
105
RegisterContainerRequestInput registrationRequest = new RegisterContainerRequestInput ( containerNameList , AzureBackupContainerType . IaasVMContainer . ToString ( ) ) ;
@@ -108,16 +136,14 @@ private void RefreshContainer()
108
136
private bool WaitForDiscoveryToCOmplete ( string operationId , out bool isDiscoverySuccessful )
109
137
{
110
138
bool isRetryNeeded = false ;
111
-
112
-
113
- BMSOperationStatusResponse status = new BMSOperationStatusResponse ( )
139
+ AzureBackupOperationStatusResponse status = new AzureBackupOperationStatusResponse ( )
114
140
{
115
141
OperationStatus = AzureBackupOperationStatus . InProgress . ToString ( )
116
142
} ;
117
-
118
143
while ( status . OperationStatus != AzureBackupOperationStatus . Completed . ToString ( ) )
119
144
{
120
145
status = AzureBackupClient . OperationStatus . GetAsync ( operationId , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
146
+ WriteDebug ( String . Format ( "Status of DiscoveryOperation: {0}" , status . OperationStatus ) ) ;
121
147
System . Threading . Thread . Sleep ( TimeSpan . FromSeconds ( 15 ) ) ;
122
148
}
123
149
@@ -126,11 +152,13 @@ private bool WaitForDiscoveryToCOmplete(string operationId, out bool isDiscovery
126
152
if ( status . OperationResult != AzureBackupOperationResult . Succeeded . ToString ( ) )
127
153
{
128
154
isDiscoverySuccessful = false ;
155
+ WriteDebug ( String . Format ( "DiscoveryOperation failed wit ErrorCOde: {0}" , status . ErrorCode ) ) ;
129
156
if ( ( status . ErrorCode == AzureBackupOperationErrorCode . DiscoveryInProgress . ToString ( ) ||
130
157
( status . ErrorCode == AzureBackupOperationErrorCode . BMSUserErrorObjectLocked . ToString ( ) ) ) )
131
158
{
132
159
//Need to retry for this errors
133
160
isRetryNeeded = true ;
161
+ WriteDebug ( String . Format ( "Going to retry Discovery if retry count is not exceeded" ) ) ;
134
162
}
135
163
}
136
164
return isRetryNeeded ;
@@ -148,22 +176,24 @@ private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo c
148
176
149
177
ListContainerResponse containers = AzureBackupClient . Container . ListAsync ( queryString ,
150
178
GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
179
+ WriteDebug ( String . Format ( "Container count returned from service: {0}." , containers . Objects . Count ( ) ) ) ;
151
180
if ( containers . Objects . Count ( ) == 0 )
152
181
{
153
182
//Container is not discover
154
- WriteVerbose ( "Container is not discovered" ) ;
183
+ WriteDebug ( "Container is not discovered" ) ;
155
184
container = null ;
156
185
isDiscoveryNeed = true ;
157
186
}
158
187
159
188
else
160
189
{
161
190
//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
163
192
container = containers . Objects . Where ( c => c . ParentContainerFriendlyName . ToLower ( ) . Equals ( rgName . ToLower ( ) ) ) . FirstOrDefault ( ) ;
164
193
if ( container == null )
165
194
{
166
195
//Container is not in list of registered container
196
+ WriteDebug ( String . Format ( "Desired Container is not found. Returning with isDiscoveryNeed = true" ) ) ;
167
197
isDiscoveryNeed = true ;
168
198
}
169
199
}
0 commit comments