Skip to content

Commit

Permalink
fix: empty project_id in google_project data source
Browse files Browse the repository at this point in the history
A validation change hashicorp/terraform-provider-google#12846
was introduced in version `4.42.0` of the google provider. This
validation makes all our GCP modules to fail with:
```
│ Error: "" project_id must be 6 to 30 with lowercase letters, digits, hyphens and start with a letter. Trailing hyphens are prohibited.
│
│   with module.gcp_service_account.data.google_project.selected,
│   on .terraform/modules/gcp_service_account/main.tf line 96, in data "google_project" "selected":
│   96:   project_id = var.project_id
```

To solve this issue we are avoiding using the `google_project` data
source when we know the `project_id` that was provided by the user.

If the user does not provide a `project_id`, then we use the data
source to discover the project from the google provider.

Signed-off-by: Salim Afiune Maya <afiune@lacework.net>
  • Loading branch information
afiune committed Nov 15, 2022
1 parent 5008bad commit 58b7de5
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions main.tf
@@ -1,19 +1,17 @@
locals {
project_id = data.google_project.selected.project_id
project_id = length(var.project_id) > 0 ? var.project_id : data.google_project.selected.project_id
service_account_email = var.create ? (
length(google_service_account.lacework) > 0 ? google_service_account.lacework[0].email : ""
) : ""
service_account_name = length(var.service_account_name) > 0 ? (
var.service_account_name ) : "lwsvc-${random_id.uniq.hex}"
var.service_account_name) : "lwsvc-${random_id.uniq.hex}"
}

resource "random_id" "uniq" {
byte_length = 4
}

data "google_project" "selected" {
project_id = var.project_id
}
data "google_project" "selected" {}

resource "google_service_account" "lacework" {
count = var.create ? 1 : 0
Expand Down

0 comments on commit 58b7de5

Please sign in to comment.