-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathservers.go
More file actions
212 lines (186 loc) · 6.43 KB
/
servers.go
File metadata and controls
212 lines (186 loc) · 6.43 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cloudscale
import (
"context"
"fmt"
"net/http"
"reflect"
"time"
)
const serverBasePath = "v1/servers"
const ServerRunning = "running"
const ServerStopped = "stopped"
const ServerRebooted = "rebooted"
type Server struct {
ZonalResource
TaggedResource
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
Status string `json:"status"`
Flavor FlavorStub `json:"flavor"`
Image ImageServerStub `json:"image"`
Volumes []VolumeStub `json:"volumes"`
Interfaces []Interface `json:"interfaces"`
SSHFingerprints []string `json:"ssh_fingerprints"`
SSHHostKeys []string `json:"ssh_host_keys"`
AntiAfinityWith []ServerStub `json:"anti_affinity_with"`
ServerGroups []ServerGroupStub `json:"server_groups"`
CreatedAt time.Time `json:"created_at"`
}
type ServerStub struct {
HREF string `json:"href"`
UUID string `json:"uuid"`
}
type ServerGroupStub struct {
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
}
type FlavorStub struct {
Slug string `json:"slug"`
Name string `json:"name"`
VCPUCount int `json:"vcpu_count"`
MemoryGB int `json:"memory_gb"`
}
type ImageServerStub struct {
Slug string `json:"slug"`
Name string `json:"name"`
OperatingSystem string `json:"operating_system"`
DefaultUsername string `json:"default_username"`
}
type VolumeStub struct {
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
Type string `json:"type"`
SizeGB int `json:"size_gb"`
}
type Interface struct {
Type string `json:"type,omitempty"`
Network NetworkStub `json:"network,omitempty"`
Addresses []Address `json:"addresses,omitempty"`
}
type Address struct {
Version int `json:"version"`
Address string `json:"address"`
PrefixLength int `json:"prefix_length"`
Gateway string `json:"gateway"`
ReversePtr string `json:"reverse_ptr"`
Subnet SubnetStub `json:"subnet"`
}
type ServerRequest struct {
ZonalResourceRequest
TaggedResourceRequest
Name string `json:"name"`
Flavor string `json:"flavor"`
Image string `json:"image"`
Zone string `json:"zone,omitempty"`
VolumeSizeGB int `json:"volume_size_gb,omitempty"`
Volumes *[]ServerVolumeRequest `json:"volumes,omitempty"`
Interfaces *[]InterfaceRequest `json:"interfaces,omitempty"`
BulkVolumeSizeGB int `json:"bulk_volume_size_gb,omitempty"`
SSHKeys []string `json:"ssh_keys"`
Password string `json:"password,omitempty"`
UsePublicNetwork *bool `json:"use_public_network,omitempty"`
UsePrivateNetwork *bool `json:"use_private_network,omitempty"`
UseIPV6 *bool `json:"use_ipv6,omitempty"`
AntiAffinityWith string `json:"anti_affinity_with,omitempty"`
ServerGroups []string `json:"server_groups,omitempty"`
UserData string `json:"user_data,omitempty"`
}
type ServerUpdateRequest struct {
TaggedResourceRequest
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Flavor string `json:"flavor,omitempty"`
Interfaces *[]InterfaceRequest `json:"interfaces,omitempty"`
}
type ServerVolumeRequest struct {
SizeGB int `json:"size_gb,omitempty"`
Type string `json:"type,omitempty"`
}
type InterfaceRequest struct {
Network string `json:"network,omitempty"`
Addresses *[]AddressRequest `json:"addresses,omitempty"`
}
type AddressRequest struct {
Subnet string `json:"subnet,omitempty"`
Address string `json:"address,omitempty"`
}
type ServerService interface {
GenericCreateService[Server, ServerRequest]
GenericGetService[Server]
GenericListService[Server]
GenericUpdateService[Server, ServerUpdateRequest]
GenericDeleteService[Server]
GenericWaitForService[Server]
Reboot(ctx context.Context, serverID string) error
Start(ctx context.Context, serverID string) error
Stop(ctx context.Context, serverID string) error
}
type ServerServiceOperations struct {
GenericServiceOperations[Server, ServerRequest, ServerUpdateRequest]
client *Client
}
func (s ServerServiceOperations) Reboot(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/reboot", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) Start(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/start", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) Stop(ctx context.Context, serverID string) error {
path := fmt.Sprintf("%s/%s/stop", serverBasePath, serverID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}
func (s ServerServiceOperations) Update(ctx context.Context, id string, req *ServerUpdateRequest) error {
if req.Status != "" {
err := error(nil)
switch req.Status {
case ServerRunning:
err = s.Start(ctx, id)
case ServerStopped:
err = s.Stop(ctx, id)
case ServerRebooted:
err = s.Reboot(ctx, id)
default:
return fmt.Errorf("Status Not Supported %s", req.Status)
}
if err != nil {
return err
}
// Get rid of status
req.Status = ""
}
emptyRequest := ServerUpdateRequest{}
if reflect.DeepEqual(emptyRequest, *req) {
return nil
}
// Call the generic Update method directly using the embedded field name
return s.GenericServiceOperations.Update(ctx, id, req)
}
var ServerIsRunning = func(server *Server) (bool, error) {
if server.Status == ServerRunning {
return true, nil
}
return false, fmt.Errorf("waiting for status: %s, current status: %s", ServerRunning, server.Status)
}
var ServerIsStopped = func(server *Server) (bool, error) {
if server.Status == ServerStopped {
return true, nil
}
return false, fmt.Errorf("waiting for status: %s, current status: %s", ServerStopped, server.Status)
}