Skip to content

Commit

Permalink
PF tests add default update and import to buildprovider (#2215)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
VenelinMartinov authored Jul 23, 2024
1 parent 9878226 commit 6796105
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 13 additions & 5 deletions pf/tests/internal/providerbuilder/build_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ type Resource struct {
Name string
ResourceSchema schema.Schema

CreateFunc func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
ReadFunc func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
UpdateFunc func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
DeleteFunc func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
CreateFunc func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
ReadFunc func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
UpdateFunc func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
DeleteFunc func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
ImportStateFunc func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse)
}

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

var _ resource.Resource = &Resource{}
func (r *Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
if r.ImportStateFunc == nil {
return
}
r.ImportStateFunc(ctx, req, resp)
}

var _ resource.ResourceWithImportState = &Resource{}
15 changes: 13 additions & 2 deletions pf/tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand Down Expand Up @@ -63,16 +65,25 @@ func ensureProviderValid(prov *providerbuilder.Provider) {
for i := range prov.AllResources {
r := &prov.AllResources[i]
if r.ResourceSchema.Attributes["id"] == nil {
r.ResourceSchema.Attributes["id"] = rschema.StringAttribute{Computed: true}
r.ResourceSchema.Attributes["id"] = rschema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
}
}
if r.CreateFunc == nil {
r.CreateFunc = func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
resp.State = tfsdk.State(req.Config)
resp.State.SetAttribute(ctx, path.Root("id"), "test-id")
}
}
if r.UpdateFunc == nil {
r.UpdateFunc = func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.State = tfsdk.State(req.Config)
}
}
}

}

func bridgedProvider(prov *providerbuilder.Provider) info.Provider {
Expand Down

0 comments on commit 6796105

Please sign in to comment.