hashicorp / terraform-plugin-framework

A next-generation framework for building Terraform providers.
https://developer.hashicorp.com/terraform/plugin/framework
Mozilla Public License 2.0
298 stars 92 forks source link

Consider documenting how to share schema between resources and data resources #673

Open remyleone opened 1 year ago

remyleone commented 1 year ago

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?

////
// 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 copied
func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.Schema {
    ds := make(map[string]*schema.Schema, len(rs))
    for k, v := range rs {
        dv := &schema.Schema{
            Computed:    true,
            ForceNew:    false,
            Description: v.Description,
            Type:        v.Type,
        }

        switch v.Type {
        case schema.TypeSet:
            dv.Set = v.Set
            fallthrough
        case schema.TypeList:
            // List & Set types are generally used for 2 cases:
            // - a list/set of simple primitive values (e.g. list of strings)
            // - a sub resource
            if elem, ok := v.Elem.(*schema.Resource); ok {
                // handle the case where the Element is a sub-resource
                dv.Elem = &schema.Resource{
                    Schema: datasourceSchemaFromResourceSchema(elem.Schema),
                }
            } else {
                // handle simple primitive case
                dv.Elem = v.Elem
            }

        default:
            // Elem of all other types are copied as-is
            dv.Elem = v.Elem
        }
        ds[k] = dv
    }
    return ds
}

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

bflad commented 1 year ago

Related: https://github.com/hashicorp/terraform-plugin-framework/issues/568

grenader commented 11 months ago

This is exactly what we need