forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
147 lines (130 loc) · 3.73 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
package huaweicloud
import (
"fmt"
"reflect"
"regexp"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/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 replaceVars(d *schema.ResourceData, linkTmpl string, kv map[string]interface{}) (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 convertToStr(v)
}
}
if m == "project" {
return "replace_holder"
}
if d != nil {
if m == "id" {
return d.Id()
}
v, ok := d.GetOk(m)
if ok {
return convertToStr(v)
}
}
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 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 convertToStr(v interface{}) string {
return fmt.Sprintf("%v", v)
}
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()
}