forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tfresource | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
multierror "github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Delete calls the specified resource's delete handler. | ||
func Delete(resource *schema.Resource, d *schema.ResourceData, meta interface{}) error { | ||
if resource.DeleteContext != nil || resource.DeleteWithoutTimeout != nil { | ||
var diags diag.Diagnostics | ||
var err *multierror.Error | ||
|
||
if resource.DeleteContext != nil { | ||
diags = resource.DeleteContext(context.Background(), d, meta) | ||
} else { | ||
diags = resource.DeleteWithoutTimeout(context.Background(), d, meta) | ||
} | ||
|
||
for i := range diags { | ||
if diags[i].Severity == diag.Error { | ||
err = multierror.Append(err, errors.New(diags[i].Summary)) | ||
} | ||
} | ||
|
||
return err.ErrorOrNil() | ||
} | ||
|
||
return resource.Delete(d, meta) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tfresource_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" | ||
) | ||
|
||
func TestDeleteNoError(t *testing.T) { | ||
theError := errors.New("fail") | ||
|
||
testCases := []struct { | ||
Name string | ||
Resource *schema.Resource | ||
ExpectError bool | ||
}{ | ||
{ | ||
Name: "no error Delete function", | ||
Resource: &schema.Resource{ | ||
Delete: func(rd *schema.ResourceData, i interface{}) error { | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "no error DeleteContext function", | ||
Resource: &schema.Resource{ | ||
DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "no error DeleteWithoutTimeout function", | ||
Resource: &schema.Resource{ | ||
DeleteWithoutTimeout: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "error Delete function", | ||
Resource: &schema.Resource{ | ||
Delete: func(rd *schema.ResourceData, i interface{}) error { | ||
return theError | ||
}, | ||
}, | ||
ExpectError: true, | ||
}, | ||
{ | ||
Name: "error DeleteContext function", | ||
Resource: &schema.Resource{ | ||
DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return diag.FromErr(theError) | ||
}, | ||
}, | ||
ExpectError: true, | ||
}, | ||
{ | ||
Name: "error DeleteWithoutTimeout function", | ||
Resource: &schema.Resource{ | ||
DeleteWithoutTimeout: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
return diag.FromErr(theError) | ||
}, | ||
}, | ||
ExpectError: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
r := testCase.Resource | ||
d := r.Data(nil) | ||
|
||
err := tfresource.Delete(r, d, nil) | ||
|
||
if testCase.ExpectError && err == nil { | ||
t.Fatal("expected error") | ||
} else if !testCase.ExpectError && err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters