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
/
util.go
169 lines (147 loc) · 4.61 KB
/
util.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
package opentelekomcloud
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/unknwon/com"
)
// BuildRequest takes an opts struct and builds a request body for
// Gophercloud to execute
func BuildRequest(opts interface{}, parent string) (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
b = AddValueSpecs(b)
return map[string]interface{}{parent: b}, nil
}
// CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
// sets the resource ID to the empty string instead of throwing an error.
func CheckDeleted(d *schema.ResourceData, err error, msg string) error {
_, ok := err.(golangsdk.ErrDefault404)
_, ok1 := err.(golangsdk.ErrDefault404)
if ok || ok1 {
d.SetId("")
return nil
}
return fmt.Errorf("%s: %s", msg, err)
}
// GetRegion returns the region that was specified in the resource. If a
// region was not set, the provider-level region is checked. The provider-level
// region can either be set by the region argument or by OS_REGION_NAME.
func GetRegion(d *schema.ResourceData, config *Config) string {
n := config.TenantName
if n == "" {
n = config.DelegatedProject
}
return strings.Split(n, "_")[0]
}
// GetProjectName returns the project name that was specified in the resource.
func GetProjectName(d *schema.ResourceData, config *Config) string {
projectName := d.Get("project_name").(string)
if projectName != "" {
return projectName
}
tenantName := config.TenantName
if tenantName == "" {
tenantName = config.DelegatedProject
}
return tenantName
}
// AddValueSpecs expands the 'value_specs' object and removes 'value_specs'
// from the request body.
func AddValueSpecs(body map[string]interface{}) map[string]interface{} {
if body["value_specs"] != nil {
for k, v := range body["value_specs"].(map[string]interface{}) {
body[k] = v
}
delete(body, "value_specs")
}
return body
}
// MapValueSpecs converts ResourceData into a map
func MapValueSpecs(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("value_specs").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
// MapResourceProp converts ResourceData property into a map
func MapResourceProp(d *schema.ResourceData, prop string) map[string]interface{} {
m := make(map[string]interface{})
for key, val := range d.Get(prop).(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
// List of headers that need to be redacted
var REDACT_HEADERS = []string{"x-auth-token", "x-auth-key", "x-service-token",
"x-storage-token", "x-account-meta-temp-url-key", "x-account-meta-temp-url-key-2",
"x-container-meta-temp-url-key", "x-container-meta-temp-url-key-2", "set-cookie",
"x-subject-token"}
// RedactHeaders processes a headers object, returning a redacted list
func RedactHeaders(headers http.Header) (processedHeaders []string) {
for name, header := range headers {
for _, v := range header {
if com.IsSliceContainsStr(REDACT_HEADERS, name) {
processedHeaders = append(processedHeaders, fmt.Sprintf("%v: %v", name, "***"))
} else {
processedHeaders = append(processedHeaders, fmt.Sprintf("%v: %v", name, v))
}
}
}
return
}
// FormatHeaders processes a headers object plus a deliminator, returning a string
func FormatHeaders(headers http.Header, seperator string) string {
redactedHeaders := RedactHeaders(headers)
sort.Strings(redactedHeaders)
return strings.Join(redactedHeaders, seperator)
}
func checkForRetryableError(err error) *resource.RetryError {
switch errCode := err.(type) {
case golangsdk.ErrDefault500:
return resource.RetryableError(err)
case golangsdk.ErrUnexpectedResponseCode:
switch errCode.Actual {
case 409, 503:
return resource.RetryableError(err)
default:
return resource.NonRetryableError(err)
}
default:
return resource.NonRetryableError(err)
}
}
func isResourceNotFound(err error) bool {
if err == nil {
return false
}
_, ok := err.(golangsdk.ErrDefault404)
_, ok1 := err.(golangsdk.ErrDefault404)
return ok || ok1
}
func expandToStringSlice(v []interface{}) []string {
s := make([]string, len(v))
for i, val := range v {
if strVal, ok := val.(string); ok {
s[i] = strVal
}
}
return s
}
// strSliceContains checks if a given string is contained in a slice
// When anybody asks why Go needs generics, here you go.
func strSliceContains(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}