Skip to content

Commit cc9251c

Browse files
authored
Fix shared-env validator (#381)
This validator is incorrectly not skipping validation for unknown values. This is a bit of a pain because calculated values are then subject to validation which can be incorrect. Instead, for unknown values rely on the API validating them.
1 parent de0eb4a commit cc9251c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

vercel/resource_shared_environment_variable.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,15 @@ func (v *sharedEnvTargetValidator) ValidateResource(ctx context.Context, req res
193193
if resp.Diagnostics.HasError() {
194194
return
195195
}
196+
197+
// If apply_to_all_custom_environments is unknown (computed), skip validation
198+
// since we can't determine the configuration's validity during planning.
199+
if sev.ApplyToAllCustomEnvironments.IsUnknown() {
200+
return
201+
}
202+
196203
// If apply_to_all_custom_environments is explicitly true, allow target to be omitted or empty.
197-
if !sev.ApplyToAllCustomEnvironments.IsNull() && !sev.ApplyToAllCustomEnvironments.IsUnknown() && sev.ApplyToAllCustomEnvironments.ValueBool() {
204+
if !sev.ApplyToAllCustomEnvironments.IsNull() && sev.ApplyToAllCustomEnvironments.ValueBool() {
198205
return
199206
}
200207
// Otherwise, target must be provided with at least one element.

vercel/resource_shared_environment_variable_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,47 @@ func testAccCheckSetEmpty(n, attr string) resource.TestCheckFunc {
279279
return nil
280280
}
281281
}
282+
283+
func TestAcc_SharedEnvironmentVariables_ComputedCustomOnly(t *testing.T) {
284+
nameSuffix := acctest.RandString(16)
285+
resource.Test(t, resource.TestCase{
286+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
287+
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
288+
testAccProjectDestroy(testClient(t), "vercel_project.example", testTeam(t)),
289+
),
290+
Steps: []resource.TestStep{
291+
{
292+
Config: cfg(testAccSharedEnvironmentVariablesComputedCustomOnlyConfig(nameSuffix)),
293+
Check: resource.ComposeAggregateTestCheckFunc(
294+
testAccSharedEnvironmentVariableExists(testClient(t), "vercel_shared_environment_variable.computed_custom", testTeam(t)),
295+
resource.TestCheckResourceAttr("vercel_shared_environment_variable.computed_custom", "apply_to_all_custom_environments", "true"),
296+
testAccCheckSetEmpty("vercel_shared_environment_variable.computed_custom", "target.#"),
297+
),
298+
},
299+
},
300+
})
301+
}
302+
303+
func testAccSharedEnvironmentVariablesComputedCustomOnlyConfig(projectName string) string {
304+
return fmt.Sprintf(`
305+
locals {
306+
env_config = {
307+
key = "test_acc_computed_custom_%[1]s"
308+
value = "bar"
309+
target = ["sandbox"]
310+
}
311+
}
312+
313+
resource "vercel_project" "example" {
314+
name = "test-acc-example-project-%[1]s"
315+
}
316+
317+
resource "vercel_shared_environment_variable" "computed_custom" {
318+
key = local.env_config.key
319+
value = local.env_config.value
320+
target = contains(local.env_config.target, "sandbox") ? tolist(setsubtract(toset(local.env_config.target), toset(["sandbox"]))) : local.env_config.target
321+
apply_to_all_custom_environments = contains(local.env_config.target, "sandbox")
322+
project_ids = [vercel_project.example.id]
323+
}
324+
`, projectName)
325+
}

0 commit comments

Comments
 (0)