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.25k stars 1.7k forks source link

Create data source `google_pubsub_project_service_account` #12921

Closed joe-a-t closed 1 day ago

joe-a-t commented 1 year ago

Community Note

Description

In the same way that https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/storage_project_service_account exists for the magic storage service account, an equivalent data source should exist for the pubsub service account. This would be helpful so that we don't have to construct it ourselves for setting up dead letter queues and granting the relevant permissions associated with that process.

New or Affected Resource(s)

Potential Terraform Configuration

data "google_pubsub_project_service_account" "gsa" {}

References

rileykarson commented 1 year ago

Does https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service_identity work? It's a generic resource that works across most APIs, and obsoletes the need for service-specific methods.

joe-a-t commented 1 year ago

Hey @rileykarson,

Unfortunately https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service_identity does not work for us since the email value is not known until apply with it being a resource where email is an attribute instead of a data source. If https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service_identity was instead a data source that functioned like https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/storage_project_service_account and others, that could work

rileykarson commented 1 year ago

Sorry for the delay in getting back here! What changes when you don't know the email between plan and apply time, sorry? This resource does make a mutate call to its API, as not all APIs provision the service account prior to use.

joe-a-t commented 1 year ago

The issue is related to doing a for_each over a list of GCP IAM members (or a map where the members are the keys). In a case like project_service_identity where the value is not known until apply, it means that the output then cannot be used as a key in a for_each or else you will get the dreaded plan failure since all of the keys of a for_each must be known during the plan phase.

As a result, right now we would have to either 1) order the addition of the project_service_identity in a separate apply before adding the IAM permissions (which has a bunch of downsides in any re-creation scenario like disaster recovery) or 2) hardcode the GSA/construct it with already known values and hope the naming convention never changes.

If you had some generic project_service_identity data source that took a service = "pubsub" or something argument, that could potentially work if it provides the GSA's identity on plan phase.

LordMike commented 1 year ago

Just chiming in that I didn't know about google_project_service_identity and it resolved my issue. I needed exactly to find the pubsub service account, and I managed to do this with the below. I'm adding it for future searchers who might not know what the service should be.

resource "google_project_service_identity" "pubsub_sa" {
  provider = google-beta

  project = data.google_client_config.current.project
  service = "pubsub.googleapis.com"
}
rileykarson commented 1 day ago

Closing in favour of service identity- there's no API method to get this under https://cloud.google.com/pubsub/docs/reference/rest