Skip to content

Commit 8c081de

Browse files
committed
added a few try-catches
1 parent 2e6107c commit 8c081de

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,22 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
307307
Tags = vmParameters.Tags
308308
};
309309

310-
AzureOperationResponse<VirtualMachine> updateResult;
310+
AzureOperationResponse<VirtualMachine> updateResult = null;
311311

312312
// The 2nd pass. If something goes wrong here, try to revert to encryptionSettingsBackup.
313313
if (encryptionSettingsBackup.Enabled != true)
314314
{
315-
updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
316-
this.ResourceGroupName,
317-
vmParameters.Name,
318-
parameters).GetAwaiter().GetResult();
315+
try
316+
{
317+
updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
318+
this.ResourceGroupName,
319+
vmParameters.Name,
320+
parameters).GetAwaiter().GetResult();
321+
} catch (Exception e)
322+
{
323+
revertVm(encryptionSettingsBackup);
324+
throw e;
325+
}
319326

320327
if (!updateResult.Response.IsSuccessStatusCode)
321328
{
@@ -324,6 +331,7 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
324331
}
325332
else
326333
{
334+
327335
// For premium storage VMs, stop-update-start
328336
// stop vm
329337
this.ComputeClient.ComputeManagementClient.VirtualMachines
@@ -346,14 +354,28 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
346354
Location = vmParameters.Location,
347355
Tags = vmParameters.Tags
348356
};
349-
updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
350-
this.ResourceGroupName,
351-
vmParameters.Name,
352-
parameters).GetAwaiter().GetResult();
357+
AzureOperationResponse<Azure.Management.Compute.Models.OperationStatusResponse> startOp = null;
358+
try
359+
{
360+
updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
361+
this.ResourceGroupName,
362+
vmParameters.Name,
363+
parameters).GetAwaiter().GetResult();
353364

354-
// start vm
355-
var startOp = this.ComputeClient.ComputeManagementClient.VirtualMachines
356-
.StartWithHttpMessagesAsync(this.ResourceGroupName, this.VMName).GetAwaiter().GetResult();
365+
// start vm
366+
startOp = this.ComputeClient.ComputeManagementClient.VirtualMachines
367+
.StartWithHttpMessagesAsync(ResourceGroupName, this.VMName).GetAwaiter().GetResult();
368+
} catch (Exception e)
369+
{
370+
// in case of error: stop-revert-start
371+
this.ComputeClient.ComputeManagementClient.VirtualMachines
372+
.DeallocateWithHttpMessagesAsync(this.ResourceGroupName, this.VMName).GetAwaiter()
373+
.GetResult();
374+
revertVm(encryptionSettingsBackup);
375+
this.ComputeClient.ComputeManagementClient.VirtualMachines
376+
.StartWithHttpMessagesAsync(this.ResourceGroupName, this.VMName).GetAwaiter().GetResult();
377+
throw e;
378+
}
357379
if (!updateResult.Response.IsSuccessStatusCode || !startOp.Response.IsSuccessStatusCode)
358380
{
359381
// in case of error: stop-revert-start

0 commit comments

Comments
 (0)