Hey there!
I’m working on a Terraform Provider for a resource, using the “new” Plugin Framework. In the Read
implementation for that resource, I need to get access to the Resource Configuration e.g. the state in the .tf file, declaring that resource.
Usually, this wouldn’t be an issue, as the state should align with the values in that file, however this is not the case when the Read
step is performed after a resource import. Sadly, I am not able to fetch the values in question from the actual resource’s API (which I know is likely bad practice), because I can only fetch parts of that data from the remote resource. With the state in the configuration, I would be able to build the correct Terraform State for the imported resource.
Because I haven’t been able to get to that configuration, I tried a workaround using a Plan Modifier, which would be run on the first apply after the state has been imported. Here I ran into another issue, where I could not access values that should be set on the resource struct. Example:
type resource struct {
client *someClient
}
func (r *resource) Schema(
_ context.Context,
_ resource.SchemaRequest,
resp *resource.SchemaResponse
) {
if (r.client == nil) {
panic("Oh no!") // This happens when I run `terraform plan`
}
// ...
}
In the plan modifier I do need access to the “client” above, in order to fetch other parts of the data, which can then be applied to the plan. I’m not sure if that behavior is intended or if I’m doing something wrong. It would make senese to me that the Configure
function setting up the client has not been called yet when Schema
is called, however I would expect it to be present when the plan modifier is called…
I’d much appreciate any input regarding either of my issues :)
Best,
Michael