PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 654 lines | 505 code | 66 blank | 83 comment | 26 complexity | b9bbc3181bcec88a6768b0574bb37bd1 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.LoadBalancer;
  12. import com.azure.management.network.Network;
  13. import com.azure.management.network.TransportProtocol;
  14. import com.azure.management.network.implementation.NetworkManager;
  15. import com.azure.management.resources.ResourceGroup;
  16. import com.azure.management.resources.core.TestBase;
  17. import com.azure.management.resources.fluentcore.arm.Region;
  18. import com.azure.management.resources.fluentcore.model.Creatable;
  19. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  20. import com.azure.management.resources.implementation.ResourceManager;
  21. import java.io.IOException;
  22. import java.util.Iterator;
  23. import java.util.Set;
  24. import org.junit.jupiter.api.Assertions;
  25. import org.junit.jupiter.api.Test;
  26. import reactor.core.publisher.Mono;
  27. public class VirtualMachineScaleSetEMSILMSIOperationsTests extends TestBase {
  28. private String rgName = "";
  29. private Region region = Region.fromName("West Central US");
  30. private final String vmssName = "javavmss";
  31. private ComputeManager computeManager;
  32. private MSIManager msiManager;
  33. private ResourceManager resourceManager;
  34. private NetworkManager networkManager;
  35. @Override
  36. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile)
  37. throws IOException {
  38. this.msiManager = MSIManager.authenticate(httpPipeline, profile, sdkContext);
  39. this.resourceManager = msiManager.resourceManager();
  40. this.computeManager = ComputeManager.authenticate(httpPipeline, profile, sdkContext);
  41. this.networkManager = NetworkManager.authenticate(httpPipeline, profile, sdkContext);
  42. }
  43. @Override
  44. protected void cleanUpResources() {
  45. this.resourceManager.resourceGroups().deleteByName(rgName);
  46. }
  47. @Test
  48. public void canCreateUpdateVirtualMachineScaleSetWithEMSI() throws Exception {
  49. rgName = generateRandomResourceName("java-ems-c-rg", 15);
  50. String identityName1 = generateRandomResourceName("msi-id", 15);
  51. String identityName2 = generateRandomResourceName("msi-id", 15);
  52. String networkName = generateRandomResourceName("nw", 10);
  53. ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(rgName).withRegion(region).create();
  54. // Create a virtual network to which we will assign "EMSI" with reader access
  55. //
  56. Network network =
  57. networkManager
  58. .networks()
  59. .define(networkName)
  60. .withRegion(region)
  61. .withExistingResourceGroup(resourceGroup)
  62. .create();
  63. // Create an "User Assigned (External) MSI" residing in the above RG and assign reader access to the virtual
  64. // network
  65. //
  66. Identity createdIdentity =
  67. msiManager
  68. .identities()
  69. .define(identityName1)
  70. .withRegion(region)
  71. .withExistingResourceGroup(resourceGroup)
  72. .withAccessTo(network, BuiltInRole.READER)
  73. .create();
  74. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  75. // resource group
  76. // it resides
  77. //
  78. Creatable<Identity> creatableIdentity =
  79. msiManager
  80. .identities()
  81. .define(identityName2)
  82. .withRegion(region)
  83. .withExistingResourceGroup(resourceGroup)
  84. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  85. // Create a virtual network for VMSS
  86. //
  87. Network vmssNetwork =
  88. this
  89. .networkManager
  90. .networks()
  91. .define("vmssvnet")
  92. .withRegion(region)
  93. .withExistingResourceGroup(resourceGroup)
  94. .withAddressSpace("10.0.0.0/28")
  95. .withSubnet("subnet1", "10.0.0.0/28")
  96. .create();
  97. // Create a Load balancer for VMSS
  98. //
  99. LoadBalancer vmssInternalLoadBalancer = createInternalLoadBalancer(region, resourceGroup, vmssNetwork, "1");
  100. VirtualMachineScaleSet virtualMachineScaleSet =
  101. this
  102. .computeManager
  103. .virtualMachineScaleSets()
  104. .define(vmssName)
  105. .withRegion(region)
  106. .withExistingResourceGroup(resourceGroup)
  107. .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0)
  108. .withExistingPrimaryNetworkSubnet(vmssNetwork, "subnet1")
  109. .withoutPrimaryInternetFacingLoadBalancer()
  110. .withExistingPrimaryInternalLoadBalancer(vmssInternalLoadBalancer)
  111. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  112. .withRootUsername("jvuser")
  113. .withRootPassword("123OData!@#123")
  114. .withExistingUserAssignedManagedServiceIdentity(createdIdentity)
  115. .withNewUserAssignedManagedServiceIdentity(creatableIdentity)
  116. .create();
  117. Assertions.assertNotNull(virtualMachineScaleSet);
  118. Assertions.assertNotNull(virtualMachineScaleSet.inner());
  119. Assertions.assertTrue(virtualMachineScaleSet.isManagedServiceIdentityEnabled());
  120. Assertions
  121. .assertNull(
  122. virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId()); // No Local MSI enabled
  123. Assertions
  124. .assertNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId()); // No Local MSI enabled
  125. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine scale set
  126. //
  127. Set<String> emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
  128. Assertions.assertNotNull(emsiIds);
  129. Assertions.assertEquals(2, emsiIds.size());
  130. // Ensure the "User Assigned (External) MSI"s matches with the those provided as part of VMSS create
  131. //
  132. Identity implicitlyCreatedIdentity = null;
  133. for (String emsiId : emsiIds) {
  134. Identity identity = msiManager.identities().getById(emsiId);
  135. Assertions.assertNotNull(identity);
  136. Assertions
  137. .assertTrue(
  138. identity.name().equalsIgnoreCase(identityName1) || identity.name().equalsIgnoreCase(identityName2));
  139. Assertions.assertNotNull(identity.principalId());
  140. if (identity.name().equalsIgnoreCase(identityName2)) {
  141. implicitlyCreatedIdentity = identity;
  142. }
  143. }
  144. Assertions.assertNotNull(implicitlyCreatedIdentity);
  145. // Ensure expected role assignment exists for explicitly created EMSI
  146. //
  147. PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
  148. this.msiManager.graphRbacManager().roleAssignments().listByScope(network.id());
  149. boolean found = false;
  150. for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
  151. if (roleAssignment.principalId() != null
  152. && roleAssignment.principalId().equalsIgnoreCase(createdIdentity.principalId())) {
  153. found = true;
  154. break;
  155. }
  156. }
  157. Assertions
  158. .assertTrue(
  159. found,
  160. "Expected role assignment not found for the virtual network for identity" + createdIdentity.name());
  161. RoleAssignment assignment =
  162. lookupRoleAssignmentUsingScopeAndRoleAsync(network.id(), BuiltInRole.READER, createdIdentity.principalId())
  163. .block();
  164. Assertions
  165. .assertNotNull(
  166. assignment, "Expected role assignment with ROLE not found for the virtual network for identity");
  167. // Ensure expected role assignment exists for explicitly created EMSI
  168. //
  169. PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
  170. this.msiManager.graphRbacManager().roleAssignments().listByScope(resourceGroup.id());
  171. found = false;
  172. for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
  173. if (roleAssignment.principalId() != null
  174. && roleAssignment.principalId().equalsIgnoreCase(implicitlyCreatedIdentity.principalId())) {
  175. found = true;
  176. break;
  177. }
  178. }
  179. Assertions
  180. .assertTrue(
  181. found,
  182. "Expected role assignment not found for the resource group for identity"
  183. + implicitlyCreatedIdentity.name());
  184. assignment =
  185. lookupRoleAssignmentUsingScopeAndRoleAsync(
  186. resourceGroup.id(), BuiltInRole.CONTRIBUTOR, implicitlyCreatedIdentity.principalId())
  187. .block();
  188. Assertions
  189. .assertNotNull(
  190. assignment, "Expected role assignment with ROLE not found for the resource group for identity");
  191. emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
  192. Iterator<String> itr = emsiIds.iterator();
  193. // Remove both (all) identities
  194. virtualMachineScaleSet
  195. .update()
  196. .withoutUserAssignedManagedServiceIdentity(itr.next())
  197. .withoutUserAssignedManagedServiceIdentity(itr.next())
  198. .apply();
  199. //
  200. Assertions.assertEquals(0, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  201. if (virtualMachineScaleSet.managedServiceIdentityType() != null) {
  202. Assertions
  203. .assertTrue(virtualMachineScaleSet.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  204. }
  205. // fetch vm again and validate
  206. virtualMachineScaleSet.refresh();
  207. //
  208. Assertions.assertEquals(0, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  209. if (virtualMachineScaleSet.managedServiceIdentityType() != null) {
  210. Assertions
  211. .assertTrue(virtualMachineScaleSet.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  212. }
  213. //
  214. //
  215. itr = emsiIds.iterator();
  216. Identity identity1 = msiManager.identities().getById(itr.next());
  217. Identity identity2 = msiManager.identities().getById(itr.next());
  218. //
  219. // Update VM by enabling System-MSI and add two identities
  220. virtualMachineScaleSet
  221. .update()
  222. .withSystemAssignedManagedServiceIdentity()
  223. .withExistingUserAssignedManagedServiceIdentity(identity1)
  224. .withExistingUserAssignedManagedServiceIdentity(identity2)
  225. .apply();
  226. Assertions.assertNotNull(virtualMachineScaleSet.userAssignedManagedServiceIdentityIds());
  227. Assertions.assertEquals(2, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  228. Assertions.assertNotNull(virtualMachineScaleSet.managedServiceIdentityType());
  229. Assertions
  230. .assertTrue(
  231. virtualMachineScaleSet
  232. .managedServiceIdentityType()
  233. .equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  234. //
  235. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  236. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  237. //
  238. virtualMachineScaleSet.refresh();
  239. Assertions.assertNotNull(virtualMachineScaleSet.userAssignedManagedServiceIdentityIds());
  240. Assertions.assertEquals(2, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  241. Assertions.assertNotNull(virtualMachineScaleSet.managedServiceIdentityType());
  242. Assertions
  243. .assertTrue(
  244. virtualMachineScaleSet
  245. .managedServiceIdentityType()
  246. .equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  247. //
  248. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  249. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  250. //
  251. itr = emsiIds.iterator();
  252. // Remove identities one by one (first one)
  253. virtualMachineScaleSet.update().withoutUserAssignedManagedServiceIdentity(itr.next()).apply();
  254. //
  255. Assertions.assertNotNull(virtualMachineScaleSet.userAssignedManagedServiceIdentityIds());
  256. Assertions.assertEquals(1, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  257. Assertions.assertNotNull(virtualMachineScaleSet.managedServiceIdentityType());
  258. Assertions
  259. .assertTrue(
  260. virtualMachineScaleSet
  261. .managedServiceIdentityType()
  262. .equals(ResourceIdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED));
  263. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  264. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  265. // Remove identities one by one (second one)
  266. virtualMachineScaleSet.update().withoutUserAssignedManagedServiceIdentity(itr.next()).apply();
  267. //
  268. Assertions.assertEquals(0, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  269. Assertions.assertNotNull(virtualMachineScaleSet.managedServiceIdentityType());
  270. Assertions
  271. .assertTrue(
  272. virtualMachineScaleSet.managedServiceIdentityType().equals(ResourceIdentityType.SYSTEM_ASSIGNED));
  273. //
  274. virtualMachineScaleSet.update().withoutSystemAssignedManagedServiceIdentity().apply();
  275. Assertions.assertEquals(0, virtualMachineScaleSet.userAssignedManagedServiceIdentityIds().size());
  276. if (virtualMachineScaleSet.managedServiceIdentityType() != null) {
  277. Assertions
  278. .assertTrue(virtualMachineScaleSet.managedServiceIdentityType().equals(ResourceIdentityType.NONE));
  279. }
  280. Assertions.assertNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  281. Assertions.assertNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  282. }
  283. @Test
  284. public void canCreateVirtualMachineScaleSetWithLMSIAndEMSI() throws Exception {
  285. rgName = generateRandomResourceName("java-emsi-c-rg", 15);
  286. String identityName1 = generateRandomResourceName("msi-id", 15);
  287. String networkName = generateRandomResourceName("nw", 10);
  288. // Create a resource group
  289. //
  290. ResourceGroup resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(region).create();
  291. // Create a virtual network to which we will assign "EMSI" with reader access
  292. //
  293. Network network =
  294. networkManager
  295. .networks()
  296. .define(networkName)
  297. .withRegion(region)
  298. .withExistingResourceGroup(resourceGroup)
  299. .create();
  300. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  301. // resource group
  302. // it resides
  303. //
  304. Creatable<Identity> creatableIdentity =
  305. msiManager
  306. .identities()
  307. .define(identityName1)
  308. .withRegion(region)
  309. .withExistingResourceGroup(resourceGroup)
  310. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  311. // Create a virtual network for VMSS
  312. //
  313. Network vmssNetwork =
  314. this
  315. .networkManager
  316. .networks()
  317. .define("vmssvnet")
  318. .withRegion(region)
  319. .withExistingResourceGroup(resourceGroup)
  320. .withAddressSpace("10.0.0.0/28")
  321. .withSubnet("subnet1", "10.0.0.0/28")
  322. .create();
  323. // Create a Load balancer for VMSS
  324. //
  325. LoadBalancer vmssInternalLoadBalancer = createInternalLoadBalancer(region, resourceGroup, vmssNetwork, "1");
  326. VirtualMachineScaleSet virtualMachineScaleSet =
  327. this
  328. .computeManager
  329. .virtualMachineScaleSets()
  330. .define(vmssName)
  331. .withRegion(region)
  332. .withExistingResourceGroup(resourceGroup)
  333. .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0)
  334. .withExistingPrimaryNetworkSubnet(vmssNetwork, "subnet1")
  335. .withoutPrimaryInternetFacingLoadBalancer()
  336. .withExistingPrimaryInternalLoadBalancer(vmssInternalLoadBalancer)
  337. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  338. .withRootUsername("jvuser")
  339. .withRootPassword("123OData!@#123")
  340. .withSystemAssignedManagedServiceIdentity()
  341. .withSystemAssignedIdentityBasedAccessTo(network.id(), BuiltInRole.CONTRIBUTOR)
  342. .withNewUserAssignedManagedServiceIdentity(creatableIdentity)
  343. .create();
  344. Assertions.assertNotNull(virtualMachineScaleSet);
  345. Assertions.assertNotNull(virtualMachineScaleSet.inner());
  346. Assertions.assertTrue(virtualMachineScaleSet.isManagedServiceIdentityEnabled());
  347. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  348. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  349. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  350. //
  351. Set<String> emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
  352. Assertions.assertNotNull(emsiIds);
  353. Assertions.assertEquals(1, emsiIds.size());
  354. Identity identity = msiManager.identities().getById(emsiIds.iterator().next());
  355. Assertions.assertNotNull(identity);
  356. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName1));
  357. // Ensure expected role assignment exists for LMSI
  358. //
  359. PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
  360. this.msiManager.graphRbacManager().roleAssignments().listByScope(network.id());
  361. boolean found = false;
  362. for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
  363. if (roleAssignment.principalId() != null
  364. && roleAssignment
  365. .principalId()
  366. .equalsIgnoreCase(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId())) {
  367. found = true;
  368. break;
  369. }
  370. }
  371. Assertions
  372. .assertTrue(
  373. found,
  374. "Expected role assignment not found for the virtual network for local identity"
  375. + virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  376. RoleAssignment assignment =
  377. lookupRoleAssignmentUsingScopeAndRoleAsync(
  378. network.id(),
  379. BuiltInRole.CONTRIBUTOR,
  380. virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId())
  381. .block();
  382. Assertions
  383. .assertNotNull(
  384. assignment,
  385. "Expected role assignment with ROLE not found for the virtual network for system assigned identity");
  386. // Ensure expected role assignment exists for EMSI
  387. //
  388. PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
  389. this
  390. .msiManager
  391. .graphRbacManager()
  392. .roleAssignments()
  393. .listByScope(
  394. resourceManager.resourceGroups().getByName(virtualMachineScaleSet.resourceGroupName()).id());
  395. found = false;
  396. for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
  397. if (roleAssignment.principalId() != null
  398. && roleAssignment.principalId().equalsIgnoreCase(identity.principalId())) {
  399. found = true;
  400. break;
  401. }
  402. }
  403. Assertions
  404. .assertTrue(
  405. found, "Expected role assignment not found for the resource group for identity" + identity.name());
  406. assignment =
  407. lookupRoleAssignmentUsingScopeAndRoleAsync(
  408. resourceGroup.id(), BuiltInRole.CONTRIBUTOR, identity.principalId())
  409. .block();
  410. Assertions
  411. .assertNotNull(
  412. assignment,
  413. "Expected role assignment with ROLE not found for the resource group for system assigned identity");
  414. }
  415. @Test
  416. public void canUpdateVirtualMachineScaleSetWithEMSIAndLMSI() throws Exception {
  417. rgName = generateRandomResourceName("java-emsi-c-rg", 15);
  418. String identityName1 = generateRandomResourceName("msi-id-1", 15);
  419. String identityName2 = generateRandomResourceName("msi-id-2", 15);
  420. // Create a resource group
  421. //
  422. ResourceGroup resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(region).create();
  423. // Create a virtual network for VMSS
  424. //
  425. Network vmssNetwork =
  426. this
  427. .networkManager
  428. .networks()
  429. .define("vmssvnet")
  430. .withRegion(region)
  431. .withExistingResourceGroup(resourceGroup)
  432. .withAddressSpace("10.0.0.0/28")
  433. .withSubnet("subnet1", "10.0.0.0/28")
  434. .create();
  435. // Create a Load balancer for VMSS
  436. //
  437. LoadBalancer vmssInternalLoadBalancer = createInternalLoadBalancer(region, resourceGroup, vmssNetwork, "1");
  438. VirtualMachineScaleSet virtualMachineScaleSet =
  439. this
  440. .computeManager
  441. .virtualMachineScaleSets()
  442. .define(vmssName)
  443. .withRegion(region)
  444. .withExistingResourceGroup(resourceGroup)
  445. .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0)
  446. .withExistingPrimaryNetworkSubnet(vmssNetwork, "subnet1")
  447. .withoutPrimaryInternetFacingLoadBalancer()
  448. .withExistingPrimaryInternalLoadBalancer(vmssInternalLoadBalancer)
  449. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  450. .withRootUsername("jvuser")
  451. .withRootPassword("123OData!@#123")
  452. .create();
  453. // Prepare a definition for yet-to-be-created "User Assigned (External) MSI" with contributor access to the
  454. // resource group
  455. // it resides
  456. //
  457. Creatable<Identity> creatableIdentity =
  458. msiManager
  459. .identities()
  460. .define(identityName1)
  461. .withRegion(region)
  462. .withExistingResourceGroup(virtualMachineScaleSet.resourceGroupName())
  463. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR);
  464. // Update virtual machine so that it depends on the EMSI
  465. //
  466. virtualMachineScaleSet =
  467. virtualMachineScaleSet.update().withNewUserAssignedManagedServiceIdentity(creatableIdentity).apply();
  468. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  469. //
  470. Set<String> emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
  471. Assertions.assertNotNull(emsiIds);
  472. Assertions.assertEquals(1, emsiIds.size());
  473. Identity identity = msiManager.identities().getById(emsiIds.iterator().next());
  474. Assertions.assertNotNull(identity);
  475. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName1));
  476. // Creates an EMSI
  477. //
  478. Identity createdIdentity =
  479. msiManager
  480. .identities()
  481. .define(identityName2)
  482. .withRegion(region)
  483. .withExistingResourceGroup(virtualMachineScaleSet.resourceGroupName())
  484. .withAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR)
  485. .create();
  486. // Update the virtual machine by removing the an EMSI and adding existing EMSI
  487. //
  488. virtualMachineScaleSet =
  489. virtualMachineScaleSet
  490. .update()
  491. .withoutUserAssignedManagedServiceIdentity(identity.id())
  492. .withExistingUserAssignedManagedServiceIdentity(createdIdentity)
  493. .apply();
  494. // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
  495. //
  496. emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
  497. Assertions.assertNotNull(emsiIds);
  498. Assertions.assertEquals(1, emsiIds.size());
  499. identity = msiManager.identities().getById(emsiIds.iterator().next());
  500. Assertions.assertNotNull(identity);
  501. Assertions.assertTrue(identity.name().equalsIgnoreCase(identityName2));
  502. // Update the virtual machine by enabling "LMSI"
  503. virtualMachineScaleSet.update().withSystemAssignedManagedServiceIdentity().apply();
  504. Assertions.assertNotNull(virtualMachineScaleSet);
  505. Assertions.assertNotNull(virtualMachineScaleSet.inner());
  506. Assertions.assertTrue(virtualMachineScaleSet.isManagedServiceIdentityEnabled());
  507. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
  508. Assertions.assertNotNull(virtualMachineScaleSet.systemAssignedManagedServiceIdentityTenantId());
  509. }
  510. private LoadBalancer createInternalLoadBalancer(
  511. Region region, ResourceGroup resourceGroup, Network network, String id) throws Exception {
  512. final String loadBalancerName = generateRandomResourceName("InternalLb" + id + "-", 18);
  513. final String privateFrontEndName = loadBalancerName + "-FE1";
  514. final String backendPoolName1 = loadBalancerName + "-BAP1";
  515. final String backendPoolName2 = loadBalancerName + "-BAP2";
  516. final String natPoolName1 = loadBalancerName + "-INP1";
  517. final String natPoolName2 = loadBalancerName + "-INP2";
  518. final String subnetName = "subnet1";
  519. LoadBalancer loadBalancer =
  520. this
  521. .networkManager
  522. .loadBalancers()
  523. .define(loadBalancerName)
  524. .withRegion(region)
  525. .withExistingResourceGroup(resourceGroup)
  526. // Add two rules that uses above backend and probe
  527. .defineLoadBalancingRule("httpRule")
  528. .withProtocol(TransportProtocol.TCP)
  529. .fromFrontend(privateFrontEndName)
  530. .fromFrontendPort(1000)
  531. .toBackend(backendPoolName1)
  532. .withProbe("httpProbe")
  533. .attach()
  534. .defineLoadBalancingRule("httpsRule")
  535. .withProtocol(TransportProtocol.TCP)
  536. .fromFrontend(privateFrontEndName)
  537. .fromFrontendPort(1001)
  538. .toBackend(backendPoolName2)
  539. .withProbe("httpsProbe")
  540. .attach()
  541. // Add two NAT pools to enable direct VM connectivity to port 44 and 45
  542. .defineInboundNatPool(natPoolName1)
  543. .withProtocol(TransportProtocol.TCP)
  544. .fromFrontend(privateFrontEndName)
  545. .fromFrontendPortRange(8000, 8099)
  546. .toBackendPort(44)
  547. .attach()
  548. .defineInboundNatPool(natPoolName2)
  549. .withProtocol(TransportProtocol.TCP)
  550. .fromFrontend(privateFrontEndName)
  551. .fromFrontendPortRange(9000, 9099)
  552. .toBackendPort(45)
  553. .attach()
  554. // Explicitly define the frontend
  555. .definePrivateFrontend(privateFrontEndName)
  556. .withExistingSubnet(network, subnetName) // Frontend with VNET means internal load-balancer
  557. .attach()
  558. // Add two probes one per rule
  559. .defineHttpProbe("httpProbe")
  560. .withRequestPath("/")
  561. .attach()
  562. .defineHttpProbe("httpsProbe")
  563. .withRequestPath("/")
  564. .attach()
  565. .create();
  566. return loadBalancer;
  567. }
  568. private Mono<RoleAssignment> lookupRoleAssignmentUsingScopeAndRoleAsync(
  569. final String scope, BuiltInRole role, final String principalId) {
  570. return this
  571. .msiManager
  572. .graphRbacManager()
  573. .roleDefinitions()
  574. .getByScopeAndRoleNameAsync(scope, role.toString())
  575. .flatMap(
  576. roleDefinition ->
  577. msiManager
  578. .graphRbacManager()
  579. .roleAssignments()
  580. .listByScopeAsync(scope)
  581. .filter(
  582. roleAssignment ->
  583. roleAssignment.roleDefinitionId().equalsIgnoreCase(roleDefinition.id())
  584. && roleAssignment.principalId().equalsIgnoreCase(principalId))
  585. .singleOrEmpty())
  586. .switchIfEmpty(Mono.defer(() -> Mono.empty()));
  587. }
  588. }