|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.dataprotection; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 8 | +import com.azure.core.http.policy.HttpLogOptions; |
| 9 | +import com.azure.core.management.AzureEnvironment; |
| 10 | +import com.azure.core.management.Region; |
| 11 | +import com.azure.core.management.profile.AzureProfile; |
| 12 | +import com.azure.core.test.TestBase; |
| 13 | +import com.azure.core.test.annotation.DoNotRecord; |
| 14 | +import com.azure.core.util.Configuration; |
| 15 | +import com.azure.core.util.CoreUtils; |
| 16 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 17 | +import com.azure.resourcemanager.dataprotection.models.AlertsState; |
| 18 | +import com.azure.resourcemanager.dataprotection.models.AzureMonitorAlertSettings; |
| 19 | +import com.azure.resourcemanager.dataprotection.models.BackupVault; |
| 20 | +import com.azure.resourcemanager.dataprotection.models.BackupVaultResource; |
| 21 | +import com.azure.resourcemanager.dataprotection.models.CrossSubscriptionRestoreSettings; |
| 22 | +import com.azure.resourcemanager.dataprotection.models.CrossSubscriptionRestoreState; |
| 23 | +import com.azure.resourcemanager.dataprotection.models.DppIdentityDetails; |
| 24 | +import com.azure.resourcemanager.dataprotection.models.FeatureSettings; |
| 25 | +import com.azure.resourcemanager.dataprotection.models.ImmutabilitySettings; |
| 26 | +import com.azure.resourcemanager.dataprotection.models.ImmutabilityState; |
| 27 | +import com.azure.resourcemanager.dataprotection.models.MonitoringSettings; |
| 28 | +import com.azure.resourcemanager.dataprotection.models.SecuritySettings; |
| 29 | +import com.azure.resourcemanager.dataprotection.models.SoftDeleteSettings; |
| 30 | +import com.azure.resourcemanager.dataprotection.models.SoftDeleteState; |
| 31 | +import com.azure.resourcemanager.dataprotection.models.StorageSetting; |
| 32 | +import com.azure.resourcemanager.dataprotection.models.StorageSettingStoreTypes; |
| 33 | +import com.azure.resourcemanager.dataprotection.models.StorageSettingTypes; |
| 34 | +import com.azure.resourcemanager.resources.ResourceManager; |
| 35 | +import org.junit.jupiter.api.Assertions; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.Random; |
| 40 | + |
| 41 | +public class DataProtectionManagerTest extends TestBase { |
| 42 | + private static final Random RANDOM = new Random(); |
| 43 | + private static final Region REGION = Region.US_WEST2; |
| 44 | + private String resourceGroupName = "rg" + randomPadding(); |
| 45 | + private DataProtectionManager dataProtectionManager; |
| 46 | + private ResourceManager resourceManager; |
| 47 | + private boolean testEnv; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void beforeTest() { |
| 51 | + final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); |
| 52 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 53 | + |
| 54 | + dataProtectionManager = DataProtectionManager |
| 55 | + .configure() |
| 56 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 57 | + .authenticate(credential, profile); |
| 58 | + |
| 59 | + resourceManager = ResourceManager |
| 60 | + .configure() |
| 61 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 62 | + .authenticate(credential, profile) |
| 63 | + .withDefaultSubscription(); |
| 64 | + |
| 65 | + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI |
| 66 | + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); |
| 67 | + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); |
| 68 | + if (testEnv) { |
| 69 | + resourceGroupName = testResourceGroup; |
| 70 | + } else { |
| 71 | + resourceManager.resourceGroups() |
| 72 | + .define(resourceGroupName) |
| 73 | + .withRegion(REGION) |
| 74 | + .create(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void afterTest() { |
| 80 | + if (!testEnv) { |
| 81 | + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DoNotRecord(skipInPlayback = true) |
| 87 | + public void testCreateBackupVault() { |
| 88 | + BackupVaultResource resource = null; |
| 89 | + try { |
| 90 | + String vaultName = "vault" + randomPadding(); |
| 91 | + // @embedmeStart |
| 92 | + resource = dataProtectionManager |
| 93 | + .backupVaults() |
| 94 | + .define(vaultName) |
| 95 | + .withRegion(REGION) |
| 96 | + .withExistingResourceGroup(resourceGroupName) |
| 97 | + .withProperties( |
| 98 | + new BackupVault() |
| 99 | + .withMonitoringSettings( |
| 100 | + new MonitoringSettings() |
| 101 | + .withAzureMonitorAlertSettings( |
| 102 | + new AzureMonitorAlertSettings() |
| 103 | + .withAlertsForAllJobFailures(AlertsState.ENABLED))) |
| 104 | + .withSecuritySettings( |
| 105 | + new SecuritySettings() |
| 106 | + .withSoftDeleteSettings( |
| 107 | + new SoftDeleteSettings() |
| 108 | + .withState(SoftDeleteState.ALWAYS_ON) |
| 109 | + .withRetentionDurationInDays(14.0D)) |
| 110 | + .withImmutabilitySettings( |
| 111 | + new ImmutabilitySettings() |
| 112 | + .withState(ImmutabilityState.LOCKED))) |
| 113 | + .withStorageSettings( |
| 114 | + Collections.singletonList( |
| 115 | + new StorageSetting() |
| 116 | + .withDatastoreType(StorageSettingStoreTypes.VAULT_STORE) |
| 117 | + .withType(StorageSettingTypes.LOCALLY_REDUNDANT))) |
| 118 | + .withFeatureSettings( |
| 119 | + new FeatureSettings() |
| 120 | + .withCrossSubscriptionRestoreSettings( |
| 121 | + new CrossSubscriptionRestoreSettings() |
| 122 | + .withState(CrossSubscriptionRestoreState.ENABLED)))) |
| 123 | + .withIdentity(new DppIdentityDetails().withType("systemAssigned")) |
| 124 | + .create(); |
| 125 | + // @embedmeEnd |
| 126 | + resource.refresh(); |
| 127 | + Assertions.assertEquals(resource.name(), vaultName); |
| 128 | + Assertions.assertEquals(resource.name(), dataProtectionManager.backupVaults().getById(resource.id()).name()); |
| 129 | + Assertions.assertTrue(dataProtectionManager.backupVaults().list().stream().count() > 0); |
| 130 | + } finally { |
| 131 | + if (resource != null) { |
| 132 | + dataProtectionManager.backupVaults().deleteById(resource.id()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static String randomPadding() { |
| 138 | + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments