Azure Java SDK-同时使用映像创建多个VM

时间:2019-02-04 10:42:15

标签: java azure azure-virtual-machine

我正在尝试使用Java SDK创建多个VM。 事实是,在创建机器之后,我无法连接(在Java代码创建VM之后手动添加了公共ip-这对我来说不是强制性的,不需要公共ip)。

这是我用来创建机器的代码。

List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
    List<Creatable<NetworkInterface>> creatableNetworkInterfaces = new ArrayList<>();
    List<Creatable<Disk>> creatableDisk = new ArrayList<>();

    for (int i = 0; i < vmCount; i++) {

        Creatable<NetworkInterface> networkInterfaceCreatable = azure.networkInterfaces()
                .define("testNIC-" + i)
                .withRegion(Region.EUROPE_WEST)
                .withExistingResourceGroup("TestRes2")
                .withExistingPrimaryNetwork(network)
                .withSubnet("default")
                .withPrimaryPrivateIPAddressDynamic();
        creatableNetworkInterfaces.add(networkInterfaceCreatable);

        Creatable<Disk> disksCreatable= azure.disks()
                .define(SdkContext.randomResourceName("test_dsk", 30))
                .withRegion(Region.EUROPE_WEST)
                .withNewResourceGroup("TestRes2")
                .withWindowsFromVhd("image url")
                .withSizeInGB(127)
                .withSku(DiskSkuTypes.STANDARD_LRS);
        creatableDisk.add(disksCreatable);
    }

    networkInterfaces = azure.networkInterfaces().create(creatableNetworkInterfaces);
    networkInterfacesKeys = new ArrayList(networkInterfaces.keySet());
    disks = azure.disks().create(creatableDisk);
    disksKeys = new ArrayList(disks.keySet());

    for (int i = 0; i < vmCount; i++) {
        // Create one virtual machine Creatable
        Creatable<VirtualMachine> virtualMachineCreatable = azure.virtualMachines()
                .define("TESTVM-" + i)
                .withRegion(Region.EUROPE_WEST)
                .withExistingResourceGroup("TestRes2")
                .withExistingPrimaryNetworkInterface(networkInterfaces.get(networkInterfacesKeys.get(i)))
                .withSpecializedOSDisk(disks.get(disksKeys.get(i)), OperatingSystemTypes.WINDOWS)
                .withSize(VirtualMachineSizeTypes.STANDARD_B2S);
        // add the virtual machine Creatable to the list
        creatableVirtualMachines.add(virtualMachineCreatable);
    }
    virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
    virtualMachinesKeys = new ArrayList(virtualMachines.keySet());

编辑:

当我不使用图像而是使用“ LatestImage”

.withLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")

有效。

0 个答案:

没有答案