diff --git a/env0/resource_configuration_variable.go b/env0/resource_configuration_variable.go index 38c162b0..4c3ce016 100644 --- a/env0/resource_configuration_variable.go +++ b/env0/resource_configuration_variable.go @@ -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" ) @@ -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) } diff --git a/env0/resource_configuration_variable_test.go b/env0/resource_configuration_variable_test.go index 0a65938d..95a8ab57 100644 --- a/env0/resource_configuration_variable_test.go +++ b/env0/resource_configuration_variable_test.go @@ -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" ) @@ -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