github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/legacy/helper/schema/data_source_resource_shim.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package schema 5 6 import ( 7 "fmt" 8 ) 9 10 // DataSourceResourceShim takes a Resource instance describing a data source 11 // (with a Read implementation and a Schema, at least) and returns a new 12 // Resource instance with additional Create and Delete implementations that 13 // allow the data source to be used as a resource. 14 // 15 // This is a backward-compatibility layer for data sources that were formerly 16 // read-only resources before the data source concept was added. It should not 17 // be used for any *new* data sources. 18 // 19 // The Read function for the data source *must* call d.SetId with a non-empty 20 // id in order for this shim to function as expected. 21 // 22 // The provided Resource instance, and its schema, will be modified in-place 23 // to make it suitable for use as a full resource. 24 func DataSourceResourceShim(name string, dataSource *Resource) *Resource { 25 // Recursively, in-place adjust the schema so that it has ForceNew 26 // on any user-settable resource. 27 dataSourceResourceShimAdjustSchema(dataSource.Schema) 28 29 dataSource.Create = CreateFunc(dataSource.Read) 30 dataSource.Delete = func(d *ResourceData, meta interface{}) error { 31 d.SetId("") 32 return nil 33 } 34 dataSource.Update = nil // should already be nil, but let's make sure 35 36 // FIXME: Link to some further docs either on the website or in the 37 // changelog, once such a thing exists. 38 dataSource.DeprecationMessage = fmt.Sprintf( 39 "using %s as a resource is deprecated; consider using the data source instead", 40 name, 41 ) 42 43 return dataSource 44 } 45 46 func dataSourceResourceShimAdjustSchema(schema map[string]*Schema) { 47 for _, s := range schema { 48 // If the attribute is configurable then it must be ForceNew, 49 // since we have no Update implementation. 50 if s.Required || s.Optional { 51 s.ForceNew = true 52 } 53 54 // If the attribute is a nested resource, we need to recursively 55 // apply these same adjustments to it. 56 if s.Elem != nil { 57 if r, ok := s.Elem.(*Resource); ok { 58 dataSourceResourceShimAdjustSchema(r.Schema) 59 } 60 } 61 } 62 }