Skip to content

Backup encryption settings and restore on failure (Supersedes #3824) #3849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ResourceManager/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--
<!--
Please leave this section at the top of the change log.

Changes for the current release should go under the section titled "Current Release", and should adhere to the following format:
Expand All @@ -19,6 +19,8 @@
-->
## Current Release

* Backup encryption settings for IaaS VMs and restore on failure

## Version 2.9.0

* Fix bug in Get-* cmdlets, to allow retrieving multiple pages of data (more than 120 items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
null));
}

DiskEncryptionSettings encryptionSettingsBackup = vmParameters.StorageProfile.OsDisk.EncryptionSettings;

if (encryptionSettingsBackup == null)
{
encryptionSettingsBackup = new DiskEncryptionSettings();
encryptionSettingsBackup.Enabled = false;
}

DiskEncryptionSettings encryptionSettings = new DiskEncryptionSettings();
encryptionSettings.Enabled = true;
encryptionSettings.DiskEncryptionKey = new KeyVaultSecretReference();
Expand All @@ -306,10 +314,25 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings()
Location = vmParameters.Location,
Tags = vmParameters.Tags
};
return this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(

AzureOperationResponse<VirtualMachine> updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
this.ResourceGroupName,
vmParameters.Name,
parameters).GetAwaiter().GetResult();

if(!updateResult.Response.IsSuccessStatusCode)
{
vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(
this.ResourceGroupName, this.VMName));
vmParameters.StorageProfile.OsDisk.EncryptionSettings = encryptionSettingsBackup;

this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
this.ResourceGroupName,
vmParameters.Name,
parameters).GetAwaiter().GetResult();
}

return updateResult;
}

private Hashtable GetExtensionPublicSettings()
Expand Down Expand Up @@ -444,12 +467,23 @@ public override void ExecuteCmdlet()

VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse);

this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
AzureOperationResponse<VirtualMachineExtension> extensionPushResult = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
this.ResourceGroupName,
this.VMName,
this.Name,
parameters).GetAwaiter().GetResult();

if (!extensionPushResult.Response.IsSuccessStatusCode)
{
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture,
"Installation failed for extension {0} with error {1}",
parameters.VirtualMachineExtensionType,
extensionPushResult.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult())),
"InvalidResult",
ErrorCategory.InvalidResult,
null));
}

var op = UpdateVmEncryptionSettings();
var result = Mapper.Map<PSAzureOperationResponse>(op);
WriteObject(result);
Expand Down