【问题标题】:Azure functions to get a list of resource groups in a subscription - NodejsAzure函数获取订阅中的资源组列表-Nodejs
【发布时间】:2020-08-10 11:42:46
【问题描述】:

我创建了一个函数,可以让我获得订阅中的资源组列表


// Function to get the resource group
const getHttp = async (subscriptionId) => {
    const response = await fetch(
        `https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups?api-version=2019-10-01`
    );

    if (response.status === 200) {
        const data = await response.json();
        return data;
    } else {
        throw new Error(
            'Unable to get list of resource group from Azure' +
                ' ' +
                response.status
        );
    }
};

module.exports = async function (context, req) {
    context.log('ListRG proccessed a request');
    const subscriptionId = 'xxx';

    const json = await getHttp(subscriptionId);

    console.log(json);
    context.log(json);
    context.res = { body: json };
};

但我明白了:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

这个错误:

2020-08-07T11:25:27.915 [Error] Executed 'Functions.ListRg' (Failed, Id=4a10c117-5ded-4bb4-882e-94ec4eb6e28b, Duration=36ms)Result: FailureException: Error: Unable to get list of resource group from AzureStack: Error: Unable to get list of resource group from Azureat getHttp (D:\home\site\wwwroot\ListRg\index.js:12:9)at processTicksAndRejections (internal/process/task_queues.js:97:5)at async module.exports (D:\home\site\wwwroot\ListRg\index.js:20:15)

对于权限,我使用托管身份并在我的订阅中赋予函数 Reader 角色

我错过了什么?

【问题讨论】:

    标签: node.js azure azure-functions


    【解决方案1】:

    根据您的要求,虽然我们已启用 MSI,但我们无法直接调用其余 api。如果您想使用 MSI 请求资源组列表,请参考下面我提供的解决方案:

    1. 安装模块:

    npm install ms-rest-azure
    npm install azure-arm-resource
    

    2.请参考下面我的代码:

    const msRestAzure = require("ms-rest-azure");
    const { ResourceManagementClient } = require("azure-arm-resource");
    
    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
    
        credentials = await msRestAzure.loginWithAppServiceMSI({ resource: 'https://management.azure.com' });
    
        const subscriptionId = '<your subscription id>';
    
        const resourceClient = new ResourceManagementClient(credentials, subscriptionId);
    
        let finalResult = await resourceClient.resourceGroups.list();
    
        finalResult.forEach(element => { 
            console.log(element); 
        });
    
        context.res = { body: 'success' };
    
    }
    

    顺便说一句,我的解决方案使用 sdk 来执行请求。如果你想直接在你的函数代码中请求apihttps://management.azure.com/subscriptions/{subscriptionId}/resourcegroups?api-version=2019-10-01,你需要先获取访问令牌,然后在你的“获取”的标题中设置令牌。但在我看来,我建议你使用 sdk。

    【讨论】:

    • 嘿,快,非常感谢!确实有很大帮助,虽然是一个问题。你怎么知道 ?在问这里之前我真的环顾了很多地方,发现什么都没有
    • 嗨@Renm我知道我们不能直接在azure函数中请求api,尽管我们启用了MSI(这相当于来自任何客户端的请求)。所以我找了一些资料,发现这个sample和你的问题很相似。
    猜你喜欢
    • 2013-07-09
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多