diff --git a/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/pom.xml b/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/pom.xml index 3f4eaa55952a4..8370c3717523f 100644 --- a/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/pom.xml +++ b/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/pom.xml @@ -57,6 +57,12 @@ azure-core-management 1.11.2 + + com.azure.resourcemanager + azure-resourcemanager-resources + 2.27.0 + test + com.azure azure-core-test diff --git a/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/src/test/java/com/azure/resourcemanager/postgresqlflexibleserver/PostgreSqlManagerTests.java b/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/src/test/java/com/azure/resourcemanager/postgresqlflexibleserver/PostgreSqlManagerTests.java new file mode 100644 index 0000000000000..8328c752c0085 --- /dev/null +++ b/sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/src/test/java/com/azure/resourcemanager/postgresqlflexibleserver/PostgreSqlManagerTests.java @@ -0,0 +1,134 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.resourcemanager.postgresqlflexibleserver; + +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.management.AzureEnvironment; +import com.azure.core.management.Region; +import com.azure.core.management.profile.AzureProfile; +import com.azure.core.test.TestBase; +import com.azure.core.test.annotation.DoNotRecord; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.resourcemanager.postgresqlflexibleserver.models.ActiveDirectoryAuthEnum; +import com.azure.resourcemanager.postgresqlflexibleserver.models.ArmServerKeyType; +import com.azure.resourcemanager.postgresqlflexibleserver.models.AuthConfig; +import com.azure.resourcemanager.postgresqlflexibleserver.models.Backup; +import com.azure.resourcemanager.postgresqlflexibleserver.models.DataEncryption; +import com.azure.resourcemanager.postgresqlflexibleserver.models.GeoRedundantBackupEnum; +import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailability; +import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailabilityMode; +import com.azure.resourcemanager.postgresqlflexibleserver.models.IdentityType; +import com.azure.resourcemanager.postgresqlflexibleserver.models.PasswordAuthEnum; +import com.azure.resourcemanager.postgresqlflexibleserver.models.ReplicationRole; +import com.azure.resourcemanager.postgresqlflexibleserver.models.Server; +import com.azure.resourcemanager.postgresqlflexibleserver.models.ServerVersion; +import com.azure.resourcemanager.postgresqlflexibleserver.models.Sku; +import com.azure.resourcemanager.postgresqlflexibleserver.models.SkuTier; +import com.azure.resourcemanager.postgresqlflexibleserver.models.Storage; +import com.azure.resourcemanager.postgresqlflexibleserver.models.UserAssignedIdentity; +import com.azure.resourcemanager.resources.ResourceManager; +import io.netty.util.internal.StringUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Random; +import java.util.UUID; + +public class PostgreSqlManagerTests extends TestBase { + private static final Random RANDOM = new Random(); + private static final Region REGION = Region.US_EAST; + private String resourceGroupName = "rg" + randomPadding(); + private PostgreSqlManager postgreSqlManager; + private ResourceManager resourceManager; + private boolean testEnv; + + @Override + public void beforeTest() { + final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); + + postgreSqlManager = PostgreSqlManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile); + + resourceManager = ResourceManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile) + .withDefaultSubscription(); + + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); + if (testEnv) { + resourceGroupName = testResourceGroup; + } else { + resourceManager.resourceGroups() + .define(resourceGroupName) + .withRegion(REGION) + .create(); + } + } + + @Override + protected void afterTest() { + if (!testEnv) { + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); + } + } + + @Test + @DoNotRecord(skipInPlayback = true) + public void testCreateServer() { + Server server = null; + String randomPadding = randomPadding(); + try { + String serverName = "postgresql" + randomPadding; + String adminName = "sqlAdmin" + randomPadding; + String adminPwd = "sqlAdmin" + + UUID.randomUUID().toString().replace("-", StringUtil.EMPTY_STRING).substring(0, 8); + // @embedmeStart + server = postgreSqlManager.servers() + .define(serverName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .withAdministratorLogin(adminName) + .withAdministratorLoginPassword(adminPwd) + .withSku(new Sku().withName("Standard_D2ds_v4").withTier(SkuTier.GENERAL_PURPOSE)) + .withAuthConfig(new AuthConfig() + .withActiveDirectoryAuth(ActiveDirectoryAuthEnum.DISABLED) + .withPasswordAuth(PasswordAuthEnum.ENABLED)) + .withIdentity(new UserAssignedIdentity().withType(IdentityType.NONE)) + .withDataEncryption(new DataEncryption().withType(ArmServerKeyType.SYSTEM_MANAGED)) + .withVersion(ServerVersion.ONE_FOUR) + .withAvailabilityZone("2") + .withStorage(new Storage().withStorageSizeGB(128)) + .withBackup(new Backup() + .withGeoRedundantBackup(GeoRedundantBackupEnum.DISABLED) + .withBackupRetentionDays(7)) + .withHighAvailability(new HighAvailability().withMode(HighAvailabilityMode.DISABLED)) + .withReplicationRole(ReplicationRole.PRIMARY) + .withReplicaCapacity(5) + .create(); + // @embedmeEnd + server.refresh(); + Assertions.assertEquals(server.name(), serverName); + Assertions.assertEquals(server.name(), postgreSqlManager.servers().getById(server.id()).name()); + Assertions.assertTrue(postgreSqlManager.servers().list().stream().count() > 0); + } finally { + if (server != null) { + postgreSqlManager.servers().deleteById(server.id()); + } + } + } + + private static String randomPadding() { + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); + } +} diff --git a/sdk/postgresqlflexibleserver/test-resources.bicep b/sdk/postgresqlflexibleserver/test-resources.bicep new file mode 100644 index 0000000000000..2250946806c02 --- /dev/null +++ b/sdk/postgresqlflexibleserver/test-resources.bicep @@ -0,0 +1,27 @@ +@description('The tenant id to which the application and resources belong.') +param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47' + +@description('The client id of the service principal used to run tests.') +param testApplicationId string + +@description('This is the object id of the service principal used to run tests.') +param testApplicationOid string + +@description('The application client secret used to run tests.') +param testApplicationSecret string + +var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' + +resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = { + name: guid('contributorRoleId${resourceGroup().name}') + properties: { + roleDefinitionId: contributorRoleId + principalId: testApplicationOid + } +} + +output AZURE_TENANT_ID string = tenantId +output AZURE_CLIENT_ID string = testApplicationId +output AZURE_CLIENT_SECRET string = testApplicationSecret +output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId +output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name diff --git a/sdk/postgresqlflexibleserver/tests.mgmt.yml b/sdk/postgresqlflexibleserver/tests.mgmt.yml new file mode 100644 index 0000000000000..ec062f978a78c --- /dev/null +++ b/sdk/postgresqlflexibleserver/tests.mgmt.yml @@ -0,0 +1,16 @@ +trigger: none + +pr: none + +stages: + - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml + parameters: + ServiceDirectory: postgresqlflexibleserver + Artifacts: + - name: azure-resourcemanager-postgresqlflexibleserver + groupId: com.azure.resourcemanager + safeName: azureresourcemanagerpostgresqlflexibleserver + Clouds: 'Public' + # Only run tests on Windows to save cost. + MatrixFilters: + - pool=.*(win).*