简体   繁体   中英

How can I get the power state of an Azure virtual machine using ARM

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.

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"
    }
  ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM