Skip to content

Commit

Permalink
Fix: handle drift in configuration variables (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Mar 27, 2022
1 parent 75c487f commit 22b50a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -199,6 +201,13 @@ func resourceConfigurationVariableRead(ctx context.Context, d *schema.ResourceDa
variable, err := apiClient.ConfigurationVariablesById(id)

if err != nil {
if frerr, ok := err.(*http.FailedResponseError); ok {
if frerr.NotFound() {
log.Printf("[WARN] Drift Detected: Terraform will remove %s from state", d.Id())
d.SetId("")
return nil
}
}
return diag.Errorf("could not get configurationVariable: %v", err)
}

Expand Down
25 changes: 25 additions & 0 deletions env0/resource_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"text/template"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/golang/mock/gomock"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
Expand Down Expand Up @@ -374,6 +375,30 @@ resource "%s" "test" {
})
})

t.Run("Configuration Removed in UI", func(t *testing.T) {
createTestCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: stepConfig,
},
{
Config: stepConfig,
},
},
}

runUnitTest(t, createTestCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().ConfigurationVariableCreate(configurationVariableCreateParams).Times(1).Return(configVar, nil),
mock.EXPECT().ConfigurationVariablesById(configVar.Id).Times(1).Return(configVar, nil),
mock.EXPECT().ConfigurationVariablesById(configVar.Id).Times(1).Return(client.ConfigurationVariable{}, http.NewMockFailedResponseError(404)),
mock.EXPECT().ConfigurationVariableCreate(configurationVariableCreateParams).Times(1).Return(configVar, nil),
mock.EXPECT().ConfigurationVariablesById(configVar.Id).Times(1).Return(configVar, nil),
mock.EXPECT().ConfigurationVariableDelete(configVar.Id).Times(1).Return(nil),
)
})
})

t.Run("Update", func(t *testing.T) {
newIsReadonly := false
newIsRequired := true
Expand Down

0 comments on commit 22b50a3

Please sign in to comment.