/
provider_schema.go
302 lines (267 loc) · 8.84 KB
/
provider_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
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
package tools
import (
"errors"
"fmt"
"strings"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
// ErrInvalidType is returned when given value is of invalid type (i.e. different type than requested)
ErrInvalidType = errors.New("value must be of the specified type")
// ErrNotFound is returned when value is not present on schema
ErrNotFound = errors.New("value not found")
// ErrValueSet is returned when setting property value returned an error
ErrValueSet = errors.New("setting property value")
// ErrEmptyKey is returned when setting property value returned an error
ErrEmptyKey = errors.New("provided key cannot be empty")
// ErrEmptyPath is returned when path is empty
ErrEmptyPath = errors.New("path cannot be empty")
)
// ResourceDataFetcher ...
type ResourceDataFetcher interface {
GetOk(string) (interface{}, bool)
}
// GetSchemaFieldNameFromPath returns schema field name from given path
//
// if len of path is zero it returns empty string and error
func GetSchemaFieldNameFromPath(path cty.Path) (string, error) {
if len(path) == 0 {
return "", ErrEmptyPath
}
attrStep, ok := path[len(path)-1].(cty.GetAttrStep)
if !ok {
return "", ErrInvalidType
}
return attrStep.Name, nil
}
// GetStringValue fetches value with given key from ResourceData object and attempts type cast to string
//
// if value is not present on provided resource for key, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetStringValue(key string, rd ResourceDataFetcher) (string, error) {
if key == "" {
return "", fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
if ok {
str, ok := value.(string)
if !ok {
return "", fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "string")
}
return str, nil
}
return "", fmt.Errorf("%w: %s", ErrNotFound, key)
}
// GetInterfaceArrayValue fetches value with given key from ResourceData object and attempts type cast to []interface{}
//
// if value is not present on provided resource for key, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetInterfaceArrayValue(key string, rd ResourceDataFetcher) ([]interface{}, error) {
if key == "" {
return nil, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
if ok {
interf, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "[]interface{}")
}
return interf, nil
}
return nil, fmt.Errorf("%w: %s", ErrNotFound, key)
}
// GetIntValue fetches value with given key from ResourceData object and attempts type cast to int
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetIntValue(key string, rd ResourceDataFetcher) (int, error) {
if key == "" {
return 0, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrNotFound, key)
}
var num int
if num, ok = value.(int); !ok {
return 0, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "int")
}
return num, nil
}
// GetFloat64Value fetches value with given key from ResourceData object and attempts type cast to float64
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetFloat64Value(key string, rd ResourceDataFetcher) (float64, error) {
if key == "" {
return 0, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrNotFound, key)
}
var num float64
if num, ok = value.(float64); !ok {
return 0, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "float64")
}
return num, nil
}
// GetFloat32Value fetches value with given key from ResourceData object and attempts type cast to float64
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetFloat32Value(key string, rd ResourceDataFetcher) (float32, error) {
if key == "" {
return 0, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrNotFound, key)
}
var num float32
if num, ok = value.(float32); !ok {
return 0, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "float32")
}
return num, nil
}
// GetBoolValue fetches value with given key from ResourceData object and attempts type cast to bool
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetBoolValue(key string, rd ResourceDataFetcher) (bool, error) {
if key == "" {
return false, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, _ := rd.GetOk(key)
val, ok := value.(bool)
if !ok {
return false, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "bool")
}
return val, nil
}
// GetSetValue fetches value with given key from ResourceData object and attempts type cast to *schema.Set
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetSetValue(key string, rd ResourceDataFetcher) (*schema.Set, error) {
if key == "" {
return nil, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
val := new(schema.Set)
value, ok := rd.GetOk(key)
if !ok {
return val, fmt.Errorf("%w: %s", ErrNotFound, key)
}
if val, ok = value.(*schema.Set); !ok {
return val, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "*schema.Set")
}
return val, nil
}
// GetListValue fetches value with given key from ResourceData object and attempts type cast to []interface{}
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetListValue(key string, rd ResourceDataFetcher) ([]interface{}, error) {
if key == "" {
return nil, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
value, ok := rd.GetOk(key)
val := make([]interface{}, 0)
if !ok {
return val, fmt.Errorf("%w: %s", ErrNotFound, key)
}
if val, ok = value.([]interface{}); !ok {
return nil, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "[]interface{}")
}
return val, nil
}
// GetMapValue fetches value with given key from ResourceData object and attempts type cast to map[string]interface{}
//
// if value is not present on provided resource, ErrNotFound is returned
// if casting is not successful, ErrInvalidType is returned
func GetMapValue(key string, rd ResourceDataFetcher) (map[string]interface{}, error) {
if key == "" {
return nil, fmt.Errorf("%w: %s", ErrEmptyKey, key)
}
val := make(map[string]interface{}, 0)
value, ok := rd.GetOk(key)
if !ok {
return val, fmt.Errorf("%w: %s", ErrNotFound, key)
}
if val, ok = value.(map[string]interface{}); !ok {
return nil, fmt.Errorf("%w: %s, %q", ErrInvalidType, key, "map[string]interface{}")
}
return val, nil
}
// FindStringValues searches the ResourceData for the list of keys and returns the array of values
//
// if the value does not exist it is skipped
// if the value cannot be cast to string it is skipped
func FindStringValues(rd ResourceDataFetcher, keys ...string) []string {
rval := make([]string, 0)
for _, key := range keys {
value, ok := rd.GetOk(key)
if ok {
str, ok := value.(string)
if !ok {
continue
}
rval = append(rval, str)
}
}
return rval
}
// ResolveKeyStringState fetches value with given key (or fallbackKey) from ResourceData object
func ResolveKeyStringState(rd ResourceDataFetcher, key, fallbackKey string) (value string, err error) {
value, err = GetStringValue(key, rd)
if errors.Is(err, ErrNotFound) {
value, err = GetStringValue(fallbackKey, rd)
}
if err != nil {
return "", err
}
return value, nil
}
// StateNetwork changes the value of the input before storing it in state
func StateNetwork(i interface{}) string {
val, ok := i.(string)
if !ok {
panic(fmt.Sprintf("value type is not a string: %T", i))
}
switch strings.ToLower(val) {
case "production", "prod", "p":
return "production"
case "staging", "stag", "s":
return "staging"
}
// this should never happen :-)
return val
}
// RestoreOldValues reverts the value in schema of the given keys
func RestoreOldValues(rd *schema.ResourceData, keys []string) error {
for _, key := range keys {
if rd.HasChange(key) {
oldVersion, _ := rd.GetChange(key)
if err := rd.Set(key, oldVersion); err != nil {
return err
}
}
}
return nil
}
// GetExactlyOneOf extracts exactly one value with given keys from ResourceData object
// if multiple values are present the function returns first one found
func GetExactlyOneOf(rd ResourceDataFetcher, keys []string) (foundKey string, value interface{}, err error) {
for _, key := range keys {
value, err := GetSetValue(key, rd)
if errors.Is(err, ErrNotFound) {
continue
}
if err != nil {
return "", nil, err
}
return key, value, nil
}
return "", nil, ErrNotFound
}