This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
/
utils.go
113 lines (99 loc) · 2.67 KB
/
utils.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
package ksyun
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
)
func SchemaSetToInstanceMap(s interface{}, prefix string, input *map[string]interface{}) {
count := int(0)
for _, v := range s.(*schema.Set).List() {
count = count + 1
(*input)[prefix+"."+strconv.Itoa(count)] = v
}
}
func SchemaSetToFilterMap(s interface{}, prefix string, index int, input *map[string]interface{}) {
(*input)["Filter."+strconv.Itoa(index)+".Name"] = prefix
count := int(0)
for _, v := range s.(*schema.Set).List() {
count = count + 1
(*input)["Filter."+strconv.Itoa(index)+".Value."+strconv.Itoa(count)] = v
}
}
func SchemaSetsToFilterMap(d *schema.ResourceData, filters []string, req *map[string]interface{}) *map[string]interface{} {
index := 0
for _, v := range filters {
var idsString []string
if ids, ok := d.GetOk(v); ok {
idsString = SchemaSetToStringSlice(ids)
}
if len(idsString) > 0 {
index++
(*req)[fmt.Sprintf("Filter.%v.Name", index)] = strings.Replace(v, "_", "-", -1)
}
for k1, v1 := range idsString {
if v1 == "" {
continue
}
(*req)[fmt.Sprintf("Filter.%v.Value.%d", index, k1+1)] = v1
}
}
return req
}
func hashStringArray(arr []string) string {
var buf bytes.Buffer
for _, s := range arr {
buf.WriteString(fmt.Sprintf("%s-", s))
}
return fmt.Sprintf("%d", hashcode.String(buf.String()))
}
func writeToFile(filePath string, data interface{}) error {
absPath, err := getAbsPath(filePath)
if err != nil {
return err
}
os.Remove(absPath)
var bs []byte
switch data := data.(type) {
case string:
bs = []byte(data)
default:
bs, err = json.MarshalIndent(data, "", "\t")
if err != nil {
return fmt.Errorf("MarshalIndent data %#v and got an error: %#v", data, err)
}
}
return ioutil.WriteFile(absPath, bs, 0422)
}
func getAbsPath(filePath string) (string, error) {
if strings.HasPrefix(filePath, "~") {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("get current user got an error: %#v", err)
}
if usr.HomeDir != "" {
filePath = strings.Replace(filePath, "~", usr.HomeDir, 1)
}
}
return filepath.Abs(filePath)
}
func merageResultDirect(result *[]map[string]interface{}, source []interface{}) {
for _, v := range source {
*result = append(*result, v.(map[string]interface{}))
}
}
// schemaSetToStringSlice used for converting terraform schema set to a string slice
func SchemaSetToStringSlice(s interface{}) []string {
vL := []string{}
for _, v := range s.(*schema.Set).List() {
vL = append(vL, v.(string))
}
return vL
}