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 ;
2
16
using Microsoft . Azure . Commands . Compute . Models ;
3
17
using Microsoft . Azure . Management . Compute ;
4
18
using Microsoft . Azure . Management . Compute . Models ;
@@ -151,6 +165,8 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
151
165
[ ValidateNotNullOrEmpty ]
152
166
public SwitchParameter Force { get ; set ; }
153
167
168
+ private string currentOSType = null ;
169
+
154
170
private void ValidateInputParameters ( )
155
171
{
156
172
if ( false == Uri . IsWellFormedUriString ( DiskEncryptionKeyVaultId , UriKind . Absolute ) )
@@ -192,24 +208,24 @@ private string GetExtensionStatusMessage()
192
208
ErrorCategory . InvalidResult ,
193
209
null ) ) ;
194
210
}
195
- bool publisherMismatch = false ;
211
+ bool publisherMatch = false ;
196
212
if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
197
213
{
198
214
if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
199
- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
215
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
200
216
{
201
- publisherMismatch = true ;
217
+ publisherMatch = true ;
202
218
}
203
219
}
204
- else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
220
+ else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
205
221
{
206
222
if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
207
- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
223
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
208
224
{
209
- publisherMismatch = true ;
225
+ publisherMatch = true ;
210
226
}
211
227
}
212
- if ( publisherMismatch )
228
+ if ( publisherMatch )
213
229
{
214
230
AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
215
231
if ( ( context == null ) ||
@@ -305,13 +321,11 @@ private string GetExtensionProtectedSettings()
305
321
return JsonConvert . SerializeObject ( protectedSettings ) ;
306
322
}
307
323
308
- private VirtualMachineExtension GetVmExtensionParameters ( )
324
+ private VirtualMachineExtension GetVmExtensionParameters ( VirtualMachine vmParameters )
309
325
{
310
326
string SettingString = GetExtensionPublicSettings ( ) ;
311
327
string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
312
328
313
-
314
- VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
315
329
if ( vmParameters == null )
316
330
{
317
331
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()
353
367
return vmExtensionParameters ;
354
368
}
355
369
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
+ }
357
393
358
394
protected override void ProcessRecord ( )
359
395
{
@@ -368,37 +404,16 @@ protected override void ProcessRecord()
368
404
369
405
currentOSType = virtualMachineResponse . StorageProfile . OSDisk . OperatingSystemType ;
370
406
371
- if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
407
+ if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
372
408
{
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 ( ) ;
393
410
}
394
411
395
- VirtualMachineExtension parameters = GetVmExtensionParameters ( ) ;
412
+ VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse ) ;
396
413
397
414
this . VirtualMachineExtensionClient . CreateOrUpdate ( this . ResourceGroupName ,
398
- this . VMName ,
399
- parameters ) ;
400
-
401
-
415
+ this . VMName ,
416
+ parameters ) ;
402
417
403
418
var op = UpdateVmEncryptionSettings ( ) ;
404
419
WriteObject ( Mapper . Map < PSComputeLongRunningOperation > ( op ) ) ;
0 commit comments