forked from hashicorp/terraform-provider-vsphere
/
resource_vsphere_datastore_cluster_vm_anti_affinity_rule.go
449 lines (391 loc) · 15.6 KB
/
resource_vsphere_datastore_cluster_vm_anti_affinity_rule.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package vsphere
import (
"encoding/json"
"errors"
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/storagepod"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/structure"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/viapi"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
const resourceVSphereDatastoreClusterVMAntiAffinityRuleName = "vsphere_datastore_cluster_vm_anti_affinity_rule"
func resourceVSphereDatastoreClusterVMAntiAffinityRule() *schema.Resource {
return &schema.Resource{
Create: resourceVSphereDatastoreClusterVMAntiAffinityRuleCreate,
Read: resourceVSphereDatastoreClusterVMAntiAffinityRuleRead,
Update: resourceVSphereDatastoreClusterVMAntiAffinityRuleUpdate,
Delete: resourceVSphereDatastoreClusterVMAntiAffinityRuleDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereDatastoreClusterVMAntiAffinityRuleImport,
},
Schema: map[string]*schema.Schema{
"datastore_cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The managed object ID of the datastore cluster.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The unique name of the virtual machine group in the cluster.",
},
"virtual_machine_ids": {
Type: schema.TypeSet,
Required: true,
Description: "The UUIDs of the virtual machines to run on different datastores from each other.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Enable this rule in the cluster.",
},
"mandatory": {
Type: schema.TypeBool,
Optional: true,
Description: "When true, prevents any virtual machine operations that may violate this rule.",
},
},
}
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceVSphereDatastoreClusterVMAntiAffinityRuleValidateRuleVMCount(d); err != nil {
return err
}
log.Printf("[DEBUG] %s: Beginning create", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
pod, _, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects(d, meta)
if err != nil {
return err
}
info, err := expandClusterAntiAffinityRuleSpec(d, meta)
if err != nil {
return err
}
spec := types.StorageDrsConfigSpec{
PodConfigSpec: &types.StorageDrsPodConfigSpec{
Rule: []types.ClusterRuleSpec{
{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationAdd,
},
Info: info,
},
},
},
}
if err = resourceVSphereDatastoreClusterVMAntiAffinityRuleApplySDRSConfigSpec(pod, spec); err != nil {
return err
}
info, err = resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntryByName(pod, info.Name)
if err != nil {
return err
}
id, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleFlattenID(pod, info.Key)
if err != nil {
return fmt.Errorf("cannot compute ID of created resource: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] %s: Create finished successfully", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
return resourceVSphereDatastoreClusterVMAntiAffinityRuleRead(d, meta)
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning read", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
pod, key, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects(d, meta)
if err != nil {
return err
}
info, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry(pod, key)
if err != nil {
return err
}
if info == nil {
// The configuration is missing, blank out the ID so it can be re-created.
d.SetId("")
return nil
}
// Save the datastore_cluster_id. This is ForceNew, but we set these for
// completeness on import so that if the wrong pod/VM combo was used, it
// will be noted.
if err = d.Set("datastore_cluster_id", pod.Reference().Value); err != nil {
return fmt.Errorf("error setting attribute \"datastore_cluster_id\": %s", err)
}
if err = flattenClusterAntiAffinityRuleSpec(d, meta, info); err != nil {
return err
}
log.Printf("[DEBUG] %s: Read completed successfully", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
return nil
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleUpdate(d *schema.ResourceData, meta interface{}) error {
if err := resourceVSphereDatastoreClusterVMAntiAffinityRuleValidateRuleVMCount(d); err != nil {
return err
}
log.Printf("[DEBUG] %s: Beginning update", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
pod, key, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects(d, meta)
if err != nil {
return err
}
info, err := expandClusterAntiAffinityRuleSpec(d, meta)
if err != nil {
return err
}
info.Key = key
spec := types.StorageDrsConfigSpec{
PodConfigSpec: &types.StorageDrsPodConfigSpec{
Rule: []types.ClusterRuleSpec{
{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationEdit,
},
Info: info,
},
},
},
}
if err := resourceVSphereDatastoreClusterVMAntiAffinityRuleApplySDRSConfigSpec(pod, spec); err != nil {
return err
}
log.Printf("[DEBUG] %s: Update finished successfully", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
return resourceVSphereDatastoreClusterVMAntiAffinityRuleRead(d, meta)
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] %s: Beginning delete", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
pod, key, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects(d, meta)
if err != nil {
return err
}
spec := types.StorageDrsConfigSpec{
PodConfigSpec: &types.StorageDrsPodConfigSpec{
Rule: []types.ClusterRuleSpec{
{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationRemove,
RemoveKey: key,
},
},
},
},
}
if err := resourceVSphereDatastoreClusterVMAntiAffinityRuleApplySDRSConfigSpec(pod, spec); err != nil {
return err
}
log.Printf("[DEBUG] %s: Deleted successfully", resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d))
return nil
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
var data map[string]string
if err := json.Unmarshal([]byte(d.Id()), &data); err != nil {
return nil, err
}
podPath, ok := data["datastore_cluster_path"]
if !ok {
return nil, errors.New("missing datastore_cluster_path in input data")
}
name, ok := data["name"]
if !ok {
return nil, errors.New("missing name in input data")
}
client, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleClient(meta)
if err != nil {
return nil, err
}
pod, err := storagepod.FromPath(client, podPath, nil)
if err != nil {
return nil, fmt.Errorf("cannot locate datastore cluster %q: %s", podPath, err)
}
info, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntryByName(pod, name)
if err != nil {
return nil, err
}
id, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleFlattenID(pod, info.Key)
if err != nil {
return nil, fmt.Errorf("cannot compute ID of imported resource: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString prints a friendly string for the
// vsphere_datastore_cluster_vm_anti_affinity_rule resource.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleIDString(d structure.ResourceIDStringer) string {
return structure.ResourceIDString(d, resourceVSphereDatastoreClusterVMAntiAffinityRuleName)
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFlattenID makes an ID for the
// vsphere_datastore_cluster_vm_anti_affinity_rule resource.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFlattenID(pod *object.StoragePod, key int32) (string, error) {
podID := pod.Reference().Value
return strings.Join([]string{podID, strconv.Itoa(int(key))}, ":"), nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleParseID parses an ID for the
// vsphere_datastore_cluster_vm_anti_affinity_rule and outputs its parts.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleParseID(id string) (string, int32, error) {
parts := strings.SplitN(id, ":", 3)
if len(parts) < 2 {
return "", 0, fmt.Errorf("bad ID %q", id)
}
key, err := strconv.Atoi(parts[1])
if err != nil {
return "", 0, fmt.Errorf("bad key in ID %q: %s", parts[1], err)
}
return parts[0], int32(key), nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry attempts to
// locate an existing VM anti-affinity rule in a datastore cluster's
// configuration by key. It's used by the resource's read functionality and
// tests. nil is returned if the entry cannot be found.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry(
pod *object.StoragePod,
key int32,
) (*types.ClusterAntiAffinityRuleSpec, error) {
props, err := storagepod.Properties(pod)
if err != nil {
return nil, fmt.Errorf("error fetching datastore cluster properties: %s", err)
}
for _, info := range props.PodStorageDrsEntry.StorageDrsConfig.PodConfig.Rule {
if info.GetClusterRuleInfo().Key == key {
if vmAntiAffinityRuleInfo, ok := info.(*types.ClusterAntiAffinityRuleSpec); ok {
log.Printf("[DEBUG] Found VM anti-affinity rule key %d in datastore cluster %q", key, pod.Name())
return vmAntiAffinityRuleInfo, nil
}
return nil, fmt.Errorf("rule key %d in datastore cluster %q is not a VM anti-affinity rule", key, pod.Name())
}
}
log.Printf("[DEBUG] No VM anti-affinity rule key %d found in datastore cluster %q", key, pod.Name())
return nil, nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntryByName attempts to
// locate an existing VM anti-affinity rule in a datastore cluster's
// configuration by name. It differs from the standard
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry in that we don't
// allow missing entries, as it's designed to be used in places where we don't
// want to allow for missing entries, such as during creation and import.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntryByName(
pod *object.StoragePod,
name string,
) (*types.ClusterAntiAffinityRuleSpec, error) {
props, err := storagepod.Properties(pod)
if err != nil {
return nil, fmt.Errorf("error fetching datastore cluster properties: %s", err)
}
for _, info := range props.PodStorageDrsEntry.StorageDrsConfig.PodConfig.Rule {
if info.GetClusterRuleInfo().Name == name {
if vmAntiAffinityRuleInfo, ok := info.(*types.ClusterAntiAffinityRuleSpec); ok {
log.Printf("[DEBUG] Found VM anti-affinity rule %q in datastore cluster %q", name, pod.Name())
return vmAntiAffinityRuleInfo, nil
}
return nil, fmt.Errorf("rule %q in datastore cluster %q is not a VM anti-affinity rule", name, pod.Name())
}
}
return nil, fmt.Errorf("no VM anti-affinity rule %q found in datastore cluster %q", name, pod.Name())
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects handles the
// fetching of the cluster and rule key depending on what attributes are
// available:
// * If the resource ID is available, the data is derived from the ID.
// * If not, only the cluster is retrieved from datastore_cluster_id. -1 is
// returned for the key.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleObjects(
d *schema.ResourceData,
meta interface{},
) (*object.StoragePod, int32, error) {
if d.Id() != "" {
return resourceVSphereDatastoreClusterVMAntiAffinityRuleObjectsFromID(d, meta)
}
return resourceVSphereDatastoreClusterVMAntiAffinityRuleObjectsFromAttributes(d, meta)
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleObjectsFromAttributes(
d *schema.ResourceData,
meta interface{},
) (*object.StoragePod, int32, error) {
return resourceVSphereDatastoreClusterVMAntiAffinityRuleFetchObjects(
meta,
d.Get("datastore_cluster_id").(string),
-1,
)
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleObjectsFromID(
d structure.ResourceIDStringer,
meta interface{},
) (*object.StoragePod, int32, error) {
// Note that this function uses structure.ResourceIDStringer to satisfy
// interfacer. Adding exceptions in the comments does not seem to work.
// Change this back to ResourceData if it's needed in the future.
podID, key, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleParseID(d.Id())
if err != nil {
return nil, 0, err
}
return resourceVSphereDatastoreClusterVMAntiAffinityRuleFetchObjects(meta, podID, key)
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleFetchObjects fetches the
// "objects" for a cluster rule. This is currently just the cluster object as
// the rule key a static value and a pass-through - this is to keep its
// workflow consistent with other cluster-dependent resources that derive from
// ArrayUpdateSpec that have managed object as keys, such as VM and host
// overrides.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleFetchObjects(
meta interface{},
podID string,
key int32,
) (*object.StoragePod, int32, error) {
client, err := resourceVSphereDatastoreClusterVMAntiAffinityRuleClient(meta)
if err != nil {
return nil, 0, err
}
pod, err := storagepod.FromID(client, podID)
if err != nil {
return nil, 0, fmt.Errorf("cannot locate datastore cluster: %s", err)
}
return pod, key, nil
}
func resourceVSphereDatastoreClusterVMAntiAffinityRuleClient(meta interface{}) (*govmomi.Client, error) {
client := meta.(*VSphereClient).vimClient
if err := viapi.ValidateVirtualCenter(client); err != nil {
return nil, err
}
return client, nil
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleApplySDRSConfigSpec
// applying a SDRS config spec for the
// vsphere_datastore_cluster_vm_anti_affinity_rule resource.
//
// This is wrapped to abstract the fact that we are deriving the client from
// the StoragePod. This is because helper workflows that have been created more
// recently (ie: cluster helpers) do this, and more than likely the storagepod
// helper will do it eventually as well. If there is ever an issue with this,
// it can be changed here. There should be no issue though as govmomi.Client
// is mainly just vim25.Client with some additional session helper bits that is
// not normally needed during normal operation.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleApplySDRSConfigSpec(
pod *object.StoragePod,
spec types.StorageDrsConfigSpec,
) error {
return storagepod.ApplyDRSConfiguration(
&govmomi.Client{
Client: pod.Client(),
},
pod,
spec,
)
}
// resourceVSphereDatastoreClusterVMAntiAffinityRuleValidateRuleVMCount ensures
// that the VM count in the anti-affinity rule at any point in time before it's
// created or updated is a length of at least 2.
//
// This validation is necessary as a rule of only 1 VM here is a no-op and
// ultimately will result in a broken resource (the rule will not exist after
// creation, or example). Unfortunately, this needs to happen at apply time
// right now due to issues with TF core and how it processes lists when values
// are computed. Once these issues are fixed with TF core, the validation here
// should be removed and moved to schema.
func resourceVSphereDatastoreClusterVMAntiAffinityRuleValidateRuleVMCount(d *schema.ResourceData) error {
if d.Get("virtual_machine_ids").(*schema.Set).Len() < 2 {
return errors.New("length of virtual_machine_ids must be 2 or more")
}
return nil
}