/
helpers.go
147 lines (123 loc) · 3.56 KB
/
helpers.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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"fmt"
"math/rand"
"time"
"strconv"
"encoding/json"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-oci/httpreplay"
)
const charset = charsetWithoutDigits + "0123456789"
const charsetWithoutDigits = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func literalTypeHashCodeForSets(m interface{}) int {
return hashcode.String(fmt.Sprintf("%v", m))
}
func validateBoolInSlice(valid []bool) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(bool)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be bool", k))
return
}
for _, str := range valid {
if v == str {
return
}
}
es = append(es, fmt.Errorf("expected %s to be one of %v, got %t", k, valid, v))
return
}
}
func validateNotEmptyString() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
if len(v) == 0 {
es = append(es, fmt.Errorf("%s cannot be an empty string", k))
}
return
}
}
func objectMapToStringMap(rm map[string]interface{}) map[string]string {
result := map[string]string{}
for k, v := range rm {
result[k] = v.(string)
}
return result
}
func validateInt64TypeString(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := strconv.ParseInt(value, 10, 64)
if err != nil {
errors = append(errors, fmt.Errorf("%q (%q) must be a 64-bit integer", k, v))
}
return
}
func int64StringDiffSuppressFunction(key string, old string, new string, d *schema.ResourceData) bool {
// We may get interpolation syntax in this function call as well; so be sure to check for errors.
oldIntVal, err := strconv.ParseInt(old, 10, 64)
if err != nil {
return false
}
newIntVal, err := strconv.ParseInt(new, 10, 64)
if err != nil {
return false
}
return oldIntVal == newIntVal
}
// Ignore differences in floating point numbers after the second decimal place, ex: 1.001 == 1.002
func monetaryDiffSuppress(key string, old string, new string, d *schema.ResourceData) bool {
oldVal, err := strconv.ParseFloat(old, 10)
if err != nil {
return false
}
newVal, err := strconv.ParseFloat(new, 10)
if err != nil {
return false
}
return fmt.Sprintf("%.2f", oldVal) == fmt.Sprintf("%.2f", newVal)
}
func timeDiffSuppressFunction(key string, old string, new string, d *schema.ResourceData) bool {
oldTime, err := time.Parse(time.RFC3339Nano, old)
if err != nil {
return false
}
newTime, err := time.Parse(time.RFC3339Nano, new)
if err != nil {
return false
}
return oldTime.Equal(newTime)
}
func convertMapOfStringSlicesToMapOfStrings(rm map[string][]string) (map[string]string, error) {
result := map[string]string{}
for k, v := range rm {
val, err := json.Marshal(v)
if err == nil {
result[k] = string(val)
} else {
return nil, err
}
}
return result, nil
}
func randomString(length int, charset string) string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func randomStringOrHttpReplayValue(length int, charset string, httpReplayValue string) string {
if httpreplay.ModeRecordReplay() {
return httpReplayValue
}
return randomString(length, charset)
}