【问题标题】:Azure node SDK to get more than 50 virtual machinesAzure 节点 SDK 获取 50 多台虚拟机
【发布时间】:2019-02-28 17:39:51
【问题描述】:

我使用 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 个 vm 吗? https://github.com/Azure-Samples/compute-node-manage-vm

谢谢

【问题讨论】:

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


    【解决方案1】:

    我试图重现您的问题,但未能通过我的代码列出所有虚拟机,如下所示。在运行我的代码之前,我为我在 AzureAD 中注册的应用程序分配了一个角色Virtual Machine Contributor(或者您可以使用更高级别的角色,如ContributerOwner),用于我当前的订阅,您可以参考官方文档@987654321 @知道它。

    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 个虚拟机。我不知道为什么结果不同,但我的代码结果与 Azure CLI 命令az vm list | grep vmId | wc -l 相同。

    图 1. 我当前订阅的虚拟机数量

    图 2. 我的代码的结果

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

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

    任何问题或更新对于了解您的真正问题非常有帮助,请随时告诉我。

    【讨论】:

    • 感谢您的回答 Peter,我在 AzureAD 角色中注册的应用程序是贡献者,但我尝试使用所有者,但运气不好仍然给我 50 Vm。 Azure CLI 命令使用我的帐户(所有者)返回 89 个虚拟机。我正在使用 azure-arm-compute Ver。 9.1.0
    • 我尝试了另一个订阅,包含 65 个虚拟机,但代码仅返回 50 个虚拟机。限制总是一样的。
    【解决方案2】:

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

    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's

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-02
      • 2019-10-20
      • 2014-05-30
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多