|
12 | 12 | import com.azure.core.util.CoreUtils; |
13 | 13 | import com.azure.core.util.polling.LongRunningOperationStatus; |
14 | 14 | import com.azure.core.util.polling.PollResponse; |
| 15 | +import com.azure.resourcemanager.compute.models.ApiErrorException; |
15 | 16 | import com.azure.resourcemanager.compute.models.AvailabilitySet; |
16 | 17 | import com.azure.resourcemanager.compute.models.CachingTypes; |
17 | 18 | import com.azure.resourcemanager.compute.models.DeleteOptions; |
|
25 | 26 | import com.azure.resourcemanager.compute.models.ProximityPlacementGroupType; |
26 | 27 | import com.azure.resourcemanager.compute.models.RunCommandInputParameter; |
27 | 28 | import com.azure.resourcemanager.compute.models.RunCommandResult; |
| 29 | +import com.azure.resourcemanager.compute.models.UpgradeMode; |
28 | 30 | import com.azure.resourcemanager.compute.models.VirtualMachine; |
29 | 31 | import com.azure.resourcemanager.compute.models.VirtualMachineEvictionPolicyTypes; |
30 | 32 | import com.azure.resourcemanager.compute.models.VirtualMachineInstanceView; |
31 | 33 | import com.azure.resourcemanager.compute.models.VirtualMachinePriorityTypes; |
| 34 | +import com.azure.resourcemanager.compute.models.VirtualMachineScaleSet; |
| 35 | +import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetSkuTypes; |
32 | 36 | import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes; |
33 | 37 | import com.azure.resourcemanager.compute.models.VirtualMachineUnmanagedDataDisk; |
| 38 | +import com.azure.resourcemanager.network.models.LoadBalancer; |
| 39 | +import com.azure.resourcemanager.network.models.LoadBalancerSkuType; |
34 | 40 | import com.azure.resourcemanager.network.models.Network; |
35 | 41 | import com.azure.resourcemanager.network.models.NetworkInterface; |
36 | 42 | import com.azure.resourcemanager.network.models.NetworkSecurityGroup; |
37 | 43 | import com.azure.resourcemanager.network.models.NicIpConfiguration; |
| 44 | +import com.azure.resourcemanager.network.models.PublicIPSkuType; |
38 | 45 | import com.azure.resourcemanager.network.models.PublicIpAddress; |
39 | 46 | import com.azure.resourcemanager.network.models.SecurityRuleProtocol; |
40 | 47 | import com.azure.resourcemanager.network.models.Subnet; |
@@ -1305,6 +1312,139 @@ public void canCreateVirtualMachineWithEphemeralOSDisk() { |
1305 | 1312 | Assertions.assertThrows(Exception.class, vm::deallocate); |
1306 | 1313 | } |
1307 | 1314 |
|
| 1315 | + @Test |
| 1316 | + public void canCreateVirtualMachineWithExistingScaleSet() throws Exception { |
| 1317 | + // can add regular vm to vmss |
| 1318 | + final String vmssName = generateRandomResourceName("vmss", 10); |
| 1319 | + Network network = |
| 1320 | + this |
| 1321 | + .networkManager |
| 1322 | + .networks() |
| 1323 | + .define("vmssvnet") |
| 1324 | + .withRegion(region.name()) |
| 1325 | + .withNewResourceGroup(rgName) |
| 1326 | + .withAddressSpace("10.0.0.0/28") |
| 1327 | + .withSubnet("subnet1", "10.0.0.0/28") |
| 1328 | + .create(); |
| 1329 | + ResourceGroup resourceGroup = this.resourceManager.resourceGroups().getByName(rgName); |
| 1330 | + LoadBalancer publicLoadBalancer = createHttpLoadBalancers(region, resourceGroup, "1", LoadBalancerSkuType.STANDARD, PublicIPSkuType.STANDARD, true); |
| 1331 | + VirtualMachineScaleSet flexibleVMSS = this.computeManager |
| 1332 | + .virtualMachineScaleSets() |
| 1333 | + .define(vmssName) |
| 1334 | + .withRegion(region) |
| 1335 | + .withExistingResourceGroup(rgName) |
| 1336 | + .withFlexibleOrchestrationMode() |
| 1337 | + .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_DS1_V2) |
| 1338 | + .withExistingPrimaryNetworkSubnet(network, "subnet1") |
| 1339 | + .withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer) |
| 1340 | + .withoutPrimaryInternalLoadBalancer() |
| 1341 | + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) |
| 1342 | + .withRootUsername("jvuser") |
| 1343 | + .withSsh(sshPublicKey()) |
| 1344 | + .withCapacity(1) |
| 1345 | + .withUpgradeMode(UpgradeMode.AUTOMATIC) |
| 1346 | + .create(); |
| 1347 | + |
| 1348 | + String regularVMName = generateRandomResourceName("vm", 10); |
| 1349 | + final String pipDnsLabel = generateRandomResourceName("pip", 10); |
| 1350 | + VirtualMachine regularVM = this.computeManager |
| 1351 | + .virtualMachines() |
| 1352 | + .define(regularVMName) |
| 1353 | + .withRegion(region) |
| 1354 | + .withNewResourceGroup(rgName) |
| 1355 | + .withNewPrimaryNetwork("10.0.1.0/28") |
| 1356 | + .withPrimaryPrivateIPAddressDynamic() |
| 1357 | + .withNewPrimaryPublicIPAddress(pipDnsLabel) |
| 1358 | + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS) |
| 1359 | + .withRootUsername("jvuser2") |
| 1360 | + .withSsh(sshPublicKey()) |
| 1361 | + .withExistingVirtualMachineScaleSet(flexibleVMSS) |
| 1362 | + .create(); |
| 1363 | + flexibleVMSS.refresh(); |
| 1364 | + Assertions.assertEquals(flexibleVMSS.id(), regularVM.virtualMachineScaleSetId()); |
| 1365 | + Assertions.assertEquals(2, flexibleVMSS.capacity()); |
| 1366 | + // Flexible vmss vm instance ids are all null, which means VMs in flexible vmss can only be operated by individual `VirtualMachine` APIs. |
| 1367 | + Assertions.assertTrue(flexibleVMSS.virtualMachines().list().stream().allMatch(vm -> vm.instanceId() == null)); |
| 1368 | + |
| 1369 | + regularVM.deallocate(); |
| 1370 | + Assertions.assertEquals(regularVM.powerState(), PowerState.DEALLOCATED); |
| 1371 | + |
| 1372 | + this.computeManager |
| 1373 | + .virtualMachines().deleteById(regularVM.id()); |
| 1374 | + flexibleVMSS.refresh(); |
| 1375 | + Assertions.assertEquals(flexibleVMSS.capacity(), 1); |
| 1376 | + |
| 1377 | + // can't add vm with unmanaged disk to vmss |
| 1378 | + final String storageAccountName = generateRandomResourceName("stg", 17); |
| 1379 | + Assertions.assertThrows( |
| 1380 | + ApiErrorException.class, |
| 1381 | + () -> computeManager |
| 1382 | + .virtualMachines() |
| 1383 | + .define(vmName) |
| 1384 | + .withRegion(region) |
| 1385 | + .withNewResourceGroup(rgName) |
| 1386 | + .withNewPrimaryNetwork("10.0.1.0/28") |
| 1387 | + .withPrimaryPrivateIPAddressDynamic() |
| 1388 | + .withoutPrimaryPublicIPAddress() |
| 1389 | + .withLatestLinuxImage("Canonical", "UbuntuServer", "14.04.2-LTS") |
| 1390 | + .withRootUsername("jvuser3") |
| 1391 | + .withSsh(sshPublicKey()) |
| 1392 | + .withUnmanagedDisks() /* UN-MANAGED OS and DATA DISKS */ |
| 1393 | + .withSize(VirtualMachineSizeTypes.fromString("Standard_D2a_v4")) |
| 1394 | + .withNewStorageAccount(storageAccountName) |
| 1395 | + .withOSDiskCaching(CachingTypes.READ_WRITE) |
| 1396 | + .withExistingVirtualMachineScaleSet(flexibleVMSS) |
| 1397 | + .create() |
| 1398 | + ); |
| 1399 | + |
| 1400 | + // can't add vm to `UNIFORM` vmss |
| 1401 | + final String vmssName2 = generateRandomResourceName("vmss", 10); |
| 1402 | + Network network2 = |
| 1403 | + this |
| 1404 | + .networkManager |
| 1405 | + .networks() |
| 1406 | + .define("vmssvnet2") |
| 1407 | + .withRegion(region.name()) |
| 1408 | + .withExistingResourceGroup(rgName) |
| 1409 | + .withAddressSpace("192.168.0.0/28") |
| 1410 | + .withSubnet("subnet2", "192.168.0.0/28") |
| 1411 | + .create(); |
| 1412 | + LoadBalancer publicLoadBalancer2 = createHttpLoadBalancers(region, resourceGroup, "2", LoadBalancerSkuType.STANDARD, PublicIPSkuType.STANDARD, true); |
| 1413 | + VirtualMachineScaleSet uniformVMSS = this.computeManager |
| 1414 | + .virtualMachineScaleSets() |
| 1415 | + .define(vmssName2) |
| 1416 | + .withRegion(region) |
| 1417 | + .withNewResourceGroup(rgName) |
| 1418 | + .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0) |
| 1419 | + .withExistingPrimaryNetworkSubnet(network2, "subnet2") |
| 1420 | + .withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer2) |
| 1421 | + .withoutPrimaryInternalLoadBalancer() |
| 1422 | + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) |
| 1423 | + .withRootUsername("jvuser4") |
| 1424 | + .withSsh(sshPublicKey()) |
| 1425 | + .withCapacity(1) |
| 1426 | + .create(); |
| 1427 | + Assertions.assertTrue(uniformVMSS.virtualMachines().list().stream().allMatch(v -> v.instanceId() != null)); |
| 1428 | + |
| 1429 | + String regularVMName2 = generateRandomResourceName("vm", 10); |
| 1430 | + Assertions.assertThrows( |
| 1431 | + ApiErrorException.class, |
| 1432 | + () -> this.computeManager |
| 1433 | + .virtualMachines() |
| 1434 | + .define(regularVMName2) |
| 1435 | + .withRegion(region) |
| 1436 | + .withNewResourceGroup(rgName) |
| 1437 | + .withNewPrimaryNetwork("10.0.1.0/28") |
| 1438 | + .withPrimaryPrivateIPAddressDynamic() |
| 1439 | + .withoutPrimaryPublicIPAddress() |
| 1440 | + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS) |
| 1441 | + .withRootUsername("jvuser5") |
| 1442 | + .withSsh(sshPublicKey()) |
| 1443 | + .withExistingVirtualMachineScaleSet(uniformVMSS) |
| 1444 | + .create() |
| 1445 | + ); |
| 1446 | + } |
| 1447 | + |
1308 | 1448 | private CreatablesInfo prepareCreatableVirtualMachines( |
1309 | 1449 | Region region, String vmNamePrefix, String networkNamePrefix, String publicIpNamePrefix, int vmCount) { |
1310 | 1450 |
|
|
0 commit comments