-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.go
272 lines (252 loc) · 6.6 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
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
package utils
import (
"bytes"
"errors"
"fmt"
"math"
"sort"
"strconv"
"strings"
"github.com/vmware/govmomi/vim25/types"
)
// Min : get the minimum of values
func Min(n ...int64) int64 {
var min int64 = -1
for _, i := range n {
if i >= 0 {
if min == -1 {
min = i
} else {
if i < min {
min = i
}
}
}
}
return min
}
// Max : get the maximum of the values
func Max(n ...int64) int64 {
var max int64 = -1
for _, i := range n {
if i >= 0 {
if max == -1 {
max = i
} else {
if i > max {
max = i
}
}
}
}
return max
}
// Sum : Sum the values
func Sum(n ...int64) int64 {
var total int64
for _, i := range n {
if i > 0 {
total += i
}
}
return total
}
// Average : average the values
func Average(n ...int64) int64 {
var total int64
var count int64
for _, i := range n {
if i >= 0 {
count++
total += i
}
}
favg := float64(total) / float64(count)
return int64(math.Floor(favg + .5))
}
// MapObjRefs fills in object references into a map to another object reference
func MapObjRefs(property string, sourceVal *types.AnyType, dest map[string]*[]types.ManagedObjectReference, index string) error {
mors, ok := (*sourceVal).(types.ArrayOfManagedObjectReference)
if ok {
if len(mors.ManagedObjectReference) > 0 {
dest[index] = &mors.ManagedObjectReference
return nil
}
return errors.New("Property " + property + " of " + index + " didn't contain any object references")
}
return errors.New("Property " + property + " of " + index + " was not a ManagedObjectReferences, it was " + fmt.Sprintf("%T", sourceVal))
}
// MapObjRef fills in object reference into a map to another object reference
func MapObjRef(property string, sourceVal *types.AnyType, dest map[string]*string, index string) error {
mor, ok := (*sourceVal).(types.ManagedObjectReference)
if ok {
dest[index] = &mor.Value
return nil
}
return errors.New("Property " + property + " of " + index + " was not a ManagedObjectReference, it was " + fmt.Sprintf("%T", sourceVal))
}
// MapObjInt32 fills in an int32 into a map to another object reference
func MapObjInt32(property string, sourceVal *types.AnyType, dest map[string]*int32, index string) error {
val, ok := (*sourceVal).(int32)
if ok {
dest[index] = &val
return nil
}
return errors.New("Property " + property + " of " + index + " was not an int32, it was " + fmt.Sprintf("%T", sourceVal))
}
// StringMaptoString converts a string map to csv or get the first value
func StringMaptoString(value []string, separator string, noarray bool) string {
if len(value) == 0 {
return ""
}
if noarray {
return value[0]
}
return strings.Join(value, separator)
}
// IntMaptoString converts a int map to csv or get the first value
func IntMaptoString(value []int, separator string, noarray bool) string {
if len(value) == 0 {
return ""
}
if noarray {
return strconv.Itoa(value[0])
}
var strval []string
for _, i := range value {
strval = append(strval, strconv.Itoa(i))
}
return strings.Join(strval, separator)
}
// Int32MaptoString converts a int32 map to csv or get the first value
func Int32MaptoString(value []int32, separator string, noarray bool) string {
if len(value) == 0 {
return ""
}
if noarray {
return strconv.FormatInt(int64(value[0]), 10)
}
var strval []string
for _, i := range value {
strval = append(strval, strconv.FormatInt(int64(i), 10))
}
return strings.Join(strval, separator)
}
// Int64MaptoString converts a int64 map to csv or get the first value
func Int64MaptoString(value []int64, separator string, noarray bool) string {
if len(value) == 0 {
return ""
}
if noarray {
return strconv.FormatInt(value[0], 10)
}
var strval []string
for _, i := range value {
strval = append(strval, strconv.FormatInt(i, 10))
}
return strings.Join(strval, separator)
}
// ValToString : try to convert interface to string. Separated by separator if slice
func ValToString(value interface{}, separator string, noarray bool) string {
switch v := value.(type) {
case string:
return v
case []string:
return StringMaptoString(v, separator, noarray)
case int:
return strconv.Itoa(v)
case []int:
return IntMaptoString(v, separator, noarray)
case int32:
return strconv.FormatInt(int64(v), 10)
case []int32:
return Int32MaptoString(v, separator, noarray)
case int64:
return strconv.FormatInt(v, 10)
case []int64:
return Int64MaptoString(v, separator, noarray)
default:
return ""
}
}
// Join map[int]string into a string
func Join(values map[int]string, separator string) string {
var keys []int
for k := range values {
keys = append(keys, k)
}
sort.Ints(keys)
// create a map with the key parts in order
var tmp []string
for _, k := range keys {
tmp = append(tmp, values[k])
}
return strings.Join(tmp, separator)
}
// MustAtoi converts a string to integer and return 0 i case of error
func MustAtoi(value string) int {
i, err := strconv.Atoi(value)
if err != nil {
i = 0
}
return i
}
// FindHostAndCluster Find cluster for managed entity
func FindHostAndCluster(entity *string,
vmToHost map[string]*string,
morToParent map[string]*string,
morToName map[string]*string) (*string, *string, error) {
// get host
var host *string
var hostname *string
var cluster *string
if strings.HasPrefix(*entity, "vm-") {
if vmhost, ok := vmToHost[*entity]; ok {
host = vmhost
if vmhostname, ok := morToName[*host]; ok {
hostname = vmhostname
}
}
} else if strings.HasPrefix(*entity, "host-") {
host = entity
}
// check if a host was found
if host == nil {
return nil, nil, errors.New("No host found for " + *entity)
}
if parmor, ok := morToParent[*host]; ok {
if strings.HasPrefix(*parmor, "domain-c") {
cluster = morToName[*parmor]
} else if !strings.HasPrefix(*parmor, "domain-s") {
// ComputeRessource parent denotes a standalong host
// Any other is weird
return hostname, nil, errors.New("No suitable parent for host " + *host + " to determine cluster (" + *parmor + ")")
}
}
return hostname, cluster, nil
}
// ConvertToKV converts a map[string]string to a csv with k=v pairs
func ConvertToKV(values map[string]string) string {
var tmp []string
for key, val := range values {
if len(val) == 0 {
continue
}
tmp = append(tmp, fmt.Sprintf("%s=%s", key, val))
}
return strings.Join(tmp, ",")
}
// Reverse reverses the values of an array
func Reverse(array []string) {
for i := len(array)/2 - 1; i >= 0; i-- {
opp := len(array) - 1 - i
array[i], array[opp] = array[opp], array[i]
}
}
// MustWriteString writes to a buffer and panics on error
func MustWriteString(buffer *bytes.Buffer, s string) {
_, err := buffer.WriteString(s)
if err != nil {
panic(err)
}
}