Skip to content

Commit

Permalink
Fix: destroy already destroyed workflow environment fails with error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Sep 4, 2023
1 parent d7b2e68 commit 3265e71
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ func (e *FailedResponseError) Error() string {
func (e *FailedResponseError) NotFound() bool {
return e.res.StatusCode() == 404
}

func (e *FailedResponseError) BadRequest() bool {
return e.res.StatusCode() == 400
}
5 changes: 5 additions & 0 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -756,6 +757,10 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta

_, err := apiClient.EnvironmentDestroy(d.Id())
if err != nil {
if frerr, ok := err.(*http.FailedResponseError); ok && frerr.BadRequest() {
tflog.Warn(ctx, "Could not delete environment. Already deleted?", map[string]interface{}{"id": d.Id(), "error": frerr.Error()})
return nil
}
return diag.Errorf("could not delete environment: %v", err)
}
return nil
Expand Down
23 changes: 23 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -1619,6 +1620,28 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
}

t.Run("Failure in delete", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: createEnvironmentResourceConfig(environment),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(1).Return(template, nil)
mock.EXPECT().EnvironmentCreate(gomock.Any()).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentDeploy(updatedEnvironment.Id, client.DeployRequest{
BlueprintId: updatedEnvironment.LatestDeploymentLog.BlueprintId,
BlueprintRevision: updatedEnvironment.LatestDeploymentLog.BlueprintRevision,
}).Times(1).Return(client.EnvironmentDeployResponse{}, errors.New("error"))
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil)
mock.EXPECT().Environment(gomock.Any()).Times(2).Return(environment, nil)
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(environment, http.NewMockFailedResponseError(400))
})
})

testSuccess()
testTTL()
testTriggers()
Expand Down

0 comments on commit 3265e71

Please sign in to comment.