Skip to content

Utility to call Read() from the end of Create()/Update() #277

@magodo

Description

@magodo

Module version

v0.6.0

Use-cases

In plugin SDK, it is prevalent to call Read() at the end of Create()/Update() in the resource implementation. This is avoid code duplication for APIs that doesn't return the full model in its "create" response.

I'd like to continue this pattern in the framework.

Attempted Solutions

I manually convert to and from the tfsdk.ReadResource{Request|Response}, as below:

func (r resourceFoo) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
        //...
	rreq := tfsdk.ReadResourceRequest{
		State:        resp.State,
		ProviderMeta: req.ProviderMeta,
	}
	rresp := tfsdk.ReadResourceResponse{
		State:       resp.State,
		Diagnostics: resp.Diagnostics,
	}
	r.Read(ctx, rreq, &rresp)

	*resp = tfsdk.CreateResourceResponse{
		State:       rresp.State,
		Diagnostics: rresp.Diagnostics,
	}
}

Full example can be found here: https://github.com/magodo/terraform-provider-demo/blob/41121592396989284faad906d3943fcd084b4272/demo/resource_foo.go#L124-L137

Proposal

Apparently, above solution is not ideal. Propose there can be some builtin utilities to allow users to do that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions