use of com.microsoft.azure.management.compute.Snapshot in project azure-sdk-for-java by Azure.
the class ManageManagedDisks 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_EAST;
final String rgName = Utils.createRandomName("rgCOMV");
final String userName = "tirekicker";
final String sshkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
try {
// ::==Create a VM
// Create a virtual machine with an implicit Managed OS disk and explicit Managed data disk
System.out.println("Creating VM [with an implicit Managed OS disk and explicit Managed data disk]");
final String linuxVM1Name = SdkContext.randomResourceName("vm" + "-", 18);
final String linuxVM1Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM1 = azure.virtualMachines().define(linuxVM1Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM1Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with an implicit Managed OS disk and explicit Managed data disk]");
// Creation is simplified with implicit creation of managed disks without specifying all the disk details. You will notice that you do not require storage accounts
// ::== Update the VM
// Create a VMSS with implicit managed OS disks and explicit managed data disks
System.out.println("Creating VMSS [with implicit managed OS disks and explicit managed data disks]");
final String vmScaleSetName = SdkContext.randomResourceName("vmss" + "-", 18);
VirtualMachineScaleSet vmScaleSet = azure.virtualMachineScaleSets().define(vmScaleSetName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2).withExistingPrimaryNetworkSubnet(prepareNetwork(azure, region, rgName), "subnet1").withExistingPrimaryInternetFacingLoadBalancer(prepareLoadBalancer(azure, region, rgName)).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_ONLY).withCapacity(3).create();
System.out.println("Created VMSS [with implicit managed OS disks and explicit managed data disks]");
// Create an empty disk and attach to a VM (Manage Virtual Machine With Disk)
System.out.println("Creating empty data disk [to attach to a VM]");
final String diskName = SdkContext.randomResourceName("dsk" + "-", 18);
Disk dataDisk = azure.disks().define(diskName).withRegion(region).withExistingResourceGroup(rgName).withData().withSizeInGB(50).create();
System.out.println("Created empty data disk [to attach to a VM]");
System.out.println("Creating VM [with new managed data disks and disk attached]");
final String linuxVM2Name = SdkContext.randomResourceName("vm" + "-", 10);
final String linuxVM2Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVM2Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM2Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withExistingDataDisk(dataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with new managed data disks and disk attached]");
// Update a VM
System.out.println("Updating VM [by detaching a disk and adding empty disk]");
linuxVM2.update().withoutDataDisk(2).withNewDataDisk(200).apply();
System.out.println("Updated VM [by detaching a disk and adding empty disk]");
// Create a VM from an image (Create Virtual Machine Using Custom Image from VM)
System.out.println("Preparing specialized virtual machine with un-managed disk");
final VirtualMachine linuxVM = prepareSpecializedUnmanagedVirtualMachine(azure, region, rgName);
System.out.println("Prepared specialized virtual machine with un-managed disk");
System.out.println("Creating custom image from specialized virtual machine");
final String customImageName = SdkContext.randomResourceName("cimg" + "-", 10);
VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).fromVirtualMachine(// from a deallocated and generalized VM
linuxVM).create();
System.out.println("Created custom image from specialized virtual machine");
System.out.println("Creating VM [from custom image]");
final String linuxVM3Name = SdkContext.randomResourceName("vm" + "-", 10);
VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVM3Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withSsh(sshkey).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [from custom image]");
// Create a VM from a VHD (Create Virtual Machine Using Specialized VHD)
final String linuxVMName4 = SdkContext.randomResourceName("vm" + "-", 10);
final String specializedVhd = linuxVM.osUnmanagedDiskVhdUri();
azure.virtualMachines().deleteById(linuxVM.id());
System.out.println("Creating VM [by attaching un-managed disk]");
VirtualMachine linuxVM4 = azure.virtualMachines().define(linuxVMName4).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSUnmanagedDisk(specializedVhd, OperatingSystemTypes.LINUX).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [by attaching un-managed disk]");
// Create a Snapshot (Create Virtual Machine using specialized disks from snapshot)
System.out.println("Preparing specialized virtual machine with managed disks");
final VirtualMachine linuxVM5 = prepareSpecializedManagedVirtualMachine(azure, region, rgName);
Disk osDisk = azure.disks().getById(linuxVM5.osDiskId());
List<Disk> dataDisks = new ArrayList<>();
for (VirtualMachineDataDisk disk : linuxVM5.dataDisks().values()) {
Disk d = azure.disks().getById(disk.id());
dataDisks.add(d);
}
System.out.println("Prepared specialized virtual machine with managed disks");
System.out.println("Deleting VM: " + linuxVM5.id());
azure.virtualMachines().deleteById(linuxVM5.id());
System.out.println("Deleted the VM: " + linuxVM5.id());
System.out.println("Creating snapshot [from managed OS disk]");
// Create a managed snapshot for an OS disk
final String managedOSSnapshotName = SdkContext.randomResourceName("snp" + "-", 10);
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromDisk(osDisk).create();
System.out.println("Created snapshot [from managed OS disk]");
System.out.println("Creating managed OS disk [from snapshot]");
// Create a managed disk from the managed snapshot for the OS disk
final String managedNewOSDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
Disk newOSDisk = azure.disks().define(managedNewOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromSnapshot(osSnapshot).withSizeInGB(100).create();
System.out.println("Created managed OS disk [from snapshot]");
System.out.println("Creating managed data snapshot [from managed data disk]");
// Create a managed snapshot for a data disk
final String managedDataDiskSnapshotName = SdkContext.randomResourceName("dsk" + "-", 10);
Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withDataFromDisk(dataDisks.get(0)).withSku(DiskSkuTypes.STANDARD_LRS).create();
System.out.println("Created managed data snapshot [from managed data disk]");
System.out.println("Creating managed data disk [from managed snapshot]");
// Create a managed disk from the managed snapshot for the data disk
final String managedNewDataDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
Disk newDataDisk = azure.disks().define(managedNewDataDiskName).withRegion(region).withExistingResourceGroup(rgName).withData().fromSnapshot(dataSnapshot).create();
System.out.println("Created managed data disk [from managed snapshot]");
System.out.println("Creating VM [with specialized OS managed disk]");
final String linuxVM6Name = SdkContext.randomResourceName("vm" + "-", 10);
VirtualMachine linuxVM6 = azure.virtualMachines().define(linuxVM6Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(newOSDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(newDataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with specialized OS managed disk]");
// ::== Migrate a VM to managed disks with a single reboot
System.out.println("Creating VM [with un-managed disk for migration]");
final String linuxVM7Name = SdkContext.randomResourceName("vm" + "-", 10);
final String linuxVM7Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM7 = azure.virtualMachines().define(linuxVM7Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM7Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withUnmanagedDisks().withNewUnmanagedDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with un-managed disk for migration]");
System.out.println("De-allocating VM :" + linuxVM7.id());
linuxVM7.deallocate();
System.out.println("De-allocated VM :" + linuxVM7.id());
System.out.println("Migrating VM");
linuxVM7.convertToManaged();
System.out.println("Migrated VM");
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;
}
use of com.microsoft.azure.management.compute.Snapshot in project azure-sdk-for-java by Azure.
the class CreateVirtualMachineUsingSpecializedDiskFromSnapshot 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 managedOSSnapshotName = Utils.createRandomName("ss-os-");
final String managedDataDiskSnapshotPrefix = Utils.createRandomName("ss-data-");
final String managedNewOSDiskName = Utils.createRandomName("ds-os-nw-");
final String managedNewDataDiskNamePrefix = Utils.createRandomName("ds-data-nw-");
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 a PIR image with managed OS and data disks and customize virtual
// machine using custom script extension
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).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).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 managed OS and data disks: " + linuxVM.id());
Utils.print(linuxVM);
// Gets the specialized managed OS and Data disks of the virtual machine
//
Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
List<Disk> dataDisks = new ArrayList<>();
for (VirtualMachineDataDisk disk : linuxVM.dataDisks().values()) {
Disk dataDisk = azure.disks().getById(disk.id());
dataDisks.add(dataDisk);
}
//=============================================================
// Delete the virtual machine
System.out.println("Deleting VM: " + linuxVM.id());
azure.virtualMachines().deleteById(linuxVM.id());
System.out.println("Deleted the VM");
//=============================================================
// Create Snapshot from the OS managed disk
System.out.println(String.format("Creating managed snapshot from the managed disk (holding specialized OS): %s ", osDisk.id()));
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromDisk(osDisk).create();
System.out.println("Created managed snapshot holding OS: " + osSnapshot.id());
// Utils.print(osSnapshot); TODO
//=============================================================
// Create Managed snapshot from the Data managed disks
List<Snapshot> dataSnapshots = new ArrayList<>();
int i = 0;
for (Disk dataDisk : dataDisks) {
System.out.println(String.format("Creating managed snapshot from the managed disk (holding data): %s ", dataDisk.id()));
Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotPrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withDataFromDisk(dataDisk).withSku(DiskSkuTypes.STANDARD_LRS).create();
dataSnapshots.add(dataSnapshot);
System.out.println("Created managed snapshot holding data: " + dataSnapshot.id());
// Utils.print(dataDisk); TODO
i++;
}
//=============================================================
// Create Managed disk from the specialized OS snapshot
System.out.println(String.format("Creating managed disk from the snapshot holding OS: %s ", osSnapshot.id()));
Disk newOSDisk = azure.disks().define(managedNewOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromSnapshot(osSnapshot).withSizeInGB(100).create();
System.out.println("Created managed disk holding OS: " + osDisk.id());
// Utils.print(osDisk); TODO
//=============================================================
// Create Managed disks from the data snapshots
List<Disk> newDataDisks = new ArrayList<>();
i = 0;
for (Snapshot dataSnapshot : dataSnapshots) {
System.out.println(String.format("Creating managed disk from the Data snapshot: %s ", dataSnapshot.id()));
Disk dataDisk = azure.disks().define(managedNewDataDiskNamePrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withData().fromSnapshot(dataSnapshot).create();
newDataDisks.add(dataDisk);
System.out.println("Created managed disk holding data: " + dataDisk.id());
// Utils.print(dataDisk); TODO
i++;
}
//
//=============================================================
// Create a Linux VM by attaching the managed 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(newOSDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(newDataDisks.get(0)).withExistingDataDisk(newDataDisks.get(1), 1, CachingTypes.READ_WRITE).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
Utils.print(linuxVM2);
//=============================================================
//
System.out.println("Deleting OS snapshot - " + osSnapshot.id());
azure.snapshots().deleteById(osSnapshot.id());
System.out.println("Deleted OS snapshot");
for (Snapshot dataSnapshot : dataSnapshots) {
System.out.println("Deleting data snapshot - " + dataSnapshot.id());
azure.snapshots().deleteById(dataSnapshot.id());
System.out.println("Deleted data snapshot");
}
// Getting the SAS URIs requires virtual machines to be de-allocated
// [Access not permitted because'disk' is currently attached to running VM]
//
System.out.println("De-allocating the virtual machine - " + linuxVM2.id());
linuxVM2.deallocate();
//=============================================================
// Get the readonly SAS URI to the OS and data disks
System.out.println("Getting OS and data disks SAS Uris");
// OS Disk SAS Uri
osDisk = azure.disks().getById(linuxVM2.osDiskId());
String osDiskSasUri = osDisk.grantAccess(24 * 60);
System.out.println("OS disk SAS Uri: " + osDiskSasUri);
// Data disks SAS Uri
for (VirtualMachineDataDisk disk : linuxVM2.dataDisks().values()) {
Disk dataDisk = azure.disks().getById(disk.id());
String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
System.out.println(String.format("Data disk (lun: %d) SAS Uri: %s", disk.lun(), 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;
}
Aggregations