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.

2 answers

  • answered 2021-07-09 04:54 Dondi

    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. Projects created with Client Libraries or with REST don't have any APIs enabled by default.

    Unfortunately, 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 automate the process of project creation on some sort of a bash script and create them using gcloud projects create commands.

  • answered 2021-07-09 05:09 jabbson

    I wrote a complete block of code, which worked for me. I apologize in advance if the code quality suffers (I probably butchered it) - literally do not know any nodejs - I compiled it from your code and couple of examples on the Internet.

    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();
    

    This successfully creates the project and enables three APIs. I would make sure the project is fully created before trying to enable apis (this is just a theory).

    Regarding the link, you've mentioned earlier, I am going to speculate here, but I think what they meant by Cloud SDK is gcloud CLI tool, which is a part of Cloud SDK.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum