如何使用ARM获取Azure虚拟机的电源状态

时间:2016-01-29 14:54:35

标签: java azure sdk

我正在寻找一种通过Azure ARM API(更具体地说是Java SDK)获取VM的电源状态的方法,到目前为止没有任何运气。

检索VM的代码如下:

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();
}

此结果的REST调用是:

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

通过ARM获取Azure中虚拟机电源状态的推荐方法是什么?

我不介意使用普通的HTTP请求,只要我不必回退到ASM API并使用客户端证书进行身份验证。

1 个答案:

答案 0 :(得分:1)

您需要调用虚拟机的“InstanceView”,它将为您提供相关磁盘和计算机的状态。

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

哪个应该给你一些包含这样的片段的内容 -

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