Skip to content

resourcemanager: data.google_project.project_id should be canonical ID, not number (#19732)#73

Open
jbbqqf wants to merge 11 commits into
mainfrom
feat/19732-data-google-project-id
Open

resourcemanager: data.google_project.project_id should be canonical ID, not number (#19732)#73
jbbqqf wants to merge 11 commits into
mainfrom
feat/19732-data-google-project-id

Conversation

@jbbqqf

@jbbqqf jbbqqf commented May 9, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes a bug where data.google_project.project_id is set to the project number instead of the canonical string project ID whenever the data source is queried by number (either via project_id = "<number>" or implicitly through provider.project = "<number>").

Fixes hashicorp/terraform-provider-google#19732 — see hashicorp/terraform-provider-google#19732

Why

The Cloud Resource Manager Project API resource has two distinct fields:

  • projectId — the unique string ID (e.g. gcp-masterclasses)
  • projectNumber — the numeric ID (e.g. 922462624752)

The data.google_project schema documents project_id as the string ID and exposes number separately. But resourceGoogleProjectRead (shared with the data source) sets project_id from pid, which is the trailing segment of d.Id(). When the data source is looked up by number, pid is the number, so project_id ends up being the number too — silently misleading anyone who uses the field downstream.

GCP API reference: https://cloud.google.com/resource-manager/reference/rest/v1/projects/get

What changed

This change is to a hand-written Terraform resource (not magic-modules-generated). Files touched in terraform-provider-google[-beta]:

google/services/resourcemanager/resource_google_project.go | 9 ++++++++-

Use the API-canonical Project.projectId from the response when populating state. Falls back to pid only if the API response is empty (defensive — the field is required by the API contract).

Edge cases tested

# Scenario HCL Expected Verified by
1 Data source called via provider.project = "<number>" data "google_project" "by_number" {} project_id = canonical string ID smoke + assert.sh (BEFORE fails, AFTER passes)
2 Data source called with project_id = "<number>" data "google_project" "n" { project_id = "922462624752" } project_id = canonical string ID smoke + assert.sh
3 Data source called with project_id = "<canonical_id>" data "google_project" "i" { project_id = "gcp-masterclasses" } project_id = same canonical string ID (sanity) smoke + assert.sh

Test protocol

Test Result Notes
go build ./... (full provider compile) pass tpg + tpgb
Live BEFORE smoke (binary built from origin/main) red (assert_failed) bug reproduced — project_id returned the number
Live AFTER smoke (binary built from this branch) green (ok) fix proven — project_id returned the canonical string ID
Destroy (both phases) clean data source only, nothing to leak

Verdict: 🟢 GREEN.

Disclosure

This PR was implemented with assistance from Claude Code as part of a focused contribution batch on hand-written resources. The diff was reviewed manually against the Cloud Resource Manager v1 API documentation linked above. The live before/after smoke harness exercised the data source against the author's sandbox project; both phases destroyed cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data google_project incorrectly sets project number as project id

8 participants