-
Notifications
You must be signed in to change notification settings - Fork 365
/
customizable_schema.go
269 lines (237 loc) · 8.14 KB
/
customizable_schema.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package common
import (
"fmt"
"log"
"slices"
"strings"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type CustomizableSchema struct {
Schema *schema.Schema
path []string
isSuppressDiff bool
context schemaPathContext
}
func (s *CustomizableSchema) pathContainsMultipleItemsList() bool {
schemaPath := s.context.schemaPath
for _, scm := range schemaPath {
if scm.Type == schema.TypeList && scm.MaxItems != 1 {
return true
}
}
return false
}
// Used to get the prefix path for functions like ConflictsWith, by joining `path` in SchemaPathContext.
func getPrefixedValue(path []string, value []string) []string {
var prefix string
if len(path) != 0 {
prefix = strings.Join(path, ".") + "."
} else {
prefix = ""
}
prefixedPaths := make([]string, len(value))
for i, item := range value {
prefixedPaths[i] = prefix + item
}
return prefixedPaths
}
func CustomizeSchemaPath(s map[string]*schema.Schema, path ...string) *CustomizableSchema {
if len(path) == 0 {
// Wrapping the input map into a schema when the path is empty.
// The primary use case for this situation is for adding a new field at the top level.
wrappedSch := &schema.Schema{
Elem: &schema.Resource{
Schema: s,
},
}
return &CustomizableSchema{Schema: wrappedSch}
}
sch := MustSchemaPath(s, path...)
return &CustomizableSchema{Schema: sch, path: path}
}
func (s *CustomizableSchema) SchemaPath(path ...string) *CustomizableSchema {
sch := MustSchemaPath(s.GetSchemaMap(), path...)
return &CustomizableSchema{Schema: sch, path: path, context: s.context}
}
func (s *CustomizableSchema) GetSchemaMap() map[string]*schema.Schema {
if s.Schema.Elem == nil {
panic("Elem of Schema field for CustomizableSchema is nil.")
}
schemaResource, ok := s.Schema.Elem.(*schema.Resource)
if !ok {
panic("Elem of Schema field for CustomizableSchema is not a *schema.Resource.")
}
return schemaResource.Schema
}
func (s *CustomizableSchema) SetOptional() *CustomizableSchema {
s.Schema.Optional = true
s.Schema.Required = false
return s
}
func (s *CustomizableSchema) SetComputed() *CustomizableSchema {
s.Schema.Computed = true
return s
}
func (s *CustomizableSchema) SetSliceSet() *CustomizableSchema {
s.Schema.Type = schema.TypeSet
return s
}
func (s *CustomizableSchema) SetDefault(value any) *CustomizableSchema {
s.Schema.Default = value
s.Schema.Optional = true
s.Schema.Required = false
return s
}
// SetReadOnly sets the schema to be read-only (i.e. computed, non-optional).
// This should be used for fields that are not user-configurable but are returned
// by the platform.
func (s *CustomizableSchema) SetReadOnly() *CustomizableSchema {
s.Schema.Optional = false
s.Schema.Required = false
s.Schema.MaxItems = 0
s.Schema.Computed = true
return s
}
// SetRequired sets the schema to be required.
func (s *CustomizableSchema) SetRequired() *CustomizableSchema {
s.Schema.Optional = false
s.Schema.Required = true
s.Schema.Computed = false
return s
}
func (s *CustomizableSchema) SetSuppressDiff() *CustomizableSchema {
s.Schema.DiffSuppressFunc = diffSuppressor(s.path[len(s.path)-1], s.Schema)
s.isSuppressDiff = true
if s.Schema.Type == schema.TypeList && s.Schema.MaxItems == 1 {
// If it is a list with max items = 1, it means the corresponding sdk schema type is a struct or a ptr.
// In this case we would like to set the diff suppressor for the underlying fields as well.
resource, ok := s.Schema.Elem.(*schema.Resource)
if !ok {
panic("Cannot cast Elem into Resource type.")
}
nestedSchema := resource.Schema
for k, v := range nestedSchema {
v.DiffSuppressFunc = diffSuppressor(k, v)
}
}
return s
}
// SetSuppressDiffWithDefault suppresses the diff if the
// new value (ie value from HCL config) is not set and
// the old value (ie value from state / platform) is equal to the default value.
//
// Often Databricks HTTP APIs will return values for fields that were not set by
// the author in their terraform configuration. This function allows us to suppress
// the diff in these cases.
func (s *CustomizableSchema) SetSuppressDiffWithDefault(dv any) *CustomizableSchema {
primitiveTypes := []schema.ValueType{schema.TypeBool, schema.TypeString, schema.TypeInt, schema.TypeFloat}
if !slices.Contains(primitiveTypes, s.Schema.Type) {
panic(fmt.Errorf("expected primitive type, got: %s", s.Schema.Type))
}
// Get zero value for the schema type
zero := fmt.Sprintf("%v", s.Schema.Type.Zero())
// Get string representation of the default value
sv := fmt.Sprintf("%v", dv)
// Suppress diff if the new value (ie value from HCL config) is not set and
// the old value (ie value from state / platform) is equal to the default value.
s.Schema.DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
if new == zero && old == sv {
return true
}
return false
}
return s
}
func (s *CustomizableSchema) SetCustomSuppressDiff(suppressor func(k, old, new string, d *schema.ResourceData) bool) *CustomizableSchema {
s.Schema.DiffSuppressFunc = suppressor
return s
}
func (s *CustomizableSchema) SetSensitive() *CustomizableSchema {
s.Schema.Sensitive = true
return s
}
func (s *CustomizableSchema) SetForceNew() *CustomizableSchema {
s.Schema.ForceNew = true
return s
}
func (s *CustomizableSchema) SetMaxItems(value int) *CustomizableSchema {
s.Schema.MaxItems = value
return s
}
func (s *CustomizableSchema) SetMinItems(value int) *CustomizableSchema {
s.Schema.MinItems = value
return s
}
func (s *CustomizableSchema) SetConflictsWith(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetConflictsWith cannot take in an empty list")
}
if s.pathContainsMultipleItemsList() {
log.Printf("[DEBUG] ConflictsWith skipped for %v, path contains TypeList block with MaxItems not equal to 1", strings.Join(s.path, "."))
return s
}
s.Schema.ConflictsWith = getPrefixedValue(s.context.path, value)
return s
}
func (s *CustomizableSchema) SetExactlyOneOf(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetExactlyOneOf cannot take in an empty list")
}
if s.pathContainsMultipleItemsList() {
log.Printf("[DEBUG] ExactlyOneOf skipped for %v, path contains TypeList block with MaxItems not equal to 1", strings.Join(s.path, "."))
return s
}
s.Schema.ExactlyOneOf = getPrefixedValue(s.context.path, value)
return s
}
func (s *CustomizableSchema) SetAtLeastOneOf(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetAtLeastOneOf cannot take in an empty list")
}
if s.pathContainsMultipleItemsList() {
log.Printf("[DEBUG] AtLeastOneOf skipped for %v, path contains TypeList block with MaxItems not equal to 1", strings.Join(s.path, "."))
return s
}
s.Schema.AtLeastOneOf = getPrefixedValue(s.context.path, value)
return s
}
func (s *CustomizableSchema) SetRequiredWith(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetRequiredWith cannot take in an empty list")
}
if s.pathContainsMultipleItemsList() {
log.Printf("[DEBUG] SetRequiredWith skipped for %v, path contains TypeList block with MaxItems not equal to 1", strings.Join(s.path, "."))
return s
}
s.Schema.RequiredWith = getPrefixedValue(s.context.path, value)
return s
}
func (s *CustomizableSchema) SetDeprecated(reason string) *CustomizableSchema {
s.Schema.Deprecated = reason
return s
}
func (s *CustomizableSchema) SetValidateFunc(validate func(interface{}, string) ([]string, []error)) *CustomizableSchema {
s.Schema.ValidateFunc = validate
return s
}
func (s *CustomizableSchema) SetValidateDiagFunc(validate func(interface{}, cty.Path) diag.Diagnostics) *CustomizableSchema {
s.Schema.ValidateDiagFunc = validate
return s
}
func (s *CustomizableSchema) AddNewField(key string, newField *schema.Schema) *CustomizableSchema {
cv, ok := s.Schema.Elem.(*schema.Resource)
if !ok {
panic("Cannot add new field, target is not nested resource")
}
_, exists := cv.Schema[key]
if exists {
panic("Cannot add new field, " + key + " already exists in the schema")
}
cv.Schema[key] = newField
if s.isSuppressDiff {
newField.DiffSuppressFunc = diffSuppressor(key, newField)
}
return s
}