|
18 | 18 | using Microsoft.Azure.Management.Compute.Models;
|
19 | 19 | using Microsoft.Rest.Azure;
|
20 | 20 | using System;
|
| 21 | +using System.Collections; |
21 | 22 | using System.Globalization;
|
22 | 23 | using System.Management.Automation;
|
23 | 24 |
|
@@ -54,6 +55,54 @@ public class GetAzureDiskEncryptionStatusCommand : VirtualMachineExtensionBaseCm
|
54 | 55 | HelpMessage = "The extension name. If this parameter is not specified, default values used are AzureDiskEncryption for windows VMs and AzureDiskEncryptionForLinux for Linux VMs")]
|
55 | 56 | [ValidateNotNullOrEmpty]
|
56 | 57 | public string Name { get; set; }
|
| 58 | + |
| 59 | + private VirtualMachineExtension GetVmExtensionParameters(VirtualMachine vmParameters, OSType currentOSType) |
| 60 | + { |
| 61 | + Hashtable publicSettings = new Hashtable(); |
| 62 | + Hashtable protectedSettings = new Hashtable(); |
| 63 | + |
| 64 | + publicSettings.Add(AzureDiskEncryptionExtensionConstants.encryptionOperationKey, AzureDiskEncryptionExtensionConstants.queryEncryptionStatusOperation); |
| 65 | + publicSettings.Add(AzureDiskEncryptionExtensionConstants.sequenceVersionKey, Guid.NewGuid().ToString()); |
| 66 | + |
| 67 | + if (vmParameters == null) |
| 68 | + { |
| 69 | + ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "Get-AzureDiskEncryptionExtension can enable encryption only on a VM that was already created ")), |
| 70 | + "InvalidResult", |
| 71 | + ErrorCategory.InvalidResult, |
| 72 | + null)); |
| 73 | + } |
| 74 | + |
| 75 | + VirtualMachineExtension vmExtensionParameters = null; |
| 76 | + |
| 77 | + if (OperatingSystemTypes.Windows.Equals(currentOSType)) |
| 78 | + { |
| 79 | + this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultName; |
| 80 | + vmExtensionParameters = new VirtualMachineExtension |
| 81 | + { |
| 82 | + Location = vmParameters.Location, |
| 83 | + Publisher = AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, |
| 84 | + VirtualMachineExtensionType = this.Name, |
| 85 | + TypeHandlerVersion = AzureDiskEncryptionExtensionContext.ExtensionDefaultVersion, |
| 86 | + Settings = publicSettings, |
| 87 | + ProtectedSettings = protectedSettings |
| 88 | + }; |
| 89 | + } |
| 90 | + else if (OperatingSystemTypes.Linux.Equals(currentOSType)) |
| 91 | + { |
| 92 | + this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName; |
| 93 | + vmExtensionParameters = new VirtualMachineExtension |
| 94 | + { |
| 95 | + Location = vmParameters.Location, |
| 96 | + Publisher = AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, |
| 97 | + VirtualMachineExtensionType = this.Name, |
| 98 | + TypeHandlerVersion = AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultVersion, |
| 99 | + Settings = publicSettings, |
| 100 | + ProtectedSettings = protectedSettings |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + return vmExtensionParameters; |
| 105 | + } |
57 | 106 |
|
58 | 107 | private string GetExtensionStatusMessage(OSType currentOSType)
|
59 | 108 | {
|
@@ -284,13 +333,33 @@ public override void ExecuteCmdlet()
|
284 | 333 | EncryptionStatus osVolumeEncrypted = IsOsVolumeEncrypted(vmParameters);
|
285 | 334 | DiskEncryptionSettings osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings(vmParameters);
|
286 | 335 | EncryptionStatus dataVolumesEncrypted = AreDataVolumesEncrypted(vmParameters);
|
| 336 | + AzureDiskEncryptionStatusContext encryptionStatus = null; |
287 | 337 |
|
288 | 338 | OSType osType = GetOSType(vmParameters);
|
289 | 339 | switch (osType)
|
290 | 340 | {
|
291 | 341 | case OSType.Windows:
|
| 342 | + encryptionStatus = new AzureDiskEncryptionStatusContext |
| 343 | + { |
| 344 | + OsVolumeEncrypted = osVolumeEncrypted, |
| 345 | + DataVolumesEncrypted = dataVolumesEncrypted, |
| 346 | + OsVolumeEncryptionSettings = osVolumeEncryptionSettings, |
| 347 | + ProgressMessage = GetExtensionStatusMessage(osType) |
| 348 | + }; |
| 349 | + WriteObject(encryptionStatus); |
| 350 | + break; |
292 | 351 | case OSType.Linux:
|
293 |
| - AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext |
| 352 | + VirtualMachine virtualMachineResponse = this.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView( |
| 353 | + this.ResourceGroupName, VMName).Body; |
| 354 | + VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse, osType); |
| 355 | + |
| 356 | + this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync( |
| 357 | + this.ResourceGroupName, |
| 358 | + this.VMName, |
| 359 | + this.Name, |
| 360 | + parameters).GetAwaiter().GetResult(); |
| 361 | + |
| 362 | + encryptionStatus = new AzureDiskEncryptionStatusContext |
294 | 363 | {
|
295 | 364 | OsVolumeEncrypted = osVolumeEncrypted,
|
296 | 365 | DataVolumesEncrypted = dataVolumesEncrypted,
|
|
0 commit comments