PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/compute/mgmt/src/test/java/com/azure/management/compute/VirtualMachineEMSILMSIOperationsTests.java

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 532 lines | 400 code | 56 blank | 76 comment | 26 complexity | 04e7e5eb31378ae666f7e4f345c436a2 MD5 | raw file
Possible License(s): MIT
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.management.compute;
  4. import com.azure.core.http.HttpPipeline;
  5. import com.azure.core.http.rest.PagedIterable;
  6. import com.azure.management.compute.implementation.ComputeManager;
  7. import com.azure.management.graphrbac.BuiltInRole;
  8. import com.azure.management.graphrbac.RoleAssignment;
  9. import com.azure.management.msi.Identity;
  10. import com.azure.management.msi.implementation.MSIManager;
  11. import com.azure.management.network.Network;
  12. import com.azure.management.network.implementation.NetworkManager;
  13. import com.azure.management.resources.ResourceGroup;
  14. import com.azure.management.resources.core.TestBase;
  15. import com.azure.management.resources.fluentcore.arm.Region;
  16. import com.azure.management.resources.fluentcore.model.Creatable;
  17. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  18. import com.azure.management.resources.implementation.ResourceManager;
  19. import java.io.IOException;
  20. import java.util.Iterator;
  21. import java.util.Set;
  22. import org.junit.jupiter.api.Assertions;
  23. import org.junit.jupiter.api.Test;
  24. import reactor.core.publisher.Mono;
  25. public class VirtualMachineEMSILMSIOperationsTests extends TestBase {
  26. private String rgName = "";
  27. private Region region = Region.fromName("West Central US");
  28. private final String vmName = "javavm";
  29. private ComputeManager computeManager;
  30. private MSIManager msiManager;
  31. private ResourceManager resourceManager;
  32. private NetworkManager networkManager;
  33. @Override
  34. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile)
  35. throws IOException {
  36. this.msiManager = MSIManager.authenticate(httpPipeline, profile, sdkContext);
  37. this.resourceManager = msiManager.resourceManager();
  38. this.computeManager = ComputeManager.authenticate(httpPipeline, profile, sdkContext);
  39. this.networkManager = NetworkManager.authenticate(httpPipeline, profile, sdkContext);
  40. }
  41. @Override
  42. protected void cleanUpResources() {
  43. this.resourceManager.resourceGroups().deleteByName(rgName);
  44. }
  45. @Test
  46. public void canCreateUpdateVirtualMachineWithEMSI() {
  47. // this.resourceManager.resourceGroups().beginDeleteByName("41522c6e938c4f6");
  48. rgName = generateRandomResourceName("java-emsi-c-rg", 15);
  49. String identityName1 = generateRandomResourceName("msi-id", 15);
  50. String identityName2 = generateRandomResourceName("msi-id", 15);
  51. String networkName = generateRandomResourceName("nw", 10);
  52. // Prepare a definition for yet-to-be-created resource group
  53. //
  54. Creatable<ResourceGroup> creatableRG = resourceManager.resourceGroups().define(rgName).withRegion(region);
  55. // Create a virtual network residing in the above RG
  56. //
  57. final Network network =
  58. networkManager.networks().define(networkName).withRegion(region).withNewResourceGroup(creatableRG).create();
  59. // Create an "User Assigned (External) MSI" residing in the above RG and assign reader access to the virtual
  60. // network
  61. //
  62. final Identity createdIdentity =
  63. msiManager
  64. .identities()
  65. .define(identityName1)
  66. .withRegion(region)
  67. .withNewResourceGroup(creatableRG)
  68. .withAccessTo(network, BuiltInRole.READER)
  69. .create();
  70. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  71. // resource group
  72. // it resides
  73. //
  74. Creatable<Identity> creatableIdentity =
  75. msiManager
  76. .identities()
  77. .define(identityName2)
  78. .withRegion(region)
  79. .withNewResourceGroup(creatableRG)
  80. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  81. // Create a virtual machine and associate it with existing and yet-t-be-created identities
  82. //
  83. VirtualMachine virtualMachine =
  84. computeManager
  85. .virtualMachines()
  86. .define(vmName)
  87. .withRegion(region)
  88. .withNewResourceGroup(rgName)
  89. .withNewPrimaryNetwork("10.0.0.0/28")
  90. .withPrimaryPrivateIPAddressDynamic()
  91. .withoutPrimaryPublicIPAddress()
  92. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  93. .withRootUsername("Foo12")
  94. .withRootPassword("abc!@#F0orL")
  95. .withExistingUserAssignedManagedServiceIdentity(createdIdentity)
  96. .withNewUserAssignedManagedServiceIdentity(creatableIdentity)
  97. .create();
  98. Assertions.assertNotNull(virtualMachine);
  99. Assertions.assertNotNull(virtualMachine.inner());
  100. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  101. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId()); // No Local MSI enabled
  102. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId()); // No Local MSI enabled
  103. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  104. //
  105. Set<String> emsiIds = virtualMachine.userAssignedManagedServiceIdentityIds();
  106. Assertions.assertNotNull(emsiIds);
  107. Assertions.assertEquals(2, emsiIds.size());
  108. // Ensure the "User Assigned (External) MSI"s matches with the those provided as part of VM create
  109. //
  110. Identity implicitlyCreatedIdentity = null;
  111. for (String emsiId : emsiIds) {
  112. Identity identity = msiManager.identities().getById(emsiId);
  113. Assertions.assertNotNull(identity);
  114. Assertions
  115. .assertTrue(
  116. identity.name().equalsIgnoreCase(identityName1) || identity.name().equalsIgnoreCase(identityName2));
  117. Assertions.assertNotNull(identity.principalId());
  118. if (identity.name().equalsIgnoreCase(identityName2)) {
  119. implicitlyCreatedIdentity = identity;
  120. }
  121. }
  122. Assertions.assertNotNull(implicitlyCreatedIdentity);
  123. // Ensure expected role assignment exists for explicitly created EMSI
  124. //
  125. PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
  126. this.msiManager.graphRbacManager().roleAssignments().listByScope(network.id());
  127. boolean found = false;
  128. for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
  129. if (roleAssignment.principalId() != null
  130. && roleAssignment.principalId().equalsIgnoreCase(createdIdentity.principalId())) {
  131. found = true;
  132. break;
  133. }
  134. }
  135. Assertions
  136. .assertTrue(
  137. found,
  138. "Expected role assignment not found for the virtual network for identity" + createdIdentity.name());
  139. RoleAssignment assignment =
  140. lookupRoleAssignmentUsingScopeAndRoleAsync(network.id(), BuiltInRole.READER, createdIdentity.principalId())
  141. .block();
  142. Assertions
  143. .assertNotNull(
  144. assignment, "Expected role assignment with ROLE not found for the virtual network for identity");
  145. // Ensure expected role assignment exists for explicitly created EMSI
  146. //
  147. ResourceGroup resourceGroup = resourceManager.resourceGroups().getByName(virtualMachine.resourceGroupName());
  148. Assertions.assertNotNull(resourceGroup);
  149. PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
  150. this.msiManager.graphRbacManager().roleAssignments().listByScope(resourceGroup.id());
  151. found = false;
  152. for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
  153. if (roleAssignment.principalId() != null
  154. && roleAssignment.principalId().equalsIgnoreCase(implicitlyCreatedIdentity.principalId())) {
  155. found = true;
  156. break;
  157. }
  158. }
  159. Assertions
  160. .assertTrue(
  161. found,
  162. "Expected role assignment not found for the resource group for identity"
  163. + implicitlyCreatedIdentity.name());
  164. assignment =
  165. lookupRoleAssignmentUsingScopeAndRoleAsync(
  166. resourceGroup.id(), BuiltInRole.CONTRIBUTOR, implicitlyCreatedIdentity.principalId())
  167. .block();
  168. Assertions
  169. .assertNotNull(
  170. assignment, "Expected role assignment with ROLE not found for the resource group for identity");
  171. emsiIds = virtualMachine.userAssignedManagedServiceIdentityIds();
  172. Iterator<String> itr = emsiIds.iterator();
  173. // Remove both (all) identities
  174. virtualMachine
  175. .update()
  176. .withoutUserAssignedManagedServiceIdentity(itr.next())
  177. .withoutUserAssignedManagedServiceIdentity(itr.next())
  178. .apply();
  179. //
  180. Assertions.assertEquals(0, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  181. if (virtualMachine.managedServiceIdentityType() != null) {
  182. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  183. }
  184. // fetch vm again and validate
  185. virtualMachine.refresh();
  186. //
  187. Assertions.assertEquals(0, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  188. if (virtualMachine.managedServiceIdentityType() != null) {
  189. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  190. }
  191. //
  192. itr = emsiIds.iterator();
  193. Identity identity1 = msiManager.identities().getById(itr.next());
  194. Identity identity2 = msiManager.identities().getById(itr.next());
  195. //
  196. // Update VM by enabling System-MSI and add two identities
  197. virtualMachine
  198. .update()
  199. .withSystemAssignedManagedServiceIdentity()
  200. .withExistingUserAssignedManagedServiceIdentity(identity1)
  201. .withExistingUserAssignedManagedServiceIdentity(identity2)
  202. .apply();
  203. Assertions.assertNotNull(virtualMachine.userAssignedManagedServiceIdentityIds());
  204. Assertions.assertEquals(2, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  205. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  206. Assertions
  207. .assertTrue(
  208. virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  209. //
  210. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  211. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  212. //
  213. virtualMachine.refresh();
  214. Assertions.assertNotNull(virtualMachine.userAssignedManagedServiceIdentityIds());
  215. Assertions.assertEquals(2, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  216. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  217. Assertions
  218. .assertTrue(
  219. virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  220. //
  221. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  222. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  223. //
  224. itr = emsiIds.iterator();
  225. // Remove identities one by one (first one)
  226. virtualMachine.update().withoutUserAssignedManagedServiceIdentity(itr.next()).apply();
  227. //
  228. Assertions.assertNotNull(virtualMachine.userAssignedManagedServiceIdentityIds());
  229. Assertions.assertEquals(1, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  230. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  231. Assertions
  232. .assertTrue(
  233. virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  234. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  235. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  236. // Remove identities one by one (second one)
  237. virtualMachine.update().withoutUserAssignedManagedServiceIdentity(itr.next()).apply();
  238. //
  239. Assertions.assertEquals(0, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  240. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  241. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED));
  242. //
  243. }
  244. @Test
  245. public void canCreateVirtualMachineWithLMSIAndEMSI() {
  246. rgName = generateRandomResourceName("java-emsi-c-rg", 15);
  247. String identityName1 = generateRandomResourceName("msi-id", 15);
  248. String networkName = generateRandomResourceName("nw", 10);
  249. // Create a resource group
  250. //
  251. ResourceGroup resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(region).create();
  252. // Create a virtual network
  253. //
  254. Network network =
  255. networkManager
  256. .networks()
  257. .define(networkName)
  258. .withRegion(region)
  259. .withExistingResourceGroup(resourceGroup)
  260. .create();
  261. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  262. // resource group
  263. // it resides
  264. //
  265. Creatable<Identity> creatableIdentity =
  266. msiManager
  267. .identities()
  268. .define(identityName1)
  269. .withRegion(region)
  270. .withExistingResourceGroup(resourceGroup)
  271. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  272. // Create a virtual machine and associate it with existing and yet-to-be-created identities
  273. //
  274. VirtualMachine virtualMachine =
  275. computeManager
  276. .virtualMachines()
  277. .define(vmName)
  278. .withRegion(region)
  279. .withNewResourceGroup(rgName)
  280. .withNewPrimaryNetwork("10.0.0.0/28")
  281. .withPrimaryPrivateIPAddressDynamic()
  282. .withoutPrimaryPublicIPAddress()
  283. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  284. .withRootUsername("Foo12")
  285. .withRootPassword("abc!@#F0orL")
  286. .withSystemAssignedManagedServiceIdentity()
  287. .withSystemAssignedIdentityBasedAccessTo(network.id(), BuiltInRole.CONTRIBUTOR)
  288. .withNewUserAssignedManagedServiceIdentity(creatableIdentity)
  289. .create();
  290. Assertions.assertNotNull(virtualMachine);
  291. Assertions.assertNotNull(virtualMachine.inner());
  292. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  293. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  294. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  295. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  296. //
  297. Set<String> emsiIds = virtualMachine.userAssignedManagedServiceIdentityIds();
  298. Assertions.assertNotNull(emsiIds);
  299. Assertions.assertEquals(1, emsiIds.size());
  300. Identity identity = msiManager.identities().getById(emsiIds.iterator().next());
  301. Assertions.assertNotNull(identity);
  302. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName1));
  303. // Ensure expected role assignment exists for LMSI
  304. //
  305. PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
  306. this.msiManager.graphRbacManager().roleAssignments().listByScope(network.id());
  307. boolean found = false;
  308. for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
  309. if (roleAssignment.principalId() != null
  310. && roleAssignment
  311. .principalId()
  312. .equalsIgnoreCase(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())) {
  313. found = true;
  314. break;
  315. }
  316. }
  317. Assertions
  318. .assertTrue(
  319. found,
  320. "Expected role assignment not found for the virtual network for local identity"
  321. + virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  322. RoleAssignment assignment =
  323. lookupRoleAssignmentUsingScopeAndRoleAsync(
  324. network.id(),
  325. BuiltInRole.CONTRIBUTOR,
  326. virtualMachine.systemAssignedManagedServiceIdentityPrincipalId())
  327. .block();
  328. Assertions
  329. .assertNotNull(
  330. assignment,
  331. "Expected role assignment with ROLE not found for the virtual network for system assigned identity");
  332. // Ensure expected role assignment exists for EMSI
  333. //
  334. ResourceGroup resourceGroup1 = resourceManager.resourceGroups().getByName(virtualMachine.resourceGroupName());
  335. PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
  336. this.msiManager.graphRbacManager().roleAssignments().listByScope(resourceGroup1.id());
  337. found = false;
  338. for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
  339. if (roleAssignment.principalId() != null
  340. && roleAssignment.principalId().equalsIgnoreCase(identity.principalId())) {
  341. found = true;
  342. break;
  343. }
  344. }
  345. Assertions
  346. .assertTrue(
  347. found, "Expected role assignment not found for the resource group for identity" + identity.name());
  348. assignment =
  349. lookupRoleAssignmentUsingScopeAndRoleAsync(
  350. resourceGroup1.id(), BuiltInRole.CONTRIBUTOR, identity.principalId())
  351. .block();
  352. Assertions
  353. .assertNotNull(
  354. assignment,
  355. "Expected role assignment with ROLE not found for the resource group for system assigned identity");
  356. }
  357. @Test
  358. public void canUpdateVirtualMachineWithEMSIAndLMSI() throws Exception {
  359. rgName = generateRandomResourceName("java-emsi-c-rg", 15);
  360. String identityName1 = generateRandomResourceName("msi-id-1", 15);
  361. String identityName2 = generateRandomResourceName("msi-id-2", 15);
  362. // Create a virtual machine with no EMSI & LMSI
  363. //
  364. VirtualMachine virtualMachine =
  365. computeManager
  366. .virtualMachines()
  367. .define(vmName)
  368. .withRegion(region)
  369. .withNewResourceGroup(rgName)
  370. .withNewPrimaryNetwork("10.0.0.0/28")
  371. .withPrimaryPrivateIPAddressDynamic()
  372. .withoutPrimaryPublicIPAddress()
  373. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  374. .withRootUsername("Foo12")
  375. .withRootPassword("abc!@#F0orL")
  376. .create();
  377. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  378. // resource group
  379. // it resides
  380. //
  381. Creatable<Identity> creatableIdentity =
  382. msiManager
  383. .identities()
  384. .define(identityName1)
  385. .withRegion(region)
  386. .withExistingResourceGroup(virtualMachine.resourceGroupName())
  387. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  388. // Update virtual machine so that it depends on the EMSI
  389. //
  390. virtualMachine = virtualMachine.update().withNewUserAssignedManagedServiceIdentity(creatableIdentity).apply();
  391. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  392. //
  393. Set<String> emsiIds = virtualMachine.userAssignedManagedServiceIdentityIds();
  394. Assertions.assertNotNull(emsiIds);
  395. Assertions.assertEquals(1, emsiIds.size());
  396. Identity identity = msiManager.identities().getById(emsiIds.iterator().next());
  397. Assertions.assertNotNull(identity);
  398. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName1));
  399. // Creates an EMSI
  400. //
  401. Identity createdIdentity =
  402. msiManager
  403. .identities()
  404. .define(identityName2)
  405. .withRegion(region)
  406. .withExistingResourceGroup(virtualMachine.resourceGroupName())
  407. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR)
  408. .create();
  409. // Update the virtual machine by removing the an EMSI and adding existing EMSI
  410. //
  411. virtualMachine =
  412. virtualMachine
  413. .update()
  414. .withoutUserAssignedManagedServiceIdentity(identity.id())
  415. .withExistingUserAssignedManagedServiceIdentity(createdIdentity)
  416. .apply();
  417. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  418. //
  419. emsiIds = virtualMachine.userAssignedManagedServiceIdentityIds();
  420. Assertions.assertNotNull(emsiIds);
  421. Assertions.assertEquals(1, emsiIds.size());
  422. identity = msiManager.identities().getById(emsiIds.iterator().next());
  423. Assertions.assertNotNull(identity);
  424. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName2));
  425. // Update the virtual machine by enabling "LMSI"
  426. virtualMachine.update().withSystemAssignedManagedServiceIdentity().apply();
  427. //
  428. Assertions.assertNotNull(virtualMachine);
  429. Assertions.assertNotNull(virtualMachine.inner());
  430. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  431. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  432. Assertions
  433. .assertTrue(
  434. virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  435. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  436. Assertions.assertNotNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  437. Assertions.assertEquals(1, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  438. //
  439. virtualMachine.update().withoutSystemAssignedManagedServiceIdentity().apply();
  440. Assertions.assertTrue(virtualMachine.isManagedServiceIdentityEnabled());
  441. Assertions.assertNotNull(virtualMachine.managedServiceIdentityType());
  442. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.USER_ASSIGNED));
  443. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  444. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  445. Assertions.assertEquals(1, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  446. //
  447. virtualMachine.update().withoutUserAssignedManagedServiceIdentity(identity.id()).apply();
  448. Assertions.assertFalse(virtualMachine.isManagedServiceIdentityEnabled());
  449. if (virtualMachine.managedServiceIdentityType() != null) {
  450. Assertions.assertTrue(virtualMachine.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  451. }
  452. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityPrincipalId());
  453. Assertions.assertNull(virtualMachine.systemAssignedManagedServiceIdentityTenantId());
  454. Assertions.assertEquals(0, virtualMachine.userAssignedManagedServiceIdentityIds().size());
  455. }
  456. private Mono<RoleAssignment> lookupRoleAssignmentUsingScopeAndRoleAsync(
  457. final String scope, BuiltInRole role, final String principalId) {
  458. return this
  459. .msiManager
  460. .graphRbacManager()
  461. .roleDefinitions()
  462. .getByScopeAndRoleNameAsync(scope, role.toString())
  463. .flatMap(
  464. roleDefinition ->
  465. msiManager
  466. .graphRbacManager()
  467. .roleAssignments()
  468. .listByScopeAsync(scope)
  469. .filter(
  470. roleAssignment ->
  471. roleAssignment.roleDefinitionId().equalsIgnoreCase(roleDefinition.id())
  472. && roleAssignment.principalId().equalsIgnoreCase(principalId))
  473. .singleOrEmpty())
  474. .switchIfEmpty(Mono.defer(() -> Mono.empty()));
  475. }
  476. }