-
Notifications
You must be signed in to change notification settings - Fork 135
/
helper.go
299 lines (260 loc) · 6.76 KB
/
helper.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
package helper
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"hash/crc32"
"reflect"
"sort"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
)
// Generates a hash for the set hash function used by the IDs
func DataResourceIdsHash(ids []string) string {
var buf bytes.Buffer
for _, id := range ids {
buf.WriteString(fmt.Sprintf("%s-", id))
}
return fmt.Sprintf("%d", HashString(buf.String()))
}
// Generates a hash for the resource
func ResourceIdsHash(ids []string) string {
return DataResourceIdsHash(ids)
}
// HashString hashes a string to a unique hashcode.
//
// This will be removed in v2 without replacement. So we place here instead of import.
func HashString(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}
// Strings hashes a list of strings to a unique hashcode.
func HashStrings(strings []string) string {
var buf bytes.Buffer
for _, s := range strings {
buf.WriteString(fmt.Sprintf("%s-", s))
}
return fmt.Sprintf("%d", String(buf.String()))
}
// Generates a hash for the set hash function used by the ID
func DataResourceIdHash(id string) string {
return fmt.Sprintf("%d", HashString(id))
}
func GetTags(d *schema.ResourceData, k string) map[string]string {
tags := make(map[string]string)
if raw, ok := d.GetOk(k); ok {
for k, v := range raw.(map[string]interface{}) {
tags[k] = v.(string)
}
}
return tags
}
func BuildToken() string {
buf := make([]byte, 16)
_, _ = rand.Read(buf)
return base64.StdEncoding.EncodeToString(buf)
}
func FormatUnixTime(n uint64) string {
return time.Unix(int64(n), 0).UTC().Format("2006-01-02T15:04:05Z")
}
func ParseTime(s string) (time.Time, error) {
return time.ParseInLocation("2006-01-02T15:04:05Z", s, time.UTC)
}
// compose all schema.SchemaValidateFunc to a schema.SchemaValidateFunc,
// like resource.ComposeTestCheckFunc, so that we can reuse exist schema.SchemaValidateFunc
// and reduce custom schema.SchemaValidateFunc codes size.
func ComposeValidateFunc(fns ...schema.SchemaValidateFunc) schema.SchemaValidateFunc {
return func(v interface{}, k string) (wssRet []string, errsRet []error) {
for _, fn := range fns {
wss, errs := fn(v, k)
if len(errs) > 0 {
errsRet = append(errsRet, errs...)
return
}
wssRet = append(wssRet, wss...)
}
return
}
}
// CheckIfSetTogether will check all args, they should be all nil or not nil.
//
// Such as vpc_id and subnet_id should set together, or don't set them.
func CheckIfSetTogether(d *schema.ResourceData, args ...string) error {
var notNil bool
for _, arg := range args {
if _, ok := d.GetOk(arg); ok {
notNil = true
continue
}
if notNil {
return errors.Errorf("%v must be set together", args)
}
}
return nil
}
func StringsContain(ss []string, str string) bool {
for _, s := range ss {
if str == s {
return true
}
}
return false
}
func DiffSupressJSON(k, olds, news string, d *schema.ResourceData) bool {
var oldJson interface{}
err := json.Unmarshal([]byte(olds), &oldJson)
if err != nil {
return olds == news
}
var newJson interface{}
err = json.Unmarshal([]byte(news), &newJson)
if err != nil {
return olds == news
}
flag := reflect.DeepEqual(oldJson, newJson)
return flag
}
/*
Serialize slice into the usage document
eg["status_change","abnormal"] will be "`abnormal`,`status_change`"
*/
func SliceFieldSerialize(slice []string) string {
types := []string{}
for _, v := range slice {
types = append(types, "`"+v+"`")
}
sort.Strings(types)
return strings.Trim(strings.Join(types, ","), ",")
}
// InterfacesHeadMap returns string key map if argument is MaxItem: 1 List Type
func InterfacesHeadMap(d *schema.ResourceData, key string) (result map[string]interface{}, ok bool) {
v, ok := d.GetOk(key)
if !ok {
return
}
interfaces, ok := v.([]interface{})
if !ok || len(interfaces) == 0 {
ok = false
return
}
head := interfaces[0]
result, ok = head.(map[string]interface{})
return
}
// ConvertInterfacesHeadToMap returns string key map if argument is MaxItem: 1 List Type
func ConvertInterfacesHeadToMap(v interface{}) (result map[string]interface{}, ok bool) {
interfaces, ok := v.([]interface{})
if !ok || len(interfaces) == 0 {
ok = false
return
}
head := interfaces[0]
result, ok = head.(map[string]interface{})
return
}
// CovertInterfaceMapToStrPtr returns [string:string] map from a [string:interface] map
func CovertInterfaceMapToStrPtr(m map[string]interface{}) map[string]*string {
result := make(map[string]*string)
for k, v := range m {
if s, ok := v.(string); ok {
result[k] = &s
}
}
return result
}
func SetMapInterfaces(d *schema.ResourceData, key string, values ...map[string]interface{}) error {
val := make([]interface{}, 0, len(values))
for i := range values {
item := values[i]
val = append(val, item)
}
return d.Set(key, val)
}
func InterfaceToMap(d map[string]interface{}, key string) (result map[string]interface{}, ok bool) {
if v, ok := d[key]; ok {
if len(v.([]interface{})) != 1 || v.([]interface{})[0] == nil {
return nil, false
}
return v.([]interface{})[0].(map[string]interface{}), true
}
return nil, false
}
func ImportWithDefaultValue(defaultValues map[string]interface{}) schema.StateFunc {
return func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
for k, v := range defaultValues {
_ = d.Set(k, v)
}
return []*schema.ResourceData{d}, nil
}
}
func ImmutableArgsChek(d *schema.ResourceData, arguments ...string) error {
for _, v := range arguments {
if d.HasChange(v) {
o, _ := d.GetChange(v)
_ = d.Set(v, o)
return fmt.Errorf("argument `%s` cannot be changed", v)
}
}
return nil
}
func IsEmptyStr(s *string) bool {
if s == nil {
return true
}
return *s == ""
}
func MapToString(param map[string]interface{}) (string, bool) {
data, err := json.Marshal(param)
if err != nil {
return "", false
}
return string(data), true
}
func JsonToMap(str string) (map[string]interface{}, error) {
if str == "" {
return nil, nil
}
var temp map[string]interface{}
err := json.Unmarshal([]byte(str), &temp)
if err != nil {
return nil, err
}
return temp, nil
}
func CheckElementsExist(slice1 []string, slice2 []string) (bool, []string) {
exist := true
diff := make([]string, 0)
slice1Map := make(map[string]bool)
slice2Map := make(map[string]bool)
for _, element := range slice1 {
slice1Map[element] = true
}
for _, element := range slice2 {
slice2Map[element] = true
}
for _, element := range slice1 {
if _, ok := slice2Map[element]; !ok {
exist = false
break
}
}
if exist {
for _, element := range slice2 {
if _, ok := slice1Map[element]; !ok {
diff = append(diff, element)
}
}
}
return exist, diff
}