This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
forked from vultr/terraform-provider-vultr
/
resource_vultr_snapshot.go
161 lines (134 loc) · 3.9 KB
/
resource_vultr_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
154
155
156
157
158
159
160
161
package vultr
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vultr/govultr"
)
func resourceVultrSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceVultrSnapshotCreate,
Read: resourceVultrSnapshotRead,
Delete: resourceVultrSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"vps_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"os_id": {
Type: schema.TypeString,
Computed: true,
},
"app_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceVultrSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).govultrClient()
vpsID := d.Get("vps_id").(string)
var description string
desc, ok := d.GetOk("description")
if ok {
description = desc.(string)
}
snapshot, err := client.Snapshot.Create(context.Background(), vpsID, description)
if err != nil {
return fmt.Errorf("Error creating snapshot: %v", err)
}
d.SetId(snapshot.SnapshotID)
_, err = waitForSnapshot(d, "complete", []string{"pending"}, "status", meta)
if err != nil {
return fmt.Errorf(
"Error while waiting for Snapshot %s to be completed: %s", d.Id(), err)
}
log.Printf("[INFO] Snapshot ID: %s", d.Id())
return resourceVultrSnapshotRead(d, meta)
}
func resourceVultrSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).govultrClient()
snapshots, err := client.Snapshot.List(context.Background())
if err != nil {
return fmt.Errorf("Error getting snapshots: %v", err)
}
var snapshot *govultr.Snapshot
for i := range snapshots {
if snapshots[i].SnapshotID == d.Id() {
snapshot = &snapshots[i]
break
}
}
if snapshot == nil {
log.Printf("[WARN] Vultr snapshot (%s) not found", d.Id())
d.SetId("")
return nil
}
d.Set("description", snapshot.Description)
d.Set("date_created", snapshot.DateCreated)
d.Set("size", snapshot.Size)
d.Set("status", snapshot.Status)
d.Set("os_id", snapshot.OsID)
d.Set("app_id", snapshot.AppID)
return nil
}
func resourceVultrSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).govultrClient()
log.Printf("[INFO] Deleting snapshot: %s", d.Id())
if err := client.Snapshot.Delete(context.Background(), d.Id()); err != nil {
return fmt.Errorf("Error destroying snapshot (%s): %v", d.Id(), err)
}
return nil
}
func waitForSnapshot(d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}) (interface{}, error) {
log.Printf(
"[INFO] Waiting for Snapshot (%s) to have %s of %s",
d.Id(), attribute, target)
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: []string{target},
Refresh: newSnapStateRefresh(d, meta),
Timeout: 60 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
NotFoundChecks: 60,
}
return stateConf.WaitForState()
}
func newSnapStateRefresh(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
client := meta.(*Client).govultrClient()
return func() (interface{}, string, error) {
log.Printf("[INFO] Creating Snapshot")
snap, err := client.Snapshot.Get(context.Background(), d.Id())
if err != nil {
return nil, "", fmt.Errorf("Error retrieving Snapshot %s : %s", d.Id(), err)
}
log.Printf("[INFO] The SnapShot Status is %s", snap.Status)
return snap, snap.Status, nil
}
}