From 0c906ea9d129e3a21252d4a1f847a6ef2c5da7b9 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Mon, 29 Jul 2024 09:30:17 -0500 Subject: [PATCH] Fix: Drift Notification for Variable Sets (due to ordering) in env0_environment (#910) --- env0/resource_environment.go | 29 ++++++++++++++++++++++++++++- env0/resource_environment_test.go | 16 ++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/env0/resource_environment.go b/env0/resource_environment.go index 71cc9ccf..f7f8dfb1 100644 --- a/env0/resource_environment.go +++ b/env0/resource_environment.go @@ -446,7 +446,34 @@ func setEnvironmentSchema(ctx context.Context, d *schema.ResourceData, environme setEnvironmentConfigurationSchema(ctx, d, configurationVariables) if d.Get("variable_sets") != nil { - if err := d.Set("variable_sets", variableSetsIds); err != nil { + // To avoid drifts keep the schema order as much as possible. + variableSetsFromSchema := getEnvironmentVariableSetIdsFromSchema(d) + sortedVariablesSet := []string{} + + for _, schemav := range variableSetsFromSchema { + for _, newv := range variableSetsIds { + if schemav == newv { + sortedVariablesSet = append(sortedVariablesSet, schemav) + break + } + } + } + + for _, newv := range variableSetsIds { + found := false + for _, sortedv := range sortedVariablesSet { + if newv == sortedv { + found = true + break + } + } + + if !found { + sortedVariablesSet = append(sortedVariablesSet, newv) + } + } + + if err := d.Set("variable_sets", sortedVariablesSet); err != nil { return fmt.Errorf("failed to set variable_sets value: %w", err) } } diff --git a/env0/resource_environment_test.go b/env0/resource_environment_test.go index 13730cc5..7bd060d9 100644 --- a/env0/resource_environment_test.go +++ b/env0/resource_environment_test.go @@ -371,6 +371,18 @@ func TestUnitEnvironmentResource(t *testing.T) { } updatedConfigurationSets := []client.ConfigurationSet{ + { + Id: "id2", + AssignmentScope: "ENVIRONMENT", + }, + { + Id: "id3", + AssignmentScope: "ENVIRONMENT", + }, + } + + // Same as updatedConfigurationSets, but in a different order. + updatedConfigurationSets2 := []client.ConfigurationSet{ { Id: "id3", AssignmentScope: "ENVIRONMENT", @@ -398,7 +410,7 @@ func TestUnitEnvironmentResource(t *testing.T) { environmentDeployRequest := client.DeployRequest{ BlueprintId: environment.LatestDeploymentLog.BlueprintId, ConfigurationSetChanges: &client.ConfigurationSetChanges{ - Assign: []string{updatedConfigurationSets[0].Id}, + Assign: []string{updatedConfigurationSets[1].Id}, Unassign: []string{configurationSets[0].Id}, }, } @@ -465,7 +477,7 @@ func TestUnitEnvironmentResource(t *testing.T) { mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil), mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil), - mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(updatedConfigurationSets, nil), + mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(updatedConfigurationSets2, nil), mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1), )