Skip to content

feat: convert datasource to resource #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 7, 2024
Merged

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Aug 6, 2024

This avoids us having to re-read the cached image every time we plan.
It does result in additional complexity around detecting changes.
I still need to add modifiers to the schema to ensure that when e.g. the git URL is changed or the latest commit changes, we force re-creation of the resource.

@johnstcn johnstcn self-assigned this Aug 6, 2024
tflog.Debug(ctx, "Remote image does not exist", map[string]any{"ref": data.Image.ValueString()})
// Image does not exist any longer! Remove the resource so we can re-create
// it next time.
resp.State.RemoveResource(ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have your resource type implement resource.ResourceWithModifyPlan, you could have both of these checks that potentially remove the resource be done in a plan modifier instead, where the planmodifier.*Response has a RequiresReplace field you can set to true, which would mean the resource can just go Plan Modification -> Delete -> Create, skipping the Read.

If there's any benefits to this compared to doing it during the Read, I actually don't know, but it's certainly the recommended way to replace resources when updates aren't supported.

https://developer.hashicorp.com/terraform/plugin/framework/resources/plan-modification

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to implement the resource.WithModifyPlan interface if you just use attribute plan modifiers. This appears to suffice for this use-case for now.

tpl := `provider envbuilder {}
resource "envbuilder_cached_image" "test" {
builder_image = {{ quote .BuilderImage }}
cache_repo = {{ quote .CacheRepo }}
Copy link
Member

@ethanndickson ethanndickson Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the coderd provider we test omitting fields by storing *T in the config, and having a FuncMap function just replace nil with null. Worth considering if you want to test that going ahead.

Colin wrote this function, which has been really useful, also handles quoting: https://github.com/coder/terraform-provider-coderd/blob/8af42aa587afd66fd838e36f2181b5f23909e075/internal/provider/util.go#L36

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely did take inspiration from that. I don't foresee us needing to test this just now, but will keep in mind fo the future!

Copy link

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor nits

@johnstcn johnstcn merged commit ad8b1fd into main Aug 7, 2024
6 checks passed
@johnstcn johnstcn deleted the cj/datasource2resource branch August 7, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants