node.jsgoogle-cloud-platformgoogle-cloud-sdkgoogle-cloud-consolegoogle-cloud-resource-manager

GCP project creation via API doesn't enable Service Usage API


I'm trying to automate the entire process of project creation using the official Google SDK for Node.js. For project creation, I use the Resource Manager SDK:

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

I also have to enable some services in order to use them in the process. When I run:

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

I receive:

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.

I find it suspicious that Service Usage API isn't enabled by default when I create a project via API. Obviously, it takes the benefit of using APIs if I had to enable something manually. When I create a project via Could Console, Service Usage API is enabled by default, so this issue affects only the API. Maybe there's some other way to enable Service Usage API programmatically.

I would appreciate any form of help.


Solution

  • As described in GCP docs:

    When you create a Cloud project using the Cloud Console or Cloud SDK, the following APIs and services are enabled by default...

    In your case, you're creating a project with a Client Library. The doc needs improvement as when it mentioned Cloud SDK, they actually meant the CLI tool, not the Client libraries.

    To clarify, projects currently created with Client Libraries or with REST don't have any APIs enabled by default.

    You can't call Service Usage to enable Service Usage on a project, as making the call would require Service Usage to already be enabled on the resource project.

    My suggestion is to follow this flow:

    1. Some process, using application project X (where Service Usage API is enabled), creates the new project Y.
    2. The same process, using application project X, batch enables API services on project Y.

    Or:

    Automate the process of project creation on some sort of a bash script and create them using gcloud projects create commands.