-
Notifications
You must be signed in to change notification settings - Fork 4
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
Conversation
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 }} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, minor nits
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.