generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
utils.go
97 lines (77 loc) · 2.16 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
package utils
import (
"fmt"
"hash/crc32"
"strings"
)
const CacDescription string = "This resource uses the config-as-code API's. When updating the `name` or `path` of this resource you should typically also set the `create_before_destroy = true` lifecycle setting."
const NextgenDescription string = "This resource is part of the Harness nextgen platform."
func ConfigAsCodeDescription(descripton string) string {
return fmt.Sprintf("%s %s", descripton, CacDescription)
}
func GetNextgenDescription(description string) string {
return fmt.Sprintf("[NG] - %s %s", description, NextgenDescription)
}
func ExpandDelegateSelectors(ds []interface{}) []string {
selectors := make([]string, 0)
for _, v := range ds {
selectors = append(selectors, v.(string))
}
return selectors
}
func FlattenDelgateSelectors(ds []string) []interface{} {
selectors := make([]interface{}, 0)
for _, v := range ds {
selectors = append(selectors, v)
}
return selectors
}
func InterfaceSliceToStringSlice(slice []interface{}) []string {
ss := make([]string, 0)
for _, v := range slice {
ss = append(ss, v.(string))
}
return ss
}
func ExpandTags(tags []interface{}) map[string]string {
result := map[string]string{}
for _, tag := range tags {
parts := strings.Split(tag.(string), ":")
result[parts[0]] = parts[1]
}
return result
}
func FlattenTags(tags map[string]string) []string {
var result []string
for k, v := range tags {
result = append(result, k+":"+v)
}
return result
}
func GetConflictsWithSlice(source []string, self string) []string {
tmp := make([]string, len(source))
copy(tmp, source)
for i, v := range tmp {
if v == self {
return append(tmp[:i], tmp[i+1:]...)
}
}
return tmp
}
// Borrowed from https://github.com/hashicorp/terraform-provider-aws/blob/main/internal/create/hashcode.go
// StringHashcode hashes a string to a unique hashcode.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
func StringHashcode(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}