-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
178 lines (163 loc) · 4.77 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
package appgate
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"sort"
"github.com/appgate/sdp-api-client-go/api/v14/openapi"
"github.com/google/uuid"
"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 {
return 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,
},
"tags": {
Type: schema.TypeSet,
Description: "Array of tags.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}
}
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, 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
}