hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.35k stars 1.75k forks source link

Enable Service Usage API (serviceusage.googleapis.com) by default when creating new projects #14174

Open verbanicm opened 1 year ago

verbanicm commented 1 year ago

Community Note

Description

After creating a project no APIs will be enabled. If the user_project_override config is true and no billing_project is given, subsequent requests to list APIs via the serviceusage API will fail. This is caused by the list services request using the newly created project.ProjectId as the X-Goog-User-Project creating an impossible to resolve situation without manually enabling the serviceusage API.

By default the Google Cloud console enables a set of service that is too broad to enable all by default: https://cloud.google.com/service-usage/docs/enabled-service#default

For this use case it makes sense to always enable the Service Usage API as it is required to list and enable any other services after creating a project.

Terraform Version

v1.3.6

Affected Resource(s)

Terraform Configuration Files

This is modified from my configuration...

resource "google_project" "test" {
  folder_id  = google_folder.default.name
  project_id = "some-unique-project-id"

  name            = "some-unique-project-id"
}

resource "google_project_service" "test" {
  project = google_project.test.project_id

  service                    = "serviceusage.googleapis.com"
  disable_on_destroy         = false
  disable_dependent_services = false
}

terraform {
  required_version = ">= 1.0"

  required_providers {
    google = {
      version = ">= 4.45"
    }
  }
}

provider "google" {
  user_project_override = true
}

Debug Output

Error: Error when reading or editing Project Service : Request `List Project Services XXXXXXXXXX` returned error: Batch request 
  and retried single request "List Project Services XXXXXXXXXX" both failed. Final error: Failed to list enabled services for project
  XXXXXXXXXX: googleapi: Error 403: Service Usage API has not been used in project XXXXXXXXXX before or it is disabled. Enable it by
  visiting https://console.developers.google.com/apis/api/serviceusage.googleapis.com/overview?project=XXXXXXXXXX then retry. If you
  enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

Details:
[
  {
    "@type": "type.googleapis.com/google.rpc.Help",
    "links": [
      {
        "description": "Google developers console API activation",
        "url": "https://console.developers.google.com/apis/api/serviceusage.googleapis.com/overview?project=XXXXXXXXXX"
      }
    ]
  },
  {
    "@type": "type.googleapis.com/google.rpc.ErrorInfo",
    "domain": "googleapis.com",
    "metadata": {
      "consumer": "projects/XXXXXXXXXX",
      "service": "serviceusage.googleapis.com"
    },
    "reason": "SERVICE_DISABLED"
  }
]
, accessNotConfigured

Expected Behavior

Project is created and serviceusage.googleapis.com is enabled.

Actual Behavior

I get an error scenario where the service usage api can list itself on the project so it is unable to turn it on.

Steps to Reproduce

  1. terraform apply

Important Factoids

rileykarson commented 1 year ago

This is a known interaction, I'm not sure if we'd want to modify the google_project resource to enable services as proposed in https://github.com/hashicorp/terraform-provider-google/pull/14173- particularly in a minor release. Will respond in more depth when I have cycles, unblocking our release right now.

rileykarson commented 1 year ago

Alright, took me a bit longer to get back than expected. Sorry about that! Also fyi @c2thorn in case you have input.

There are numerous reasons we likely want to avoid making this change, in my opinion:

This is caused by the list services request using the newly created project.ProjectId as the X-Goog-User-Project creating an impossible to resolve situation without manually enabling the serviceusage API.

It's not impossible to resolve- this can be resolved by using a billing project or by using an aliased provider without UPO enabled.


I'll also note that the impact here is likely fairly small. It's relatively uncommon that a configuration both needs UPO set and is creating a project:

rileykarson commented 1 year ago

Note: We discussed this. We're somewhat undecided- this would help new users and users with complex ticket-based provisioning systems, but tainting the project in the case of a failure is a pretty rough outcome.

roaks3 commented 1 year ago

I'm not sure if this issue should be forwarded to the service team in its current state, so exempting it for now.

jean-mercier-hivebrite commented 8 months ago

Same problem i didn't get what is the workaround sorry ?

today we enable the API by hand everytime we need a new project but it's not something perfect ...

TakumiHaruta commented 3 weeks ago

Same here. Does anyone have the workaround for this issue? I believe it's almost synonymous with not being able to create and manage projects from Terraform if we can't solve this issue...