Francisco Fernandes
Francisco Fernandes

Reputation: 193

Azure node SDK to get more than 50 virtual machines

I' using the Azure node SDK to get all virtual machines for the subscription :

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

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

But I have subscription with more than 50 vm's and that call only return 50 vm's max.

It's possible to get more than 50 vms with this function computeClient.virtualMachines.listAll ? https://github.com/Azure-Samples/compute-node-manage-vm

Thx

Upvotes: 1

Views: 687

Answers (2)

Francisco Fernandes
Francisco Fernandes

Reputation: 193

I don't know if this is the best way to solve the problem, but I find a solution:

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

The first call (listAll) return 50 Vm's and "nextLink" value. Than I call listAllNext(nextLink,... that return the others 39 Vm's

Upvotes: 1

Peter Pan
Peter Pan

Reputation: 24138

I tried to reproduce your issue, but failed that I can list all VMs via my code as below. Before to run my code, I assigned a role Virtual Machine Contributor(or you can use higher level role like Contributer or Owner) to my app registed in AzureAD for my current subscription, you can refer to the offical document Manage access to Azure resources using RBAC and the Azure portal to know it.

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

On Azure portal, there are 155 VMs list in my current subscription as the figure below. However, the result of my code only is 153 VMs. I don't know why the results are different, but my code result is same with Azure CLI command az vm list | grep vmId | wc -l.

Fig 1. The number of VMs in my current subscription

enter image description here

Fig 2. The result of my code

enter image description here

Fig 3. The result of Azure CLI command az vm list|grep vmId|wc -l

enter image description here

Per my experience, I guess your issue was caused by assigning the lower permission role for your app to only list VMs that you have default accessing permission.

Any concern or update is very helpful for understanding what your real issue is, please feel free to let me know.

Upvotes: 1

Related Questions