【问题标题】:google-cloud/resource' cloud function not listing all projects in responsegoogle-cloud/resource 的云功能未列出所有项目作为响应
【发布时间】:2020-06-30 11:23:26
【问题描述】:

我正在使用用 node.js 编写的云函数来列出项目,这是包含该方法的 index.js 文件,当我触发此函数时,我只打印了 1 个项目。 ProjectA -> 云功能也驻留在 ProjectA 中,我有另一个 ProjectB 没有打印,它也处于活动模式。我拥有这两个项目的所有者权限。

const {Resource} = require('@google-cloud/resource');
const resource = new Resource();

async function getProjects() {

    try {
        // Lists all current projects
        const [projects] = await resource.getProjects();
        
        console.log(`success in getProjects() call`);

        // Set a uniform endTime for all the resulting messages
        const endTime = new Date();
        const endTimeStr = endTime.toISOString();

        // sample 2019-11-12T17:58:26.068483Z

        for (var i=0; i<projects.length;i++) {
            console.log("Total Projects ",projects.length) //Printing as 1 instead of correct 2
            // Only publish messages for active projects
            if (projects[i]["metadata"]["lifecycleState"] === config.ACTIVE)  {   
                // Construct a Pub/Sub message 
                console.log(`About to send Pub/Sub message ${projects[i]}}`);
                const pubsubMessage = {
                        "token": config.METRIC_EXPORT_PUBSUB_VERIFICATION_TOKEN,
                        "project_id": projects[i]["id"],
                        "end_time": endTimeStr
                }
           } 

    }
} catch(err) {
    console.error("Error in getProjects()");
    console.error(err);
    throw err;
}

}

但是,如果我尝试使用 google api 链接 https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#try-it 我收到了 2 个项目作为我可以访问的响应。

【问题讨论】:

  • 使用正确的身份验证信息,您的代码就可以工作!在 Cloud Function 上使用自定义服务帐户并将其授予 2 个项目。
  • 非常感谢,我正在使用自定义服务帐户,添加角色/查看器后,它正在列出

标签: node.js google-cloud-platform google-cloud-functions


【解决方案1】:

当您执行云函数时,您选择将执行它的服务帐户,通常是“App Engine 默认服务帐户 (project-id@appspot.gserviceaccount.com),该服务帐户应具有“项目所有者”角色。

来自API explorer 的 API 调用使用与您的用户帐户相关联的 API 密钥,而不是用于执行 Cloud Functions 的服务帐户,这就是它向您显示所有项目的原因。

要解决您的问题,只需将您用于执行云功能的服务帐户添加到您的所有具有Project Owner 角色的项目中,尽管其他角色(如Project Viewer)足以列出它。

【讨论】:

  • 您好,非常感谢,我没有为服务帐户找到任何特定的项目查看者角色,因此我将此角色角色/查看者添加到该服务帐户,现在云功能列出了 2 个项目,是这个角色 ok 角色/查​​看者?
  • 尝试在 IAM 中添加服务账户 -> 点击添加 -> 选择项目,然后点击新成员上的查看器,只需粘贴用于执行云功能的服务账户。但是,如前所述,应该有其他角色可以满足要求
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 2019-12-18
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2017-11-28
相关资源
最近更新 更多