This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathresource_data.go
377 lines (317 loc) · 9.92 KB
/
resource_data.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package auth0
import (
"reflect"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"gopkg.in/auth0.v5"
)
// ResourceData generalises schema.ResourceData so that we can reuse the
// accessor methods defined below.
type ResourceData interface {
// IsNewResource reports whether or not the resource is seen for the first
// time. If so, checks for change won't be carried out.
IsNewResource() bool
// HasChange reports whether or not the given key has been changed.
HasChange(key string) bool
// GetChange returns the old and new value for a given key.
GetChange(key string) (interface{}, interface{})
// Get returns the data for the given key, or nil if the key doesn't exist
// in the schema.
Get(key string) interface{}
// GetOk returns the data for the given key and whether or not the key
// has been set to a non-zero value at some point.
//
// The first result will not necessarilly be nil if the value doesn't exist.
// The second result should be checked to determine this information.
GetOk(key string) (interface{}, bool)
// GetOkExists can check if TypeBool attributes that are Optional with
// no Default value have been set.
//
// Deprecated: usage is discouraged due to undefined behaviors and may be
// removed in a future version of the SDK
GetOkExists(key string) (interface{}, bool)
// Set sets the value for the given key.
//
// If the key is invalid or the value is not a correct type, an error
// will be returned.
Set(key string, value interface{}) error
}
type resourceData struct {
ResourceData
prefix string
}
func newResourceDataAtKey(key string, d ResourceData) ResourceData {
return &resourceData{d, key}
}
func newResourceDataAtIndex(i int, d ResourceData) ResourceData {
return &resourceData{d, strconv.Itoa(i)}
}
func (d *resourceData) IsNewResource() bool {
return d.ResourceData.IsNewResource()
}
func (d *resourceData) HasChange(key string) bool {
return d.ResourceData.HasChange(d.prefix + "." + key)
}
func (d *resourceData) GetChange(key string) (interface{}, interface{}) {
return d.ResourceData.GetChange(d.prefix + "." + key)
}
func (d *resourceData) Get(key string) interface{} {
return d.ResourceData.Get(d.prefix + "." + key)
}
func (d *resourceData) GetOk(key string) (interface{}, bool) {
return d.ResourceData.GetOk(d.prefix + "." + key)
}
func (d *resourceData) GetOkExists(key string) (interface{}, bool) {
return d.ResourceData.GetOkExists(d.prefix + "." + key)
}
func (d *resourceData) Set(key string, value interface{}) error {
return d.ResourceData.Set(d.prefix+"."+key, value)
}
// MapData wraps a map satisfying the Data interface, so it can be used in the
// accessor methods defined below.
type MapData map[string]interface{}
func (md MapData) IsNewResource() bool {
return false
}
func (md MapData) HasChange(key string) bool {
_, ok := md[key]
return ok
}
func (md MapData) GetChange(key string) (interface{}, interface{}) {
return md[key], md[key]
}
func (md MapData) Get(key string) interface{} {
return md[key]
}
func (md MapData) GetOk(key string) (interface{}, bool) {
v, ok := md[key]
return v, ok && !isNil(v) && !isZero(v)
}
func (md MapData) GetOkExists(key string) (interface{}, bool) {
v, ok := md[key]
return v, ok && !isNil(v)
}
func (md MapData) Set(key string, value interface{}) error {
if !isNil(value) {
md[key] = value
}
return nil
}
func isNil(v interface{}) bool {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map:
return rv.IsNil()
}
return v == nil
}
func isZero(v interface{}) bool {
return reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface())
}
var _ ResourceData = (*schema.ResourceData)(nil)
// Condition is a function that checks whether a condition holds true for a
// value being accessed.
//
// It is used with accessor functions such as Int, String, etc to only retrieve
// the value if the conditions hold true.
type Condition func(d ResourceData, key string) bool
// Eval performs the evaluation of the condition.
func (c Condition) Eval(d ResourceData, key string) bool {
return c(d, key)
}
// IsNewResource is a condition that evaluates to true if the resource access is
// new.
func IsNewResource() Condition {
return func(d ResourceData, key string) bool {
return d.IsNewResource()
}
}
// HasChange is a condition that evaluates to true if the value accessed has
// changed.
func HasChange() Condition {
return func(d ResourceData, key string) bool {
return d.HasChange(key)
}
}
// Any is a condition that evaluates to true if any of its enclosed conditions
// evaluate to true. If it is not passed any conditions it will be considered
// unconditional, therefore it will evaluate to true.
func Any(conditions ...Condition) Condition {
return func(d ResourceData, key string) bool {
for _, condition := range conditions {
if condition.Eval(d, key) {
return true
}
}
return len(conditions) == 0
}
}
// All is a condition that evaluates to true if all of its child conditions
// evaluate to true.
func All(conditions ...Condition) Condition {
return func(d ResourceData, key string) bool {
for _, condition := range conditions {
if !condition.Eval(d, key) {
return false
}
}
return true
}
}
// Not is a condition that evaluates to true if its child condition evaluates to
// false. False otherwise.
func Not(condition Condition) Condition {
return func(d ResourceData, key string) bool {
return !condition.Eval(d, key)
}
}
// String accesses the value held by key and type asserts it to a pointer to a
// string.
func String(d ResourceData, key string, conditions ...Condition) (s *string) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
s = auth0.String(v.(string))
}
return
}
// Int accesses the value held by key and type asserts it to a pointer to a
// int.
func Int(d ResourceData, key string, conditions ...Condition) (i *int) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
i = auth0.Int(v.(int))
}
return
}
// Float64 accesses the value held by key and type asserts it to a pointer to a
// float64.
func Float64(d ResourceData, key string, conditions ...Condition) (f *float64) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
f = auth0.Float64(v.(float64))
}
return
}
// Bool accesses the value held by key and type asserts it to a pointer to a
// bool.
func Bool(d ResourceData, key string, conditions ...Condition) (b *bool) {
v, ok := d.GetOkExists(key)
if ok && Any(conditions...).Eval(d, key) {
b = auth0.Bool(v.(bool))
}
return
}
// Slice accesses the value held by key and type asserts it to a slice.
func Slice(d ResourceData, key string, conditions ...Condition) (s []interface{}) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
s = v.([]interface{})
}
return
}
// Map accesses the value held by key and type asserts it to a map.
func Map(d ResourceData, key string, conditions ...Condition) (m map[string]interface{}) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
m = v.(map[string]interface{})
}
return
}
// List accesses the value held by key and returns an iterator able to go over
// its elements.
func List(d ResourceData, key string, conditions ...Condition) Iterator {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
return &list{newResourceDataAtKey(key, d), v.([]interface{})}
}
return &list{}
}
// Set accesses the value held by key, type asserts it to a set and returns an
// iterator able to go over its elements.
func Set(d ResourceData, key string, conditions ...Condition) Iterator {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
if s, ok := v.(*schema.Set); ok {
return &set{newResourceDataAtKey(key, d), s}
}
}
return &set{nil, &schema.Set{}}
}
// Iterator is used to iterate over a list or set.
//
// Elem iterates over all elements of the iterator, calling fn with each
// iteration. The callback takes a Data interface as argument which is prefixed
// with its parents key, allowing for convenient nested data access.
//
// List returns all elements of the iterator as a Go slice.
//
// Len returns the number of elements held by the iterator.
type Iterator interface {
Elem(func(d ResourceData))
List() []interface{}
Len() int
}
type list struct {
d ResourceData
v []interface{}
}
func (l *list) Elem(fn func(ResourceData)) {
for idx := range l.v {
fn(newResourceDataAtIndex(idx, l.d))
}
}
func (l *list) List() []interface{} {
return l.v
}
func (l *list) Len() int {
return len(l.v)
}
type set struct {
d ResourceData
s *schema.Set
}
func (s *set) hash(item interface{}) string {
code := s.s.F(item)
if code < 0 {
code = -code
}
return strconv.Itoa(code)
}
func (s *set) Elem(fn func(ResourceData)) {
for _, v := range s.s.List() {
fn(newResourceDataAtKey(s.hash(v), s.d))
}
}
func (s *set) List() []interface{} {
return s.s.List()
}
func (s *set) Len() int {
return s.s.Len()
}
// Diff accesses the value held by key and type asserts it to a set. It then
// compares it's changes if any and returns what needs to be added and what
// needs to be removed.
func Diff(d ResourceData, key string) (add Iterator, rm Iterator) {
// Zero the add and rm sets. These may be modified if the diff observed any
// changes.
add = &set{newResourceDataAtKey(key, d), d.Get(key).(*schema.Set)}
rm = &set{newResourceDataAtKey(key, d), &schema.Set{}}
if d.HasChange(key) {
o, n := d.GetChange(key)
add = &set{newResourceDataAtKey(key, d), n.(*schema.Set).Difference(o.(*schema.Set))}
rm = &set{newResourceDataAtKey(key, d), o.(*schema.Set).Difference(n.(*schema.Set))}
}
return
}
// JSON accesses the value held by key and unmarshals it into a map.
func JSON(d ResourceData, key string, conditions ...Condition) (m map[string]interface{}, err error) {
v, ok := d.GetOk(key)
if ok && Any(conditions...).Eval(d, key) {
m, err = structure.ExpandJsonFromString(v.(string))
if err != nil {
return
}
}
return
}