/
structure.go
368 lines (324 loc) · 11.2 KB
/
structure.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package vcd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func expandIPRange(configured []interface{}) (types.IPRanges, error) {
ipRange := make([]*types.IPRange, 0, len(configured))
for _, ipRaw := range configured {
data := ipRaw.(map[string]interface{})
startAddress := data["start_address"].(string)
endAddress := data["end_address"].(string)
ip := types.IPRange{
StartAddress: startAddress,
EndAddress: endAddress,
}
ipRange = append(ipRange, &ip)
}
ipRanges := types.IPRanges{
IPRange: ipRange,
}
return ipRanges, nil
}
func getProtocol(protocol types.FirewallRuleProtocols) string {
if protocol.TCP {
return "tcp"
}
if protocol.TCP && protocol.UDP {
return "tcp&udp"
}
if protocol.UDP {
return "udp"
}
if protocol.ICMP {
return "icmp"
}
return "any"
}
func convertToStringMap(param map[string]interface{}) map[string]string {
temp := make(map[string]string)
for k, v := range param {
temp[k] = v.(string)
}
return temp
}
// filterVdcId returns a bare UUID if the initial value contains a VDC ID
// otherwise it returns the initial value
func filterVdcId(i interface{}) string {
s := i.(string)
if strings.HasPrefix(s, "urn:vcloud:vdc:") {
return extractUuid(s)
}
return s
}
// convertSchemaSetToSliceOfStrings accepts Terraform's *schema.Set object and converts it to slice
// of strings.
// This is useful for extracting values from a set of strings
func convertSchemaSetToSliceOfStrings(param *schema.Set) []string {
paramList := param.List()
result := make([]string, len(paramList))
for index, value := range paramList {
result[index] = fmt.Sprint(value)
}
return result
}
// convertTypeListToSliceOfStrings accepts Terraform's TypeList structure `[]interface{}` and
// converts it to slice of strings.
func convertTypeListToSliceOfStrings(param []interface{}) []string {
result := make([]string, len(param))
for i, v := range param {
result[i] = v.(string)
}
return result
}
// convertStringsToTypeSet accepts a slice of strings and returns a *schema.Set suitable for storing in Terraform
// set of strings
func convertStringsToTypeSet(param []string) *schema.Set {
sliceOfInterfaces := make([]interface{}, len(param))
for index, value := range param {
sliceOfInterfaces[index] = value
}
set := schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), sliceOfInterfaces)
return set
}
// addrOf is a generic function to return the address of a variable
// Note. It is mainly meant for converting literal values to pointers (e.g. `addrOf(true)`) or cases
// for converting variables coming out straight from Terraform schema (e.g.
// `addrOf(d.Get("name").(string))`).
func addrOf[T any](variable T) *T {
return &variable
}
// stringPtrOrNil takes a string and returns a pointer to it, but if the string is empty, returns nil
func stringPtrOrNil(s string) *string {
if s == "" {
return nil
}
return &s
}
// getStringAttributeAsPointer returns a pointer to the value of the given attribute from the current resource data.
// If the attribute is empty, returns a nil pointer.
func getStringAttributeAsPointer(d *schema.ResourceData, attrName string) *string {
attributeValue := d.Get(attrName).(string)
if attributeValue == "" {
return nil
}
return &attributeValue
}
// getUuidRegex returns a regular expression that matches UUIDs used by VCD
func getUuidRegex(prefix, suffix string) *regexp.Regexp {
uuidRegexFormat := fmt.Sprintf("%s[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}%s", prefix, suffix)
return regexp.MustCompile(uuidRegexFormat)
}
// extractUuid finds an UUID in the input string
// Returns an empty string if no UUID was found
func extractUuid(input string) string {
reGetID := getUuidRegex("(", ")")
matchListIds := reGetID.FindAllStringSubmatch(input, -1)
if len(matchListIds) > 0 && len(matchListIds[0]) > 0 {
return matchListIds[len(matchListIds)-1][len(matchListIds[0])-1]
}
return ""
}
// normalizeId checks if the ID contains a wanted prefix
// If it does, the function returns the original ID.
// Otherwise, it returns the prefix + the ID
func normalizeId(prefix, id string) string {
if strings.Contains(id, prefix) {
return id
}
return prefix + id
}
// haveSameUuid compares two IDs (or HREF)
// and returns true if the UUID part of the two input strings are the same.
// This is useful when comparing a HREF to a ID, or a HREF from an admin path
// to a HREF from a regular user path.
func haveSameUuid(s1, s2 string) bool {
return extractUuid(s1) == extractUuid(s2)
}
// extractIdsFromOpenApiReferences extracts []string with IDs from []types.OpenApiReference which contains ID and Names
func extractIdsFromOpenApiReferences(refs []types.OpenApiReference) []string {
resultStrings := make([]string, len(refs))
for index := range refs {
resultStrings[index] = refs[index].ID
}
return resultStrings
}
// extractNamesFromOpenApiReferences extracts []string with names from []types.OpenApiReference which contains ID and Names
func extractNamesFromOpenApiReferences(refs []types.OpenApiReference) []string {
resultStrings := make([]string, len(refs))
for index := range refs {
resultStrings[index] = refs[index].Name
}
return resultStrings
}
// extractIdsFromReferences extracts []string with IDs from []*types.Reference which contains ID and Names
func extractIdsFromReferences(refs []*types.Reference) []string {
resultStrings := make([]string, len(refs))
for index := range refs {
resultStrings[index] = refs[index].ID
}
return resultStrings
}
// convertSliceOfStringsToOpenApiReferenceIds converts []string to []types.OpenApiReference by filling
// types.OpenApiReference.ID fields
func convertSliceOfStringsToOpenApiReferenceIds(ids []string) []types.OpenApiReference {
resultReferences := make([]types.OpenApiReference, len(ids))
for i, v := range ids {
resultReferences[i].ID = v
}
return resultReferences
}
// contains returns true if `sliceToSearch` contains `searched`. Returns false otherwise.
func contains(sliceToSearch []string, searched string) bool {
found := false
for _, idInSlice := range sliceToSearch {
if searched == idInSlice {
found = true
break
}
}
return found
}
// jsonToCompactString transforms an unmarshalled JSON in form of a map of string->any to a plain string without any spacing.
func jsonToCompactString(inputJson map[string]interface{}) (string, error) {
rawJson, err := json.Marshal(inputJson)
if err != nil {
return "", err
}
compactedJson := new(bytes.Buffer)
err = json.Compact(compactedJson, rawJson)
if err != nil {
return "", err
}
return compactedJson.String(), nil
}
// areMarshaledJsonEqual compares that two marshaled JSON strings are equal or not. Returns an error if something
// wrong happens during the comparison process.
func areMarshaledJsonEqual(json1, json2 []byte) (bool, error) {
if !json.Valid(json1) {
return false, fmt.Errorf("first JSON is not valid: '%s'", json1)
}
if !json.Valid(json2) {
return false, fmt.Errorf("second JSON is not valid: '%s'", json2)
}
var unmarshaledJson1, unmarshaledJson2 interface{}
err := json.Unmarshal(json1, &unmarshaledJson1)
if err != nil {
return false, fmt.Errorf("could not unmarshal first JSON '%s': %s", json1, err)
}
err = json.Unmarshal(json2, &unmarshaledJson2)
if err != nil {
return false, fmt.Errorf("could not unmarshal second JSON '%s': %s", json2, err)
}
return reflect.DeepEqual(unmarshaledJson1, unmarshaledJson2), nil
}
// createOrUpdateMetadata creates or updates metadata entries for the given resource and attribute name
// TODO: This function implementation should be replaced with the implementation of `createOrUpdateMetadataEntryInVcd`
// once "metadata" field is removed.
func createOrUpdateMetadata(d *schema.ResourceData, resource metadataCompatible, attributeName string) error {
// We invoke the new "metadata_entry" metadata creation here to have it centralized and reduce duplication.
// Ideally, once "metadata" is removed in a new major version, the implementation of `createOrUpdateMetadataEntryInVcd` should
// just go here in the `createOrUpdateMetadata` body.
err := createOrUpdateMetadataEntryInVcd(d, resource)
if err != nil {
return err
}
if d.HasChange(attributeName) && !d.HasChange("metadata_entry") {
oldRaw, newRaw := d.GetChange(attributeName)
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
err = resource.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
if len(newMetadata) > 0 {
err = resource.MergeMetadata(types.MetadataStringValue, newMetadata)
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
}
}
return nil
}
// stringOnNotNil returns the contents of a string pointer
// if the pointer is nil, returns an empty string
func stringOnNotNil(p *string) string {
if p == nil {
return ""
}
return *p
}
// Checks if a file exists
func fileExists(filename string) bool {
f, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
fileMode := f.Mode()
return fileMode.IsRegular()
}
// referenceToId is an auxiliary function to be used with ObjectMap
func referenceToId(reference *types.Reference) string {
if reference.ID != "" {
return reference.ID
}
return extractUuid(reference.HREF)
}
// referenceToName is an auxiliary function to be used with ObjectMap
func referenceToName(reference *types.Reference) string {
return reference.Name
}
// vimObjectRefToMoref is an auxiliary function to be used with ObjectMap
func vimObjectRefToMoref(input *types.VimObjectRef) string {
return input.MoRef
}
// ObjectMap extracts an array of wanted elements from an array of complex objects.
// The Input type is the complex object.
// The Output type could be a simple data type, such as a string or a number, but could
// also be a different object.
// The conversion is performed by the f function, which takes one complex input object and
// produces the wanted output.
// examples:
//
// ids := ObjectMap[*types.VimObjectRef, string](extendedProviderVdc.VMWProviderVdc.ResourcePoolRefs.VimObjectRef,
// vimObjectRefToMoref)
//
// ids := ObjectMap[*types.Reference, string](extendedProviderVdc.VMWProviderVdc.StorageProfiles.ProviderVdcStorageProfile,
// referenceToId)
//
// names := ObjectMap[*types.Reference, string](extendedProviderVdc.VMWProviderVdc.StorageProfiles.ProviderVdcStorageProfile,
// referenceToName)
func ObjectMap[Input any, Output any](input []Input, f func(Input) Output) []Output {
var result = make([]Output, len(input))
for i := 0; i < len(input); i++ {
result[i] = f(input[i])
}
return result
}
// firstNonEmpty returns the first non empty string from a list
// If all arguments are empty, returns an empty string
func firstNonEmpty(args ...string) string {
for _, s := range args {
if s != "" {
return s
}
}
return ""
}