Thanks for the detailed explanation. Issue raised in repo. I am actually super glad the solution is to use a tftype
as opposed to a custom type.
When accessing data for a single nested attribute, the recommendation is types.Object instead of a struct or struct pointer.
So this opened up an enormous can of worms. As you may notice in the issue you linked against at the end, I was eventually able to determine usage of ElementsAs
and MapValueFrom
. However, ObjectValueFrom
has very cryptic usage. My best initial guess is along the lines of:
type TopModel struct {
MyModel types.Object `tfsdk:"my"`
}
type myModel struct {
Foo types.String `tfsdk:"foo"`
Bar types.Bool `tfsdk:"bar"`
}
var myModelTF map[string]attr.Type = map[string]attr.Type{
"foo": types.String,
"bar": types.Bool,
}
// assume that sdk.My is the analogous deserialized struct from the response body from my corresponding SDK Go bindings
func MyModelGoToTerraform(ctx context.Context, my sdk.My) (types.Object, diag.Diagnostics) {
return types.ObjectValueFrom(ctx, myModelTF, myModel{
Foo: types.StringValue(my.Foo),
Bar: types.BoolValue(my.Bar),
})
}
// invoked in Create like
var objectConvertDiags diag.Diagnostics
state.My, objectConvertDiags = util.MyModelGoToTerraform(ctx, responseBody.my)
resp.Diagnostics.Append(objectConvertDiags...)
but throws compilation error:
types.String (type) is not an expression
Unsure from attr package - github.com/hashicorp/terraform-plugin-framework/attr - Go Packages and https://github.com/hashicorp/terraform-plugin-framework/blob/55f0d6bc4be2a57adb06a5c25c28640712c88129/types/basetypes/object_value_test.go#L174 what magical values are required in myModelTF
. I could modify to attr.Type.TerraformType
but that violates usage. I also tried values of attr.Type{TerraformType: types.String}
but no joy. The documentation claims:
// TerraformType returns the tftypes.Type that should be used to
// represent this type.
but that does not seem to be my experience? Also Plugin Development - Framework: Types | Terraform | HashiCorp Developer honestly seems to contradict your information and the code if I understand it correctly (my assumption being the documentation is incorrect).
As another side note: most of my code for this plugin now seems to be schema, models, and converters among the SDK struct, TF Go type models, and TF tftype models vis a vis “utility code”. Is there any possibility of some helper functions in the plugin framework to facilitate these kinds of functionality? Packer plugins and possibly in the future Vault plugins have mapstructure
for HCL serialization and deserialization, and I have no idea what kind of schema serialization and deserialization (other than of course the tfsdk
) could be available.