Skip to content

[Management] Synchronous calls

Rujun Chen edited this page Aug 20, 2021 · 1 revision

Access to resource collections

Once you have your AzureResourceManager client created, you will have a set of resource collections available to you. You can access them through

VirtualMachines virtualMachines = azureResourceManager.virtualMachines();

This will give you complete access to the virtual machines in your subscription.

List, Get, and Delete

Each operation is a method group containing a set of methods accessing a type of resource.

PagedIterable<VirtualMachine> vms = virtualMachines.list();

will list all the VMs in your subscription.

VirtualMachine vm = virtualMachines.getByResourceGroup("myRg1", "myVm1");

will get you a specific VM.

virtualMachines.deleteById(vm.id());
// Equivalent:
// virtualMachines.deleteByResourceGroup("myRg1", "myVm1");

will delete the VM.

Create

Resource collections that allow you to create a new resource will have a define() method available. This will start the fluent builder pattern for defining a virtual machine in Azure. You will be prompted with a chain of withBlah() methods to provide all the required information, before .create() method is available to you.

You will first be asked for the region to deploy the virtual machine. At this stage, you cannot specify any other information:

virtualMachines.define(linuxVmName)
    .withRegion()

You will then be asked about the resource group, the network, and operation system about the VM. In many situations, depending on the previous withBlah() method you called, more required information may be required.

For example, SSH key information is only available in Linux VM configuration:

VirtualMachine linuxVM = virtualMachines.define(linuxVmName)
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withExistingPrimaryNetwork(network)
    .withSubnet("subnet1")
    .withPrimaryPrivateIPAddressDynamic()
    .withoutPrimaryPublicIPAddress()
    .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS)
    .withRootUsername(userName)
    .withSsh()

but not on Windows VM configuration:

VirtualMachine windowsVM = virtualMachines.define(windowsVmName)
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withExistingPrimaryNetwork(network)
    .withSubnet("subnet1")
    .withPrimaryPrivateIPAddressDynamic()
    .withoutPrimaryPublicIPAddress()
    .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2019_DATACENTER)
    .withAdminUsername(userName)
    .withSsh    // withSsh method not available

In the above example, you can also see that withSubnet() is required if you are providing an exiting virtual network.

After you've provided all the required information you will have access to all the optional configurations and .create() method:

VirtualMachine linuxVM = virtualMachines.define(linuxVmName)
    .withRegion(Region.US_EAST)
    .withExistingResourceGroup(rgName)
    .withExistingPrimaryNetwork(network)
    .withSubnet("subnet1")
    .withPrimaryPrivateIPAddressDynamic()
    .withoutPrimaryPublicIPAddress()
    .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS)
    .withRootUsername(userName)
    .withSsh(publicKey)
    .create();

create() will block, and return you a resource model upon completion.

Update

Resource models that allow you to make updates will have an update() method:

windowsVM = windowsVM.update()
    .withNewDataDisk(10)
        .defineNewDataDisk(dataDiskName)
        .withSizeInGB(20)
        .withCaching(CachingTypes.READ_WRITE)
        .attach()
    .apply();

All the updatable properties of the model will be available after update(). apply() will block, and return you a resource model upon completion.

Refresh

Resource models that allow you to sync with latest state on Azure cloud will have an refresh() method:

windowsVM = windowsVM.refresh();

refresh() will block, and return you a resource model upon completion.

Resource actions

There are a bunch of actions associated with resources. For example, on a virtual machine instance, you can call .powerOff(), .start(), .restart() etc. These calls will block, and return upon completion.

Clone this wiki locally