-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsession_util.go
100 lines (86 loc) · 2.83 KB
/
session_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
// Copyright (c) 2018-2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package session
import (
"bytes"
"compress/gzip"
"encoding/base64"
vimTypes "github.com/vmware/govmomi/vim25/types"
)
const (
// OvfEnvironmentTransportGuestInfo is the OVF transport type that uses
// GuestInfo. The other valid type is "iso".
OvfEnvironmentTransportGuestInfo = "com.vmware.guestInfo"
)
func ExtraConfigToMap(input []vimTypes.BaseOptionValue) (output map[string]string) {
output = make(map[string]string)
for _, opt := range input {
if optValue := opt.GetOptionValue(); optValue != nil {
// Only set string type values
if val, ok := optValue.Value.(string); ok {
output[optValue.Key] = val
}
}
}
return
}
// MergeExtraConfig adds the key/value to the ExtraConfig if the key is not present, to let to the value be
// changed by the VM. The existing usage of ExtraConfig is hard to fit in the reconciliation model.
func MergeExtraConfig(extraConfig []vimTypes.BaseOptionValue, newMap map[string]string) []vimTypes.BaseOptionValue {
merged := make([]vimTypes.BaseOptionValue, 0)
ecMap := ExtraConfigToMap(extraConfig)
for k, v := range newMap {
if _, exists := ecMap[k]; !exists {
merged = append(merged, &vimTypes.OptionValue{Key: k, Value: v})
}
}
return merged
}
// GetMergedvAppConfigSpec prepares a vApp VmConfigSpec which will set the vmMetadata supplied key/value fields. Only
// fields marked userConfigurable and pre-existing on the VM (ie. originated from the OVF Image)
// will be set, and all others will be ignored.
func GetMergedvAppConfigSpec(inProps map[string]string, vmProps []vimTypes.VAppPropertyInfo) *vimTypes.VmConfigSpec {
outProps := make([]vimTypes.VAppPropertySpec, 0)
for _, vmProp := range vmProps {
if vmProp.UserConfigurable == nil || !*vmProp.UserConfigurable {
continue
}
inPropValue, found := inProps[vmProp.Id]
if !found || vmProp.Value == inPropValue {
continue
}
vmPropCopy := vmProp
vmPropCopy.Value = inPropValue
outProp := vimTypes.VAppPropertySpec{
ArrayUpdateSpec: vimTypes.ArrayUpdateSpec{
Operation: vimTypes.ArrayUpdateOperationEdit,
},
Info: &vmPropCopy,
}
outProps = append(outProps, outProp)
}
if len(outProps) == 0 {
return nil
}
return &vimTypes.VmConfigSpec{
Property: outProps,
// Ensure the transport is guestInfo in case the VM does not have
// a CD-ROM device required to use the ISO transport.
OvfEnvironmentTransport: []string{OvfEnvironmentTransportGuestInfo},
}
}
func EncodeGzipBase64(s string) (string, error) {
var zbuf bytes.Buffer
zw := gzip.NewWriter(&zbuf)
if _, err := zw.Write([]byte(s)); err != nil {
return "", err
}
if err := zw.Flush(); err != nil {
return "", err
}
if err := zw.Close(); err != nil {
return "", err
}
b64 := base64.StdEncoding.EncodeToString(zbuf.Bytes())
return b64, nil
}