/sdk/compute/mgmt/src/test/java/com/azure/management/compute/VirtualMachineEncryptionOperationsTests.java

http://github.com/WindowsAzure/azure-sdk-for-java · Java · 77 lines · 62 code · 8 blank · 7 comment · 0 complexity · 89f2d4fb89725540a744a9461b0426ed MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.management.compute;
  4. import com.azure.core.http.HttpPipeline;
  5. import com.azure.management.resources.fluentcore.arm.Region;
  6. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  7. import org.junit.jupiter.api.Assertions;
  8. import org.junit.jupiter.api.Disabled;
  9. import org.junit.jupiter.api.Test;
  10. public class VirtualMachineEncryptionOperationsTests extends ComputeManagementTest {
  11. private String rgName = "";
  12. private Region region = Region.US_EAST;
  13. @Override
  14. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
  15. rgName = generateRandomResourceName("vmencryptst", 18);
  16. super.initializeClients(httpPipeline, profile);
  17. }
  18. @Override
  19. protected void cleanUpResources() {
  20. resourceManager.resourceGroups().deleteByName(rgName);
  21. }
  22. @Test
  23. @Disabled("Requires manually creating service principal and setting SP credentials in the test")
  24. public void canEncryptVirtualMachine() {
  25. // https://docs.microsoft.com/en-us/azure/security/azure-security-disk-encryption
  26. //
  27. // KeyVault Resource ID
  28. String keyVaultId = "KEY_VAULT_ID_HERE";
  29. // Azure AD service principal client (application) ID
  30. String aadClientId = "AAD_APPLICATION_ID_HERE";
  31. // Azure AD service principal client secret
  32. String aadSecret = "AAD_CLIENT_SECRET_HERE";
  33. final String vmName1 = "myvm1";
  34. final String publicIpDnsLabel = generateRandomResourceName("pip", 20);
  35. final String uname = "juser";
  36. final String password = "123tEst!@|ac";
  37. VirtualMachine virtualMachine =
  38. computeManager
  39. .virtualMachines()
  40. .define(vmName1)
  41. .withRegion(region)
  42. .withNewResourceGroup(rgName)
  43. .withNewPrimaryNetwork("10.0.0.0/28")
  44. .withPrimaryPrivateIPAddressDynamic()
  45. .withNewPrimaryPublicIPAddress(publicIpDnsLabel)
  46. .withLatestLinuxImage("RedHat", "RHEL", "7.2")
  47. .withRootUsername(uname)
  48. .withRootPassword(password)
  49. .withSize(VirtualMachineSizeTypes.STANDARD_D5_V2)
  50. .withOSDiskCaching(CachingTypes.READ_WRITE)
  51. .create();
  52. DiskVolumeEncryptionMonitor monitor1 = virtualMachine.diskEncryption().getMonitor();
  53. Assertions.assertNotNull(monitor1);
  54. Assertions.assertNotNull(monitor1.osDiskStatus());
  55. Assertions.assertNotNull(monitor1.dataDiskStatus());
  56. Assertions.assertTrue(monitor1.osDiskStatus().equals(EncryptionStatus.NOT_ENCRYPTED));
  57. Assertions.assertTrue(monitor1.dataDiskStatus().equals(EncryptionStatus.NOT_ENCRYPTED));
  58. DiskVolumeEncryptionMonitor monitor2 =
  59. virtualMachine.diskEncryption().enable(keyVaultId, aadClientId, aadSecret);
  60. Assertions.assertNotNull(monitor2);
  61. Assertions.assertNotNull(monitor2.osDiskStatus());
  62. Assertions.assertNotNull(monitor2.dataDiskStatus());
  63. monitor1.refresh();
  64. Assertions.assertTrue(monitor1.osDiskStatus().equals(monitor2.osDiskStatus()));
  65. Assertions.assertTrue(monitor1.dataDiskStatus().equals(monitor2.dataDiskStatus()));
  66. monitor2.refresh();
  67. Assertions.assertTrue(monitor2.osDiskStatus().equals(EncryptionStatus.ENCRYPTION_INPROGRESS));
  68. }
  69. }