-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema_helpers.go
90 lines (77 loc) · 2.76 KB
/
schema_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package taikun
import "github.com/hashicorp/terraform-plugin-sdk/v2/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 {
dv := &schema.Schema{
Computed: true,
Sensitive: v.Sensitive,
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 removeForceNewsFromSchema(schema map[string]*schema.Schema) {
for _, v := range schema {
v.ForceNew = false
}
}
func setFieldInSchema(schema map[string]*schema.Schema, key string, value *schema.Schema) {
schema[key] = value
}
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...)
}
func setValidateDiagFuncToSchema(schema map[string]*schema.Schema, key string, f schema.SchemaValidateDiagFunc) {
schema[key].ValidateDiagFunc = f
}
func deleteFieldsFromSchema(schema map[string]*schema.Schema, keys ...string) {
for _, v := range keys {
delete(schema, v)
}
}