forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_virtual_machine_snapshot.go
153 lines (142 loc) · 5.05 KB
/
resource_vsphere_virtual_machine_snapshot.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
package vsphere
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/vim25/types"
)
func resourceVSphereVirtualMachineSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceVSphereVirtualMachineSnapshotCreate,
Read: resourceVSphereVirtualMachineSnapshotRead,
Delete: resourceVSphereVirtualMachineSnapshotDelete,
Schema: map[string]*schema.Schema{
"virtual_machine_uuid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"snapshot_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"memory": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"quiesce": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"remove_children": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"consolidate": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceVSphereVirtualMachineSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
vm, err := virtualMachineFromUUID(client, d.Get("virtual_machine_uuid").(string))
if err != nil {
return fmt.Errorf("Error while getting the VirtualMachine :%s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) // This is 5 mins
defer cancel()
task, err := vm.CreateSnapshot(ctx, d.Get("snapshot_name").(string), d.Get("description").(string), d.Get("memory").(bool), d.Get("quiesce").(bool))
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
taskInfo, err := task.WaitForResult(tctx, nil)
if err != nil {
log.Printf("[DEBUG] Error While Creating the Task for Create Snapshot: %v", err)
return fmt.Errorf(" Error While Creating the Task for Create Snapshot: %s", err)
}
log.Printf("[DEBUG] Task created for Create Snapshot: %v", task)
if err != nil {
log.Printf("[DEBUG] Error While waiting for the Task for Create Snapshot: %v", err)
return fmt.Errorf(" Error While waiting for the Task for Create Snapshot: %s", err)
}
log.Printf("[DEBUG] Create Snapshot completed %v", d.Get("snapshot_name").(string))
log.Println("[DEBUG] Managed Object Reference: " + taskInfo.Result.(types.ManagedObjectReference).Value)
d.SetId(taskInfo.Result.(types.ManagedObjectReference).Value)
return nil
}
func resourceVSphereVirtualMachineSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
vm, err := virtualMachineFromUUID(client, d.Get("virtual_machine_uuid").(string))
if err != nil {
return fmt.Errorf("Error while getting the VirtualMachine :%s", err)
}
resourceVSphereVirtualMachineSnapshotRead(d, meta)
if d.Id() == "" {
log.Printf("[DEBUG] Error While finding the Snapshot: %v", err)
return nil
}
log.Printf("[DEBUG] Deleting snapshot with name: %v", d.Get("snapshot_name").(string))
var consolidatePtr *bool
var removeChildren bool
if v, ok := d.GetOk("consolidate"); ok {
consolidate := v.(bool)
consolidatePtr = &consolidate
} else {
consolidate := true
consolidatePtr = &consolidate
}
if v, ok := d.GetOk("remove_children"); ok {
removeChildren = v.(bool)
} else {
removeChildren = false
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) // This is 5 mins
defer cancel()
task, err := vm.RemoveSnapshot(ctx, d.Id(), removeChildren, consolidatePtr)
if err != nil {
log.Printf("[DEBUG] Error While Creating the Task for Delete Snapshot: %v", err)
return fmt.Errorf("Error While Creating the Task for Delete Snapshot: %s", err)
}
log.Printf("[DEBUG] Task created for Delete Snapshot: %v", task)
err = task.Wait(ctx)
if err != nil {
log.Printf("[DEBUG] Error While waiting for the Task of Delete Snapshot: %v", err)
return fmt.Errorf("Error While waiting for the Task of Delete Snapshot: %s", err)
}
log.Printf("[DEBUG] Delete Snapshot completed %v", d.Get("snapshot_name").(string))
return nil
}
func resourceVSphereVirtualMachineSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
vm, err := virtualMachineFromUUID(client, d.Get("virtual_machine_uuid").(string))
if err != nil {
return fmt.Errorf("Error while getting the VirtualMachine :%s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) // This is 5 mins
defer cancel()
snapshot, err := vm.FindSnapshot(ctx, d.Id())
if err != nil {
if strings.Contains(err.Error(), "No snapshots for this VM") || strings.Contains(err.Error(), "snapshot \""+d.Get("snapshot_name").(string)+"\" not found") {
log.Printf("[DEBUG] Error While finding the Snapshot: %v", err)
d.SetId("")
return nil
}
log.Printf("[DEBUG] Error While finding the Snapshot: %v", err)
return fmt.Errorf("Error while finding the Snapshot :%s", err)
}
log.Printf("[DEBUG] Snapshot found: %v", snapshot)
return nil
}