|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.postgresqlflexibleserver; |
| 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.postgresqlflexibleserver.models.ActiveDirectoryAuthEnum; |
| 18 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.ArmServerKeyType; |
| 19 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.AuthConfig; |
| 20 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.Backup; |
| 21 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.DataEncryption; |
| 22 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.GeoRedundantBackupEnum; |
| 23 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailability; |
| 24 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailabilityMode; |
| 25 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.IdentityType; |
| 26 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.PasswordAuthEnum; |
| 27 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.ReplicationRole; |
| 28 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.Server; |
| 29 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.ServerVersion; |
| 30 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.Sku; |
| 31 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.SkuTier; |
| 32 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.Storage; |
| 33 | +import com.azure.resourcemanager.postgresqlflexibleserver.models.UserAssignedIdentity; |
| 34 | +import com.azure.resourcemanager.resources.ResourceManager; |
| 35 | +import io.netty.util.internal.StringUtil; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import java.util.Random; |
| 40 | +import java.util.UUID; |
| 41 | + |
| 42 | +public class PostgreSqlManagerTests extends TestBase { |
| 43 | + private static final Random RANDOM = new Random(); |
| 44 | + private static final Region REGION = Region.US_EAST; |
| 45 | + private String resourceGroupName = "rg" + randomPadding(); |
| 46 | + private PostgreSqlManager postgreSqlManager; |
| 47 | + private ResourceManager resourceManager; |
| 48 | + private boolean testEnv; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void beforeTest() { |
| 52 | + final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); |
| 53 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 54 | + |
| 55 | + postgreSqlManager = PostgreSqlManager |
| 56 | + .configure() |
| 57 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 58 | + .authenticate(credential, profile); |
| 59 | + |
| 60 | + resourceManager = ResourceManager |
| 61 | + .configure() |
| 62 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 63 | + .authenticate(credential, profile) |
| 64 | + .withDefaultSubscription(); |
| 65 | + |
| 66 | + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI |
| 67 | + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); |
| 68 | + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); |
| 69 | + if (testEnv) { |
| 70 | + resourceGroupName = testResourceGroup; |
| 71 | + } else { |
| 72 | + resourceManager.resourceGroups() |
| 73 | + .define(resourceGroupName) |
| 74 | + .withRegion(REGION) |
| 75 | + .create(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void afterTest() { |
| 81 | + if (!testEnv) { |
| 82 | + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DoNotRecord(skipInPlayback = true) |
| 88 | + public void testCreateServer() { |
| 89 | + Server server = null; |
| 90 | + String randomPadding = randomPadding(); |
| 91 | + try { |
| 92 | + String serverName = "postgresql" + randomPadding; |
| 93 | + String adminName = "sqlAdmin" + randomPadding; |
| 94 | + String adminPwd = "sqlAdmin" |
| 95 | + + UUID.randomUUID().toString().replace("-", StringUtil.EMPTY_STRING).substring(0, 8); |
| 96 | + // @embedmeStart |
| 97 | + server = postgreSqlManager.servers() |
| 98 | + .define(serverName) |
| 99 | + .withRegion(REGION) |
| 100 | + .withExistingResourceGroup(resourceGroupName) |
| 101 | + .withAdministratorLogin(adminName) |
| 102 | + .withAdministratorLoginPassword(adminPwd) |
| 103 | + .withSku(new Sku().withName("Standard_D2ds_v4").withTier(SkuTier.GENERAL_PURPOSE)) |
| 104 | + .withAuthConfig(new AuthConfig() |
| 105 | + .withActiveDirectoryAuth(ActiveDirectoryAuthEnum.DISABLED) |
| 106 | + .withPasswordAuth(PasswordAuthEnum.ENABLED)) |
| 107 | + .withIdentity(new UserAssignedIdentity().withType(IdentityType.NONE)) |
| 108 | + .withDataEncryption(new DataEncryption().withType(ArmServerKeyType.SYSTEM_MANAGED)) |
| 109 | + .withVersion(ServerVersion.ONE_FOUR) |
| 110 | + .withAvailabilityZone("2") |
| 111 | + .withStorage(new Storage().withStorageSizeGB(128)) |
| 112 | + .withBackup(new Backup() |
| 113 | + .withGeoRedundantBackup(GeoRedundantBackupEnum.DISABLED) |
| 114 | + .withBackupRetentionDays(7)) |
| 115 | + .withHighAvailability(new HighAvailability().withMode(HighAvailabilityMode.DISABLED)) |
| 116 | + .withReplicationRole(ReplicationRole.PRIMARY) |
| 117 | + .withReplicaCapacity(5) |
| 118 | + .create(); |
| 119 | + // @embedmeEnd |
| 120 | + server.refresh(); |
| 121 | + Assertions.assertEquals(server.name(), serverName); |
| 122 | + Assertions.assertEquals(server.name(), postgreSqlManager.servers().getById(server.id()).name()); |
| 123 | + Assertions.assertTrue(postgreSqlManager.servers().list().stream().count() > 0); |
| 124 | + } finally { |
| 125 | + if (server != null) { |
| 126 | + postgreSqlManager.servers().deleteById(server.id()); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static String randomPadding() { |
| 132 | + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); |
| 133 | + } |
| 134 | +} |
0 commit comments