-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.go
75 lines (58 loc) · 2.31 KB
/
framework.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package acctest
import (
"context"
"fmt"
"log"
"strings"
"github.com/cloudbuzzbyone/terraform-provider-appflow/internal/errs/fwdiag"
"github.com/hashicorp/terraform-plugin-framework/path"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
// Terraform Plugin Framework variants of standard acceptance test helpers.
func deleteFrameworkResource(ctx context.Context, factory func(context.Context) (fwresource.ResourceWithConfigure, error), is *terraform.InstanceState, meta interface{}) error {
resource, err := factory(ctx)
if err != nil {
return err
}
resource.Configure(ctx, fwresource.ConfigureRequest{ProviderData: meta}, &fwresource.ConfigureResponse{})
schemaResp := fwresource.SchemaResponse{}
resource.Schema(ctx, fwresource.SchemaRequest{}, &schemaResp)
// Construct a simple Framework State that contains just top-level attributes.
state := tfsdk.State{
Raw: tftypes.NewValue(schemaResp.Schema.Type().TerraformType(ctx), nil),
Schema: schemaResp.Schema,
}
for name, v := range is.Attributes {
if name == "%" || strings.Contains(name, ".") {
continue
}
if err := fwdiag.DiagnosticsError(state.SetAttribute(ctx, path.Root(name), v)); err != nil {
log.Printf("[WARN] %s(%s): %s", name, v, err)
}
}
response := fwresource.DeleteResponse{}
resource.Delete(ctx, fwresource.DeleteRequest{State: state}, &response)
if response.Diagnostics.HasError() {
return fwdiag.DiagnosticsError(response.Diagnostics)
}
return nil
}
func CheckFrameworkResourceDisappears(ctx context.Context, provo *schema.Provider, factory func(context.Context) (fwresource.ResourceWithConfigure, error), n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("resource not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource ID missing: %s", n)
}
return deleteFrameworkResource(ctx, factory, rs.Primary, provo.Meta())
}
}