Skip to content

Commit 6796105

Browse files
PF tests add default update and import to buildprovider (#2215)
This adds a few additions to the PF test utils: - Adds an option to buildprovider for tests to provide an import function - Adds `UseStateForUnknown` planmodifier to the id property in Create, as that seems to be used almost universally in pf providers in the `bridgedProvider` util - Adds a default Update implementation in the `bridgedProvider` util Stacked on #2186
1 parent 9878226 commit 6796105

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pf/tests/internal/providerbuilder/build_resource.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ type Resource struct {
2525
Name string
2626
ResourceSchema schema.Schema
2727

28-
CreateFunc func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
29-
ReadFunc func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
30-
UpdateFunc func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
31-
DeleteFunc func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
28+
CreateFunc func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
29+
ReadFunc func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
30+
UpdateFunc func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
31+
DeleteFunc func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
32+
ImportStateFunc func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse)
3233
}
3334

3435
func (r *Resource) Metadata(ctx context.Context, req resource.MetadataRequest, re *resource.MetadataResponse) {
@@ -67,4 +68,11 @@ func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp
6768
r.DeleteFunc(ctx, req, resp)
6869
}
6970

70-
var _ resource.Resource = &Resource{}
71+
func (r *Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
72+
if r.ImportStateFunc == nil {
73+
return
74+
}
75+
r.ImportStateFunc(ctx, req, resp)
76+
}
77+
78+
var _ resource.ResourceWithImportState = &Resource{}

pf/tests/util.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/hashicorp/terraform-plugin-framework/path"
2727
"github.com/hashicorp/terraform-plugin-framework/resource"
2828
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
29+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
30+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2931
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
3032
"github.com/stretchr/testify/require"
3133
"google.golang.org/grpc"
@@ -63,16 +65,25 @@ func ensureProviderValid(prov *providerbuilder.Provider) {
6365
for i := range prov.AllResources {
6466
r := &prov.AllResources[i]
6567
if r.ResourceSchema.Attributes["id"] == nil {
66-
r.ResourceSchema.Attributes["id"] = rschema.StringAttribute{Computed: true}
68+
r.ResourceSchema.Attributes["id"] = rschema.StringAttribute{
69+
Computed: true,
70+
PlanModifiers: []planmodifier.String{
71+
stringplanmodifier.UseStateForUnknown(),
72+
},
73+
}
6774
}
6875
if r.CreateFunc == nil {
6976
r.CreateFunc = func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
7077
resp.State = tfsdk.State(req.Config)
7178
resp.State.SetAttribute(ctx, path.Root("id"), "test-id")
7279
}
7380
}
81+
if r.UpdateFunc == nil {
82+
r.UpdateFunc = func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
83+
resp.State = tfsdk.State(req.Config)
84+
}
85+
}
7486
}
75-
7687
}
7788

7889
func bridgedProvider(prov *providerbuilder.Provider) info.Provider {

0 commit comments

Comments
 (0)