1
votes

I am looking for a way to get the power state of a VM through the Azure ARM API (more specifically with the Java SDK), so far without any luck.

The code which retrieves the VM is the following:

import com.microsoft.azure.management.compute.ComputeManagementService;
import com.microsoft.azure.management.compute.ComputeManagementClient;
import com.microsoft.azure.management.compute.models.VirtualMachineGetResponse;
import com.microsoft.azure.management.compute.models.VirtualMachine;

@Test
public void getVirtualMachine() throws Exception {
    ComputeManagementClient client = ComputeManagementService.create(createConfiguration());
    VirtualMachineGetResponse response = client.getVirtualMachinesOperations().get("vdimtest5rg", "vdim-test-5");
    VirtualMachine virtualMachine = response.getVirtualMachine();
}

The REST call to which this results is:

GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{virtualMachineName}?api-version=2015-06-15

What is the recommended way to get the power state of a virtual machine in Azure through ARM?

I don't mind using a plain HTTP request, as long as I don't have to fall back to the ASM API and authenticate with a client certificate.

1

1 Answers

1
votes

You need to be calling the 'InstanceView' of the virtual machine, which will give you the status of the associated disks and machine.

/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}/InstanceView?api-version=2015-05-01-preview

Which should give you something that contains a snippet like this -

  "statuses": [
    {
      "code": "ProvisioningState/succeeded",
      "level": "Info",
      "displayStatus": "Provisioning succeeded",
      "time": "2016-01-26T20:00:46.4647319+00:00"
    },
    {
      "code": "PowerState/running",
      "level": "Info",
      "displayStatus": "VM running"
    }
  ]