【问题标题】:GCP project creation via API doesn't enable Service Usage API通过 API 创建 GCP 项目不会启用服务使用 API
【发布时间】:2021-09-19 07:15:33
【问题描述】:

我正在尝试使用适用于 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。

我将不胜感激任何形式的帮助。

【问题讨论】:

  • 您需要启用serviceusage.googleapis.com。问题是什么? Stack Overflow 不是讨论供应商政策或服务要求的地方。将您的问题转化为编程问题。
  • 我在 Google Cloud 集体中提出一个编程问题。该问题与如何仅使用公共 API 以编程方式启用服务使用 API 而无需触摸 Cloud Console 相关。与政策或服务要求无关。
  • 您在启用 API 时遇到什么问题?您的问题包含I find it suspicious 字样,它要求提供离题的意见。重写您的问题以清楚地说明问题,包括您编写的代码和您的代码生成的错误消息。
  • 某些 API 默认启用,serviceusage.googleapis.com 不是其中之一,无论您是否觉得它可疑 :)
  • @jabbson 如您在此处看到的 cloud.google.com/service-usage/docs/enabled-service#default 服务使用 API 被列为默认服务之一。他们还提到这适用于 SDK,所以也许我在这个难题中遗漏了一些东西。

标签: node.js google-cloud-platform google-cloud-sdk google-cloud-console google-cloud-resource-manager


【解决方案1】:

作为 GCP 文档中的 described

当您使用 Cloud Console 或 Cloud SDK 创建 Cloud 项目时,默认启用以下 API 和服务...

在您的情况下,您正在使用Client Library 创建一个项目。该文档需要改进,因为当它提到 Cloud SDK 时,它们实际上是指 CLI 工具,而不是客户端库。

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

您不能调用服务使用来启用项目的服务使用,因为进行调用需要已经在资源项目上启用服务使用。

我的建议是遵循这个流程:

  1. 某个进程使用应用程序项目 X(已启用服务使用 API)创建新项目 Y。
  2. 同样的流程,使用应用项目 X,在项目 Y 上批量启用 API 服务。

或者:

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

【讨论】:

  • 客户端库是 Cloud SDK 的part
  • @jabbson - 对于新用户来说,Cloud SDK 一词的使用令人困惑,因为它在多个上下文中使用。在某些地方它表示 CLI,在其他地方它表示 Client Library/SDK
  • 我最终使用了 gcloud 命令。该解决方案不太优雅,但可以完成工作。由于某种未知的原因,我无法让它工作。
【解决方案2】:

我写了一个完整的代码块,它对我有用。如果代码质量受到影响,我提前道歉(我可能把它宰了) - 实际上不知道任何 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 之前,我会确保项目已完全创建(这只是一个理论)。

关于link,你之前提到过,我在这里推测一下,但我认为Cloud SDK的意思是gcloud CLI工具,它是Cloud SDK的一部分。

【讨论】:

  • 我运行了相同的代码,但在启用服务期间仍然失败:Service Usage API has not been used in project
  • @MarianoItaliano 出于好奇,您能否尝试使用 gcloud 命令执行相同的操作,看看它是否会让您,如果不是,错误会是什么?
  • 令人惊讶的是 gcloud 命令可以完美运行,更准确地创建项目并默认启用服务。不幸的是,我不能使用 gloud,我需要 API。
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多