查看为项目启用了哪些 GCP 服务(节点 js)?



根据文档,如果您运行gcloud services list --available,则使用gcloudcli,您可以获得为Google Cloud项目启用或可启用的服务列表。在节点中用于执行此操作的等效库/调用是什么?我已经查看了此处列出的库,似乎找不到如何做到这一点。

我正在使用在节点js env中运行的terraformer 以编程方式抓取帐户,但是如果在您尝试运行项目时未为项目启用某些服务,它将出错。基本上,在我运行terraformer之前,我想获取已启用的服务的列表,并且只导入这些服务。

以防其他人好奇:

import { google } from 'googleapis'
const usage = google.serviceusage('v1')
const project = "myProject"
const authorize = async (scopes) => {
const auth = new google.auth.GoogleAuth({ scopes })
return await auth.getClient()
}
const {
data: { services }
} = await usage.services.list({
parent: `projects/${project}`,
filter: 'state:ENABLED',
auth: await authorize(['https://www.googleapis.com/auth/cloud-platform'])
})

GoogleCloud文档非常好,在大多数情况下,我建议快速进行Google搜索。您可以在此处找到您正在寻找的几个示例。

实际的 http 请求如下所示(此示例不显示如何附加身份验证信息(

curl 'https://serviceusage.googleapis.com/v1/projects/357084163378/services?filter=state:ENABLED'

我建议向此端点提交 Ajax 请求。如果您已经在使用 Google SDK,您应该能够获取要附加到 api 请求的访问令牌

最新更新