Azure节点SDK可获取50多个虚拟机

时间:2019-02-28 17:39:51

标签: node.js azure azure-virtual-machine azure-node-sdk

我正在使用Azure节点SDK来获取所有要订阅的虚拟机:

var computeClient = new computeManagementClient.ComputeManagementClient(credentials, subscriptionId);
var clientNetworkManagement = new NetworkManagementClient(credentials, subscriptionId);

    computeClient.virtualMachines.listAll(function (err, result) {  
        returnResult(result);
    });

但是我订阅的虚拟机超过50个,而该调用最多返回50个虚拟机。

使用此函数computeClient.virtualMachines.listAll可以获取超过50个vms? https://github.com/Azure-Samples/compute-node-manage-vm

Thx

2 个答案:

答案 0 :(得分:1)

我试图重现您的问题,但未能通过下面的代码列出所有VM。在运行代码之前,我为当前订阅的AzureAD注册应用分配了一个角色Virtual Machine Contributor(或者您可以使用更高级别的角色,例如ContributerOwner),您可以参考进入官方文档Manage access to Azure resources using RBAC and the Azure portal就可以知道。

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

var clientId = process.env['CLIENT_ID'] || '<your client id>';
var domain = process.env['DOMAIN'] || '<your tenant id>';
var secret = process.env['APPLICATION_SECRET'] || '<your client secret>';
var subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'] || '<your subscription id for listing all VMs in it>';

var computeClient;

msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials, subscriptions) {
    computeClient = new ComputeManagementClient(credentials, subscriptionId);
    computeClient.virtualMachines.listAll(function (err, result) {  
        console.log(result.length);
    });
});

在Azure门户上,我的当前订阅中有155个VM列表,如下图。但是,我的代码结果仅为153个VM。我不知道为什么结果不同,但是我的代码结果与Azure CLI命令az vm list | grep vmId | wc -l相同。

图1.我当前订阅中的VM数量

enter image description here

图2.我的代码的结果

enter image description here

图3. Azure CLI命令az vm list|grep vmId|wc -l

的结果

enter image description here

根据我的经验,我想您的问题是由于为您的应用分配了较低权限角色而仅列出了您具有默认访问权限的VM。

任何关注或更新都有助于您了解真正的问题,请随时告诉我。

答案 1 :(得分:0)

我不知道这是否是解决问题的最佳方法,但我找到了解决方法:

msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials, subscriptions) {
    computeClient = new ComputeManagementClient(credentials, subscriptionId);

    computeClient.virtualMachines.listAll(function (err, result, httpRequest, response) {  
        let myResult = JSON.parse(response.body);
        console.log(result.length);
        nextLink = myResult.nextLink;

        console.log(nextLink);
        computeClient.virtualMachines.listAllNext(nextLink, function (err, result, request, response) { 
            console.log(result.length);
        });
    });
});

第一个调用(listAll)返回50 Vm和“ nextLink”值。 比我打电话给listAllNext(nextLink,...返回其他39 Vm的

相关问题