-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.go
200 lines (169 loc) · 4.91 KB
/
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
package utils
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func FilterZoneIDs(vs []upcloud.Zone, f func(upcloud.Zone) bool) []string {
vsf := make([]string, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v.ID)
}
}
return vsf
}
func FilterZones(vs []upcloud.Zone, f func(upcloud.Zone) bool) []upcloud.Zone {
vsf := make([]upcloud.Zone, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
func FilterNetworks(vs []upcloud.Network, fns ...func(upcloud.Network) (bool, error)) ([]upcloud.Network, error) {
vsf := []upcloud.Network{}
for _, v := range vs {
matched := true
for _, fn := range fns {
m, err := fn(v)
if err != nil {
return nil, err
}
if !m {
matched = false
break
}
}
if matched {
vsf = append(vsf, v)
}
}
return vsf, nil
}
// WithRetry attempts to call the provided function until it has been successfully called or the number of calls exceeds retries delaying the consecutive calls by given delay
func WithRetry(fn func() (interface{}, error), retries int, delay time.Duration) (interface{}, error) {
var err error
var res interface{}
for count := 0; true; count++ {
if delay > 0 {
time.Sleep(delay)
}
if count >= retries {
break
}
res, err = fn()
if err == nil {
return res, nil
}
continue
}
return nil, err
}
// ExpandStrings expands a terraform list to slice of str
func ExpandStrings(data interface{}) []string {
strSlice := []string{}
for _, s := range data.(*schema.Set).List() {
strSlice = append(strSlice, s.(string))
}
return strSlice
}
// SetOfStringsToSlice transforms a terraform set of strings to a slice of strings
func SetOfStringsToSlice(ctx context.Context, data interface{}) ([]string, error) {
result := []string{}
providerErrMsg := "provider error: failed to transform set data"
debugLogPrefix := "transforming set of strings into slice failed;"
stringsSet, ok := data.(*schema.Set)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected input data to be a schema.TypeSet but received %T", debugLogPrefix, data))
return result, fmt.Errorf(providerErrMsg)
}
for _, val := range stringsSet.List() {
valStr, ok := val.(string)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected set elements to be of type string but received %T", debugLogPrefix, val))
return result, fmt.Errorf(providerErrMsg)
}
result = append(result, valStr)
}
return result, nil
}
// MapOfStringsToLabelSlice transforms a terraform map of strings to a LabelSlice
func MapOfStringsToLabelSlice(ctx context.Context, data interface{}) (upcloud.LabelSlice, error) {
result := upcloud.LabelSlice{}
providerErrMsg := "provider error: failed to transform labels data"
debugLogPrefix := "transforming map of strings into labels slice failed;"
labelsMap, ok := data.(map[string]interface{})
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected input data to be a map of strings but received %T", debugLogPrefix, data))
return result, fmt.Errorf(providerErrMsg)
}
for k, v := range labelsMap {
value, ok := v.(string)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected map elements to be of type string but received %T", debugLogPrefix, v))
return result, fmt.Errorf(providerErrMsg)
}
result = append(result, upcloud.Label{
Key: k,
Value: value,
})
}
return result, nil
}
// LabelSliceToMap transorms `upcloud.LabelSlice` into a map of strings.
// This can be used to set labels fetched from the API into a state
func LabelSliceToMap(data upcloud.LabelSlice) map[string]string {
result := map[string]string{}
for _, label := range data {
result[label.Key] = label.Value
}
return result
}
// SliceOfStringToServerUUIDSlice converts slice of strings into `upcloud.ServerUUIDSlice`
func SliceOfStringToServerUUIDSlice(strs []string) upcloud.ServerUUIDSlice {
result := make(upcloud.ServerUUIDSlice, len(strs))
copy(result, strs)
return result
}
// StorageAddressFormat takes the address in any format and extracts the bus
// type only (ide/scsi/virtio)
func StorageAddressFormat(address string) string {
if ret := strings.Split(address, ":"); len(ret) > 0 {
return ret[0]
}
return ""
}
// StorageAddressPositionFormat takes the address in any format and extracts the bus
// position only
func StorageAddressPositionFormat(address string) string {
if ret := strings.SplitN(address, ":", 2); len(ret) > 0 {
return ret[1]
}
return ""
}
func EnvKeyExists(keyPrefix string) bool {
for _, env := range os.Environ() {
if strings.HasPrefix(env, keyPrefix) {
return true
}
}
return false
}
func JoinSchemas(dst map[string]*schema.Schema, s ...map[string]*schema.Schema) map[string]*schema.Schema {
if len(s) == 0 {
return dst
}
for i := range s {
for key, value := range s[i] {
dst[key] = value
}
}
return dst
}