azure-docs-sdk-java/docs-ref-conceptual/java-sdk-manage-virtual-mac...

8.0 KiB

title description author manager ms.assetid ms.devlang ms.topic ms.service ms.technology ms.date ms.author
Manage Azure virtual machines with Java | Microsoft Docs Sample code to manage Azure virtual machines using the Azure SDK for Java rloutlaw douge 88629aee-6279-433e-a08b-4f8e290446d0 java article Azure Azure 3/30/2017 routlaw;asirveda

Manage Azure virtual machines from your Java applications

This sample uses the Azure management libraries for Java to create and work with Azure virtual machines.

Run the sample

Create an authentication file and set an environment variable AZURE_AUTH_LOCATION with the full path to the file on your computer. Then run:

git clone https://github.com/Azure-Samples/compute-java-manage-vm.git
cd compute-java-manage-vm
mvn clean compile exec:java

View the complete code sample on GitHub.

Authenticate with Azure

[!INCLUDE auth-include]

Create a Windows virtual machine

// Prepare a data disk for VM
Disk dataDisk = azure.disks().define(SdkContext.randomResourceName("dsk", 30))
            .withRegion(region)
            .withNewResourceGroup(rgName)
            .withData()
            .withSizeInGB(50)
            .create();

// create the windows virtual machine with the data disk            
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)
            .withNewDataDisk(10)
            .withNewDataDisk(dataDiskCreatable)
            .withExistingDataDisk(dataDisk)
            .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
            .create();

This code:

  1. Defines a Disk Creatable with a 50GB size and random name for use with a virtual machine.
  2. Uses the azure.virtualMachines().define()..create() chain to create the Windows Server 2012 virtual machine. The API creates the Disk defined in the previous step the same time as the virtual machine. A 10GB data disk is also attached to the virtual machine through withNewDataDisk(10).

Learn more about using Creatable objects to define local representations of resources and create them just as other Azure resources need them.

Stop, start, and restart a virtual machine

// look up a virtual machine by its ID and then restart, stop, and start it
azureVM = azure.getVirtualMachine.getById(windowsVM.id());

azureVM.restart();
azureVM.powerOff();
azureVM.start();

powerOff() stops the virtual machine operating system but does not deallocate its resources.

Add a virtual machine to an existing network

// Get the virtual network the current virtual machine is using
Network network = windowsVM.getPrimaryNetworkInterface().primaryIPConfiguration().getNetwork();

// Create a Linux VM in the same subnet
VirtualMachine linuxVM = azure.virtualMachines().define(linuxVmName)
           .withRegion(region)
           .withExistingResourceGroup(rgName)
           .withExistingPrimaryNetwork(network)
           .withSubnet("subnet1") // default subnet name when no name specified at creation
           .withPrimaryPrivateIPAddressDynamic()
           .withoutPrimaryPublicIPAddress()
           .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
           .withRootUsername(userName)
           .withRootPassword(password)
           .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
           .create();

Use withPopularLinuxImage to define a Linux VM instead of a Windows one.

List virtual machines

// get a list of VMs in the same resource group as an existing VM
String resourceGroupName = windowsVM.resourceGroupName();
PagedList<VirtualMachine> resourceGroupVMs = azure.virtualMachines()
        .listByResourceGroup(resourceGroupName); 

// for each vitual machine in the resource group, log their name and plan
for (VirtualMachine virtualMachine : azure.virtualMachines().listByResourceGroup(resourceGroupName)) {
    System.out.println("VM " + virtualMachine.computerName() + 
        " has plan " + virtualMachine.plan());
}

List all virtual machines for a subscription using azure.virtualMachines().list() and iterate through the Map returned by tags() to manage tagged collections of virtual machines across resource groups.

Update a virtual machine

// add a 10GB data disk to the virtual machine
windowsVM.update()
     .withNewDataDisk(10)
     .apply();

Update the virtual machine configuration using update()...apply() and the same methods used to configure the virtual machine when created through define()...create().

Delete a virtual machine

// delete by ID if you already are working with the VM object
azure.virtualMachines().deleteById(windowsVM.id());

// delete by resource group and name
azure.virtualMachines().deleteByResourceGroup(rgName,windowsVmName);

Sample explanation

The sample code creates a Windows virtual machine with a 50GB data disk. The sample then creates a second 10GB data disk and attaches it to this Windows virtual machine. Then the sample creates a Linux virtual machine in the same virtual network as the Windows virtual machine.

The sample logs information about both virtual machines and deletes them both before completing.

Class used in sample Notes
VirtualMachine Query properties and manage state of virtual machines. Retrieved in list form withazure.virtualMachines().list() or by name or ID azure.virtualMachines().getByResourceGroup()
VirtualMachineSizeTypes Class with static values that map to virtual machine size options, used by the withSize() method to define the resources allocated to the VM.
Disk Create a disk to store data using withData() or operating system image using the appropriate withLinux or withWindows method when defining the disk. Attach disks to virtual machines either at the time of creation (using withNewDataDisk or withExistingDataDisk) or after creation by update()..apply() on the VirtualMachine object.
DiskSkuTypes Class with static values to define a disk with a standard or premium storage plan.
KnownLinuxVirtualMachineImage Class with a set of Linux virtual machine options for use with the withPopularLinuxImage() method when defining a virtual machine.
KnownWindowsVirtualMachineImage Class with a set of Windows virtual machine image options for use with the withPopularWindowsImage() method when defining a virtual machine.

Next steps

[!INCLUDE next-steps]