通过 API 创建的 GCP 项目不会启用服务使用 API



我正在尝试使用Node.js的官方Google SDK来自动化项目创建的整个过程。对于项目创建,我使用资源管理器SDK:

const resource = new Resource();
const project = resource.project(projectName);
const [, operation,] = await project.create();

我还必须启用一些服务,以便在此过程中使用它们。当我运行时:

const client = new ServiceUsageClient();
const [operation] = await client.batchEnableServices({
parent: `projects/${projectId}`,
serviceIds: [
"apigateway.googleapis.com",
"servicecontrol.googleapis.com",
"servicemanagement.googleapis.com",
]
});

我收到:

Service Usage API has not been used in project 1014682171642 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/serviceusage.googleapis.com/overview?project=1014682171642 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

当我通过API创建项目时,我发现服务使用API在默认情况下没有启用这一点很可疑。显然,如果我必须手动启用某些功能,那么使用API会带来好处。当我通过Can Console创建项目时,服务使用API默认启用,因此此问题仅影响API。也许还有其他方法可以通过编程方式启用服务使用API。

我将感谢任何形式的帮助。

如GCP文档中所述:

使用云控制台或云SDK创建云项目时,默认情况下会启用以下API和服务

在您的案例中,您正在创建一个具有客户端库的项目。文档需要改进,因为当它提到云SDK时,它们实际上指的是CLI工具,而不是客户端库。

需要澄清的是,当前使用客户端库或REST创建的项目默认情况下没有启用任何API。

不能调用Service Usage来在项目上启用Service Usage,因为进行调用需要在资源项目上已经启用Service Usage。

我的建议是遵循以下流程:

  1. 一些进程使用应用程序项目X(其中启用了服务使用API(创建新项目Y
  2. 同样的过程,使用应用程序项目X,批量启用项目Y上的API服务

或者:

在某种bash脚本上自动执行项目创建过程,并使用gcloud projects create命令创建它们。

我写了一个完整的代码块,对我来说很有效。如果代码质量受到影响(我可能把它搞砸了(,我会提前道歉-实际上我不知道任何nodejs-我是根据你的代码和互联网上的几个例子编译的。

const {Resource} = require('@google-cloud/resource-manager');
const {ServiceUsageClient} = require('@google-cloud/service-usage');
const projectId = '<YOUR PROJECT ID>';
const orgId = '<YOUR ORG ID>'; // I had to use org for my project
const resource = new Resource();
async function create_project() {
await resource
.createProject(`${projectId}`, {
name: `${projectId}`,
parent: { type: "organization", id: `${orgId}` }
})
.then(data => {
const operation = data[1];
return operation.promise();
})
.then(data => {
console.log("Project created successfully!");
enable_apis();
});
}
const client = new ServiceUsageClient();
async function enable_apis() {
const [operation] = await client.batchEnableServices({
parent: `projects/${projectId}`,
serviceIds: [
"serviceusage.googleapis.com",
"servicecontrol.googleapis.com",
"servicemanagement.googleapis.com",
]
})
}
create_project();

这成功地创建了项目并启用了三个API。在尝试启用api之前,我会确保项目已经完全创建好(这只是一个理论(。

关于你前面提到的链接,我在这里进行推测,但我认为他们所说的云SDK是指gcloud CLI工具,它是云SDK的一部分。

我想完成同样的事情,对一个新项目进行完全编程设置,包括启用API。

这基本上是网上唯一一个涉及这种需求的结果,我认为每个人都只使用clickops或gcloud。

好吧,我在创建一个项目时在gcloud上做了一个--log-http,发现它将{}发布到:https://serviceusage.googleapis.com/v1/projects/<project-id>/services/cloudapis.googleapis.com:enable?alt=json

尽管这使用了serviceusage端点,但我在这个项目中关闭了服务使用API。我想他们可能允许启用cloudapis.googleapis.com元服务,即使服务使用尚未启用。

当我使用curl为通过API创建的新项目向该端点发送帖子时,它确实启用了基本的API集,包括服务使用API。

$ curl -X POST -H "Authorization: Bearer "$(gcloud auth print-access-token)"" -H "Content-Type: application/json; charset=utf-8" -d "{}" https://serviceusage.googleapis.com/v1/projects/<project-id>/services/cloudapis.googleapis.com:enable?alt=json
{
"name": "operations/acat.p2-3333333333-5555fffe-88888-472c-b280-444444444444"
}
$ curl -H "Authorization: Bearer "$(gcloud auth print-access-token)"" https://serviceusage.googleapis.com/v1/operations/acat.p2-3333333333-5555fffe-88888-472c-b280-444444444444?alt=json
{
"name": "operations/acat.p2-3333333333-5555fffe-88888-472c-b280-444444444444",
"metadata": {
"@type": "type.googleapis.com/google.protobuf.Empty"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.api.serviceusage.v1.EnableServiceResponse",
"service": {
"name": "projects/11111111111111/services/cloudapis.googleapis.com",
"config": {
"name": "cloudapis.googleapis.com",
"title": "Google Cloud APIs",
"documentation": {
"summary": "This is a meta service for Google Cloud APIs for convenience. Enabling this service enables all commonly used Google Cloud APIs for the project. By default, it is enabled for all projects created through Google Cloud Console and Google Cloud SDK, and should be manually enabled for all other projects that intend to use Google Cloud APIs. Note: disabling this service has no effect on other services.n"
},
"quota": {},
"authentication": {},
"usage": {
"requirements": [
"serviceusage.googleapis.com/tos/cloud"
]
},
"monitoring": {}
},
"state": "ENABLED",
"parent": "projects/11111111111111"
}
}
}

我还能够使用nodejs@google-cloud/service-usage库实现这一点:

const svcclient = new ServiceUsageClient()
async function enableServices(id) {
const [op] = await svcclient.enableService({ name: `projects/${id}/services/cloudapis.googleapis.com` })
const [resp] = await op.promise()
console.log(`Enabled services on project.`)
}

最新更新