/
vcenter.go
211 lines (163 loc) · 4.78 KB
/
vcenter.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
package vendors
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"github.com/devopsext/utils"
)
type VCenterHostOptions struct {
Cluster string
}
type VCenterVMGuestIdentityOptions struct {
VM string
}
type VCenterVMOptions struct {
Cluster string
Host string
}
type VCenterOptions struct {
Timeout int
Insecure bool
URL string
User string
Password string
Session string
}
type VCenter struct {
client *http.Client
options VCenterOptions
}
type VCenterSessionResponse struct {
Value string `json:"value"`
}
const (
VCenterContentType = "application/json"
VCenterRestSessionPath = "/rest/com/vmware/cis/session"
VCenterRestClusterPath = "/rest/vcenter/cluster"
VCenterRestHostPath = "/rest/vcenter/host"
VCenterRestVMPath = "/rest/vcenter/vm"
VCenterRestVMGuestIdentityPathFmt = "/rest/vcenter/vm/%s/guest/identity"
)
// https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/vm/
func (vc *VCenter) getAuth(opts VCenterOptions) string {
auth := ""
if !utils.IsEmpty(opts.User) {
userPass := fmt.Sprintf("%s:%s", opts.User, opts.Password)
auth = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(userPass)))
return auth
}
return auth
}
func (vc *VCenter) getSession(opts VCenterOptions) (string, error) {
u, err := url.Parse(opts.URL)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, VCenterRestSessionPath)
res, err := utils.HttpPostRaw(vc.client, u.String(), VCenterContentType, vc.getAuth(opts), nil)
if err != nil {
return "", err
}
var sr VCenterSessionResponse
err = json.Unmarshal(res, &sr)
if err != nil {
return "", err
}
return sr.Value, nil
}
func (vc *VCenter) getHeaders(session string) map[string]string {
headers := make(map[string]string)
headers["Content-type"] = VCenterContentType
headers["vmware-api-session-id"] = session
return headers
}
func (vc *VCenter) CustomGetSession(options VCenterOptions) (string, error) {
if utils.IsEmpty(options.Session) {
s, err := vc.getSession(options)
if err != nil {
return "", err
}
return s, nil
}
return options.Session, nil
}
func (vc *VCenter) CustomGetClusters(options VCenterOptions) ([]byte, error) {
session, err := vc.CustomGetSession(options)
if err != nil {
return nil, err
}
u, err := url.Parse(options.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, VCenterRestClusterPath)
return utils.HttpGetRawWithHeaders(vc.client, u.String(), vc.getHeaders(session))
}
func (vc *VCenter) GetClusters() ([]byte, error) {
return vc.CustomGetClusters(vc.options)
}
func (vc *VCenter) CustomGetHosts(options VCenterOptions, hostOptions VCenterHostOptions) ([]byte, error) {
session, err := vc.CustomGetSession(options)
if err != nil {
return nil, err
}
u, err := url.Parse(options.URL)
if err != nil {
return nil, err
}
if !utils.IsEmpty(hostOptions.Cluster) {
var params = make(url.Values)
params.Add("filter.clusters", hostOptions.Cluster)
u.RawQuery = params.Encode()
}
u.Path = path.Join(u.Path, VCenterRestHostPath)
return utils.HttpGetRawWithHeaders(vc.client, u.String(), vc.getHeaders(session))
}
func (vc *VCenter) GetHosts(options VCenterHostOptions) ([]byte, error) {
return vc.CustomGetHosts(vc.options, options)
}
func (vc *VCenter) CustomGetVMs(options VCenterOptions, vmOptions VCenterVMOptions) ([]byte, error) {
session, err := vc.CustomGetSession(options)
if err != nil {
return nil, err
}
u, err := url.Parse(options.URL)
if err != nil {
return nil, err
}
if !utils.IsEmpty(vmOptions.Cluster) {
var params = make(url.Values)
params.Add("filter.clusters", vmOptions.Cluster)
params.Add("filter.hosts", vmOptions.Host)
u.RawQuery = params.Encode()
}
u.Path = path.Join(u.Path, VCenterRestVMPath)
return utils.HttpGetRawWithHeaders(vc.client, u.String(), vc.getHeaders(session))
}
func (vc *VCenter) GetVMs(options VCenterVMOptions) ([]byte, error) {
return vc.CustomGetVMs(vc.options, options)
}
func (vc *VCenter) CustomGetVMGuestIdentity(options VCenterOptions, vmGuestidentity VCenterVMGuestIdentityOptions) ([]byte, error) {
session, err := vc.CustomGetSession(options)
if err != nil {
return nil, err
}
u, err := url.Parse(options.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, fmt.Sprintf(VCenterRestVMGuestIdentityPathFmt, vmGuestidentity.VM))
return utils.HttpGetRawWithHeaders(vc.client, u.String(), vc.getHeaders(session))
}
func (vc *VCenter) GetVMGuestIdentity(options VCenterVMGuestIdentityOptions) ([]byte, error) {
return vc.CustomGetVMGuestIdentity(vc.options, options)
}
func NewVCenter(options VCenterOptions) *VCenter {
return &VCenter{
client: utils.NewHttpClient(options.Timeout, options.Insecure),
options: options,
}
}