1

Here is my code. Here I replaced my subscription id with "subid".

const { DefaultAzureCredential } = require("@azure/identity");
const { NetworkManagementClient } = require("@azure/arm-network");

const subscriptionId="subid"
const resourceGroupName = "azure_dep";
const gatewayName = "VNet2GW";

// Set up the credentials and network management client
const credential = new DefaultAzureCredential();
const client = new NetworkManagementClient(credential, subscriptionId);

async function vpnGatewayListBySubscription() {
    const resArray = new Array();
    for await (let item of client.vpnGateways.list()) {
        resArray.push(item);
    }
    console.log(resArray);
}

vpnGatewayListBySubscription();

Output is just an empty list like this []. But I have 2 VPN gateways under my sub. Further, I can list them using Azure CLI. But I can't get the list using javascript.

Venkatesan
  • 3,748
  • 1
  • 3
  • 15

1 Answers1

0

I tried in my environment and got the below results:

You can use the below javascript code to get the list of vpn gateways.

Code:

const { DefaultAzureCredential } = require('@azure/identity');
const { NetworkManagementClient } = require('@azure/arm-network');

const subscriptionId = '';
const credential = new DefaultAzureCredential();
const client = new NetworkManagementClient(credential, subscriptionId);

async function vpnGatewayListBySubscription() {
  for await (let item of client.vpnGateways.list()) {
        console.log(item.name)
        console.log(item.location)
        console.log(item.id)
  }
}

vpnGatewayListBySubscription();

Output:

The above code executed and successfully listed the VPN gateway with name, id, and location.

908d062c7c964e78815e889ac0815c5c-westindia-gw
westindia
/subscriptions/subid/resourceGroups/Networking-Resources/providers/Microsoft.Network/vpnGateways/908d062c7c964e78815e889ac0815c5c-westindia-gw

enter image description here

Reference: Virtual Network Gateways - List - REST API (Azure Network Gateway) | Microsoft Learn

Venkatesan
  • 3,748
  • 1
  • 3
  • 15