This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transport.go
210 lines (185 loc) · 5.14 KB
/
transport.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
package opentelekomcloud
import (
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
var successHTTPCodes = []int{200, 201, 202, 203, 204, 205, 206, 207, 208, 226}
func isEmptyValue(v reflect.Value) (bool, error) {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0, nil
case reflect.Bool:
return !v.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0, nil
case reflect.Float32, reflect.Float64:
return v.Float() == 0, nil
case reflect.Interface, reflect.Ptr:
return v.IsNil(), nil
case reflect.Invalid:
return true, nil
}
return false, fmt.Errorf("isEmptyValue:: unknown type")
}
func addQueryParams(rawurl string, params map[string]string) (string, error) {
u, err := url.Parse(rawurl)
if err != nil {
return "", err
}
q := u.Query()
for k, v := range params {
q.Set(k, v)
}
u.RawQuery = q.Encode()
return u.String(), nil
}
func replaceVars(d *schema.ResourceData, linkTmpl string, kv map[string]string) (string, error) {
re := regexp.MustCompile("{([[:word:]]+)}")
replaceFunc := func(s string) string {
m := re.FindStringSubmatch(s)[1]
if kv != nil {
if v, ok := kv[m]; ok {
return v
}
}
if m == "project" {
return "replace_holder"
}
if d != nil {
if m == "id" {
return d.Id()
}
v, ok := d.GetOk(m)
if ok {
v1, _ := convertToStr(v)
return v1
}
}
return ""
}
s := re.ReplaceAllStringFunc(linkTmpl, replaceFunc)
return strings.Replace(s, "replace_holder/", "", 1), nil
}
func replaceVarsForTest(rs *terraform.ResourceState, linkTmpl string) (string, error) {
re := regexp.MustCompile("{([[:word:]]+)}")
replaceFunc := func(s string) string {
m := re.FindStringSubmatch(s)[1]
if m == "project" {
return "replace_holder"
}
if rs != nil {
if m == "id" {
return rs.Primary.ID
}
v, ok := rs.Primary.Attributes[m]
if ok {
return v
}
}
return ""
}
s := re.ReplaceAllStringFunc(linkTmpl, replaceFunc)
return strings.Replace(s, "replace_holder/", "", 1), nil
}
func navigateMap(d interface{}, index []string) (interface{}, error) {
for _, i := range index {
d1, ok := d.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("navigateMap:: Can not convert to map")
}
d, ok = d1[i]
if !ok {
return nil, fmt.Errorf("navigateMap:: '%s' may not exist", i)
}
}
return d, nil
}
func navigateValue(d interface{}, index []string, arrayIndex map[string]int) (interface{}, error) {
for n, i := range index {
if d == nil {
return nil, nil
}
if d1, ok := d.(map[string]interface{}); ok {
d, ok = d1[i]
if !ok {
msg := fmt.Sprintf("navigate value with index(%s)", strings.Join(index, "."))
return nil, fmt.Errorf("%s: '%s' may not exist", msg, i)
}
} else {
msg := fmt.Sprintf("navigate value with index(%s)", strings.Join(index, "."))
return nil, fmt.Errorf("%s: Can not convert (%s) to map", msg, reflect.TypeOf(d))
}
if arrayIndex != nil {
if j, ok := arrayIndex[strings.Join(index[:n+1], ".")]; ok {
if d == nil {
return nil, nil
}
if d2, ok := d.([]interface{}); ok {
if len(d2) == 0 {
return nil, nil
}
if j >= len(d2) {
msg := fmt.Sprintf("navigate value with index(%s)", strings.Join(index, "."))
return nil, fmt.Errorf("%s: The index is out of array", msg)
}
d = d2[j]
} else {
msg := fmt.Sprintf("navigate value with index(%s)", strings.Join(index, "."))
return nil, fmt.Errorf("%s: Can not convert (%s) to array, index=%s.%v", msg, reflect.TypeOf(d), i, j)
}
}
}
}
return d, nil
}
func isUserInput(d *schema.ResourceData, index []string, arrayIndex map[string]int) bool {
var r = make([]string, 0, len(index)*2)
for n, i := range index {
r = append(r, i)
if arrayIndex != nil {
if j, ok := arrayIndex[strings.Join(index[:n+1], ".")]; ok {
r = append(r, strconv.Itoa(j))
}
}
}
_, e := d.GetOkExists(strings.Join(r[:len(r)], "."))
return e
}
func convertToInt(v interface{}) (interface{}, error) {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
return strconv.ParseInt(strVal, 10, 64)
}
return nil, fmt.Errorf("can not convert to integer")
}
func convertToStr(v interface{}) (string, error) {
if s, ok := v.(string); ok {
return s, nil
} else if i, ok := v.(int); ok {
return strconv.Itoa(i), nil
} else if b, ok := v.(bool); ok {
return strconv.FormatBool(b), nil
}
return "", fmt.Errorf("can't convert to string")
}
func waitToFinish(target, pending []string, timeout, interval time.Duration, f resource.StateRefreshFunc) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Target: target,
Pending: pending,
Refresh: f,
Timeout: timeout,
Delay: 5 * time.Second,
MinTimeout: interval,
}
return stateConf.WaitForState()
}