Skip to content

Commit c438c77

Browse files
author
Kamran Khan
committed
Restore encryption settings if updateVm call fails
1 parent 8259119 commit c438c77

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
282282
null));
283283
}
284284

285+
DiskEncryptionSettings encryptionSettingsBackup = vmParameters.StorageProfile.OsDisk.EncryptionSettings;
286+
285287
DiskEncryptionSettings encryptionSettings = new DiskEncryptionSettings();
286288
encryptionSettings.Enabled = true;
287289
encryptionSettings.DiskEncryptionKey = new KeyVaultSecretReference();
@@ -306,10 +308,25 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
306308
Location = vmParameters.Location,
307309
Tags = vmParameters.Tags
308310
};
309-
return this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
311+
312+
AzureOperationResponse<VirtualMachine> updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
310313
this.ResourceGroupName,
311314
vmParameters.Name,
312315
parameters).GetAwaiter().GetResult();
316+
317+
if(!updateResult.Response.IsSuccessStatusCode)
318+
{
319+
vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(
320+
this.ResourceGroupName, this.VMName));
321+
vmParameters.StorageProfile.OsDisk.EncryptionSettings = encryptionSettingsBackup;
322+
323+
this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
324+
this.ResourceGroupName,
325+
vmParameters.Name,
326+
parameters).GetAwaiter().GetResult();
327+
}
328+
329+
return updateResult;
313330
}
314331

315332
private Hashtable GetExtensionPublicSettings()

0 commit comments

Comments
 (0)