这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix issue where updates to vercel_environment_variables was broke #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions vercel/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,19 @@ func (e *EnvironmentItem) equal(other *EnvironmentItem) bool {
e.Comment.ValueString() == other.Comment.ValueString()
}

func (e *EnvironmentItem) toAttrValue() attr.Value {
return types.ObjectValueMust(envVariableElemType.AttrTypes, map[string]attr.Value{
"id": e.ID,
"key": e.Key,
"value": e.Value,
"target": e.Target,
"custom_environment_ids": e.CustomEnvironmentIDs,
"git_branch": e.GitBranch,
"sensitive": e.Sensitive,
"comment": e.Comment,
})
}

func (e *EnvironmentItem) toEnvironmentVariableRequest(ctx context.Context) (req client.EnvironmentVariableRequest, diags diag.Diagnostics) {
var target []string
diags = e.Target.ElementsAs(ctx, &target, true)
Expand Down
46 changes: 24 additions & 22 deletions vercel/resource_project_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ func (e *ProjectEnvironmentVariables) toCreateEnvironmentVariablesRequest(ctx co
// convertResponseToProjectEnvironmentVariables is used to populate terraform state based on an API response.
// Where possible, values from the API response are used to populate state. If not possible,
// values from plan are used.
func convertResponseToProjectEnvironmentVariables(ctx context.Context, response []client.EnvironmentVariable, plan ProjectEnvironmentVariables) (ProjectEnvironmentVariables, diag.Diagnostics) {
func convertResponseToProjectEnvironmentVariables(
ctx context.Context,
response []client.EnvironmentVariable,
plan ProjectEnvironmentVariables,
unchanged []EnvironmentItem,
) (ProjectEnvironmentVariables, diag.Diagnostics) {
environment, diags := plan.environment(ctx)
if diags.HasError() {
return ProjectEnvironmentVariables{}, diags
Expand Down Expand Up @@ -344,20 +349,7 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
alreadyPresent[e.ID] = struct{}{}

env = append(env, types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"value": types.StringType,
"target": types.SetType{
ElemType: types.StringType,
},
"custom_environment_ids": types.SetType{
ElemType: types.StringType,
},
"git_branch": types.StringType,
"id": types.StringType,
"sensitive": types.BoolType,
"comment": types.StringType,
},
envVariableElemType.AttrTypes,
map[string]attr.Value{
"key": types.StringValue(e.Key),
"value": value,
Expand All @@ -371,6 +363,10 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
))
}

for _, e := range unchanged {
env = append(env, e.toAttrValue())
}

return ProjectEnvironmentVariables{
TeamID: toTeamID(plan.TeamID.ValueString()),
ProjectID: plan.ProjectID,
Expand Down Expand Up @@ -410,7 +406,7 @@ func (r *projectEnvironmentVariablesResource) Create(ctx context.Context, req re
)
}

result, diags := convertResponseToProjectEnvironmentVariables(ctx, created, plan)
result, diags := convertResponseToProjectEnvironmentVariables(ctx, created, plan, nil)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -475,7 +471,7 @@ func (r *projectEnvironmentVariablesResource) Read(ctx context.Context, req reso
}
}

result, diags := convertResponseToProjectEnvironmentVariables(ctx, toUse, state)
result, diags := convertResponseToProjectEnvironmentVariables(ctx, toUse, state, nil)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -530,6 +526,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
}

var toRemove []EnvironmentItem
var unchanged []EnvironmentItem
for _, e := range stateEnvs {
plannedEnv, ok := plannedEnvsByID[e.ID.ValueString()]
if !ok {
Expand All @@ -539,11 +536,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
if !plannedEnv.equal(&e) {
toRemove = append(toRemove, e)
toAdd = append(toAdd, plannedEnv)
continue
}
unchanged = append(unchanged, e)
}

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

for _, v := range toRemove {
err := r.client.DeleteEnvironmentVariable(ctx, state.ProjectID.ValueString(), state.TeamID.ValueString(), v.ID.ValueString())
Expand Down Expand Up @@ -574,7 +573,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
resp.Diagnostics.Append(diags...)
return
}
tflog.Debug(ctx, "create request", map[string]any{
tflog.Info(ctx, "create request", map[string]any{
"request": request,
})
response, err = r.client.CreateEnvironmentVariables(ctx, request)
Expand All @@ -585,10 +584,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
)
return
}
} else {
}

result, diags := convertResponseToProjectEnvironmentVariables(ctx, response, plan)
tflog.Info(ctx, "project env var response", map[string]any{
"response": response,
})

result, diags := convertResponseToProjectEnvironmentVariables(ctx, response, plan, unchanged)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down
14 changes: 12 additions & 2 deletions vercel/resource_project_environment_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func TestAcc_ProjectEnvironmentVariables(t *testing.T) {
{
Config: testAccProjectEnvironmentVariablesConfigUpdated(projectName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "variables.#", "3"),
resource.TestCheckResourceAttr(resourceName, "variables.#", "4"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "variables.*", map[string]string{
"key": "TEST_VAR_1",
"value": "test_value_1",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "variables.*", map[string]string{
"key": "TEST_VAR_2",
"value": "test_value_2_updated",
Expand Down Expand Up @@ -77,7 +81,8 @@ resource "vercel_project" "test" {
resource "vercel_project_environment_variables" "test" {
project_id = vercel_project.test.id
%[2]s
variables = [{
variables = [
{
key = "TEST_VAR_1"
value = "test_value_1"
target = ["production", "preview"]
Expand Down Expand Up @@ -109,6 +114,11 @@ resource "vercel_project_environment_variables" "test" {
project_id = vercel_project.test.id
%[2]s
variables = [
{
key = "TEST_VAR_1" // unchanged
value = "test_value_1"
target = ["production", "preview"]
},
{
key = "TEST_VAR_2"
value = "test_value_2_updated"
Expand Down