Fix: `gitlab_project_hook` update fails when computed identity attributes are unknown in plan
Fix: gitlab_project_hook update fails when computed identity attributes are unknown in plan
Problem
When performing an in-place update of a gitlab_project_hook resource (e.g. toggling an
event flag), Terraform would error with:
Error reading GitLab project hook: ... invalid id ""This happened because the Update function read the resource ID from the plan rather
than from state. The id attribute had no plan modifier, so it was unknown in the plan
during an update cycle — data.ID.ValueString() therefore returned "", and the ID parse
failed before any API call was made.
A secondary symptom was that the id attribute showed as (known after apply) in every
update plan, producing noisy diffs even when nothing identity-related had changed.
Changes
internal/provider/resource_gitlab_project_hook.go
1. Add UseStateForUnknown() to id (bug fix)
Added stringplanmodifier.UseStateForUnknown() to the id schema attribute. This tells
the framework to carry the known state value forward into the plan for update operations,
so data.ID.ValueString() returns the correct value in Update and the ID parse succeeds.
2. Add RequiresReplace() to project
Added stringplanmodifier.RequiresReplace() to the project attribute so that changing
the project a hook belongs to triggers a destroy+recreate, rather than an unsupported
in-place move.
3. Remove RequiresReplace() from project_id
project_id is a read-only computed attribute — it cannot be set by the user and will
never trigger a replace. The replace behaviour is now correctly expressed on the project
attribute instead.
4. Add UseStateForUnknown() to token (bug fix)
Added stringplanmodifier.UseStateForUnknown() to the token schema attribute. This tells
the framework to carry the known state value forward into the plan for update operations,
so data.Token.ValueString() returns the correct value in Update.
5. Fix copy-paste error message in Update
// Before
resp.Diagnostics.AddError("Error creating GitLab project hook", err.Error())
// After
resp.Diagnostics.AddError("Error updating GitLab project hook", err.Error())Testing
# Acceptance tests (requires GITLAB_TOKEN and GITLAB_BASE_URL)
make testacc RUN=TestAccGitlabProjectHook
TF_ACC=1 GITLAB_TOKEN=glpat-**** GITLAB_BASE_URL=http://127.0.0.1:8085/api/v4 GITLAB_EARLY_AUTH_CHECK=false go test --tags acceptance -v ./internal/provider/... -test.run TestAccGitlabProjectHook -timeout 120m
=== RUN TestAccGitlabProjectHook_basic
=== PAUSE TestAccGitlabProjectHook_basic
=== RUN TestAccGitlabProjectHook_customTemplate
=== PAUSE TestAccGitlabProjectHook_customTemplate
=== RUN TestAccGitlabProjectHook_customHeaders
resource_gitlab_project_hook_test.go:239: Test is skipped for CE (non-Enterprise) version of GitLab
--- SKIP: TestAccGitlabProjectHook_customHeaders (2.51s)
=== RUN TestAccGitlabProjectHook_updateProject
=== PAUSE TestAccGitlabProjectHook_updateProject
=== RUN TestAccGitlabProjectHook_importThenUpdate
=== PAUSE TestAccGitlabProjectHook_importThenUpdate
=== RUN TestAccGitlabProjectHook_migrateFromSDKToFramework
=== PAUSE TestAccGitlabProjectHook_migrateFromSDKToFramework
=== RUN TestAccGitlabProjectHook_validations
resource_gitlab_project_hook_test.go:464: Test is skipped for CE (non-Enterprise) version of GitLab
--- SKIP: TestAccGitlabProjectHook_validations (0.00s)
=== CONT TestAccGitlabProjectHook_basic
=== CONT TestAccGitlabProjectHook_importThenUpdate
=== CONT TestAccGitlabProjectHook_updateProject
=== CONT TestAccGitlabProjectHook_migrateFromSDKToFramework
=== CONT TestAccGitlabProjectHook_customTemplate
--- PASS: TestAccGitlabProjectHook_customTemplate (18.09s)
--- PASS: TestAccGitlabProjectHook_importThenUpdate (20.38s)
--- PASS: TestAccGitlabProjectHook_updateProject (29.38s)
--- PASS: TestAccGitlabProjectHook_migrateFromSDKToFramework (24.82s)
--- PASS: TestAccGitlabProjectHook_basic (36.68s)
PASS
ok gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider 54.803s
testing: warning: no tests to run
PASS
ok gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/api (cached) [no tests to run]
testing: warning: no tests to run
PASS
ok gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/sdk (cached) [no tests to run]
? gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/testutil [no test files]
? gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/testutil/framework [no test files]
testing: warning: no tests to run
PASS
ok gitlab.com/gitlab-org/terraform-provider-gitlab/internal/provider/utils (cached) [no tests to run]
Fixes #6807 (closed)