resp.state.Set(ctx, &data) cannot set custom type values

Hi,
I am developping a custom provider for the first time using the framework plugin with the help from code gen and provider spec gen tools (looks like great tools and I hope Terraform team keep maintaining it and expand on the examples and tutorials).

I am attempting to create a resource for an open api server and using the automatically generated model and schema that includes primitive and a custom type. The custom type is only associated with the response data, and so the field values are unknown until the response is received and so is the case for other primitives in the response.

All stages of the request and response data handling are successful, all primitive attribute types are correctly reflected in the state file but the custom type object is always showing “null” despite verifying the individual field elements are present in the data.

The generated data model includes primitive types attribute and a custom type ResultValue for Result attribute.

The create function for the resource include a final step as :
Resp.state.Set(ctx, &data)

when checking data.Result I can see in the log trace the field elements as received from the API. However when I look at the state file result object shows “null” while all other primitive attributes from the request and response are set correctly.

I have looked at the online documentation and tutorials and could not find a match to help me resolve this final step.
I appreciate any suggestion, insight or useful information to point to the right direction or else if this use case requires code update.

Thank you,
MB.

As a follow-up. I tried setting the attribute "result’ in the state file directly after the set state call with data but using a terraform type instead of generated custom type :
resp.State.SetAttribute(ctx, path.Root(“result”), value). In this case the state file shows the object values correctly. Attempting to set the attribute with custom type value is not effective either.

I think this issue has been resolved by properly catching the api result field in a maptype/mapvalue pair of map definitions of terraform types then setting the data.Result field using a call to the autogenerated NewResultValue(maptype, mapvalue), where Result is the custom Type value. This could not be set in the state file previously by referencing its fields directly, such as data.Result.ID eventhough it looked present in the data model declaration.

I strongly urges the Terraform team to expand on the tutorials and use of custom types and more realistic api data handling examples to help other beginners as I think you have a great capability here. Your documentation is also great.
Thank you for making it available and keep it up!

1 Like