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
/
helpers.go
101 lines (85 loc) · 2.09 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
package scaleway
import (
"fmt"
"log"
"net/http"
"time"
"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
}
// DetachIP detaches an IP from a server
func DetachIP(s *api.ScalewayAPI, ipID string) error {
var update struct {
Address string `json:"address"`
ID string `json:"id"`
Organization string `json:"organization"`
}
ip, err := s.GetIP(ipID)
if err != nil {
return err
}
update.Address = ip.IP.Address
update.ID = ip.IP.ID
update.Organization = ip.IP.Organization
resp, err := s.PutResponse(api.ComputeAPI, fmt.Sprintf("ips/%s", ipID), update)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return err
}
resp.Body.Close()
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
func deleteServerSafe(s *api.ScalewayAPI, serverID string) error {
server, err := s.GetServer(serverID)
if err != nil {
return err
}
if server.State != "stopped" {
if err := s.PostServerAction(serverID, "poweroff"); err != nil {
return err
}
if err := waitForServerState(s, serverID, "stopped"); err != nil {
return err
}
}
if err := s.DeleteServer(serverID); err != nil {
return err
}
if rootVolume, ok := server.Volumes["0"]; ok {
if err := s.DeleteVolume(rootVolume.Identifier); err != nil {
return err
}
}
return nil
}
func waitForServerState(s *api.ScalewayAPI, serverID string, targetState string) error {
var server *api.ScalewayServer
var err error
var currentState string
for {
server, err = s.GetServer(serverID)
if err != nil {
return err
}
if currentState != server.State {
log.Printf("[DEBUG] Server changed state to %q\n", server.State)
currentState = server.State
}
if server.State == targetState {
break
}
time.Sleep(1 * time.Second)
}
return nil
}