1

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.

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
  • 1
    You need to enable `serviceusage.googleapis.com`. What is the problem? Stack Overflow is not the place for discussing a vendor's policies or service requirements. Translate your problem into a programming question. – John Hanley Jul 08 '21 at 20:27
  • I'm asking a programming question in the Google Cloud collective. The question relates to how to programmatically enable Service Usage API without touching Cloud Console, using just public APIs. There's nothing about policies or service requirements. – Mariano Italiano Jul 08 '21 at 20:52
  • 1
    What problem are you having enabling the API? Your question includes the words `I find it suspicious` which asks for off-topic opinions. Rewrite your question to clearly state the problem, including the code you wrote and the error message your code generated. – John Hanley Jul 08 '21 at 20:55
  • Some APIs are enabled by the default, serviceusage.googleapis.com isn't one of them regardless of whether you find it suspicious or not :) – jabbson Jul 08 '21 at 21:00
  • @jabbson as you can see here https://cloud.google.com/service-usage/docs/enabled-service#default Service Usage API is listed as one of the default services. They also mention that this applies to SDKs, so maybe I'm missing something in this puzzle. – Mariano Italiano Jul 08 '21 at 21:38
  • @MarianoItaliano hi OP. I updated my answer. Let us know if it helped you. – Donnald Cucharo Jul 13 '21 at 06:38

2 Answers2

2

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.

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
  • Client Libraries are the [part](https://cloud.google.com/sdk#key-features) of Cloud SDK. – jabbson Jul 09 '21 at 05:12
  • @jabbson - For new users, the usage of the term **Cloud SDK** is confusing because it is used in several contexts. In some places it means the **CLI**, in other places, it means **Client Library/SDK**. – John Hanley Jul 10 '21 at 18:13
  • I ended up with the gcloud command. The solution is less elegant but does the job. For some unknown reason, I wasn't able to make it work. – Mariano Italiano Jul 15 '21 at 09:11
1

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.

jabbson
  • 4,390
  • 1
  • 13
  • 23
  • I ran the same code, but it still fails during enabling services: `Service Usage API has not been used in project` – Mariano Italiano Jul 09 '21 at 11:50
  • @MarianoItaliano out of curiosity, can you try to do the same with gcloud command and see whether it will let you and if not what the error would be? – jabbson Jul 09 '21 at 14:29
  • Surprisingly gcloud command works perfectly, more precisely creates a project and enables services by default. Unfortunately, I can't use gloud in my case, I need the API. – Mariano Italiano Jul 09 '21 at 17:40