forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_scaleway_volume.go
121 lines (107 loc) · 2.82 KB
/
resource_scaleway_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
package scaleway
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/scaleway/scaleway-cli/pkg/api"
)
const gb uint64 = 1000 * 1000 * 1000
func resourceScalewayVolume() *schema.Resource {
return &schema.Resource{
Create: resourceScalewayVolumeCreate,
Read: resourceScalewayVolumeRead,
Update: resourceScalewayVolumeUpdate,
Delete: resourceScalewayVolumeDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"size_in_gb": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateVolumeSize,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateVolumeType,
},
"server": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceScalewayVolumeCreate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
size := uint64(d.Get("size_in_gb").(int)) * gb
req := api.ScalewayVolumeDefinition{
Name: d.Get("name").(string),
Size: size,
Type: d.Get("type").(string),
Organization: scaleway.Organization,
}
volumeID, err := scaleway.PostVolume(req)
if err != nil {
return fmt.Errorf("Error Creating volume: %q", err)
}
d.SetId(volumeID)
return resourceScalewayVolumeRead(d, m)
}
func resourceScalewayVolumeRead(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
volume, err := scaleway.GetVolume(d.Id())
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
log.Printf("[DEBUG] Error reading volume: %q\n", serr.APIMessage)
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
d.Set("name", volume.Name)
if size, ok := volume.Size.(float64); ok {
d.Set("size_in_gb", uint64(size)/gb)
}
d.Set("type", volume.VolumeType)
d.Set("server", "")
if volume.Server != nil {
d.Set("server", volume.Server.Identifier)
}
return nil
}
func resourceScalewayVolumeUpdate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
var req api.ScalewayVolumePutDefinition
if d.HasChange("name") {
req.Name = String(d.Get("name").(string))
}
if d.HasChange("size_in_gb") {
size := uint64(d.Get("size_in_gb").(int)) * gb
req.Size = &size
}
scaleway.PutVolume(d.Id(), req)
return resourceScalewayVolumeRead(d, m)
}
func resourceScalewayVolumeDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
err := scaleway.DeleteVolume(d.Id())
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
d.SetId("")
return nil
}