forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
105 lines (87 loc) · 2.6 KB
/
helpers.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
package scaleway
import (
"fmt"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/scaleway/scaleway-cli/pkg/api"
)
// Bool returns a pointer to of the bool value passed in.
func Bool(val bool) *bool {
return &val
}
// String returns a pointer to of the string value passed in.
func String(val string) *string {
return &val
}
func validateVolumeType(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
}
func validateVolumeSize(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
}
// deleteRunningServer terminates the server and waits until it is removed.
func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
err := scaleway.PostServerAction(server.Identifier, "terminate")
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
return nil
}
}
return err
}
return waitForServerState(scaleway, server.Identifier, "stopped")
}
// deleteStoppedServer needs to cleanup attached root volumes. this is not done
// automatically by Scaleway
func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
if err := scaleway.DeleteServer(server.Identifier); err != nil {
return err
}
if rootVolume, ok := server.Volumes["0"]; ok {
if err := scaleway.DeleteVolume(rootVolume.Identifier); err != nil {
return err
}
}
return nil
}
// NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go
// the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway
var allStates = []string{"starting", "running", "stopping", "stopped"}
func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
pending := []string{}
for _, state := range allStates {
if state != targetState {
pending = append(pending, state)
}
}
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: []string{targetState},
Refresh: func() (interface{}, string, error) {
s, err := scaleway.GetServer(serverID)
if err == nil {
return 42, s.State, nil
}
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
return 42, "stopped", nil
}
}
return 42, s.State, err
},
Timeout: 60 * time.Minute,
MinTimeout: 5 * time.Second,
Delay: 5 * time.Second,
}
_, err := stateConf.WaitForState()
return err
}