-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
234 lines (214 loc) · 6.59 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package appgate
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"sort"
"strings"
"github.com/appgate/sdp-api-client-go/api/v15/openapi"
"github.com/appgate/terraform-provider-appgatesdp/appgate/hashcode"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func mergeSchemaMaps(maps ...map[string]*schema.Schema) map[string]*schema.Schema {
result := make(map[string]*schema.Schema)
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}
func baseEntitySchema() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "Name of the object.",
Required: true,
},
"notes": {
Type: schema.TypeString,
Description: "Notes for the object. Used for documentation purposes.",
Default: DefaultDescription,
Optional: true,
},
}
return mergeSchemaMaps(s, baseTagsSchema())
}
func baseTagsSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"tags": tagsSchema(),
}
}
func tagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Description: "Array of tags.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
Set: func(v interface{}) int {
var buf bytes.Buffer
str := v.(string)
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(str)))
return hashcode.String(buf.String())
},
}
}
func readBaseEntityFromConfig(d *schema.ResourceData) (*openapi.BaseEntity, error) {
base := &openapi.BaseEntity{}
base.Id = uuid.New().String()
if v, ok := d.GetOk("name"); ok {
base.SetName(v.(string))
}
if v, ok := d.GetOk("notes"); ok {
base.SetNotes(v.(string))
}
if _, ok := d.GetOk("tags"); ok {
base.SetTags(schemaExtractTags(d))
}
return base, nil
}
// prettyPrintAPIError is used to show a formatted error message
// from a HTTP 400-503 response from the http client.
func prettyPrintAPIError(err error) error {
if err, ok := err.(openapi.GenericOpenAPIError); ok {
model := err.Model()
if err, ok := model.(openapi.Error); ok {
return fmt.Errorf("%s - %s", err.GetId(), err.GetMessage())
}
if err, ok := model.(openapi.ValidationError); ok {
var ValidationErrors string
errorMessage := "Validation error"
for _, ve := range err.GetErrors() {
ValidationErrors = ValidationErrors + ve.GetField() + " " + ve.GetMessage() + "\n"
}
if msg, o := err.GetMessageOk(); o {
errorMessage = fmt.Sprintf("%s %s", errorMessage, *msg)
}
return fmt.Errorf("%s \n %s", errorMessage, ValidationErrors)
}
return fmt.Errorf("%s", err.Error())
}
return fmt.Errorf("%+v", err)
}
func schemaExtractTags(d *schema.ResourceData) []string {
rawtags := d.Get("tags").(*schema.Set).List()
tags := make([]string, 0)
for _, raw := range rawtags {
tags = append(tags, strings.ToLower(raw.(string)))
}
return tags
}
func listToMapList(in []interface{}) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
for _, a := range in {
source := a.(map[string]interface{})
result = append(result, source)
}
return result, nil
}
func readArrayOfStringsFromConfig(list []interface{}) ([]string, error) {
result := make([]string, 0)
for _, item := range list {
if item == nil {
continue
}
result = append(result, item.(string))
}
return result, nil
}
// validateIPaddress validate both IPv4 and IPv6 addresses.
func validateIPaddress(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if net.ParseIP(value) == nil {
errors = append(errors, fmt.Errorf("Invalid ip address, got %s", value))
return
}
return
}
func inArray(needle string, haystack []string) bool {
sort.Strings(haystack)
i := sort.Search(len(haystack),
func(i int) bool { return haystack[i] >= needle })
if i < len(haystack) && haystack[i] == needle {
return true
}
return false
}
// getResourceFileContent gets content from "file" filepath schema.ResourceData or string payload "content".
func getResourceFileContent(d *schema.ResourceData) ([]byte, error) {
var content []byte
if v, ok := d.GetOk("file"); ok {
path := v.(string)
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Error opening file (%s): %s", path, err)
}
defer func() {
err := file.Close()
if err != nil {
log.Printf("[WARN] Error closing file (%s): %s", path, err)
}
}()
reader := bufio.NewReader(file)
content, err = ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("Error reading file (%s): %s", path, err)
}
} else if v, ok := d.GetOk("content"); ok {
content = []byte(v.(string))
}
return content, nil
}
// suppressMissingOptionalConfigurationBlock handles configuration block attributes in the following scenario:
// * The resource schema includes an optional configuration block with defaults
// * The API response includes those defaults to refresh into the Terraform state
// * The operator's configuration omits the optional configuration block
func suppressMissingOptionalConfigurationBlock(k, old, new string, d *schema.ResourceData) bool {
return old == "1" && new == "0"
}
func convertStringArrToInterface(strs []string) []interface{} {
arr := make([]interface{}, len(strs))
for i, str := range strs {
arr[i] = str
}
return arr
}
// Nprintf is a Printf sibling (Nprintf; Named Printf), which handles strings like
// Nprintf("Hello %{target}!", map[string]interface{}{"target":"world"}) == "Hello world!".
// This is particularly useful for generated tests, where we don't want to use Printf,
// since that would require us to generate a very particular ordering of arguments.
func Nprintf(format string, params map[string]interface{}) string {
for key, val := range params {
format = strings.Replace(format, "%{"+key+"}", fmt.Sprintf("%v", val), -1)
}
return format
}
func applianceStatsRetryable(ctx context.Context, meta interface{}) *resource.RetryError {
statsAPI := meta.(*Client).API.ApplianceStatsApi
token := meta.(*Client).Token
stats, _, err := statsAPI.StatsAppliancesGet(ctx).Authorization(token).Execute()
if err != nil {
return resource.RetryableError(err)
}
for _, data := range stats.GetData() {
// If any controller is marked as busy, we will treat this as a retryable error.
status := data.GetStatus()
if status == "busy" {
ctrl := data.GetController()
if ctrl.GetStatus() != "n/a" {
return resource.RetryableError(fmt.Errorf("appliance is %s, waiting", status))
}
}
}
return nil
}