Search in sources :

Example 6 with VirtualMachineUnmanagedDataDisk

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

the class CreateVirtualMachineUsingSpecializedDiskFromVhd 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 String linuxVMName1 = Utils.createRandomName("VM1");
    final String linuxVMName2 = Utils.createRandomName("VM2");
    final String managedOSDiskName = Utils.createRandomName("ds-os-");
    final String managedDataDiskNamePrefix = Utils.createRandomName("ds-data-");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String publicIpDnsLabel = Utils.createRandomName("pip");
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
    final String apacheInstallCommand = "bash install_apache.sh";
    List<String> apacheInstallScriptUris = new ArrayList<>();
    apacheInstallScriptUris.add(apacheInstallScript);
    try {
        //=============================================================
        // Create a Linux VM using an image from PIR (Platform Image Repository)
        System.out.println("Creating a un-managed Linux VM");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(50).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", apacheInstallScriptUris).withPublicSetting("commandToExecute", apacheInstallCommand).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with un-managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        // Gets the specialized OS and Data disk VHDs of the virtual machine
        //
        String specializedOSVhdUri = linuxVM.osUnmanagedDiskVhdUri();
        List<String> dataVhdUris = new ArrayList<>();
        for (VirtualMachineUnmanagedDataDisk dataDisk : linuxVM.unmanagedDataDisks().values()) {
            dataVhdUris.add(dataDisk.vhdUri());
        }
        //=============================================================
        // Delete the virtual machine
        System.out.println("Deleting VM: " + linuxVM.id());
        azure.virtualMachines().deleteById(linuxVM.id());
        System.out.println("Deleted the VM");
        //=============================================================
        // Create Managed disk from the specialized OS VHD
        System.out.println(String.format("Creating managed disk from the specialized OS VHD: %s ", specializedOSVhdUri));
        Disk osDisk = azure.disks().define(managedOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromVhd(specializedOSVhdUri).withSizeInGB(100).create();
        System.out.println("Created managed disk holding OS: " + osDisk.id());
        // Utils.print(osDisk); TODO
        //=============================================================
        // Create Managed disks from the Data VHDs
        List<Disk> dataDisks = new ArrayList<>();
        int i = 0;
        for (String dataVhdUri : dataVhdUris) {
            System.out.println(String.format("Creating managed disk from the Data VHD: %s ", dataVhdUri));
            Disk dataDisk = azure.disks().define(managedDataDiskNamePrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withData().fromVhd(dataVhdUri).withSizeInGB(150).withSku(DiskSkuTypes.STANDARD_LRS).create();
            dataDisks.add(dataDisk);
            System.out.println("Created managed disk holding data: " + dataDisk.id());
            // Utils.print(dataDisk); TODO
            i++;
        }
        //=============================================================
        // Create a Linux VM by attaching the disks
        System.out.println("Creating a Linux VM using specialized OS and data disks");
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(osDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(dataDisks.get(0)).withExistingDataDisk(dataDisks.get(1), 1, CachingTypes.READ_WRITE).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM2);
        List<String> dataDiskIds = new ArrayList<>();
        for (VirtualMachineDataDisk disk : linuxVM2.dataDisks().values()) {
            dataDiskIds.add(disk.id());
        }
        //=============================================================
        // Detach the data disks from the virtual machine
        System.out.println("Updating VM by detaching the data disks");
        linuxVM2.update().withoutDataDisk(0).withoutDataDisk(1).apply();
        Utils.print(linuxVM2);
        //=============================================================
        // Get the readonly SAS URI to the data disks
        System.out.println("Getting data disks SAS Uris");
        for (String diskId : dataDiskIds) {
            Disk dataDisk = azure.disks().getById(diskId);
            String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
            System.out.println(String.format("Data disk SAS Uri: %s", dataDiskSasUri));
        }
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(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 : VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 7 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(String name) {
    // Its ok not to throw here, since in general 'withoutXX' can be NOP
    int idx = -1;
    for (VirtualMachineUnmanagedDataDisk dataDisk : this.unmanagedDataDisks) {
        idx++;
        if (dataDisk.name().equalsIgnoreCase(name)) {
            this.unmanagedDataDisks.remove(idx);
            this.inner().storageProfile().dataDisks().remove(idx);
            break;
        }
    }
    return this;
}
Also used : VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 8 with VirtualMachineUnmanagedDataDisk

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

the class UnmanagedDataDiskImpl method setDataDisksDefaults.

protected static void setDataDisksDefaults(List<VirtualMachineUnmanagedDataDisk> dataDisks, String namePrefix) {
    List<Integer> usedLuns = new ArrayList<>();
    for (VirtualMachineUnmanagedDataDisk dataDisk : dataDisks) {
        if (dataDisk.lun() != -1) {
            usedLuns.add(dataDisk.lun());
        }
    }
    for (VirtualMachineUnmanagedDataDisk dataDisk : dataDisks) {
        if (dataDisk.lun() == -1) {
            Integer i = 0;
            while (usedLuns.contains(i)) {
                i++;
            }
            dataDisk.inner().withLun(i);
            usedLuns.add(i);
        }
        if (dataDisk.name() == null) {
            dataDisk.inner().withName(namePrefix + "-data-disk-" + dataDisk.lun());
        }
        if (dataDisk.inner().caching() == null) {
            dataDisk.inner().withCaching(CachingTypes.READ_WRITE);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 9 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, StorageAccount storageAccount, String namePrefix) {
    for (VirtualMachineUnmanagedDataDisk dataDisk : dataDisks) {
        if (dataDisk.creationMethod() == DiskCreateOptionTypes.EMPTY || dataDisk.creationMethod() == DiskCreateOptionTypes.FROM_IMAGE) {
            //New empty and from image data disk requires Vhd Uri to be set
            if (dataDisk.inner().vhd() == null) {
                dataDisk.inner().withVhd(new VirtualHardDisk());
                dataDisk.inner().vhd().withUri(storageAccount.endPoints().primary().blob() + "vhds/" + 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)

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