Search in sources :

Example 1 with VirtualMachineUnmanagedDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk in project azure-sdk-for-java by Azure.

the class VirtualMachineImpl method withoutUnmanagedDataDisk.

@Override
public VirtualMachineImpl withoutUnmanagedDataDisk(int lun) {
    // Its ok not to throw here, since in general 'withoutXX' can be NOP
    int idx = -1;
    for (VirtualMachineUnmanagedDataDisk dataDisk : this.unmanagedDataDisks) {
        idx++;
        if (dataDisk.lun() == lun) {
            this.unmanagedDataDisks.remove(idx);
            this.inner().storageProfile().dataDisks().remove(idx);
            break;
        }
    }
    return this;
}
Also used : VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 2 with VirtualMachineUnmanagedDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk in project azure-sdk-for-java by Azure.

the class ManageVirtualMachineWithUnmanagedDisks method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final Region region = Region.US_WEST_CENTRAL;
    final String windowsVMName = Utils.createRandomName("wVM");
    final String linuxVMName = Utils.createRandomName("lVM");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final String dataDiskName = "disk2";
    try {
        //=============================================================
        // Create a Windows virtual machine
        System.out.println("Creating a Windows VM");
        Date t1 = new Date();
        VirtualMachine windowsVM = azure.virtualMachines().define(windowsVMName).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername(userName).withAdminPassword(password).withUnmanagedDisks().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Date t2 = new Date();
        System.out.println("Created VM: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) " + windowsVM.id());
        // Print virtual machine details
        Utils.print(windowsVM);
        //=============================================================
        // Update - Tag the virtual machine
        windowsVM.update().withTag("who-rocks", "java").withTag("where", "on azure").apply();
        System.out.println("Tagged VM: " + windowsVM.id());
        //=============================================================
        // Update - Attach data disks
        windowsVM.update().withNewUnmanagedDataDisk(10).defineUnmanagedDataDisk(dataDiskName).withNewVhd(20).withCaching(CachingTypes.READ_WRITE).attach().apply();
        System.out.println("Attached a new data disk" + dataDiskName + " to VM" + windowsVM.id());
        Utils.print(windowsVM);
        //=============================================================
        // Update - detach data disk
        windowsVM.update().withoutUnmanagedDataDisk(dataDiskName).apply();
        System.out.println("Detached data disk " + dataDiskName + " from VM " + windowsVM.id());
        //=============================================================
        // Update - Resize (expand) the data disk
        // First, deallocate the virtual machine and then proceed with resize
        System.out.println("De-allocating VM: " + windowsVM.id());
        windowsVM.deallocate();
        System.out.println("De-allocated VM: " + windowsVM.id());
        VirtualMachineUnmanagedDataDisk dataDisk = windowsVM.unmanagedDataDisks().get(0);
        windowsVM.update().updateUnmanagedDataDisk(dataDisk.name()).withSizeInGB(30).parent().apply();
        System.out.println("Expanded VM " + windowsVM.id() + "'s data disk to 30GB");
        //=============================================================
        // Update - Expand the OS drive size by 10 GB
        int osDiskSizeInGb = windowsVM.osDiskSize();
        if (osDiskSizeInGb == 0) {
            // Server is not returning the OS Disk size, possible bug in server
            System.out.println("Server is not returning the OS disk size, possible bug in the server?");
            System.out.println("Assuming that the OS disk size is 256 GB");
            osDiskSizeInGb = 256;
        }
        windowsVM.update().withOSDiskSizeInGB(osDiskSizeInGb + 10).apply();
        System.out.println("Expanded VM " + windowsVM.id() + "'s OS disk to " + (osDiskSizeInGb + 10));
        //=============================================================
        // Start the virtual machine
        System.out.println("Starting VM " + windowsVM.id());
        windowsVM.start();
        System.out.println("Started VM: " + windowsVM.id() + "; state = " + windowsVM.powerState());
        //=============================================================
        // Restart the virtual machine
        System.out.println("Restarting VM: " + windowsVM.id());
        windowsVM.restart();
        System.out.println("Restarted VM: " + windowsVM.id() + "; state = " + windowsVM.powerState());
        //=============================================================
        // Stop (powerOff) the virtual machine
        System.out.println("Powering OFF VM: " + windowsVM.id());
        windowsVM.powerOff();
        System.out.println("Powered OFF VM: " + windowsVM.id() + "; state = " + windowsVM.powerState());
        // Get the network where Windows VM is hosted
        Network network = windowsVM.getPrimaryNetworkInterface().primaryIPConfiguration().getNetwork();
        //=============================================================
        // Create a Linux VM in the same virtual network
        System.out.println("Creating a Linux VM in the network");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName).withRegion(region).withExistingResourceGroup(rgName).withExistingPrimaryNetwork(network).withSubnet(// Referencing the default subnet name when no name specified at creation
        "subnet1").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM (in the same virtual network): " + linuxVM.id());
        Utils.print(linuxVM);
        //=============================================================
        // List virtual machines in the resource group
        String resourceGroupName = windowsVM.resourceGroupName();
        System.out.println("Printing list of VMs =======");
        for (VirtualMachine virtualMachine : azure.virtualMachines().listByResourceGroup(resourceGroupName)) {
            Utils.print(virtualMachine);
        }
        //=============================================================
        // Delete the virtual machine
        System.out.println("Deleting VM: " + windowsVM.id());
        azure.virtualMachines().deleteById(windowsVM.id());
        System.out.println("Deleted VM: " + windowsVM.id());
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().deleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : Network(com.microsoft.azure.management.network.Network) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) Date(java.util.Date) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 3 with VirtualMachineUnmanagedDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk in project azure-sdk-for-java by Azure.

