88using System . Management . Automation ;
99using System . Globalization ;
1010using AutoMapper ;
11+ using Microsoft . Azure . Commands . Compute . Extension . AzureVMBackup ;
1112
1213namespace Microsoft . Azure . Commands . Compute . Extension . AzureDiskEncryption
1314{
@@ -191,8 +192,24 @@ private string GetExtensionStatusMessage()
191192 ErrorCategory . InvalidResult ,
192193 null ) ) ;
193194 }
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 )
196213 {
197214 AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
198215 if ( ( context == null ) ||
@@ -293,6 +310,7 @@ private VirtualMachineExtension GetVmExtensionParameters()
293310 string SettingString = GetExtensionPublicSettings ( ) ;
294311 string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
295312
313+
296314 VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
297315 if ( vmParameters == null )
298316 {
@@ -302,21 +320,41 @@ private VirtualMachineExtension GetVmExtensionParameters()
302320 null ) ) ;
303321 }
304322
305- VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension
323+ VirtualMachineExtension vmExtensionParameters = null ;
324+ if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
306325 {
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+ }
316352
317353 return vmExtensionParameters ;
318354 }
319355
356+ private string currentOSType = null ;
357+
320358 protected override void ProcessRecord ( )
321359 {
322360 base . ProcessRecord ( ) ;
@@ -326,11 +364,42 @@ protected override void ProcessRecord()
326364 if ( this . Force . IsPresent ||
327365 this . ShouldContinue ( Properties . Resources . EnableAzureDiskEncryptionConfirmation , Properties . Resources . EnableAzureDiskEncryptionCaption ) )
328366 {
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+
329395 VirtualMachineExtension parameters = GetVmExtensionParameters ( ) ;
330396
331397 this . VirtualMachineExtensionClient . CreateOrUpdate ( this . ResourceGroupName ,
332398 this . VMName ,
333399 parameters ) ;
400+
401+
402+
334403 var op = UpdateVmEncryptionSettings ( ) ;
335404 WriteObject ( Mapper . Map < PSComputeLongRunningOperation > ( op ) ) ;
336405 }
0 commit comments