/
zzz_utils.go
370 lines (284 loc) · 7.72 KB
/
zzz_utils.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
package schemas
import (
"context"
"time"
"github.com/eddycharly/terraform-provider-kops/pkg/version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Diff
func CustomizeDiffRevision(_ context.Context, d *schema.ResourceDiff, m interface{}) error {
// If anything changes, increment the revision
if len(d.GetChangedKeysPrefix("")) > 0 {
return d.SetNew("revision", d.Get("revision").(int)+1)
}
return nil
}
func CustomizeDiffVersion(_ context.Context, d *schema.ResourceDiff, m interface{}) error {
if d.Get("provider_version").(string) != version.BuildVersion {
return d.SetNew("provider_version", version.BuildVersion)
}
return nil
}
func Nullable(in *schema.Schema) *schema.Schema {
return OptionalStruct(
&schema.Resource{
Schema: map[string]*schema.Schema{
"value": in,
},
},
)
}
func ComplexMapElem(in *schema.Schema) *schema.Resource {
in.Required = true
return &schema.Resource{
Schema: map[string]*schema.Schema{
"key": RequiredString(),
"value": in,
},
}
}
// Tools
func Schema(t schema.ValueType, elem interface{}, required, optional, computed bool, maxItems int) *schema.Schema {
return &schema.Schema{
Type: t,
Optional: optional,
Required: required,
Computed: computed,
MaxItems: maxItems,
Elem: elem,
}
}
func Simple(t schema.ValueType, required, optional, computed bool) *schema.Schema {
return Schema(t, nil, required, optional, computed, 0)
}
func SimpleRequired(t schema.ValueType) *schema.Schema {
return Simple(t, true, false, false)
}
func SimpleOptional(t schema.ValueType) *schema.Schema {
return Simple(t, false, true, false)
}
func SimpleComputed(t schema.ValueType) *schema.Schema {
return Simple(t, false, false, true)
}
func SimpleOptionalComputed(t schema.ValueType) *schema.Schema {
return Simple(t, false, true, true)
}
func Sensitive(s *schema.Schema) *schema.Schema {
s.Sensitive = true
return s
}
func ForceNew(s *schema.Schema) *schema.Schema {
s.ForceNew = true
return s
}
func SuppressDiff(s *schema.Schema) *schema.Schema {
s.DiffSuppressFunc = func(_, _, _ string, _ *schema.ResourceData) bool {
return true
}
return s
}
// Quantity
func OptionalQuantity() *schema.Schema {
return OptionalString()
}
func ComputedQuantity() *schema.Schema {
return ComputedString()
}
func ExpandQuantity(in interface{}) resource.Quantity {
q, _ := resource.ParseQuantity(in.(string))
return q
}
func FlattenQuantity(in resource.Quantity) interface{} {
return in.String()
}
// Duration
func OptionalDuration() *schema.Schema {
return OptionalString()
}
func ComputedDuration() *schema.Schema {
return ComputedString()
}
func ExpandDuration(in interface{}) metav1.Duration {
d, _ := time.ParseDuration(in.(string))
return metav1.Duration{
Duration: d,
}
}
func FlattenDuration(in metav1.Duration) interface{} {
return in.Duration.String()
}
// IntOrString
func OptionalIntOrString() *schema.Schema {
return OptionalString()
}
func ComputedIntOrString() *schema.Schema {
return ComputedString()
}
func ExpandIntOrString(in interface{}) intstr.IntOrString {
return intstr.Parse(in.(string))
}
func FlattenIntOrString(in intstr.IntOrString) interface{} {
return in.String()
}
// Map
func RequiredMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeMap, elem, true, false, false, 0)
}
func OptionalMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeMap, elem, false, true, false, 0)
}
func ComputedMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeMap, elem, false, false, true, 0)
}
func OptionalComputedMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeMap, elem, false, true, true, 0)
}
// ComplexMap
func OptionalComplexMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeList, ComplexMapElem(elem), false, true, false, 0)
}
func ComputedComplexMap(elem *schema.Schema) *schema.Schema {
return Schema(schema.TypeList, ComplexMapElem(elem), false, true, true, 0)
}
// Struct
func RequiredStruct(elem *schema.Resource) *schema.Schema {
return Schema(schema.TypeList, elem, true, false, false, 1)
}
func OptionalStruct(elem *schema.Resource) *schema.Schema {
return Schema(schema.TypeList, elem, false, true, false, 1)
}
func ComputedStruct(elem *schema.Resource) *schema.Schema {
return Schema(schema.TypeList, elem, false, false, true, 0)
}
func OptionalComputedStruct(elem *schema.Resource) *schema.Schema {
return Schema(schema.TypeList, elem, false, true, true, 1)
}
// Set
func RequiredSetList(elem interface{}) *schema.Schema {
return Schema(schema.TypeSet, elem, true, false, false, 0)
}
func OptionalSetList(elem interface{}) *schema.Schema {
return Schema(schema.TypeSet, elem, false, true, false, 0)
}
// List
func List(elem interface{}) *schema.Schema {
return Schema(schema.TypeList, elem, false, false, false, 0)
}
func RequiredList(elem interface{}) *schema.Schema {
return Schema(schema.TypeList, elem, true, false, false, 0)
}
func OptionalList(elem interface{}) *schema.Schema {
return Schema(schema.TypeList, elem, false, true, false, 0)
}
func ComputedList(elem interface{}) *schema.Schema {
return Schema(schema.TypeList, elem, false, false, true, 0)
}
func OptionalComputedList(elem interface{}) *schema.Schema {
return Schema(schema.TypeList, elem, false, true, true, 0)
}
// String
func String() *schema.Schema {
return Simple(schema.TypeString, false, false, false)
}
func RequiredString() *schema.Schema {
return SimpleRequired(schema.TypeString)
}
func OptionalString() *schema.Schema {
return SimpleOptional(schema.TypeString)
}
func ComputedString() *schema.Schema {
return SimpleComputed(schema.TypeString)
}
func OptionalComputedString() *schema.Schema {
return SimpleOptionalComputed(schema.TypeString)
}
func ExpandString(in interface{}) string {
if in == nil {
return ""
}
return in.(string)
}
func FlattenString(in string) interface{} {
return in
}
// Bool
func Bool() *schema.Schema {
return Simple(schema.TypeBool, false, false, false)
}
func RequiredBool() *schema.Schema {
return SimpleRequired(schema.TypeBool)
}
func OptionalBool() *schema.Schema {
return SimpleOptional(schema.TypeBool)
}
func ComputedBool() *schema.Schema {
return SimpleComputed(schema.TypeBool)
}
func OptionalComputedBool() *schema.Schema {
return SimpleOptionalComputed(schema.TypeBool)
}
func ExpandBool(in interface{}) bool {
if in == nil {
return false
}
return in.(bool)
}
func FlattenBool(in bool) interface{} {
return in
}
// Int
func Int() *schema.Schema {
return Simple(schema.TypeInt, false, false, false)
}
func RequiredInt() *schema.Schema {
return SimpleRequired(schema.TypeInt)
}
func OptionalInt() *schema.Schema {
return SimpleOptional(schema.TypeInt)
}
func ComputedInt() *schema.Schema {
return SimpleComputed(schema.TypeInt)
}
func OptionalComputedInt() *schema.Schema {
return SimpleOptionalComputed(schema.TypeInt)
}
func ExpandInt(in interface{}) int {
if in == nil {
return 0
}
if _, ok := in.(float64); ok {
return int(in.(float64))
}
return in.(int)
}
func FlattenInt(in int) interface{} {
return in
}
// Float
func Float() *schema.Schema {
return Simple(schema.TypeFloat, false, false, false)
}
func RequiredFloat() *schema.Schema {
return SimpleRequired(schema.TypeFloat)
}
func OptionalFloat() *schema.Schema {
return SimpleOptional(schema.TypeFloat)
}
func ComputedFloat() *schema.Schema {
return SimpleComputed(schema.TypeFloat)
}
func OptionalComputedFloat() *schema.Schema {
return SimpleOptionalComputed(schema.TypeFloat)
}
func ExpandFloat(in interface{}) float64 {
if in == nil {
return 0
}
return in.(float64)
}
func FlattenFloat(in float64) interface{} {
return in
}