forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_digitalocean_volume.go
146 lines (119 loc) · 3.46 KB
/
resource_digitalocean_volume.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
package digitalocean
import (
"fmt"
"log"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceDigitalOceanVolume() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanVolumeCreate,
Read: resourceDigitalOceanVolumeRead,
Delete: resourceDigitalOceanVolumeDelete,
Importer: &schema.ResourceImporter{
State: resourceDigitalOceanVolumeImport,
},
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"droplet_ids": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
},
"size": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true, // Update-ability Coming Soon ™
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Update-ability Coming Soon ™
},
},
}
}
func resourceDigitalOceanVolumeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*godo.Client)
opts := &godo.VolumeCreateRequest{
Region: d.Get("region").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
SizeGigaBytes: int64(d.Get("size").(int)),
}
log.Printf("[DEBUG] Volume create configuration: %#v", opts)
volume, _, err := client.Storage.CreateVolume(opts)
if err != nil {
return fmt.Errorf("Error creating Volume: %s", err)
}
d.SetId(volume.ID)
log.Printf("[INFO] Volume name: %s", volume.Name)
return resourceDigitalOceanVolumeRead(d, meta)
}
func resourceDigitalOceanVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*godo.Client)
volume, resp, err := client.Storage.GetVolume(d.Id())
if err != nil {
// If the volume is somehow already destroyed, mark as
// successfully gone
if resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving volume: %s", err)
}
d.Set("id", volume.ID)
dids := make([]interface{}, 0, len(volume.DropletIDs))
for _, did := range volume.DropletIDs {
dids = append(dids, did)
}
d.Set("droplet_ids", schema.NewSet(
func(dropletID interface{}) int { return dropletID.(int) },
dids,
))
return nil
}
func resourceDigitalOceanVolumeDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*godo.Client)
log.Printf("[INFO] Deleting volume: %s", d.Id())
_, err := client.Storage.DeleteVolume(d.Id())
if err != nil {
return fmt.Errorf("Error deleting volume: %s", err)
}
d.SetId("")
return nil
}
func resourceDigitalOceanVolumeImport(rs *schema.ResourceData, v interface{}) ([]*schema.ResourceData, error) {
client := v.(*godo.Client)
volume, _, err := client.Storage.GetVolume(rs.Id())
if err != nil {
return nil, err
}
rs.Set("id", volume.ID)
rs.Set("name", volume.Name)
rs.Set("region", volume.Region.Slug)
rs.Set("description", volume.Description)
rs.Set("size", int(volume.SizeGigaBytes))
dids := make([]interface{}, 0, len(volume.DropletIDs))
for _, did := range volume.DropletIDs {
dids = append(dids, did)
}
rs.Set("droplet_ids", schema.NewSet(
func(dropletID interface{}) int { return dropletID.(int) },
dids,
))
return []*schema.ResourceData{rs}, nil
}