use of com.microsoft.azure.management.compute.VirtualMachineCustomImage 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.VirtualMachineCustomImage in project azure-sdk-for-java by Azure.
the class CreateVirtualMachineUsingCustomImageFromVM 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 linuxVMName3 = Utils.createRandomName("VM3");
final String customImageName = Utils.createRandomName("img");
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 un-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).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(100).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);
// De-provision the virtual machine
deprovisionAgentInLinuxVM(linuxVM.getPrimaryPublicIPAddress().fqdn(), 22, userName, password);
//=============================================================
// Deallocate the virtual machine
System.out.println("Deallocate VM: " + linuxVM.id());
linuxVM.deallocate();
System.out.println("De-allocated VM: " + linuxVM.id() + "; state = " + linuxVM.powerState());
//=============================================================
// Generalize the virtual machine
System.out.println("Generalize VM: " + linuxVM.id());
linuxVM.generalize();
System.out.println("Generalized VM: " + linuxVM.id());
//=============================================================
// Capture the virtual machine to get a 'Generalized image' with Apache
System.out.println("Capturing VM as custom image: " + linuxVM.id());
VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).fromVirtualMachine(linuxVM).create();
System.out.println("Captured VM: " + linuxVM.id());
Utils.print(virtualMachineCustomImage);
//=============================================================
// Create a Linux VM using custom image
System.out.println("Creating a Linux VM using custom image - " + virtualMachineCustomImage.id());
VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
Utils.print(linuxVM2);
//=============================================================
// Create another Linux VM using custom image and configure the data disks from image and
// add another data disk
System.out.println("Creating another Linux VM with additional data disks using custom image - " + virtualMachineCustomImage.id());
VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVMName3).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withNewDataDiskFromImage(1, 200, // TODO: Naming needs to be finalized
CachingTypes.READ_WRITE).withNewDataDiskFromImage(2, 100, CachingTypes.READ_ONLY).withNewDataDisk(50).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
Utils.print(linuxVM3);
// 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 - " + linuxVM3.id());
linuxVM3.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
Disk osDisk = azure.disks().getById(linuxVM3.osDiskId());
String osDiskSasUri = osDisk.grantAccess(24 * 60);
System.out.println("OS disk SAS Uri: " + osDiskSasUri);
// Data disks SAS Uri
for (VirtualMachineDataDisk disk : linuxVM3.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));
}
//=============================================================
// Deleting the custom image
System.out.println("Deleting custom Image: " + virtualMachineCustomImage.id());
azure.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
System.out.println("Deleted custom image");
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.VirtualMachineCustomImage in project azure-sdk-for-java by Azure.
the class CreateVirtualMachineUsingCustomImageFromVHD 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 = SdkContext.randomResourceName("VM1", 10);
final String linuxVMName2 = SdkContext.randomResourceName("VM2", 10);
final String linuxVMName3 = SdkContext.randomResourceName("VM3", 10);
final String customImageName = SdkContext.randomResourceName("img", 10);
final String rgName = SdkContext.randomResourceName("rgCOMV", 15);
final String publicIPDnsLabel = SdkContext.randomResourceName("pip", 10);
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 un-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).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(100).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().defineUnmanagedDataDisk("disk-3").withNewVhd(60).withLun(3).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);
// De-provision the virtual machine
deprovisionAgentInLinuxVM(linuxVM.getPrimaryPublicIPAddress().fqdn(), 22, userName, password);
//=============================================================
// Deallocate the virtual machine
System.out.println("Deallocate VM: " + linuxVM.id());
linuxVM.deallocate();
System.out.println("De-allocated VM: " + linuxVM.id() + "; state = " + linuxVM.powerState());
//=============================================================
// Generalize the virtual machine
System.out.println("Generalize VM: " + linuxVM.id());
linuxVM.generalize();
System.out.println("Generalized VM: " + linuxVM.id());
//=============================================================
// Create a virtual machine custom image from OS and data disks of the virtual machine
System.out.println("Creating virtual machine custom image from un-managed disk VHDs: " + linuxVM.id());
VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromVhd(linuxVM.osUnmanagedDiskVhdUri(), OperatingSystemStateTypes.GENERALIZED).defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(1).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(1).vhdUri()).attach().defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(2).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(2).vhdUri()).attach().defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(3).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(3).vhdUri()).withDiskCaching(CachingTypes.READ_ONLY).attach().create();
System.out.println("Created custom image");
Utils.print(virtualMachineCustomImage);
//=============================================================
// Create a Linux VM using custom image
System.out.println("Creating a Linux VM using custom image: " + virtualMachineCustomImage.id());
VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created Linux VM");
Utils.print(linuxVM2);
//=============================================================
// Create another Linux VM using custom image and configure the data disks from image and
// add another data disk
VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVMName3).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withNewDataDiskFromImage(1, 200, CachingTypes.READ_WRITE).withNewDataDiskFromImage(2, 100, CachingTypes.READ_ONLY).withNewDataDiskFromImage(3, 100, CachingTypes.READ_WRITE).withNewDataDisk(50).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
Utils.print(linuxVM3);
// 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 - " + linuxVM3.id());
linuxVM3.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
Disk osDisk = azure.disks().getById(linuxVM3.osDiskId());
String osDiskSasUri = osDisk.grantAccess(24 * 60);
System.out.println("OS disk SAS Uri: " + osDiskSasUri);
// Data Disks SAS Uri
for (VirtualMachineDataDisk disk : linuxVM3.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));
}
//=============================================================
// Deleting the custom image
System.out.println("Deleting custom Image: " + virtualMachineCustomImage.id());
azure.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
System.out.println("Deleted custom image");
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