|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.storageactions; |
| 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.TestProxyTestBase; |
| 13 | +import com.azure.core.test.annotation.LiveOnly; |
| 14 | +import com.azure.core.util.Configuration; |
| 15 | +import com.azure.core.util.CoreUtils; |
| 16 | +import com.azure.identity.AzurePowerShellCredentialBuilder; |
| 17 | +import com.azure.resourcemanager.resources.ResourceManager; |
| 18 | +import com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy; |
| 19 | +import com.azure.resourcemanager.storageactions.models.IfCondition; |
| 20 | +import com.azure.resourcemanager.storageactions.models.ManagedServiceIdentity; |
| 21 | +import com.azure.resourcemanager.storageactions.models.ManagedServiceIdentityType; |
| 22 | +import com.azure.resourcemanager.storageactions.models.OnFailure; |
| 23 | +import com.azure.resourcemanager.storageactions.models.OnSuccess; |
| 24 | +import com.azure.resourcemanager.storageactions.models.StorageTask; |
| 25 | +import com.azure.resourcemanager.storageactions.models.StorageTaskAction; |
| 26 | +import com.azure.resourcemanager.storageactions.models.StorageTaskOperation; |
| 27 | +import com.azure.resourcemanager.storageactions.models.StorageTaskOperationName; |
| 28 | +import com.azure.resourcemanager.storageactions.models.StorageTaskProperties; |
| 29 | +import org.junit.jupiter.api.Assertions; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.LinkedHashMap; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Random; |
| 36 | + |
| 37 | +public class StorageActionsManagerTests extends TestProxyTestBase { |
| 38 | + private static final Random RANDOM = new Random(); |
| 39 | + private static final Region REGION = Region.US_WEST3; |
| 40 | + private String resourceGroupName = "rg" + randomPadding(); |
| 41 | + private StorageActionsManager storageActionsManager = null; |
| 42 | + private ResourceManager resourceManager; |
| 43 | + private boolean testEnv; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeTest() { |
| 47 | + final TokenCredential credential = new AzurePowerShellCredentialBuilder().build(); |
| 48 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 49 | + |
| 50 | + resourceManager = ResourceManager.configure() |
| 51 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 52 | + .authenticate(credential, profile) |
| 53 | + .withDefaultSubscription(); |
| 54 | + |
| 55 | + storageActionsManager = StorageActionsManager.configure() |
| 56 | + .withPolicy(new ProviderRegistrationPolicy(resourceManager)) |
| 57 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 58 | + .authenticate(credential, profile); |
| 59 | + |
| 60 | + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI |
| 61 | + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); |
| 62 | + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); |
| 63 | + if (testEnv) { |
| 64 | + resourceGroupName = testResourceGroup; |
| 65 | + } else { |
| 66 | + resourceManager.resourceGroups().define(resourceGroupName).withRegion(REGION).create(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void afterTest() { |
| 72 | + if (!testEnv) { |
| 73 | + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @LiveOnly |
| 79 | + public void testCreateStorageTask() { |
| 80 | + StorageTask storageTask = null; |
| 81 | + try { |
| 82 | + String taskName = "task" + randomPadding(); |
| 83 | + // @embedmeStart |
| 84 | + Map<String, String> operationMap = new LinkedHashMap<>(); |
| 85 | + operationMap.put("tier", "Hot"); |
| 86 | + storageTask = storageActionsManager.storageTasks() |
| 87 | + .define(taskName) |
| 88 | + .withRegion(REGION) |
| 89 | + .withExistingResourceGroup(resourceGroupName) |
| 90 | + .withIdentity(new ManagedServiceIdentity().withType(ManagedServiceIdentityType.SYSTEM_ASSIGNED)) |
| 91 | + .withProperties(new StorageTaskProperties() |
| 92 | + .withAction(new StorageTaskAction() |
| 93 | + .withIfProperty(new IfCondition().withCondition("[[[equals(AccessTier, 'Cool')]]") |
| 94 | + .withOperations(Arrays |
| 95 | + .asList(new StorageTaskOperation().withName(StorageTaskOperationName.SET_BLOB_TIER) |
| 96 | + .withParameters(operationMap) |
| 97 | + .withOnSuccess(OnSuccess.CONTINUE) |
| 98 | + .withOnFailure(OnFailure.BREAK))))) |
| 99 | + .withDescription("Storage task") |
| 100 | + .withEnabled(true)) |
| 101 | + .create(); |
| 102 | + // @embedmeEnd |
| 103 | + storageTask.refresh(); |
| 104 | + Assertions.assertEquals(taskName, storageTask.name()); |
| 105 | + Assertions.assertEquals(taskName, storageActionsManager.storageTasks().getById(storageTask.id()).name()); |
| 106 | + Assertions.assertTrue(storageActionsManager.storageTasks() |
| 107 | + .listByResourceGroup(resourceGroupName) |
| 108 | + .stream() |
| 109 | + .findAny() |
| 110 | + .isPresent()); |
| 111 | + } finally { |
| 112 | + if (storageTask != null) { |
| 113 | + storageActionsManager.storageTasks().deleteById(storageTask.id()); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static String randomPadding() { |
| 119 | + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); |
| 120 | + } |
| 121 | +} |
0 commit comments