This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_scaleway_volume.go
127 lines (114 loc) · 3.06 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
122
123
124
125
126
127
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,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"size_in_gb": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 1 || value > 150 {
errors = append(errors, fmt.Errorf("%q be more than 1 and less than 150", k))
}
return
},
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "l_ssd" {
errors = append(errors, fmt.Errorf("%q must be l_ssd", k))
}
return
},
},
"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)
d.Set("size_in_gb", volume.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
}