forked from transip/gotransip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
405 lines (319 loc) · 16.8 KB
/
repository.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package vps
import (
"fmt"
"github.com/assi010/gotransip/v6"
"github.com/assi010/gotransip/v6/ipaddress"
"github.com/assi010/gotransip/v6/product"
"github.com/assi010/gotransip/v6/repository"
"github.com/assi010/gotransip/v6/rest"
"net"
"net/url"
"strings"
"time"
)
// Repository is the vps repository
// this repository allows you to manage all VPS services for your TransIP account
type Repository repository.RestRepository
// GetAll returns a list of all your VPSs
func (r *Repository) GetAll() ([]Vps, error) {
var response vpssWrapper
restRequest := rest.Request{Endpoint: "/vps"}
err := r.Client.Get(restRequest, &response)
return response.Vpss, err
}
// GetAllByTags returns a list of all VPSs that match the tags provided
func (r *Repository) GetAllByTags(tags []string) ([]Vps, error) {
var response vpssWrapper
restRequest := rest.Request{Endpoint: "/vps", Parameters: url.Values{"tags": tags}}
err := r.Client.Get(restRequest, &response)
return response.Vpss, err
}
// GetSelection returns a limited list of VPSs,
// specify how many and which page/chunk of VPSs you want to retrieve
func (r *Repository) GetSelection(page int, itemsPerPage int) ([]Vps, error) {
var response vpssWrapper
params := url.Values{
"pageSize": []string{fmt.Sprintf("%d", itemsPerPage)},
"page": []string{fmt.Sprintf("%d", page)},
}
restRequest := rest.Request{Endpoint: "/vps", Parameters: params}
err := r.Client.Get(restRequest, &response)
return response.Vpss, err
}
// GetByName returns information on a specific VPS by name
func (r *Repository) GetByName(vpsName string) (Vps, error) {
var response vpsWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.Vps, err
}
// Order allows you to order a new VPS
func (r *Repository) Order(vpsOrder Order) error {
restRequest := rest.Request{Endpoint: "/vps", Body: &vpsOrder}
return r.Client.Post(restRequest)
}
// OrderMultiple allows you to order multiple vpses at the same time
func (r *Repository) OrderMultiple(orders []Order) error {
requestBody := vpssOrderWrapper{Orders: orders}
restRequest := rest.Request{Endpoint: "/vps", Body: &requestBody}
return r.Client.Post(restRequest)
}
// Clone allows you to clone an existing VPS
// There are a few things to take into account when you want to clone an existing VPS to a new VPS:
//
// - If the original VPS (which you’re going to clone) is currently locked, the clone will fail;
//
// - Cloned control panels can be used on the VPS, but as the IP address changes, this does require you to synchronise
// the new license on the new VPS (licenses are often IP-based);
//
// - Possibly, your VPS has its network interface(s) configured using (a) static IP(‘s) rather than a dynamic allocation
// using DHCP. If this is the case, you have to configure the new IP(‘s) on the new VPS.
// Do note that this is not the case with our pre-installed control panel images;
//
// - VPS add-ons such as Big Storage aren’t affected by cloning - these will stay attached to the original VPS and can’t
// be swapped automatically
func (r *Repository) Clone(vpsName string) error {
requestBody := cloneRequest{VpsName: vpsName}
restRequest := rest.Request{Endpoint: "/vps", Body: &requestBody}
return r.Client.Post(restRequest)
}
// CloneToAvailabilityZone allows you to clone a vps to a specific availability zone, identified by name
func (r *Repository) CloneToAvailabilityZone(vpsName string, availabilityZone string) error {
requestBody := cloneRequest{VpsName: vpsName, AvailabilityZone: availabilityZone}
restRequest := rest.Request{Endpoint: "/vps", Body: &requestBody}
return r.Client.Post(restRequest)
}
// Update allows you to lock/unlock a VPS, update a VPS description, and add/remove tags.
//
// For locking the VPS, set isCustomerLocked to true. Set the value to false for unlocking the VPS
// You can change your VPS description by simply changing the description attribute
// To add/remove tags, you must update the tags attribute
func (r *Repository) Update(vps Vps) error {
requestBody := vpsWrapper{Vps: vps}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vps.Name), Body: &requestBody}
return r.Client.Put(restRequest)
}
// Start allows you to start a VPS, given that it’s currently in a stopped state
func (r *Repository) Start(vpsName string) error {
requestBody := actionWrapper{Action: "start"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// Stop allows you to stop a VPS
func (r *Repository) Stop(vpsName string) error {
requestBody := actionWrapper{Action: "stop"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// Reset allows you to reset a VPS, a reset is essentially the stop and start command combined into one
func (r *Repository) Reset(vpsName string) error {
requestBody := actionWrapper{Action: "reset"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// Handover will handover a VPS to another TransIP Account. This call will initiate the handover process.
// The actual handover will be done when the target customer accepts the handover
func (r *Repository) Handover(vpsName string, targetCustomerName string) error {
requestBody := handoverRequest{Action: "handover", TargetCustomerName: targetCustomerName}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// Cancel will cancel the VPS, thus deleting it
func (r *Repository) Cancel(vpsName string, endTime gotransip.CancellationTime) error {
requestBody := gotransip.CancellationRequest{EndTime: endTime}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s", vpsName), Body: &requestBody}
return r.Client.Delete(restRequest)
}
// GetUsage will allow you to request your vps usage for a specified period and usage type,
// for convenience you can also use the GetUsages or GetUsagesLast24Hours
func (r *Repository) GetUsage(vpsName string, usageTypes []UsageType, period UsagePeriod) (Usage, error) {
var response usageWrapper
types := make([]string, len(usageTypes))
for i, usageType := range usageTypes {
types[i] = string(usageType)
}
parameters := url.Values{
"dateTimeStart": []string{fmt.Sprintf("%d", period.TimeStart)},
"dateTimeEnd": []string{fmt.Sprintf("%d", period.TimeEnd)},
"types": []string{strings.Join(types, ",")},
}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/usage", vpsName), Parameters: parameters}
err := r.Client.Get(restRequest, &response)
return response.Usage, err
}
// GetAllUsage returns a Usage struct filled with all usage data for the given UsagePeriod.
// UsagePeriod is struct containing a start and end unix timestamp
func (r *Repository) GetAllUsage(vpsName string, period UsagePeriod) (Usage, error) {
return r.GetUsage(
vpsName,
[]UsageType{UsageTypeCPU, UsageTypeDisk, UsageTypeNetwork},
period,
)
}
// GetAllUsage24Hours returns all usage data for a given Vps within the last 24 hours
func (r *Repository) GetAllUsage24Hours(vpsName string) (Usage, error) {
// always define a period body, this way we don't have to depend on the empty body logic on the api server
period := UsagePeriod{TimeStart: time.Now().Add(-24 * time.Hour).Unix(), TimeEnd: time.Now().Unix()}
return r.GetAllUsage(vpsName, period)
}
// GetVNCData will return VncData about your vps.
// It allows you to get the location, token and password in order to connect directly to the VNC console of your VPS.
func (r *Repository) GetVNCData(vpsName string) (VncData, error) {
var response vncDataWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/vnc-data", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.VncData, err
}
// RegenerateVNCToken allows you to regenerate the VNC credentials for a VPS
func (r *Repository) RegenerateVNCToken(vpsName string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/vnc-data", vpsName)}
return r.Client.Patch(restRequest)
}
// GetAddons returns a struct with 'cancellable', 'available' and 'active' addons in it for the given VPS
func (r *Repository) GetAddons(vpsName string) (Addons, error) {
var response addonsWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/addons", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.Addons, err
}
// OrderAddons allows you to expand VPS specs with a given list of addons to order
func (r *Repository) OrderAddons(vpsName string, addons []string) error {
response := addonOrderRequest{Addons: addons}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/addons", vpsName), Body: &response}
return r.Client.Post(restRequest)
}
// CancelAddon allows you to cancel an add-on by name, specifying the VPS name as well.
// Due to technical restrictions (possible dataloss) storage add-ons cannot be cancelled.
func (r *Repository) CancelAddon(vpsName string, addon string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/addons/%s", vpsName, addon)}
return r.Client.Delete(restRequest)
}
// GetUpgrades returns all available product upgrades for a VPS
func (r *Repository) GetUpgrades(vpsName string) ([]product.Product, error) {
var response upgradesWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/upgrades", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.Upgrades, err
}
// Upgrade allows you to upgrade a VPS by name and productName
func (r *Repository) Upgrade(vpsName string, productName string) error {
requestBody := upgradeRequest{ProductName: productName}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/upgrades", vpsName), Body: &requestBody}
return r.Client.Post(restRequest)
}
// GetOperatingSystems returns a list of operating systems that you can install on a vps
func (r *Repository) GetOperatingSystems(vpsName string) ([]OperatingSystem, error) {
var response operatingSystemsWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/operating-systems", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.OperatingSystems, err
}
// InstallOperatingSystem allows you to install an operating system to a Vps,
// optionally you can specify a hostname and a base64InstallText,
// which would be the automatic installation configuration of your Vps
// for more information, see: https://api.transip.nl/rest/docs.html#vps-operatingsystems-post
func (r *Repository) InstallOperatingSystem(vpsName string, operatingSystemName string, hostname string, base64InstallText string) error {
requestBody := installRequest{OperatingSystemName: operatingSystemName, Hostname: hostname, Base64InstallText: base64InstallText}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/operating-systems", vpsName), Body: &requestBody}
return r.Client.Post(restRequest)
}
// InstallOperatingSystemWithOptions allows you to install an operating system to a Vps,
// in the options you can specify hostname, username, ssh keys, and base64InstallText,
// which would be the automatic installation configuration of your Vps
// for more information, see: https://api.transip.nl/rest/docs.html#vps-operatingsystems-post
func (r *Repository) InstallOperatingSystemWithOptions(vpsName string, options InstallOptions) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/operating-systems", vpsName), Body: &options}
return r.Client.Post(restRequest)
}
// GetIPAddresses returns all IPv4 and IPv6 addresses attached to the VPS
func (r *Repository) GetIPAddresses(vpsName string) ([]ipaddress.IPAddress, error) {
var response ipaddress.IPAddressesWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/ip-addresses", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.IPAddresses, err
}
// GetIPAddressByAddress returns network information for the specified IP address
func (r *Repository) GetIPAddressByAddress(vpsName string, address net.IP) (ipaddress.IPAddress, error) {
var response ipAddressWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/ip-addresses/%s", vpsName, address.String())}
err := r.Client.Get(restRequest, &response)
return response.IPAddress, err
}
// AddIPv6Address allows you to add an IPv6 address to your VPS.
// After adding an IPv6 address, you can set the reverse DNS for this address using the UpdateReverseDNS function.
func (r *Repository) AddIPv6Address(vpsName string, address net.IP) error {
requestBody := addIPRequest{IPAddress: address}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/ip-addresses", vpsName), Body: &requestBody}
return r.Client.Post(restRequest)
}
// UpdateReverseDNS allows you to update the reverse dns for IPv4 addresses as wal as IPv6 addresses
func (r *Repository) UpdateReverseDNS(vpsName string, ip ipaddress.IPAddress) error {
requestBody := ipAddressWrapper{IPAddress: ip}
restRequest := rest.Request{
Endpoint: fmt.Sprintf("/vps/%s/ip-addresses/%s", vpsName, ip.Address.String()),
Body: &requestBody,
}
return r.Client.Put(restRequest)
}
// RemoveIPv6Address allows you to remove an IPv6 address from the registered list of IPv6 address within your VPS's `/64` range.
func (r *Repository) RemoveIPv6Address(vpsName string, address net.IP) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/ip-addresses/%s", vpsName, address.String())}
return r.Client.Delete(restRequest)
}
// GetSnapshots returns a list of Snapshots for a given VPS
func (r *Repository) GetSnapshots(vpsName string) ([]Snapshot, error) {
var response snapshotsWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.Snapshots, err
}
// GetSnapshotByName returns a Snapshot for a VPS given its snapshotName and vpsName
func (r *Repository) GetSnapshotByName(vpsName string, snapshotName string) (Snapshot, error) {
var response snapshotWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots/%s", vpsName, snapshotName)}
err := r.Client.Get(restRequest, &response)
return response.Snapshot, err
}
// CreateSnapshot allows you to create a snapshot for restoring it at a later time or restoring it to another VPS.
// See the function RevertSnapshot for this.
func (r *Repository) CreateSnapshot(vpsName string, description string, shouldStartVps bool) error {
requestBody := createSnapshotRequest{Description: description, ShouldStartVps: shouldStartVps}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots", vpsName), Body: &requestBody}
return r.Client.Post(restRequest)
}
// RevertSnapshot allows you to revert a snapshot of a vps,
// if you want to revert a snapshot to a different vps you can use the RevertSnapshotToOtherVps method
func (r *Repository) RevertSnapshot(vpsName string, snapshotName string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots/%s", vpsName, snapshotName)}
return r.Client.Patch(restRequest)
}
// RevertSnapshotToOtherVps allows you to revert a snapshot to a different vps
func (r *Repository) RevertSnapshotToOtherVps(vpsName string, snapshotName string, destinationVps string) error {
requestBody := revertSnapshotRequest{DestinationVpsName: destinationVps}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots/%s", vpsName, snapshotName), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// RemoveSnapshot allows you to remove a snapshot from a given VPS
func (r *Repository) RemoveSnapshot(vpsName string, snapshotName string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/snapshots/%s", vpsName, snapshotName)}
return r.Client.Delete(restRequest)
}
// GetBackups allows you to get a list of backups for a given VPS which you can use to revert or convert to snapshot
func (r *Repository) GetBackups(vpsName string) ([]Backup, error) {
var response backupsWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/backups", vpsName)}
err := r.Client.Get(restRequest, &response)
return response.Backups, err
}
// RevertBackup allows you to revert a backup
func (r *Repository) RevertBackup(vpsName string, backupID int64) error {
requestBody := actionWrapper{Action: "revert"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/backups/%d", vpsName, backupID), Body: &requestBody}
return r.Client.Patch(restRequest)
}
// ConvertBackupToSnapshot allows you to convert a backup to a snapshot
func (r *Repository) ConvertBackupToSnapshot(vpsName string, backupID int64, snapshotDescription string) error {
requestBody := convertBackupRequest{SnapshotDescription: snapshotDescription, Action: "convert"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/backups/%d", vpsName, backupID), Body: &requestBody}
return r.Client.Patch(restRequest)
}