Reputation: 1
We are building a custom terraform provider using terraform-plugin-framework.
We are trying to copy “Object type” values from terraform model(generated using the plugin framework) to custom model which has the same structure but different data types:
Example: Plugin created Terraform model:
`type ComponentModel struct {
Access AccessValue tfsdk:"access"
}
type AccessValue struct {
Aws basetypes.ObjectValue tfsdk:"aws"
state attr.ValueState
}
Custom API model it needs to map to
type ComponentRequest struct {
Access *AccessConfig json:"access,omitempty"
}
type AccessConfig struct {
// aws
Aws *AWSConfig `json:"aws,omitempty"`
}
type AWSConfig struct {
IamPolicyARNs string json:"iamPolicyARNs"
}`
we would like to know if there is an easier way to copy all fields from terraform model to custom model so we can use the custom model as is to call the API on the backend.
func (r *componentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan *ComponentModel
//something like this
componentRequest := ComponentRequest{}
componentRequest := plan
//so that the componentRequest struct has fields mapped/copied from the plan
}
we would like to copy all values from plan to ComponentRequest instance. Is there an easier way to do this rather than traversing each and every field of plan and then setting it in ComponentRequest struct?
We have tried to copy from a terraform type to custom type and encounter type cast issues.
Upvotes: 0
Views: 86