PageRenderTime 68ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 797 lines | 667 code | 73 blank | 57 comment | 26 complexity | e3852f8fced879cc3181bff1d2aaafbe 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.core.management.CloudException;
  7. import com.azure.management.network.Network;
  8. import com.azure.management.network.NetworkInterface;
  9. import com.azure.management.network.NetworkSecurityGroup;
  10. import com.azure.management.network.NicIPConfiguration;
  11. import com.azure.management.network.PublicIPAddress;
  12. import com.azure.management.network.SecurityRuleProtocol;
  13. import com.azure.management.network.Subnet;
  14. import com.azure.management.resources.ResourceGroup;
  15. import com.azure.management.resources.fluentcore.arm.Region;
  16. import com.azure.management.resources.fluentcore.arm.models.Resource;
  17. import com.azure.management.resources.fluentcore.model.Creatable;
  18. import com.azure.management.resources.fluentcore.model.CreatedResources;
  19. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  20. import com.azure.management.storage.SkuName;
  21. import com.azure.management.storage.StorageAccount;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import org.junit.jupiter.api.Assertions;
  30. import org.junit.jupiter.api.Test;
  31. public class VirtualMachineOperationsTests extends ComputeManagementTest {
  32. private String rgName = "";
  33. private String rgName2 = "";
  34. private final Region region = Region.US_EAST;
  35. private final Region regionProxPlacementGroup = Region.US_WEST_CENTRAL;
  36. private final Region regionProxPlacementGroup2 = Region.US_SOUTH_CENTRAL;
  37. private final String vmName = "javavm";
  38. private final String proxGroupName = "testproxgroup1";
  39. private final String proxGroupName2 = "testproxgroup2";
  40. private final String availabilitySetName = "availset1";
  41. private final String availabilitySetName2 = "availset2";
  42. private final ProximityPlacementGroupType proxGroupType = ProximityPlacementGroupType.STANDARD;
  43. @Override
  44. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
  45. rgName = generateRandomResourceName("javacsmrg", 15);
  46. rgName2 = generateRandomResourceName("javacsmrg2", 15);
  47. super.initializeClients(httpPipeline, profile);
  48. }
  49. @Override
  50. protected void cleanUpResources() {
  51. resourceManager.resourceGroups().beginDeleteByName(rgName);
  52. }
  53. @Test
  54. public void canCreateVirtualMachineWithNetworking() throws Exception {
  55. NetworkSecurityGroup nsg =
  56. this
  57. .networkManager
  58. .networkSecurityGroups()
  59. .define("nsg")
  60. .withRegion(region)
  61. .withNewResourceGroup(rgName)
  62. .defineRule("rule1")
  63. .allowInbound()
  64. .fromAnyAddress()
  65. .fromPort(80)
  66. .toAnyAddress()
  67. .toPort(80)
  68. .withProtocol(SecurityRuleProtocol.TCP)
  69. .attach()
  70. .create();
  71. Creatable<Network> networkDefinition =
  72. this
  73. .networkManager
  74. .networks()
  75. .define("network1")
  76. .withRegion(region)
  77. .withNewResourceGroup(rgName)
  78. .withAddressSpace("10.0.0.0/28")
  79. .defineSubnet("subnet1")
  80. .withAddressPrefix("10.0.0.0/29")
  81. .withExistingNetworkSecurityGroup(nsg)
  82. .attach();
  83. // Create
  84. VirtualMachine vm =
  85. computeManager
  86. .virtualMachines()
  87. .define(vmName)
  88. .withRegion(region)
  89. .withNewResourceGroup(rgName)
  90. .withNewPrimaryNetwork(networkDefinition)
  91. .withPrimaryPrivateIPAddressDynamic()
  92. .withoutPrimaryPublicIPAddress()
  93. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  94. .withRootUsername("Foo12")
  95. .withRootPassword("abc!@#F0orL")
  96. .create();
  97. NetworkInterface primaryNic = vm.getPrimaryNetworkInterface();
  98. Assertions.assertNotNull(primaryNic);
  99. NicIPConfiguration primaryIpConfig = primaryNic.primaryIPConfiguration();
  100. Assertions.assertNotNull(primaryIpConfig);
  101. // Fetch the NSG the way before v1.2
  102. Assertions.assertNotNull(primaryIpConfig.networkId());
  103. Network network = primaryIpConfig.getNetwork();
  104. Assertions.assertNotNull(primaryIpConfig.subnetName());
  105. Subnet subnet = network.subnets().get(primaryIpConfig.subnetName());
  106. Assertions.assertNotNull(subnet);
  107. nsg = subnet.getNetworkSecurityGroup();
  108. Assertions.assertNotNull(nsg);
  109. Assertions.assertEquals("nsg", nsg.name());
  110. Assertions.assertEquals(1, nsg.securityRules().size());
  111. // Fetch the NSG the v1.2 way
  112. nsg = primaryIpConfig.getNetworkSecurityGroup();
  113. Assertions.assertEquals("nsg", nsg.name());
  114. }
  115. @Test
  116. public void canCreateVirtualMachine() throws Exception {
  117. // Create
  118. computeManager
  119. .virtualMachines()
  120. .define(vmName)
  121. .withRegion(region)
  122. .withNewResourceGroup(rgName)
  123. .withNewPrimaryNetwork("10.0.0.0/28")
  124. .withPrimaryPrivateIPAddressDynamic()
  125. .withoutPrimaryPublicIPAddress()
  126. .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_DATACENTER)
  127. .withAdminUsername("Foo12")
  128. .withAdminPassword("abc!@#F0orL")
  129. .withUnmanagedDisks()
  130. .withSize(VirtualMachineSizeTypes.STANDARD_D3)
  131. .withOSDiskCaching(CachingTypes.READ_WRITE)
  132. .withOSDiskName("javatest")
  133. .withLicenseType("Windows_Server")
  134. .create();
  135. VirtualMachine foundVM = null;
  136. PagedIterable<VirtualMachine> vms = computeManager.virtualMachines().listByResourceGroup(rgName);
  137. for (VirtualMachine vm1 : vms) {
  138. if (vm1.name().equals(vmName)) {
  139. foundVM = vm1;
  140. break;
  141. }
  142. }
  143. Assertions.assertNotNull(foundVM);
  144. Assertions.assertEquals(region, foundVM.region());
  145. // Get
  146. foundVM = computeManager.virtualMachines().getByResourceGroup(rgName, vmName);
  147. Assertions.assertNotNull(foundVM);
  148. Assertions.assertEquals(region, foundVM.region());
  149. Assertions.assertEquals("Windows_Server", foundVM.licenseType());
  150. // Fetch instance view
  151. PowerState powerState = foundVM.powerState();
  152. Assertions.assertEquals(powerState, PowerState.RUNNING);
  153. VirtualMachineInstanceView instanceView = foundVM.instanceView();
  154. Assertions.assertNotNull(instanceView);
  155. Assertions.assertNotNull(instanceView.statuses().size() > 0);
  156. // Delete VM
  157. computeManager.virtualMachines().deleteById(foundVM.id());
  158. }
  159. @Test
  160. public void canCreateUpdatePriorityAndPrice() throws Exception {
  161. // Create
  162. computeManager
  163. .virtualMachines()
  164. .define(vmName)
  165. .withRegion(region)
  166. .withNewResourceGroup(rgName)
  167. .withNewPrimaryNetwork("10.0.0.0/28")
  168. .withPrimaryPrivateIPAddressDynamic()
  169. .withoutPrimaryPublicIPAddress()
  170. .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_DATACENTER)
  171. .withAdminUsername("Foo12")
  172. .withAdminPassword("abc!@#F0orL")
  173. .withUnmanagedDisks()
  174. .withSize(VirtualMachineSizeTypes.STANDARD_A2)
  175. .withOSDiskCaching(CachingTypes.READ_WRITE)
  176. .withOSDiskName("javatest")
  177. .withLowPriority(VirtualMachineEvictionPolicyTypes.DEALLOCATE)
  178. .withMaxPrice(1000.0)
  179. .withLicenseType("Windows_Server")
  180. .create();
  181. VirtualMachine foundVM = null;
  182. PagedIterable<VirtualMachine> vms = computeManager.virtualMachines().listByResourceGroup(rgName);
  183. for (VirtualMachine vm1 : vms) {
  184. if (vm1.name().equals(vmName)) {
  185. foundVM = vm1;
  186. break;
  187. }
  188. }
  189. Assertions.assertNotNull(foundVM);
  190. Assertions.assertEquals(region, foundVM.region());
  191. // Get
  192. foundVM = computeManager.virtualMachines().getByResourceGroup(rgName, vmName);
  193. Assertions.assertNotNull(foundVM);
  194. Assertions.assertEquals(region, foundVM.region());
  195. Assertions.assertEquals("Windows_Server", foundVM.licenseType());
  196. Assertions.assertEquals((Double) 1000.0, foundVM.billingProfile().maxPrice());
  197. Assertions.assertEquals(VirtualMachineEvictionPolicyTypes.DEALLOCATE, foundVM.evictionPolicy());
  198. // change max price
  199. try {
  200. foundVM.update().withMaxPrice(1500.0).apply();
  201. // not run to assert
  202. Assertions.assertEquals((Double) 1500.0, foundVM.billingProfile().maxPrice());
  203. Assertions.fail();
  204. } catch (CloudException e) {
  205. } // cannot change max price when vm is running
  206. foundVM.deallocate();
  207. foundVM.update().withMaxPrice(2000.0).apply();
  208. foundVM.start();
  209. Assertions.assertEquals((Double) 2000.0, foundVM.billingProfile().maxPrice());
  210. // change priority types
  211. foundVM = foundVM.update().withPriority(VirtualMachinePriorityTypes.SPOT).apply();
  212. Assertions.assertEquals(VirtualMachinePriorityTypes.SPOT, foundVM.priority());
  213. foundVM = foundVM.update().withPriority(VirtualMachinePriorityTypes.LOW).apply();
  214. Assertions.assertEquals(VirtualMachinePriorityTypes.LOW, foundVM.priority());
  215. try {
  216. foundVM.update().withPriority(VirtualMachinePriorityTypes.REGULAR).apply();
  217. // not run to assert
  218. Assertions.assertEquals(VirtualMachinePriorityTypes.REGULAR, foundVM.priority());
  219. Assertions.fail();
  220. } catch (CloudException e) {
  221. } // cannot change priority from low to regular
  222. // Delete VM
  223. computeManager.virtualMachines().deleteById(foundVM.id());
  224. }
  225. @Test
  226. public void cannotUpdateProximityPlacementGroupForVirtualMachine() throws Exception {
  227. AvailabilitySet setCreated =
  228. computeManager
  229. .availabilitySets()
  230. .define(availabilitySetName)
  231. .withRegion(regionProxPlacementGroup)
  232. .withNewResourceGroup(rgName)
  233. .withNewProximityPlacementGroup(proxGroupName, proxGroupType)
  234. .create();
  235. Assertions.assertEquals(availabilitySetName, setCreated.name());
  236. Assertions.assertNotNull(setCreated.proximityPlacementGroup());
  237. Assertions.assertEquals(proxGroupType, setCreated.proximityPlacementGroup().proximityPlacementGroupType());
  238. Assertions.assertNotNull(setCreated.proximityPlacementGroup().availabilitySetIds());
  239. Assertions.assertFalse(setCreated.proximityPlacementGroup().availabilitySetIds().isEmpty());
  240. Assertions
  241. .assertTrue(
  242. setCreated.id().equalsIgnoreCase(setCreated.proximityPlacementGroup().availabilitySetIds().get(0)));
  243. Assertions.assertEquals(setCreated.regionName(), setCreated.proximityPlacementGroup().location());
  244. AvailabilitySet setCreated2 =
  245. computeManager
  246. .availabilitySets()
  247. .define(availabilitySetName2)
  248. .withRegion(regionProxPlacementGroup2)
  249. .withNewResourceGroup(rgName2)
  250. .withNewProximityPlacementGroup(proxGroupName2, proxGroupType)
  251. .create();
  252. Assertions.assertEquals(availabilitySetName2, setCreated2.name());
  253. Assertions.assertNotNull(setCreated2.proximityPlacementGroup());
  254. Assertions.assertEquals(proxGroupType, setCreated2.proximityPlacementGroup().proximityPlacementGroupType());
  255. Assertions.assertNotNull(setCreated2.proximityPlacementGroup().availabilitySetIds());
  256. Assertions.assertFalse(setCreated2.proximityPlacementGroup().availabilitySetIds().isEmpty());
  257. Assertions
  258. .assertTrue(
  259. setCreated2.id().equalsIgnoreCase(setCreated2.proximityPlacementGroup().availabilitySetIds().get(0)));
  260. Assertions.assertEquals(setCreated2.regionName(), setCreated2.proximityPlacementGroup().location());
  261. // Create
  262. computeManager
  263. .virtualMachines()
  264. .define(vmName)
  265. .withRegion(regionProxPlacementGroup)
  266. .withExistingResourceGroup(rgName)
  267. .withNewPrimaryNetwork("10.0.0.0/28")
  268. .withPrimaryPrivateIPAddressDynamic()
  269. .withoutPrimaryPublicIPAddress()
  270. .withProximityPlacementGroup(setCreated.proximityPlacementGroup().id())
  271. .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_DATACENTER)
  272. .withAdminUsername("Foo12")
  273. .withAdminPassword("abc!@#F0orL")
  274. .withUnmanagedDisks()
  275. .withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2)
  276. .withOSDiskCaching(CachingTypes.READ_WRITE)
  277. .withOSDiskName("javatest")
  278. .withLicenseType("Windows_Server")
  279. .create();
  280. VirtualMachine foundVM = null;
  281. PagedIterable<VirtualMachine> vms = computeManager.virtualMachines().listByResourceGroup(rgName);
  282. for (VirtualMachine vm1 : vms) {
  283. if (vm1.name().equals(vmName)) {
  284. foundVM = vm1;
  285. break;
  286. }
  287. }
  288. Assertions.assertNotNull(foundVM);
  289. Assertions.assertEquals(regionProxPlacementGroup, foundVM.region());
  290. // Get
  291. foundVM = computeManager.virtualMachines().getByResourceGroup(rgName, vmName);
  292. Assertions.assertNotNull(foundVM);
  293. Assertions.assertEquals(regionProxPlacementGroup, foundVM.region());
  294. Assertions.assertEquals("Windows_Server", foundVM.licenseType());
  295. // Fetch instance view
  296. PowerState powerState = foundVM.powerState();
  297. Assertions.assertEquals(powerState, PowerState.RUNNING);
  298. VirtualMachineInstanceView instanceView = foundVM.instanceView();
  299. Assertions.assertNotNull(instanceView);
  300. Assertions.assertNotNull(instanceView.statuses().size() > 0);
  301. Assertions.assertNotNull(foundVM.proximityPlacementGroup());
  302. Assertions.assertEquals(proxGroupType, foundVM.proximityPlacementGroup().proximityPlacementGroupType());
  303. Assertions.assertNotNull(foundVM.proximityPlacementGroup().availabilitySetIds());
  304. Assertions.assertFalse(foundVM.proximityPlacementGroup().availabilitySetIds().isEmpty());
  305. Assertions
  306. .assertTrue(
  307. setCreated.id().equalsIgnoreCase(foundVM.proximityPlacementGroup().availabilitySetIds().get(0)));
  308. Assertions.assertNotNull(foundVM.proximityPlacementGroup().virtualMachineIds());
  309. Assertions.assertFalse(foundVM.proximityPlacementGroup().virtualMachineIds().isEmpty());
  310. Assertions
  311. .assertTrue(foundVM.id().equalsIgnoreCase(setCreated.proximityPlacementGroup().virtualMachineIds().get(0)));
  312. try {
  313. // Update Vm to remove it from proximity placement group
  314. VirtualMachine updatedVm =
  315. foundVM.update().withProximityPlacementGroup(setCreated2.proximityPlacementGroup().id()).apply();
  316. } catch (CloudException clEx) {
  317. Assertions
  318. .assertTrue(
  319. clEx
  320. .getMessage()
  321. .contains(
  322. "Updating proximity placement group of VM javavm is not allowed while the VM is running."
  323. + " Please stop/deallocate the VM and retry the operation."));
  324. }
  325. // Delete VM
  326. computeManager.virtualMachines().deleteById(foundVM.id());
  327. computeManager.availabilitySets().deleteById(setCreated.id());
  328. }
  329. @Test
  330. public void canCreateVirtualMachinesAndAvailabilitySetInSameProximityPlacementGroup() throws Exception {
  331. AvailabilitySet setCreated =
  332. computeManager
  333. .availabilitySets()
  334. .define(availabilitySetName)
  335. .withRegion(regionProxPlacementGroup)
  336. .withNewResourceGroup(rgName)
  337. .withNewProximityPlacementGroup(proxGroupName, proxGroupType)
  338. .create();
  339. Assertions.assertEquals(availabilitySetName, setCreated.name());
  340. Assertions.assertNotNull(setCreated.proximityPlacementGroup());
  341. Assertions.assertEquals(proxGroupType, setCreated.proximityPlacementGroup().proximityPlacementGroupType());
  342. Assertions.assertNotNull(setCreated.proximityPlacementGroup().availabilitySetIds());
  343. Assertions.assertFalse(setCreated.proximityPlacementGroup().availabilitySetIds().isEmpty());
  344. Assertions
  345. .assertTrue(
  346. setCreated.id().equalsIgnoreCase(setCreated.proximityPlacementGroup().availabilitySetIds().get(0)));
  347. Assertions.assertEquals(setCreated.regionName(), setCreated.proximityPlacementGroup().location());
  348. // Create
  349. computeManager
  350. .virtualMachines()
  351. .define(vmName)
  352. .withRegion(regionProxPlacementGroup)
  353. .withExistingResourceGroup(rgName)
  354. .withNewPrimaryNetwork("10.0.0.0/28")
  355. .withPrimaryPrivateIPAddressDynamic()
  356. .withoutPrimaryPublicIPAddress()
  357. .withProximityPlacementGroup(setCreated.proximityPlacementGroup().id())
  358. .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_DATACENTER)
  359. .withAdminUsername("Foo12")
  360. .withAdminPassword("abc!@#F0orL")
  361. .withUnmanagedDisks()
  362. .withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2)
  363. .withOSDiskCaching(CachingTypes.READ_WRITE)
  364. .withOSDiskName("javatest")
  365. .withLicenseType("Windows_Server")
  366. .create();
  367. VirtualMachine foundVM = null;
  368. PagedIterable<VirtualMachine> vms = computeManager.virtualMachines().listByResourceGroup(rgName);
  369. for (VirtualMachine vm1 : vms) {
  370. if (vm1.name().equals(vmName)) {
  371. foundVM = vm1;
  372. break;
  373. }
  374. }
  375. Assertions.assertNotNull(foundVM);
  376. Assertions.assertEquals(regionProxPlacementGroup, foundVM.region());
  377. // Get
  378. foundVM = computeManager.virtualMachines().getByResourceGroup(rgName, vmName);
  379. Assertions.assertNotNull(foundVM);
  380. Assertions.assertEquals(regionProxPlacementGroup, foundVM.region());
  381. Assertions.assertEquals("Windows_Server", foundVM.licenseType());
  382. // Fetch instance view
  383. PowerState powerState = foundVM.powerState();
  384. Assertions.assertEquals(powerState, PowerState.RUNNING);
  385. VirtualMachineInstanceView instanceView = foundVM.instanceView();
  386. Assertions.assertNotNull(instanceView);
  387. Assertions.assertNotNull(instanceView.statuses().size() > 0);
  388. Assertions.assertNotNull(foundVM.proximityPlacementGroup());
  389. Assertions.assertEquals(proxGroupType, foundVM.proximityPlacementGroup().proximityPlacementGroupType());
  390. Assertions.assertNotNull(foundVM.proximityPlacementGroup().availabilitySetIds());
  391. Assertions.assertFalse(foundVM.proximityPlacementGroup().availabilitySetIds().isEmpty());
  392. Assertions
  393. .assertTrue(
  394. setCreated.id().equalsIgnoreCase(foundVM.proximityPlacementGroup().availabilitySetIds().get(0)));
  395. Assertions.assertNotNull(foundVM.proximityPlacementGroup().virtualMachineIds());
  396. Assertions.assertFalse(foundVM.proximityPlacementGroup().virtualMachineIds().isEmpty());
  397. Assertions
  398. .assertTrue(foundVM.id().equalsIgnoreCase(setCreated.proximityPlacementGroup().virtualMachineIds().get(0)));
  399. // Update Vm to remove it from proximity placement group
  400. VirtualMachine updatedVm = foundVM.update().withoutProximityPlacementGroup().apply();
  401. Assertions.assertNotNull(updatedVm.proximityPlacementGroup());
  402. Assertions.assertEquals(proxGroupType, updatedVm.proximityPlacementGroup().proximityPlacementGroupType());
  403. Assertions.assertNotNull(updatedVm.proximityPlacementGroup().availabilitySetIds());
  404. Assertions.assertFalse(updatedVm.proximityPlacementGroup().availabilitySetIds().isEmpty());
  405. Assertions
  406. .assertTrue(
  407. setCreated.id().equalsIgnoreCase(updatedVm.proximityPlacementGroup().availabilitySetIds().get(0)));
  408. // TODO: this does not work... can not remove cvm from the placement group
  409. // Assertions.assertNull(foundVM.proximityPlacementGroup().virtualMachineIds());
  410. // Delete VM
  411. computeManager.virtualMachines().deleteById(foundVM.id());
  412. computeManager.availabilitySets().deleteById(setCreated.id());
  413. }
  414. @Test
  415. public void canCreateVirtualMachinesAndRelatedResourcesInParallel() throws Exception {
  416. String vmNamePrefix = "vmz";
  417. String publicIpNamePrefix = generateRandomResourceName("pip-", 15);
  418. String networkNamePrefix = generateRandomResourceName("vnet-", 15);
  419. int count = 5;
  420. CreatablesInfo creatablesInfo =
  421. prepareCreatableVirtualMachines(region, vmNamePrefix, networkNamePrefix, publicIpNamePrefix, count);
  422. List<Creatable<VirtualMachine>> virtualMachineCreatables = creatablesInfo.virtualMachineCreatables;
  423. List<String> networkCreatableKeys = creatablesInfo.networkCreatableKeys;
  424. List<String> publicIpCreatableKeys = creatablesInfo.publicIpCreatableKeys;
  425. CreatedResources<VirtualMachine> createdVirtualMachines =
  426. computeManager.virtualMachines().create(virtualMachineCreatables);
  427. Assertions.assertTrue(createdVirtualMachines.size() == count);
  428. Set<String> virtualMachineNames = new HashSet<>();
  429. for (int i = 0; i < count; i++) {
  430. virtualMachineNames.add(String.format("%s-%d", vmNamePrefix, i));
  431. }
  432. for (VirtualMachine virtualMachine : createdVirtualMachines.values()) {
  433. Assertions.assertTrue(virtualMachineNames.contains(virtualMachine.name()));
  434. Assertions.assertNotNull(virtualMachine.id());
  435. }
  436. Set<String> networkNames = new HashSet<>();
  437. for (int i = 0; i < count; i++) {
  438. networkNames.add(String.format("%s-%d", networkNamePrefix, i));
  439. }
  440. for (String networkCreatableKey : networkCreatableKeys) {
  441. Network createdNetwork = (Network) createdVirtualMachines.createdRelatedResource(networkCreatableKey);
  442. Assertions.assertNotNull(createdNetwork);
  443. Assertions.assertTrue(networkNames.contains(createdNetwork.name()));
  444. }
  445. Set<String> publicIPAddressNames = new HashSet<>();
  446. for (int i = 0; i < count; i++) {
  447. publicIPAddressNames.add(String.format("%s-%d", publicIpNamePrefix, i));
  448. }
  449. for (String publicIpCreatableKey : publicIpCreatableKeys) {
  450. PublicIPAddress createdPublicIPAddress =
  451. (PublicIPAddress) createdVirtualMachines.createdRelatedResource(publicIpCreatableKey);
  452. Assertions.assertNotNull(createdPublicIPAddress);
  453. Assertions.assertTrue(publicIPAddressNames.contains(createdPublicIPAddress.name()));
  454. }
  455. }
  456. @Test
  457. public void canStreamParallelCreatedVirtualMachinesAndRelatedResources() throws Exception {
  458. String vmNamePrefix = "vmz";
  459. String publicIpNamePrefix = generateRandomResourceName("pip-", 15);
  460. String networkNamePrefix = generateRandomResourceName("vnet-", 15);
  461. int count = 5;
  462. final Set<String> virtualMachineNames = new HashSet<>();
  463. for (int i = 0; i < count; i++) {
  464. virtualMachineNames.add(String.format("%s-%d", vmNamePrefix, i));
  465. }
  466. final Set<String> networkNames = new HashSet<>();
  467. for (int i = 0; i < count; i++) {
  468. networkNames.add(String.format("%s-%d", networkNamePrefix, i));
  469. }
  470. final Set<String> publicIPAddressNames = new HashSet<>();
  471. for (int i = 0; i < count; i++) {
  472. publicIPAddressNames.add(String.format("%s-%d", publicIpNamePrefix, i));
  473. }
  474. final CreatablesInfo creatablesInfo =
  475. prepareCreatableVirtualMachines(region, vmNamePrefix, networkNamePrefix, publicIpNamePrefix, count);
  476. final AtomicInteger resourceCount = new AtomicInteger(0);
  477. List<Creatable<VirtualMachine>> virtualMachineCreatables = creatablesInfo.virtualMachineCreatables;
  478. computeManager
  479. .virtualMachines()
  480. .createAsync(virtualMachineCreatables)
  481. .map(
  482. createdResource -> {
  483. if (createdResource instanceof Resource) {
  484. Resource resource = (Resource) createdResource;
  485. System.out.println("Created: " + resource.id());
  486. if (resource instanceof VirtualMachine) {
  487. VirtualMachine virtualMachine = (VirtualMachine) resource;
  488. Assertions.assertTrue(virtualMachineNames.contains(virtualMachine.name()));
  489. Assertions.assertNotNull(virtualMachine.id());
  490. } else if (resource instanceof Network) {
  491. Network network = (Network) resource;
  492. Assertions.assertTrue(networkNames.contains(network.name()));
  493. Assertions.assertNotNull(network.id());
  494. } else if (resource instanceof PublicIPAddress) {
  495. PublicIPAddress publicIPAddress = (PublicIPAddress) resource;
  496. Assertions.assertTrue(publicIPAddressNames.contains(publicIPAddress.name()));
  497. Assertions.assertNotNull(publicIPAddress.id());
  498. }
  499. }
  500. resourceCount.incrementAndGet();
  501. return createdResource;
  502. })
  503. .collectList()
  504. .block();
  505. // 1 resource group, 1 storage, 5 network, 5 publicIp, 5 nic, 5 virtual machines
  506. // Additional one for CreatableUpdatableResourceRoot.
  507. // TODO: - ans - We should not emit CreatableUpdatableResourceRoot.
  508. Assertions.assertEquals(resourceCount.get(), 23);
  509. }
  510. @Test
  511. public void canSetStorageAccountForUnmanagedDisk() {
  512. final String storageName = sdkContext.randomResourceName("st", 14);
  513. // Create a premium storage account for virtual machine data disk
  514. //
  515. StorageAccount storageAccount =
  516. storageManager
  517. .storageAccounts()
  518. .define(storageName)
  519. .withRegion(region)
  520. .withNewResourceGroup(rgName)
  521. .withSku(SkuName.PREMIUM_LRS)
  522. .create();
  523. // Creates a virtual machine with an unmanaged data disk that gets stored in the above
  524. // premium storage account
  525. //
  526. VirtualMachine virtualMachine =
  527. computeManager
  528. .virtualMachines()
  529. .define(vmName)
  530. .withRegion(region)
  531. .withExistingResourceGroup(rgName)
  532. .withNewPrimaryNetwork("10.0.0.0/28")
  533. .withPrimaryPrivateIPAddressDynamic()
  534. .withoutPrimaryPublicIPAddress()
  535. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  536. .withRootUsername("Foo12")
  537. .withRootPassword("abc!@#F0orL")
  538. .withUnmanagedDisks()
  539. .defineUnmanagedDataDisk("disk1")
  540. .withNewVhd(100)
  541. .withLun(2)
  542. .storeAt(storageAccount.name(), "diskvhds", "datadisk1vhd.vhd")
  543. .attach()
  544. .defineUnmanagedDataDisk("disk2")
  545. .withNewVhd(100)
  546. .withLun(3)
  547. .storeAt(storageAccount.name(), "diskvhds", "datadisk2vhd.vhd")
  548. .attach()
  549. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  550. .withOSDiskCaching(CachingTypes.READ_WRITE)
  551. .create();
  552. // Validate the unmanaged data disks
  553. //
  554. Map<Integer, VirtualMachineUnmanagedDataDisk> unmanagedDataDisks = virtualMachine.unmanagedDataDisks();
  555. Assertions.assertNotNull(unmanagedDataDisks);
  556. Assertions.assertEquals(2, unmanagedDataDisks.size());
  557. VirtualMachineUnmanagedDataDisk firstUnmanagedDataDisk = unmanagedDataDisks.get(2);
  558. Assertions.assertNotNull(firstUnmanagedDataDisk);
  559. VirtualMachineUnmanagedDataDisk secondUnmanagedDataDisk = unmanagedDataDisks.get(3);
  560. Assertions.assertNotNull(secondUnmanagedDataDisk);
  561. String createdVhdUri1 = firstUnmanagedDataDisk.vhdUri();
  562. String createdVhdUri2 = secondUnmanagedDataDisk.vhdUri();
  563. Assertions.assertNotNull(createdVhdUri1);
  564. Assertions.assertNotNull(createdVhdUri2);
  565. // delete the virtual machine
  566. //
  567. computeManager.virtualMachines().deleteById(virtualMachine.id());
  568. // Creates another virtual machine by attaching existing unmanaged data disk detached from the
  569. // above virtual machine.
  570. //
  571. virtualMachine =
  572. computeManager
  573. .virtualMachines()
  574. .define(vmName)
  575. .withRegion(region)
  576. .withExistingResourceGroup(rgName)
  577. .withNewPrimaryNetwork("10.0.0.0/28")
  578. .withPrimaryPrivateIPAddressDynamic()
  579. .withoutPrimaryPublicIPAddress()
  580. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  581. .withRootUsername("Foo12")
  582. .withRootPassword("abc!@#F0orL")
  583. .withUnmanagedDisks()
  584. .withExistingUnmanagedDataDisk(storageAccount.name(), "diskvhds", "datadisk1vhd.vhd")
  585. .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
  586. .create();
  587. // Gets the vm
  588. //
  589. virtualMachine = computeManager.virtualMachines().getById(virtualMachine.id());
  590. // Validate the unmanaged data disks
  591. //
  592. unmanagedDataDisks = virtualMachine.unmanagedDataDisks();
  593. Assertions.assertNotNull(unmanagedDataDisks);
  594. Assertions.assertEquals(1, unmanagedDataDisks.size());
  595. firstUnmanagedDataDisk = null;
  596. for (VirtualMachineUnmanagedDataDisk unmanagedDisk : unmanagedDataDisks.values()) {
  597. firstUnmanagedDataDisk = unmanagedDisk;
  598. break;
  599. }
  600. Assertions.assertNotNull(firstUnmanagedDataDisk.vhdUri());
  601. Assertions.assertTrue(firstUnmanagedDataDisk.vhdUri().equalsIgnoreCase(createdVhdUri1));
  602. // Update the VM by attaching another existing data disk
  603. //
  604. virtualMachine
  605. .update()
  606. .withExistingUnmanagedDataDisk(storageAccount.name(), "diskvhds", "datadisk2vhd.vhd")
  607. .apply();
  608. // Gets the vm
  609. //
  610. virtualMachine = computeManager.virtualMachines().getById(virtualMachine.id());
  611. // Validate the unmanaged data disks
  612. //
  613. unmanagedDataDisks = virtualMachine.unmanagedDataDisks();
  614. Assertions.assertNotNull(unmanagedDataDisks);
  615. Assertions.assertEquals(2, unmanagedDataDisks.size());
  616. }
  617. @Test
  618. public void canUpdateTagsOnVM() {
  619. // Create
  620. VirtualMachine virtualMachine =
  621. computeManager
  622. .virtualMachines()
  623. .define(vmName)
  624. .withRegion(region)
  625. .withNewResourceGroup(rgName)
  626. .withNewPrimaryNetwork("10.0.0.0/28")
  627. .withPrimaryPrivateIPAddressDynamic()
  628. .withoutPrimaryPublicIPAddress()
  629. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  630. .withRootUsername("firstuser")
  631. .withRootPassword("afh123RVS!")
  632. .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
  633. .create();
  634. // checking to see if withTag correctly update
  635. virtualMachine.update().withTag("test", "testValue").apply();
  636. Assertions.assertEquals("testValue", virtualMachine.inner().tags().get("test"));
  637. // checking to see if withTags correctly updates
  638. Map<String, String> testTags = new HashMap<String, String>();
  639. testTags.put("testTag", "testValue");
  640. virtualMachine.update().withTags(testTags).apply();
  641. Assertions.assertEquals(testTags, virtualMachine.inner().tags());
  642. }
  643. @Test
  644. public void canRunScriptOnVM() {
  645. // Create
  646. VirtualMachine virtualMachine =
  647. computeManager
  648. .virtualMachines()
  649. .define(vmName)
  650. .withRegion(region)
  651. .withNewResourceGroup(rgName)
  652. .withNewPrimaryNetwork("10.0.0.0/28")
  653. .withPrimaryPrivateIPAddressDynamic()
  654. .withoutPrimaryPublicIPAddress()
  655. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  656. .withRootUsername("firstuser")
  657. .withRootPassword("afh123RVS!")
  658. .create();
  659. List<String> installGit = new ArrayList<>();
  660. installGit.add("sudo apt-get update");
  661. installGit.add("sudo apt-get install -y git");
  662. RunCommandResult runResult =
  663. virtualMachine.runShellScript(installGit, new ArrayList<RunCommandInputParameter>());
  664. Assertions.assertNotNull(runResult);
  665. Assertions.assertNotNull(runResult.value());
  666. Assertions.assertTrue(runResult.value().size() > 0);
  667. }
  668. private CreatablesInfo prepareCreatableVirtualMachines(
  669. Region region, String vmNamePrefix, String networkNamePrefix, String publicIpNamePrefix, int vmCount) {
  670. Creatable<ResourceGroup> resourceGroupCreatable =
  671. resourceManager.resourceGroups().define(rgName).withRegion(region);
  672. Creatable<StorageAccount> storageAccountCreatable =
  673. storageManager
  674. .storageAccounts()
  675. .define(generateRandomResourceName("stg", 20))
  676. .withRegion(region)
  677. .withNewResourceGroup(resourceGroupCreatable);
  678. List<String> networkCreatableKeys = new ArrayList<>();
  679. List<String> publicIpCreatableKeys = new ArrayList<>();
  680. List<Creatable<VirtualMachine>> virtualMachineCreatables = new ArrayList<>();
  681. for (int i = 0; i < vmCount; i++) {
  682. Creatable<Network> networkCreatable =
  683. networkManager
  684. .networks()
  685. .define(String.format("%s-%d", networkNamePrefix, i))
  686. .withRegion(region)
  687. .withNewResourceGroup(resourceGroupCreatable)
  688. .withAddressSpace("10.0.0.0/28");
  689. networkCreatableKeys.add(networkCreatable.key());
  690. Creatable<PublicIPAddress> publicIPAddressCreatable =
  691. networkManager
  692. .publicIPAddresses()
  693. .define(String.format("%s-%d", publicIpNamePrefix, i))
  694. .withRegion(region)
  695. .withNewResourceGroup(resourceGroupCreatable);
  696. publicIpCreatableKeys.add(publicIPAddressCreatable.key());
  697. Creatable<VirtualMachine> virtualMachineCreatable =
  698. computeManager
  699. .virtualMachines()
  700. .define(String.format("%s-%d", vmNamePrefix, i))
  701. .withRegion(region)
  702. .withNewResourceGroup(resourceGroupCreatable)
  703. .withNewPrimaryNetwork(networkCreatable)
  704. .withPrimaryPrivateIPAddressDynamic()
  705. .withNewPrimaryPublicIPAddress(publicIPAddressCreatable)
  706. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  707. .withRootUsername("tirekicker")
  708. .withRootPassword("BaR@12!#")
  709. .withUnmanagedDisks()
  710. .withNewStorageAccount(storageAccountCreatable);
  711. virtualMachineCreatables.add(virtualMachineCreatable);
  712. }
  713. CreatablesInfo creatablesInfo = new CreatablesInfo();
  714. creatablesInfo.virtualMachineCreatables = virtualMachineCreatables;
  715. creatablesInfo.networkCreatableKeys = networkCreatableKeys;
  716. creatablesInfo.publicIpCreatableKeys = publicIpCreatableKeys;
  717. return creatablesInfo;
  718. }
  719. class CreatablesInfo {
  720. private List<Creatable<VirtualMachine>> virtualMachineCreatables;
  721. List<String> networkCreatableKeys;
  722. List<String> publicIpCreatableKeys;
  723. }
  724. }