This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_digitalocean_volume_snapshot.go
135 lines (109 loc) · 3.43 KB
/
resource_digitalocean_volume_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
package digitalocean
import (
"context"
"fmt"
"log"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceDigitalOceanVolumeSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanVolumeSnapshotCreate,
Read: resourceDigitalOceanVolumeSnapshotRead,
Update: resourceDigitalOceanVolumeSnapshotUpdate,
Delete: resourceDigitalOceanVolumeSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"regions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"size": {
Type: schema.TypeFloat,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"min_disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceDigitalOceanVolumeSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
opts := &godo.SnapshotCreateRequest{
Name: d.Get("name").(string),
VolumeID: d.Get("volume_id").(string),
Tags: expandTags(d.Get("tags").(*schema.Set).List()),
}
log.Printf("[DEBUG] Volume Snapshot create configuration: %#v", opts)
snapshot, _, err := client.Storage.CreateSnapshot(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error creating Volume Snapshot: %s", err)
}
d.SetId(snapshot.ID)
log.Printf("[INFO] Volume Snapshot name: %s", snapshot.Name)
return resourceDigitalOceanVolumeSnapshotRead(d, meta)
}
func resourceDigitalOceanVolumeSnapshotUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
if d.HasChange("tags") {
err := setTags(client, d, godo.VolumeSnapshotResourceType)
if err != nil {
return fmt.Errorf("Error updating tags: %s", err)
}
}
return resourceDigitalOceanVolumeSnapshotRead(d, meta)
}
func resourceDigitalOceanVolumeSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
snapshot, resp, err := client.Snapshots.Get(context.Background(), d.Id())
if err != nil {
// If the snapshot is somehow already destroyed, mark as
// successfully gone
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving volume snapshot: %s", err)
}
d.Set("name", snapshot.Name)
d.Set("volume_id", snapshot.ResourceID)
d.Set("regions", snapshot.Regions)
d.Set("size", snapshot.SizeGigaBytes)
d.Set("created_at", snapshot.Created)
d.Set("min_disk_size", snapshot.MinDiskSize)
d.Set("tags", flattenTags(snapshot.Tags))
return nil
}
func resourceDigitalOceanVolumeSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
log.Printf("[INFO] Deleting snaphot: %s", d.Id())
_, err := client.Snapshots.Delete(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting snapshot: %s", err)
}
d.SetId("")
return nil
}