/
vsphere_host.go
324 lines (270 loc) · 8.73 KB
/
vsphere_host.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) 2015-2017 Marin Atanasov Nikolov <dnaeon@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer
// in this position and unchanged.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package resource
import (
"fmt"
"path"
"reflect"
"github.com/blang/semver"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
// HostDnsConfig type provides information about the DNS settings
// used by the ESXi host.
type HostDnsConfig struct {
// DHCP flag is used to indicate whether or not DHCP is used to
// determine DNS settings.
DHCP bool `luar:"dhcp"`
// Servers is the list of DNS servers to use.
Servers []string `luar:"servers"`
// Domain name portion of the DNS name.
Domain string `luar:"domain"`
// Hostname portion of the DNS name.
Hostname string `luar:"hostname"`
// Search list for hostname lookup.
Search []string `luar:"search"`
}
// Host type is a resource which manages settings of the
// ESXi hosts in a VMware vSphere environment.
//
// Example:
// host = vsphere.host.new("esxi01.example.org")
// host.endpoint = "https://vc01.example.org/sdk"
// host.username = "root"
// host.password = "myp4ssw0rd"
// host.path = "/MyDatacenter/host/MyCluster"
// host.state = "present"
// host.lockdown_mode = "lockdownNormal"
// host.dns = {
// servers = { "1.2.3.4", "2.3.4.5" },
// domain = "example.org",
// hostname = "esxi01",
// search = { "example.org" },
// }
type Host struct {
BaseVSphere
// LockdownMode flag specifies whether to enable or
// disable lockdown mode of the host.
// This feature is available only on ESXi 6.0 or above.
// Valid values that can be set are "lockdownDisabled",
// "lockdownNormal" and "lockdownStrict". Refer to the
// official VMware vSphere API reference for more details and
// explanation of each setting. Defaults to an empty string.
LockdownMode types.HostLockdownMode `luar:"lockdown_mode"`
// Dns configuration settings for the host.
Dns *HostDnsConfig `luar:"dns"`
}
// hostProperties is a helper which retrieves properties for the
// ESXi host managed by the resource.
func (h *Host) hostProperties(ps []string) (mo.HostSystem, error) {
var host mo.HostSystem
obj, err := h.finder.HostSystem(h.ctx, path.Join(h.Path, h.Name))
if err != nil {
return host, err
}
if err := obj.Properties(h.ctx, obj.Reference(), ps, &host); err != nil {
return host, err
}
return host, nil
}
// isDnsConfigSynced checks if the DNS configuration of the
// ESXi host is in the desired state.
func (h *Host) isDnsConfigSynced() (bool, error) {
// If we don't have a config, assume configuration is correct
if h.Dns == nil {
return true, nil
}
host, err := h.hostProperties([]string{"config"})
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
return false, ErrResourceAbsent
}
return false, err
}
dnsConfig := host.Config.Network.DnsConfig.GetHostDnsConfig()
// If DHCP is enabled we consider the settings to be correct
if dnsConfig.Dhcp {
return true, nil
}
// TODO: Get rid of reflect when comparing the two slices
if !reflect.DeepEqual(dnsConfig.Address, h.Dns.Servers) {
return false, nil
}
if dnsConfig.DomainName != h.Dns.Domain {
return false, nil
}
if dnsConfig.HostName != h.Dns.Hostname {
return false, nil
}
// TODO: Get rid of reflect when comparing the two slices
if !reflect.DeepEqual(dnsConfig.SearchDomain, h.Dns.Search) {
return false, nil
}
return true, nil
}
// setDnsConfig configures the DNS settings on the ESXi host.
func (h *Host) setDnsConfig() error {
Logf("%s configuring dns settings\n", h.ID())
obj, err := h.finder.HostSystem(h.ctx, path.Join(h.Path, h.Name))
if err != nil {
return err
}
networkSystem, err := obj.ConfigManager().NetworkSystem(h.ctx)
if err != nil {
return err
}
config := &types.HostDnsConfig{
Dhcp: h.Dns.DHCP,
HostName: h.Dns.Hostname,
DomainName: h.Dns.Domain,
Address: h.Dns.Servers,
SearchDomain: h.Dns.Search,
}
return networkSystem.UpdateDnsConfig(h.ctx, config)
}
// isLockdownSynced checks if the lockdown mode of the
// ESXi host is in sync.
func (h *Host) isLockdownSynced() (bool, error) {
// If we don't have a mode provided, assume configuration is correct
if h.LockdownMode == "" {
return true, nil
}
host, err := h.hostProperties([]string{"config"})
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
return false, ErrResourceAbsent
}
}
return h.LockdownMode == host.Config.LockdownMode, nil
}
// setLockdown sets the lockdown mode for the ESXi host.
// This feature is available only for ESXi 6.0 or above.
func (h *Host) setLockdown() error {
// Setting lockdown mode is supported starting from vSphere API 6.0
// Ensure that the ESXi host is at least at version 6.0.0
minVersion, err := semver.Make("6.0.0")
if err != nil {
return err
}
obj, err := h.finder.HostSystem(h.ctx, path.Join(h.Path, h.Name))
if err != nil {
return err
}
host, err := h.hostProperties([]string{"config", "configManager"})
if err != nil {
return err
}
productVersion, err := semver.Make(host.Config.Product.Version)
if err != nil {
return err
}
if productVersion.LT(minVersion) {
return fmt.Errorf("host is at version %s, setting lockdown requires %s or above", productVersion, minVersion)
}
Logf("%s setting lockdown mode to %s\n", h.ID(), h.LockdownMode)
var accessManager mo.HostAccessManager
if err := obj.Properties(h.ctx, *host.ConfigManager.HostAccessManager, nil, &accessManager); err != nil {
return err
}
req := &types.ChangeLockdownMode{
This: accessManager.Reference(),
Mode: h.LockdownMode,
}
_, err = methods.ChangeLockdownMode(h.ctx, h.client, req)
return err
}
// NewHost creates a new resource for managing ESXi host settings.
func NewHost(name string) (Resource, error) {
h := &Host{
BaseVSphere: BaseVSphere{
Base: Base{
Name: name,
Type: "host",
State: "present",
Require: make([]string, 0),
PresentStatesList: []string{"present"},
AbsentStatesList: []string{"absent"},
Concurrent: true,
Subscribe: make(TriggerMap),
},
Username: "",
Password: "",
Endpoint: "",
Insecure: false,
Path: "/",
},
LockdownMode: "",
Dns: nil,
}
// Set resource properties
h.PropertyList = []Property{
&ResourceProperty{
PropertyName: "dns-config",
PropertySetFunc: h.setDnsConfig,
PropertyIsSyncedFunc: h.isDnsConfigSynced,
},
&ResourceProperty{
PropertyName: "lockdown-mode",
PropertySetFunc: h.setLockdown,
PropertyIsSyncedFunc: h.isLockdownSynced,
},
}
return h, nil
}
// Evaluate evaluate the state of the ESXi host.
func (h *Host) Evaluate() (State, error) {
state := State{
Current: "unknown",
Want: h.State,
}
_, err := h.finder.HostSystem(h.ctx, path.Join(h.Path, h.Name))
if err != nil {
// Host is absent
if _, ok := err.(*find.NotFoundError); ok {
state.Current = "absent"
return state, nil
}
// Something else happened
return state, err
}
state.Current = "present"
return state, nil
}
// Create is a no-op. Adding hosts to the VMware vCenter server is
// done by using the ClusterHost resource type.
func (h *Host) Create() error {
return nil
}
// Delete disconnects the host and then removes it.
func (h *Host) Delete() error {
Logf("%s removing host from %s\n", h.ID(), h.Path)
obj, err := h.finder.HostSystem(h.ctx, path.Join(h.Path, h.Name))
if err != nil {
return err
}
return vSphereRemoveHost(h.ctx, obj)
}