the class UnmanagedDataDiskImpl method ensureDisksVhdUri.

protected static void ensureDisksVhdUri(List<VirtualMachineUnmanagedDataDisk> dataDisks, String namePrefix) {
    String containerUrl = null;
    for (VirtualMachineUnmanagedDataDisk dataDisk : dataDisks) {
        if (dataDisk.creationMethod() == DiskCreateOptionTypes.EMPTY && dataDisk.inner().vhd() != null) {
            int idx = dataDisk.inner().vhd().uri().lastIndexOf('/');
            containerUrl = dataDisk.inner().vhd().uri().substring(0, idx);
            break;
        }
    }
    if (containerUrl != null) {
        for (VirtualMachineUnmanagedDataDisk dataDisk : dataDisks) {
            if (dataDisk.creationMethod() == DiskCreateOptionTypes.EMPTY) {
                //New data disk requires Vhd Uri to be set
                if (dataDisk.inner().vhd() == null) {
                    dataDisk.inner().withVhd(new VirtualHardDisk());
                    dataDisk.inner().vhd().withUri(containerUrl + namePrefix + "-data-disk-" + dataDisk.lun() + "-" + UUID.randomUUID().toString() + ".vhd");
                }
            }
        }
    }
}
Also used : VirtualHardDisk(com.microsoft.azure.management.compute.VirtualHardDisk) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 4 with VirtualMachineUnmanagedDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk in project azure-sdk-for-java by Azure.

the class TestVirtualMachineDataDisk method createResource.

@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    final String vmName = "vm" + this.testId;
    VirtualMachine virtualMachine = virtualMachines.define(vmName).withRegion(Region.US_EAST).withNewResourceGroup().withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername("testuser").withAdminPassword("12NewPA$$w0rd!").withUnmanagedDisks().withNewUnmanagedDataDisk(30).defineUnmanagedDataDisk("disk2").withNewVhd(20).withCaching(CachingTypes.READ_ONLY).attach().withSize(VirtualMachineSizeTypes.STANDARD_A8).create();
    Assert.assertTrue(virtualMachine.size().equals(VirtualMachineSizeTypes.STANDARD_A8));
    Assert.assertTrue(virtualMachine.unmanagedDataDisks().size() == 2);
    VirtualMachineUnmanagedDataDisk disk2 = null;
    for (VirtualMachineUnmanagedDataDisk dataDisk : virtualMachine.unmanagedDataDisks().values()) {
        if (dataDisk.name().equalsIgnoreCase("disk2")) {
            disk2 = dataDisk;
            break;
        }
    }
    Assert.assertNotNull(disk2);
    Assert.assertTrue(disk2.cachingType() == CachingTypes.READ_ONLY);
    Assert.assertTrue(disk2.size() == 20);
    return virtualMachine;
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 5 with VirtualMachineUnmanagedDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk in project azure-sdk-for-java by Azure.

the class TestVirtualMachineDataDisk method updateResource.

@Override
public VirtualMachine updateResource(VirtualMachine virtualMachine) throws Exception {
    virtualMachine = virtualMachine.update().withoutUnmanagedDataDisk("disk2").defineUnmanagedDataDisk("disk3").withNewVhd(10).withLun(2).attach().apply();
    Assert.assertTrue(virtualMachine.unmanagedDataDisks().size() == 2);
    VirtualMachineUnmanagedDataDisk disk3 = null;
    for (VirtualMachineUnmanagedDataDisk dataDisk : virtualMachine.unmanagedDataDisks().values()) {
        if (dataDisk.name().equalsIgnoreCase("disk3")) {
            disk3 = dataDisk;
            break;
        }
    }
    Assert.assertNotNull(disk3);
    Assert.assertTrue(disk3.cachingType() == CachingTypes.READ_WRITE);
    Assert.assertTrue(disk3.size() == 10);
    return virtualMachine;
}
Also used : VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Aggregations

VirtualMachineUnmanagedDataDisk (com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)9 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)3 VirtualHardDisk (com.microsoft.azure.management.compute.VirtualHardDisk)2 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)2 ArrayList (java.util.ArrayList)2 Disk (com.microsoft.azure.management.compute.Disk)1 VirtualMachineDataDisk (com.microsoft.azure.management.compute.VirtualMachineDataDisk)1 Network (com.microsoft.azure.management.network.Network)1 Date (java.util.Date)1