|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.models.commontypes.managedidentity; |
| 5 | + |
| 6 | +import com.azure.core.management.Region; |
| 7 | +import com.azure.resourcemanager.models.commontypes.managedidentity.models.ManagedIdentityTrackedResource; |
| 8 | +import com.azure.resourcemanager.models.commontypes.managedidentity.models.ManagedIdentityTrackedResourceProperties; |
| 9 | +import com.azure.resourcemanager.models.commontypes.managedidentity.models.ManagedServiceIdentity; |
| 10 | +import com.azure.resourcemanager.models.commontypes.managedidentity.models.ManagedServiceIdentityType; |
| 11 | +import com.azure.resourcemanager.models.commontypes.managedidentity.models.UserAssignedIdentity; |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.utils.ArmUtils; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class ManagedIdentityManagerTests { |
| 20 | + private static final String USER_ASSIGNED_IDENTITIES_KEY = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1"; |
| 21 | + private final ManagedIdentityManager manager = ManagedIdentityManager.authenticate( |
| 22 | + ArmUtils.createTestHttpPipeline(), |
| 23 | + ArmUtils.getAzureProfile()); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testManagedIdentityManager() { |
| 27 | + Map<String, String> tagsMap = new HashMap<>(); |
| 28 | + tagsMap.put("tagKey1", "tagValue1"); |
| 29 | + ManagedIdentityTrackedResource resource = manager.managedIdentityTrackedResources() |
| 30 | + .define("identity") |
| 31 | + .withRegion(Region.US_EAST) |
| 32 | + .withExistingResourceGroup("test-rg") |
| 33 | + .withProperties(new ManagedIdentityTrackedResourceProperties()) |
| 34 | + .withIdentity(new ManagedServiceIdentity().withType(ManagedServiceIdentityType.SYSTEM_ASSIGNED)) |
| 35 | + .withTags(tagsMap) |
| 36 | + .create(); |
| 37 | + Assertions.assertEquals(ManagedServiceIdentityType.SYSTEM_ASSIGNED, resource.identity().type()); |
| 38 | + Assertions.assertNotNull(resource.identity().principalId()); |
| 39 | + Assertions.assertNotNull(resource.identity().tenantId()); |
| 40 | + |
| 41 | + resource = manager.managedIdentityTrackedResources().getById(resource.id()); |
| 42 | + Assertions.assertEquals(ManagedServiceIdentityType.SYSTEM_ASSIGNED, resource.identity().type()); |
| 43 | + Assertions.assertNotNull(resource.identity().principalId()); |
| 44 | + Assertions.assertNotNull(resource.identity().tenantId()); |
| 45 | + |
| 46 | + Map<String, UserAssignedIdentity> userAssignedIdentityMap = new HashMap<>(); |
| 47 | + userAssignedIdentityMap.put(USER_ASSIGNED_IDENTITIES_KEY, new UserAssignedIdentity()); |
| 48 | + resource.update().withIdentity(new ManagedServiceIdentity() |
| 49 | + .withType(ManagedServiceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED) |
| 50 | + .withUserAssignedIdentities(userAssignedIdentityMap)) |
| 51 | + .apply(); |
| 52 | + Assertions.assertEquals(ManagedServiceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED, resource.identity().type()); |
| 53 | + Assertions.assertNotNull(resource.identity().principalId()); |
| 54 | + Assertions.assertNotNull(resource.identity().tenantId()); |
| 55 | + Assertions.assertNotNull(resource.identity().userAssignedIdentities()); |
| 56 | + Assertions.assertEquals(1, resource.identity().userAssignedIdentities().size()); |
| 57 | + UserAssignedIdentity userAssignedIdentity = resource.identity() |
| 58 | + .userAssignedIdentities().get(USER_ASSIGNED_IDENTITIES_KEY); |
| 59 | + Assertions.assertNotNull(userAssignedIdentity.principalId()); |
| 60 | + Assertions.assertNotNull(userAssignedIdentity.clientId()); |
| 61 | + |
| 62 | + } |
| 63 | +} |
0 commit comments