1- package scaleway
1+ package datasource
22
33import (
44 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
77 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
88)
99
10- func datasourceNewZonedID (idI interface {}, fallBackZone scw.Zone ) string {
10+ func NewZonedID (idI interface {}, fallBackZone scw.Zone ) string {
1111 zone , id , err := zonal .ParseID (idI .(string ))
1212 if err != nil {
1313 id = idI .(string )
@@ -17,7 +17,7 @@ func datasourceNewZonedID(idI interface{}, fallBackZone scw.Zone) string {
1717 return zonal .NewIDString (zone , id )
1818}
1919
20- func datasourceNewRegionalID (idI interface {}, fallBackRegion scw.Region ) string {
20+ func NewRegionalID (idI interface {}, fallBackRegion scw.Region ) string {
2121 region , id , err := regional .ParseID (idI .(string ))
2222 if err != nil {
2323 id = idI .(string )
@@ -27,18 +27,16 @@ func datasourceNewRegionalID(idI interface{}, fallBackRegion scw.Region) string
2727 return regional .NewIDString (region , id )
2828}
2929
30- ////
31- // The below methods are imported from Google's terraform provider.
32- // source: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/datasource_helpers.go
33- ////
34-
35- // datasourceSchemaFromResourceSchema is a recursive func that
30+ // SchemaFromResourceSchema is a recursive func that
3631// converts an existing Resource schema to a Datasource schema.
3732// All schema elements are copied, but certain attributes are ignored or changed:
3833// - all attributes have Computed = true
3934// - all attributes have ForceNew, Required = false
4035// - Validation funcs and attributes (e.g. MaxItems) are not copied
41- func datasourceSchemaFromResourceSchema (rs map [string ]* schema.Schema ) map [string ]* schema.Schema {
36+ //
37+ // code imported from Google's terraform provider.
38+ // source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
39+ func SchemaFromResourceSchema (rs map [string ]* schema.Schema ) map [string ]* schema.Schema {
4240 ds := make (map [string ]* schema.Schema , len (rs ))
4341 for k , v := range rs {
4442 dv := & schema.Schema {
@@ -59,7 +57,7 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string
5957 if elem , ok := v .Elem .(* schema.Resource ); ok {
6058 // handle the case where the Element is a sub-resource
6159 dv .Elem = & schema.Resource {
62- Schema : datasourceSchemaFromResourceSchema (elem .Schema ),
60+ Schema : SchemaFromResourceSchema (elem .Schema ),
6361 }
6462 } else {
6563 // handle simple primitive case
@@ -75,20 +73,27 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string
7573 return ds
7674}
7775
78- // fixDatasourceSchemaFlags is a convenience func that toggles the Computed,
76+ // FixDatasourceSchemaFlags is a convenience func that toggles the Computed,
7977// Optional + Required flags on a schema element. This is useful when the schema
8078// has been generated (using `datasourceSchemaFromResourceSchema` above for
8179// example) and therefore the attribute flags were not set appropriately when
8280// first added to the schema definition. Currently only supports top-level
8381// schema elements.
84- func fixDatasourceSchemaFlags (schema map [string ]* schema.Schema , required bool , keys ... string ) {
82+ //
83+ // code imported from Google's terraform provider.
84+ // source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
85+ func FixDatasourceSchemaFlags (schema map [string ]* schema.Schema , required bool , keys ... string ) {
8586 for _ , v := range keys {
8687 schema [v ].Computed = false
8788 schema [v ].Optional = ! required
8889 schema [v ].Required = required
8990 }
9091}
9192
92- func addOptionalFieldsToSchema (schema map [string ]* schema.Schema , keys ... string ) {
93- fixDatasourceSchemaFlags (schema , false , keys ... )
93+ // AddOptionalFieldsToSchema
94+ //
95+ // code imported from Google's terraform provider.
96+ // source: https://github.com/hashicorp/terraform-provider-google/blob/main/google/tpgresource/datasource_helpers.go
97+ func AddOptionalFieldsToSchema (schema map [string ]* schema.Schema , keys ... string ) {
98+ FixDatasourceSchemaFlags (schema , false , keys ... )
9499}
0 commit comments