You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When developing a resource that is evolving during time it could be cumbersome to duplicate fields twice between resource schema and data resource schema. Could it be possible to have a way to have a common abstraction between the two and customize only the changing parts?
Attempted Solutions
We currently use a code similar to this with SDKv2 but I was wondering if terraform framework could do it more natively?
////// The below methods are imported from Google's terraform provider.// source: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/datasource_helpers.go////// datasourceSchemaFromResourceSchema is a recursive func that// converts an existing Resource schema to a Datasource schema.// All schema elements are copied, but certain attributes are ignored or changed:// - all attributes have Computed = true// - all attributes have ForceNew, Required = false// - Validation funcs and attributes (e.g. MaxItems) are not copiedfuncdatasourceSchemaFromResourceSchema(rsmap[string]*schema.Schema) map[string]*schema.Schema {
ds:=make(map[string]*schema.Schema, len(rs))
fork, v:=rangers {
dv:=&schema.Schema{
Computed: true,
ForceNew: false,
Description: v.Description,
Type: v.Type,
}
switchv.Type {
caseschema.TypeSet:
dv.Set=v.Setfallthroughcaseschema.TypeList:
// List & Set types are generally used for 2 cases:// - a list/set of simple primitive values (e.g. list of strings)// - a sub resourceifelem, ok:=v.Elem.(*schema.Resource); ok {
// handle the case where the Element is a sub-resourcedv.Elem=&schema.Resource{
Schema: datasourceSchemaFromResourceSchema(elem.Schema),
}
} else {
// handle simple primitive casedv.Elem=v.Elem
}
default:
// Elem of all other types are copied as-isdv.Elem=v.Elem
}
ds[k] =dv
}
returnds
}
Proposal
Having a standard way or at least patterns and helper functions that are common between data resources and resources. Otherwise it is going to be tedious to duplicate fields and changing whether they are computed or not.
References
The text was updated successfully, but these errors were encountered:
Use-cases
When developing a resource that is evolving during time it could be cumbersome to duplicate fields twice between resource schema and data resource schema. Could it be possible to have a way to have a common abstraction between the two and customize only the changing parts?
Attempted Solutions
We currently use a code similar to this with SDKv2 but I was wondering if terraform framework could do it more natively?
Proposal
Having a standard way or at least patterns and helper functions that are common between data resources and resources. Otherwise it is going to be tedious to duplicate fields and changing whether they are computed or not.
References
The text was updated successfully, but these errors were encountered: