Skip to content

Commit 3440c4e

Browse files
authored
Merge pull request Azure#3849 from krkhan/backup_enc_settings_rebased
Backup encryption settings and restore on failure (Supersedes Azure#3824)
2 parents c0a7f43 + f0f6d6c commit 3440c4e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/ResourceManager/Compute/ChangeLog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--
1+
<!--
22
Please leave this section at the top of the change log.
33
44
Changes for the current release should go under the section titled "Current Release", and should adhere to the following format:
@@ -19,6 +19,8 @@
1919
-->
2020
## Current Release
2121

22+
* Backup encryption settings for IaaS VMs and restore on failure
23+
2224
## Version 2.9.0
2325

2426
* Fix bug in Get-* cmdlets, to allow retrieving multiple pages of data (more than 120 items)

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/SetAzureDiskEncryptionExtension.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
282282
null));
283283
}
284284

285+
DiskEncryptionSettings encryptionSettingsBackup = vmParameters.StorageProfile.OsDisk.EncryptionSettings;
286+
287+
if (encryptionSettingsBackup == null)
288+
{
289+
encryptionSettingsBackup = new DiskEncryptionSettings();
290+
encryptionSettingsBackup.Enabled = false;
291+
}
292+
285293
DiskEncryptionSettings encryptionSettings = new DiskEncryptionSettings();
286294
encryptionSettings.Enabled = true;
287295
encryptionSettings.DiskEncryptionKey = new KeyVaultSecretReference();
@@ -306,10 +314,25 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
306314
Location = vmParameters.Location,
307315
Tags = vmParameters.Tags
308316
};
309-
return this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
317+
318+
AzureOperationResponse<VirtualMachine> updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
310319
this.ResourceGroupName,
311320
vmParameters.Name,
312321
parameters).GetAwaiter().GetResult();
322+
323+
if(!updateResult.Response.IsSuccessStatusCode)
324+
{
325+
vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(
326+
this.ResourceGroupName, this.VMName));
327+
vmParameters.StorageProfile.OsDisk.EncryptionSettings = encryptionSettingsBackup;
328+
329+
this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
330+
this.ResourceGroupName,
331+
vmParameters.Name,
332+
parameters).GetAwaiter().GetResult();
333+
}
334+
335+
return updateResult;
313336
}
314337

315338
private Hashtable GetExtensionPublicSettings()
@@ -444,12 +467,23 @@ public override void ExecuteCmdlet()
444467

445468
VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse);
446469

447-
this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
470+
AzureOperationResponse<VirtualMachineExtension> extensionPushResult = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
448471
this.ResourceGroupName,
449472
this.VMName,
450473
this.Name,
451474
parameters).GetAwaiter().GetResult();
452475

476+
if (!extensionPushResult.Response.IsSuccessStatusCode)
477+
{
478+
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture,
479+
"Installation failed for extension {0} with error {1}",
480+
parameters.VirtualMachineExtensionType,
481+
extensionPushResult.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult())),
482+
"InvalidResult",
483+
ErrorCategory.InvalidResult,
484+
null));
485+
}
486+
453487
var op = UpdateVmEncryptionSettings();
454488
var result = Mapper.Map<PSAzureOperationResponse>(op);
455489
WriteObject(result);

0 commit comments

Comments
 (0)