1- using Microsoft . Azure . Commands . Compute . Common ;
1+ // ----------------------------------------------------------------------------------
2+ //
3+ // Copyright Microsoft Corporation
4+ // Licensed under the Apache License, Version 2.0 (the "License");
5+ // you may not use this file except in compliance with the License.
6+ // You may obtain a copy of the License at
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ // Unless required by applicable law or agreed to in writing, software
9+ // distributed under the License is distributed on an "AS IS" BASIS,
10+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ // See the License for the specific language governing permissions and
12+ // limitations under the License.
13+ // ----------------------------------------------------------------------------------
14+
15+ using Microsoft . Azure . Commands . Compute . Common ;
216using Microsoft . Azure . Commands . Compute . Models ;
317using Microsoft . Azure . Management . Compute ;
418using Microsoft . Azure . Management . Compute . Models ;
@@ -151,6 +165,8 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
151165 [ ValidateNotNullOrEmpty ]
152166 public SwitchParameter Force { get ; set ; }
153167
168+ private string currentOSType = null ;
169+
154170 private void ValidateInputParameters ( )
155171 {
156172 if ( false == Uri . IsWellFormedUriString ( DiskEncryptionKeyVaultId , UriKind . Absolute ) )
@@ -192,24 +208,24 @@ private string GetExtensionStatusMessage()
192208 ErrorCategory . InvalidResult ,
193209 null ) ) ;
194210 }
195- bool publisherMismatch = false ;
211+ bool publisherMatch = false ;
196212 if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
197213 {
198214 if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
199- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
215+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
200216 {
201- publisherMismatch = true ;
217+ publisherMatch = true ;
202218 }
203219 }
204- else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
220+ else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
205221 {
206222 if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
207- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
223+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
208224 {
209- publisherMismatch = true ;
225+ publisherMatch = true ;
210226 }
211227 }
212- if ( publisherMismatch )
228+ if ( publisherMatch )
213229 {
214230 AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
215231 if ( ( context == null ) ||
@@ -305,13 +321,11 @@ private string GetExtensionProtectedSettings()
305321 return JsonConvert . SerializeObject ( protectedSettings ) ;
306322 }
307323
308- private VirtualMachineExtension GetVmExtensionParameters ( )
324+ private VirtualMachineExtension GetVmExtensionParameters ( VirtualMachine vmParameters )
309325 {
310326 string SettingString = GetExtensionPublicSettings ( ) ;
311327 string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
312328
313-
314- VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
315329 if ( vmParameters == null )
316330 {
317331 ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , "Set-AzureDiskEncryptionExtension can enable encryption only on a VM that was already created " ) ) ,
@@ -353,7 +367,29 @@ private VirtualMachineExtension GetVmExtensionParameters()
353367 return vmExtensionParameters ;
354368 }
355369
356- private string currentOSType = null ;
370+ private void CreateVMBackupForLinx ( )
371+ {
372+ try
373+ {
374+ AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil ( ) ;
375+ AzureVMBackupConfig vmConfig = new AzureVMBackupConfig ( ) ;
376+ vmConfig . ResourceGroupName = ResourceGroupName ;
377+ vmConfig . VMName = VMName ;
378+ vmConfig . VirtualMachineExtensionType = VirtualMachineExtensionType ;
379+ string tag = string . Format ( "{0}{1}" , "AzureEnc" , Guid . NewGuid ( ) . ToString ( ) ) ;
380+ // this would create shapshot only for Linux box. and we should wait for the snapshot found.
381+ azureBackupExtensionUtil . CreateSnapshotForDisks ( vmConfig , tag , this ) ;
382+
383+ WriteInformation ( new InformationRecord ( string . Format ( "one snapshot for disks are created with tag,{0}" , tag ) , string . Empty ) ) ;
384+ }
385+ catch ( AzureVMBackupException e )
386+ {
387+ ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , e . ToString ( ) ) ) ,
388+ "InvalidResult" ,
389+ ErrorCategory . InvalidResult ,
390+ null ) ) ;
391+ }
392+ }
357393
358394 protected override void ProcessRecord ( )
359395 {
@@ -368,37 +404,16 @@ protected override void ProcessRecord()
368404
369405 currentOSType = virtualMachineResponse . StorageProfile . OSDisk . OperatingSystemType ;
370406
371- if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
407+ if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
372408 {
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- }
409+ CreateVMBackupForLinx ( ) ;
393410 }
394411
395- VirtualMachineExtension parameters = GetVmExtensionParameters ( ) ;
412+ VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse ) ;
396413
397414 this . VirtualMachineExtensionClient . CreateOrUpdate ( this . ResourceGroupName ,
398- this . VMName ,
399- parameters ) ;
400-
401-
415+ this . VMName ,
416+ parameters ) ;
402417
403418 var op = UpdateVmEncryptionSettings ( ) ;
404419 WriteObject ( Mapper . Map < PSComputeLongRunningOperation > ( op ) ) ;
0 commit comments