terraform-provider-google/google/datasource_helpers.go
Matt Morrison 23ef50f5ca New datasource: google_container_cluster (#740)
* Add google_kubernetes_cluster datasource

Add documentation for google_kubernetes_cluster datasource

Rename datasource to google_container_cluster

To be consistent with the equivalent resource.

Rename datasource in docs.
google_kubernetes_cluster -> google_container_cluster.
Also add reference in google.erb file.

WIP

Datasource read needs to set an ID, then call resource read func

Add additional cluster attributes to datasource schema

* Generate datasource schema from resource

Datasource documentation also updated.

* add test for datasourceSchemaFromResourceSchema

* Code review changes
2017-12-20 13:21:11 -08:00

78 lines
2.3 KiB
Go

package google
import (
"log"
"github.com/hashicorp/terraform/helper/schema"
)
// 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 {
log.Printf("[DEBUG] datasourceSchemaFromResourceSchema: %s", k)
dv := &schema.Schema{
Computed: true,
ForceNew: false,
Required: 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
}
// fixDatasourceSchemaFlags is a convenience func that toggles the Computed,
// Optional + Required flags on a schema element. This is useful when the schema
// has been generated (using `datasourceSchemaFromResourceSchema` above for
// example) and therefore the attribute flags were not set appropriately when
// first added to the schema definition. Currently only supports top-level
// schema elements.
func fixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
for _, v := range keys {
schema[v].Computed = false
schema[v].Optional = !required
schema[v].Required = required
}
}
func addRequiredFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
fixDatasourceSchemaFlags(schema, true, keys...)
}
func addOptionalFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
fixDatasourceSchemaFlags(schema, false, keys...)
}