8
8
using System . Management . Automation ;
9
9
using System . Globalization ;
10
10
using AutoMapper ;
11
+ using Microsoft . Azure . Commands . Compute . Extension . AzureVMBackup ;
11
12
12
13
namespace Microsoft . Azure . Commands . Compute . Extension . AzureDiskEncryption
13
14
{
@@ -191,8 +192,24 @@ private string GetExtensionStatusMessage()
191
192
ErrorCategory . InvalidResult ,
192
193
null ) ) ;
193
194
}
194
- if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
195
- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
195
+ bool publisherMismatch = false ;
196
+ if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
197
+ {
198
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
199
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
200
+ {
201
+ publisherMismatch = true ;
202
+ }
203
+ }
204
+ else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
205
+ {
206
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
207
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
208
+ {
209
+ publisherMismatch = true ;
210
+ }
211
+ }
212
+ if ( publisherMismatch )
196
213
{
197
214
AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
198
215
if ( ( context == null ) ||
@@ -293,6 +310,7 @@ private VirtualMachineExtension GetVmExtensionParameters()
293
310
string SettingString = GetExtensionPublicSettings ( ) ;
294
311
string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
295
312
313
+
296
314
VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
297
315
if ( vmParameters == null )
298
316
{
@@ -302,21 +320,41 @@ private VirtualMachineExtension GetVmExtensionParameters()
302
320
null ) ) ;
303
321
}
304
322
305
- VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension
323
+ VirtualMachineExtension vmExtensionParameters = null ;
324
+ if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
306
325
{
307
- Location = vmParameters . Location ,
308
- Name = this . Name ,
309
- Type = VirtualMachineExtensionType ,
310
- Publisher = AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher ,
311
- ExtensionType = AzureDiskEncryptionExtensionContext . ExtensionDefaultName ,
312
- TypeHandlerVersion = ( this . TypeHandlerVersion ) ?? AzureDiskEncryptionExtensionContext . ExtensionDefaultVersion ,
313
- Settings = SettingString ,
314
- ProtectedSettings = ProtectedSettingString ,
315
- } ;
326
+ vmExtensionParameters = new VirtualMachineExtension
327
+ {
328
+ Location = vmParameters . Location ,
329
+ Name = this . Name ,
330
+ Type = VirtualMachineExtensionType ,
331
+ Publisher = AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher ,
332
+ ExtensionType = AzureDiskEncryptionExtensionContext . ExtensionDefaultName ,
333
+ TypeHandlerVersion = ( this . TypeHandlerVersion ) ?? AzureDiskEncryptionExtensionContext . ExtensionDefaultVersion ,
334
+ Settings = SettingString ,
335
+ ProtectedSettings = ProtectedSettingString ,
336
+ } ;
337
+ }
338
+ else if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
339
+ {
340
+ vmExtensionParameters = new VirtualMachineExtension
341
+ {
342
+ Location = vmParameters . Location ,
343
+ Name = this . Name ,
344
+ Type = VirtualMachineExtensionType ,
345
+ Publisher = AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher ,
346
+ ExtensionType = AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName ,
347
+ TypeHandlerVersion = ( this . TypeHandlerVersion ) ?? AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultVersion ,
348
+ Settings = SettingString ,
349
+ ProtectedSettings = ProtectedSettingString ,
350
+ } ;
351
+ }
316
352
317
353
return vmExtensionParameters ;
318
354
}
319
355
356
+ private string currentOSType = null ;
357
+
320
358
protected override void ProcessRecord ( )
321
359
{
322
360
base . ProcessRecord ( ) ;
@@ -326,11 +364,42 @@ protected override void ProcessRecord()
326
364
if ( this . Force . IsPresent ||
327
365
this . ShouldContinue ( Properties . Resources . EnableAzureDiskEncryptionConfirmation , Properties . Resources . EnableAzureDiskEncryptionCaption ) )
328
366
{
367
+ VirtualMachine virtualMachineResponse = this . ComputeClient . ComputeManagementClient . VirtualMachines . GetWithInstanceView ( this . ResourceGroupName , VMName ) . VirtualMachine ;
368
+
369
+ currentOSType = virtualMachineResponse . StorageProfile . OSDisk . OperatingSystemType ;
370
+
371
+ if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
372
+ {
373
+ try
374
+ {
375
+ AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil ( ) ;
376
+ AzureVMBackupConfig vmConfig = new AzureVMBackupConfig ( ) ;
377
+ vmConfig . ResourceGroupName = ResourceGroupName ;
378
+ vmConfig . VMName = VMName ;
379
+ vmConfig . VirtualMachineExtensionType = VirtualMachineExtensionType ;
380
+ string tag = string . Format ( "{0}{1}" , "AzureEnc" , Guid . NewGuid ( ) . ToString ( ) ) ;
381
+ // this would create shapshot only for Linux box. and we should wait for the snapshot found.
382
+ azureBackupExtensionUtil . CreateSnapshotForDisks ( vmConfig , tag , this ) ;
383
+
384
+ WriteInformation ( new InformationRecord ( string . Format ( "one snapshot for disks are created with tag,{0}" , tag ) , string . Empty ) ) ;
385
+ }
386
+ catch ( AzureVMBackupException e )
387
+ {
388
+ ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , e . ToString ( ) ) ) ,
389
+ "InvalidResult" ,
390
+ ErrorCategory . InvalidResult ,
391
+ null ) ) ;
392
+ }
393
+ }
394
+
329
395
VirtualMachineExtension parameters = GetVmExtensionParameters ( ) ;
330
396
331
397
this . VirtualMachineExtensionClient . CreateOrUpdate ( this . ResourceGroupName ,
332
398
this . VMName ,
333
399
parameters ) ;
400
+
401
+
402
+
334
403
var op = UpdateVmEncryptionSettings ( ) ;
335
404
WriteObject ( Mapper . Map < PSComputeLongRunningOperation > ( op ) ) ;
336
405
}
0 commit comments