Skip to content

Commit d074622

Browse files
authored
Fix issue where updates to vercel_environment_variables was broke (#276)
Turns out it missed setting state at the end for the env vars that were not being updated.
1 parent bf5a322 commit d074622

3 files changed

+49
-24
lines changed

vercel/resource_project.go

+13
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,19 @@ func (e *EnvironmentItem) equal(other *EnvironmentItem) bool {
806806
e.Comment.ValueString() == other.Comment.ValueString()
807807
}
808808

809+
func (e *EnvironmentItem) toAttrValue() attr.Value {
810+
return types.ObjectValueMust(envVariableElemType.AttrTypes, map[string]attr.Value{
811+
"id": e.ID,
812+
"key": e.Key,
813+
"value": e.Value,
814+
"target": e.Target,
815+
"custom_environment_ids": e.CustomEnvironmentIDs,
816+
"git_branch": e.GitBranch,
817+
"sensitive": e.Sensitive,
818+
"comment": e.Comment,
819+
})
820+
}
821+
809822
func (e *EnvironmentItem) toEnvironmentVariableRequest(ctx context.Context) (req client.EnvironmentVariableRequest, diags diag.Diagnostics) {
810823
var target []string
811824
diags = e.Target.ElementsAs(ctx, &target, true)

vercel/resource_project_environment_variables.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@ func (e *ProjectEnvironmentVariables) toCreateEnvironmentVariablesRequest(ctx co
284284
// convertResponseToProjectEnvironmentVariables is used to populate terraform state based on an API response.
285285
// Where possible, values from the API response are used to populate state. If not possible,
286286
// values from plan are used.
287-
func convertResponseToProjectEnvironmentVariables(ctx context.Context, response []client.EnvironmentVariable, plan ProjectEnvironmentVariables) (ProjectEnvironmentVariables, diag.Diagnostics) {
287+
func convertResponseToProjectEnvironmentVariables(
288+
ctx context.Context,
289+
response []client.EnvironmentVariable,
290+
plan ProjectEnvironmentVariables,
291+
unchanged []EnvironmentItem,
292+
) (ProjectEnvironmentVariables, diag.Diagnostics) {
288293
environment, diags := plan.environment(ctx)
289294
if diags.HasError() {
290295
return ProjectEnvironmentVariables{}, diags
@@ -344,20 +349,7 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
344349
alreadyPresent[e.ID] = struct{}{}
345350

346351
env = append(env, types.ObjectValueMust(
347-
map[string]attr.Type{
348-
"key": types.StringType,
349-
"value": types.StringType,
350-
"target": types.SetType{
351-
ElemType: types.StringType,
352-
},
353-
"custom_environment_ids": types.SetType{
354-
ElemType: types.StringType,
355-
},
356-
"git_branch": types.StringType,
357-
"id": types.StringType,
358-
"sensitive": types.BoolType,
359-
"comment": types.StringType,
360-
},
352+
envVariableElemType.AttrTypes,
361353
map[string]attr.Value{
362354
"key": types.StringValue(e.Key),
363355
"value": value,
@@ -371,6 +363,10 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
371363
))
372364
}
373365

366+
for _, e := range unchanged {
367+
env = append(env, e.toAttrValue())
368+
}
369+
374370
return ProjectEnvironmentVariables{
375371
TeamID: toTeamID(plan.TeamID.ValueString()),
376372
ProjectID: plan.ProjectID,
@@ -410,7 +406,7 @@ func (r *projectEnvironmentVariablesResource) Create(ctx context.Context, req re
410406
)
411407
}
412408

413-
result, diags := convertResponseToProjectEnvironmentVariables(ctx, created, plan)
409+
result, diags := convertResponseToProjectEnvironmentVariables(ctx, created, plan, nil)
414410
if diags.HasError() {
415411
resp.Diagnostics.Append(diags...)
416412
return
@@ -475,7 +471,7 @@ func (r *projectEnvironmentVariablesResource) Read(ctx context.Context, req reso
475471
}
476472
}
477473

478-
result, diags := convertResponseToProjectEnvironmentVariables(ctx, toUse, state)
474+
result, diags := convertResponseToProjectEnvironmentVariables(ctx, toUse, state, nil)
479475
if diags.HasError() {
480476
resp.Diagnostics.Append(diags...)
481477
return
@@ -530,6 +526,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
530526
}
531527

532528
var toRemove []EnvironmentItem
529+
var unchanged []EnvironmentItem
533530
for _, e := range stateEnvs {
534531
plannedEnv, ok := plannedEnvsByID[e.ID.ValueString()]
535532
if !ok {
@@ -539,11 +536,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
539536
if !plannedEnv.equal(&e) {
540537
toRemove = append(toRemove, e)
541538
toAdd = append(toAdd, plannedEnv)
539+
continue
542540
}
541+
unchanged = append(unchanged, e)
543542
}
544543

545-
tflog.Debug(ctx, "Removing environment variables", map[string]interface{}{"to_remove": toRemove})
546-
tflog.Debug(ctx, "Adding environment variables", map[string]interface{}{"to_add": toAdd})
544+
tflog.Info(ctx, "Removing environment variables", map[string]interface{}{"to_remove": toRemove})
545+
tflog.Info(ctx, "Adding environment variables", map[string]interface{}{"to_add": toAdd})
547546

548547
for _, v := range toRemove {
549548
err := r.client.DeleteEnvironmentVariable(ctx, state.ProjectID.ValueString(), state.TeamID.ValueString(), v.ID.ValueString())
@@ -574,7 +573,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
574573
resp.Diagnostics.Append(diags...)
575574
return
576575
}
577-
tflog.Debug(ctx, "create request", map[string]any{
576+
tflog.Info(ctx, "create request", map[string]any{
578577
"request": request,
579578
})
580579
response, err = r.client.CreateEnvironmentVariables(ctx, request)
@@ -585,10 +584,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
585584
)
586585
return
587586
}
588-
} else {
589587
}
590588

591-
result, diags := convertResponseToProjectEnvironmentVariables(ctx, response, plan)
589+
tflog.Info(ctx, "project env var response", map[string]any{
590+
"response": response,
591+
})
592+
593+
result, diags := convertResponseToProjectEnvironmentVariables(ctx, response, plan, unchanged)
592594
if diags.HasError() {
593595
resp.Diagnostics.Append(diags...)
594596
return

vercel/resource_project_environment_variables_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func TestAcc_ProjectEnvironmentVariables(t *testing.T) {
3939
{
4040
Config: testAccProjectEnvironmentVariablesConfigUpdated(projectName),
4141
Check: resource.ComposeTestCheckFunc(
42-
resource.TestCheckResourceAttr(resourceName, "variables.#", "3"),
42+
resource.TestCheckResourceAttr(resourceName, "variables.#", "4"),
43+
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "variables.*", map[string]string{
44+
"key": "TEST_VAR_1",
45+
"value": "test_value_1",
46+
}),
4347
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "variables.*", map[string]string{
4448
"key": "TEST_VAR_2",
4549
"value": "test_value_2_updated",
@@ -77,7 +81,8 @@ resource "vercel_project" "test" {
7781
resource "vercel_project_environment_variables" "test" {
7882
project_id = vercel_project.test.id
7983
%[2]s
80-
variables = [{
84+
variables = [
85+
{
8186
key = "TEST_VAR_1"
8287
value = "test_value_1"
8388
target = ["production", "preview"]
@@ -109,6 +114,11 @@ resource "vercel_project_environment_variables" "test" {
109114
project_id = vercel_project.test.id
110115
%[2]s
111116
variables = [
117+
{
118+
key = "TEST_VAR_1" // unchanged
119+
value = "test_value_1"
120+
target = ["production", "preview"]
121+
},
112122
{
113123
key = "TEST_VAR_2"
114124
value = "test_value_2_updated"

0 commit comments

Comments
 (0)