Node.js Azure sdk - 获取虚拟机状态

Node.js Azure sdk - getting the Virtual Machine state

我已经开始研究 node.js 的 azure sdk(下面的 link),有趣的是,我已经碰壁了,我认为这是最常见的情况之一人们希望使用 Azure 的 REST 端点实现的常见任务,这些端点正在检查虚拟机的状态。

我可以很容易地获得所有机器的列表,或者特别是一台机器,但此服务的响应不包括 VM 的当前状态(运行、已停止等)

除了博客 post (https://github.com/Azure/azure-xplat-cli/issues/2565) 之外,在 docos 或网络中绝对没有关于此特定场景的信息,它实际上是关于另一个库的。

请注意,我正在使用 azure-arm-compute 库,它是 Node.js azure sdk 的一部分。

非常感谢任何帮助

github 回购:https://github.com/Azure/azure-sdk-for-node

要获取虚拟机状态,请使用函数 get(resourceGroupName, vmName, optionsopt, optionalCallbackopt),并将值 {expand: 'instanceView'} 作为选项参数传递。

var msRestAzure = require('ms-rest-azure');
var computeManagementClient = require('azure-arm-compute');

// Interactive Login
// It provides a url and code that needs to be copied and pasted in a browser and authenticated over there. If successful, 
// the user will get a DeviceTokenCredentials object.
msRestAzure.interactiveLogin(function(err, credentials) {
  var client = new computeManagementClient(credentials, 'ed0caab7***');

  client.virtualMachines.get('<resourceGroupName>', '<vmName>', {expand: 'instanceView'}, function(err, result, request, response) {
    if (err) console.log(err);
    console.log(result.instanceView);
  });
});