19
19
using Microsoft . Azure . Commands . RecoveryServices . Backup . Properties ;
20
20
using Microsoft . Azure . Management . Internal . Resources . Models ;
21
21
using Microsoft . Azure . Management . RecoveryServices . Backup . Models ;
22
+ using Microsoft . Rest ;
22
23
using Microsoft . Rest . Azure . OData ;
23
24
using System ;
24
25
using System . Collections . Generic ;
@@ -74,21 +75,31 @@ public RestAzureNS.AzureOperationResponse<ProtectedItemResource> EnableProtectio
74
75
string azureVMCloudServiceName = ( string ) ProviderData [ ItemParams . AzureVMCloudServiceName ] ;
75
76
string azureVMResourceGroupName = ( string ) ProviderData [ ItemParams . AzureVMResourceGroupName ] ;
76
77
string parameterSetName = ( string ) ProviderData [ ItemParams . ParameterSetName ] ;
78
+ string [ ] inclusionDisksList = ( string [ ] ) ProviderData [ ItemParams . InclusionDisksList ] ;
79
+ string [ ] exclusionDisksList = ( string [ ] ) ProviderData [ ItemParams . ExclusionDisksList ] ;
80
+ SwitchParameter resetDiskExclusionSetting = ( SwitchParameter ) ProviderData [ ItemParams . ResetExclusionSettings ] ;
81
+ bool excludeAllDataDisks = ( bool ) ProviderData [ ItemParams . ExcludeAllDataDisks ] ;
77
82
78
83
PolicyBase policy = ( PolicyBase ) ProviderData [ ItemParams . Policy ] ;
79
84
80
85
ItemBase itemBase = ( ItemBase ) ProviderData [ ItemParams . Item ] ;
81
86
82
87
AzureVmItem item = ( AzureVmItem ) ProviderData [ ItemParams . Item ] ;
83
88
89
+ bool isDiskExclusionParamPresent = ValidateDiskExclusionParameters (
90
+ inclusionDisksList , exclusionDisksList , resetDiskExclusionSetting , excludeAllDataDisks ) ;
91
+
84
92
// do validations
85
93
string containerUri = "" ;
86
94
string protectedItemUri = "" ;
87
95
bool isComputeAzureVM = false ;
88
96
string sourceResourceId = null ;
89
97
90
98
AzureVmPolicy azureVmPolicy = ( AzureVmPolicy ) ProviderData [ ItemParams . Policy ] ;
91
- ValidateProtectedItemCount ( azureVmPolicy ) ;
99
+ if ( azureVmPolicy != null )
100
+ {
101
+ ValidateProtectedItemCount ( azureVmPolicy ) ;
102
+ }
92
103
93
104
if ( itemBase == null )
94
105
{
@@ -126,6 +137,14 @@ public RestAzureNS.AzureOperationResponse<ProtectedItemResource> EnableProtectio
126
137
sourceResourceId = iaasVmProtectableItem . VirtualMachineId ;
127
138
}
128
139
}
140
+ else if ( isDiskExclusionParamPresent && parameterSetName . Contains ( "Modify" ) )
141
+ {
142
+ isComputeAzureVM = IsComputeAzureVM ( item . VirtualMachineId ) ;
143
+ Dictionary < UriEnums , string > keyValueDict = HelperUtils . ParseUri ( item . Id ) ;
144
+ containerUri = HelperUtils . GetContainerUri ( keyValueDict , item . Id ) ;
145
+ protectedItemUri = HelperUtils . GetProtectedItemUri ( keyValueDict , item . Id ) ;
146
+ sourceResourceId = item . SourceResourceId ;
147
+ }
129
148
else
130
149
{
131
150
ValidateAzureVMWorkloadType ( item . WorkloadType , policy . WorkloadType ) ;
@@ -150,9 +169,49 @@ public RestAzureNS.AzureOperationResponse<ProtectedItemResource> EnableProtectio
150
169
properties = new AzureIaaSComputeVMProtectedItem ( ) ;
151
170
}
152
171
153
- properties . PolicyId = policy . Id ;
172
+ if ( policy != null )
173
+ {
174
+ properties . PolicyId = policy . Id ;
175
+ }
176
+ else
177
+ {
178
+ properties . PolicyId = item . PolicyId ;
179
+ }
154
180
properties . SourceResourceId = sourceResourceId ;
155
181
182
+ ExtendedProperties extendedProperties = null ;
183
+ if ( resetDiskExclusionSetting . IsPresent )
184
+ {
185
+ extendedProperties = new ExtendedProperties ( ) ;
186
+ extendedProperties . DiskExclusionProperties = null ;
187
+ }
188
+ else
189
+ {
190
+ if ( inclusionDisksList != null )
191
+ {
192
+ IList < int ? > inclusionList = Array . ConvertAll ( inclusionDisksList , s => int . Parse ( s ) ) . OfType < int ? > ( ) . ToList ( ) ;
193
+ DiskExclusionProperties diskExclusionProperties = new DiskExclusionProperties ( inclusionList , true ) ;
194
+ extendedProperties = new ExtendedProperties ( ) ;
195
+ extendedProperties . DiskExclusionProperties = diskExclusionProperties ;
196
+ }
197
+ else if ( exclusionDisksList != null )
198
+ {
199
+ IList < int ? > exclusionList = Array . ConvertAll ( exclusionDisksList , s => int . Parse ( s ) ) . OfType < int ? > ( ) . ToList ( ) ;
200
+ DiskExclusionProperties diskExclusionProperties = new DiskExclusionProperties ( exclusionList , false ) ;
201
+ extendedProperties = new ExtendedProperties ( ) ;
202
+ extendedProperties . DiskExclusionProperties = diskExclusionProperties ;
203
+ }
204
+ else if ( excludeAllDataDisks == true )
205
+ {
206
+ IList < int ? > exclusionList = new List < int ? > ( ) ;
207
+ DiskExclusionProperties diskExclusionProperties = new DiskExclusionProperties ( exclusionList , true ) ;
208
+ extendedProperties = new ExtendedProperties ( ) ;
209
+ extendedProperties . DiskExclusionProperties = diskExclusionProperties ;
210
+ }
211
+ }
212
+
213
+ properties . ExtendedProperties = extendedProperties ;
214
+
156
215
ProtectedItemResource serviceClientRequest = new ProtectedItemResource ( )
157
216
{
158
217
Properties = properties
@@ -327,6 +386,10 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
327
386
ProviderData . ContainsKey ( RestoreVMBackupItemParams . TargetResourceGroupName ) ?
328
387
ProviderData [ RestoreVMBackupItemParams . TargetResourceGroupName ] . ToString ( ) : null ;
329
388
bool osaOption = ( bool ) ProviderData [ RestoreVMBackupItemParams . OsaOption ] ;
389
+ string [ ] restoreDiskList = ( string [ ] ) ProviderData [ RestoreVMBackupItemParams . RestoreDiskList ] ;
390
+ SwitchParameter restoreOnlyOSDisk = ( SwitchParameter ) ProviderData [ RestoreVMBackupItemParams . RestoreOnlyOSDisk ] ;
391
+ SwitchParameter restoreAsUnmanagedDisks = ( SwitchParameter ) ProviderData [ RestoreVMBackupItemParams . RestoreAsUnmanagedDisks ] ;
392
+
330
393
Dictionary < UriEnums , string > uriDict = HelperUtils . ParseUri ( rp . Id ) ;
331
394
string containerUri = HelperUtils . GetContainerUri ( uriDict , rp . Id ) ;
332
395
@@ -343,11 +406,36 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
343
406
throw new Exception ( string . Format ( Resources . RestoreDiskStorageTypeError , vmType ) ) ;
344
407
}
345
408
409
+ if ( targetResourceGroupName != null && restoreAsUnmanagedDisks . IsPresent )
410
+ {
411
+ throw new Exception ( Resources . TargetRGUnmanagedRestoreDuplicateParamsException ) ;
412
+ }
413
+
346
414
if ( targetResourceGroupName != null && rp . IsManagedVirtualMachine == false )
347
415
{
348
416
Logger . Instance . WriteWarning ( Resources . UnManagedBackupVmWarning ) ;
349
417
}
350
418
419
+ if ( rp . IsManagedVirtualMachine == true && targetResourceGroupName == null
420
+ && restoreAsUnmanagedDisks . IsPresent == false )
421
+ {
422
+ Logger . Instance . WriteWarning ( Resources . UnmanagedVMRestoreWarning ) ;
423
+ }
424
+
425
+ IList < int ? > restoreDiskLUNS ;
426
+ if ( restoreOnlyOSDisk . IsPresent )
427
+ {
428
+ restoreDiskLUNS = new List < int ? > ( ) ;
429
+ }
430
+ else if ( restoreDiskList != null )
431
+ {
432
+ restoreDiskLUNS = Array . ConvertAll ( restoreDiskList , s => int . Parse ( s ) ) . OfType < int ? > ( ) . ToList ( ) ;
433
+ }
434
+ else
435
+ {
436
+ restoreDiskLUNS = null ;
437
+ }
438
+
351
439
IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest ( )
352
440
{
353
441
CreateNewCloudService = false ,
@@ -360,6 +448,7 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
360
448
"/subscriptions/" + ServiceClientAdapter . SubscriptionId + "/resourceGroups/" + targetResourceGroupName :
361
449
null ,
362
450
OriginalStorageAccountOption = useOsa ,
451
+ RestoreDiskLunList = restoreDiskLUNS
363
452
} ;
364
453
365
454
RestoreRequestResource triggerRestoreRequest = new RestoreRequestResource ( ) ;
@@ -987,6 +1076,33 @@ private void ValidateAzureVMModifyProtectionRequest(ItemBase itemBase,
987
1076
}
988
1077
}
989
1078
1079
+ private bool ValidateDiskExclusionParameters ( string [ ] inclusionDiskList , string [ ] exclusionDiskList ,
1080
+ SwitchParameter resetDiskExclusion , bool excludeAllDataDisks )
1081
+ {
1082
+ bool isDiskExclusionParamPresent = false ;
1083
+ if ( inclusionDiskList != null || exclusionDiskList != null || resetDiskExclusion . IsPresent || excludeAllDataDisks )
1084
+ {
1085
+ isDiskExclusionParamPresent = true ;
1086
+ }
1087
+
1088
+ if ( inclusionDiskList != null && exclusionDiskList != null )
1089
+ {
1090
+ throw new ArgumentException ( Resources . InclusionListRedundantError ) ;
1091
+ }
1092
+
1093
+ if ( resetDiskExclusion . IsPresent && ( inclusionDiskList != null && exclusionDiskList != null ) )
1094
+ {
1095
+ throw new ArgumentException ( Resources . DiskExclusionParametersRedundant ) ;
1096
+ }
1097
+
1098
+ if ( excludeAllDataDisks && ( inclusionDiskList != null || exclusionDiskList != null || resetDiskExclusion . IsPresent ) )
1099
+ {
1100
+ throw new ArgumentException ( Resources . DiskExclusionParametersRedundant ) ;
1101
+ }
1102
+
1103
+ return isDiskExclusionParamPresent ;
1104
+ }
1105
+
990
1106
private void ValidateAzureVMDisableProtectionRequest ( ItemBase itemBase )
991
1107
{
992
1108
@@ -1140,6 +1256,7 @@ private bool IsDiscoveryNeeded(
1140
1256
{
1141
1257
IaaSVMProtectableItem iaaSVMProtectableItem =
1142
1258
( IaaSVMProtectableItem ) protectableItem . Properties ;
1259
+
1143
1260
if ( iaaSVMProtectableItem != null &&
1144
1261
string . Compare ( iaaSVMProtectableItem . FriendlyName , vmName , true ) == 0
1145
1262
&& iaaSVMProtectableItem . VirtualMachineId . IndexOf ( virtualMachineId ,
0 commit comments