-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservers.go
More file actions
170 lines (132 loc) · 4.47 KB
/
servers.go
File metadata and controls
170 lines (132 loc) · 4.47 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
package gohvapi
import (
"encoding/base64"
"encoding/json"
"strconv"
)
// Server struct defines what a VPS looks like
type Server struct {
Name string `json:"fqdn"`
ID int `json:"mbpkgid,string"`
OS string `json:"os"`
PrimaryIPv4 string `json:"ip"`
PrimaryIPv6 string `json:"ipv6"`
PlanID int `json:"plan_id,string"`
PkgID int `json:"pkg_id,string"`
LocationID int `json:"location_id,string"`
OSID int `json:"os_id,string"`
ServerStatus string `json:"status"`
PowerStatus string `json:"state"`
}
// ServerOptions struct defines some extra options including SSH Auth
type ServerOptions struct {
SSHKeyID int
Password string
CloudConfig string
}
// JobID struct holds the current Job Id for what's being processed
type JobID struct {
ID int `json:"id,string"`
}
// GetServers external method on Client to list your instances
func (c *Client) GetServers() ([]Server, error) {
var serverList []Server
if err := c.get("cloud/servers", &serverList); err != nil {
return nil, err
}
return serverList, nil
}
// GetServer external method on Client to get an instance
func (c *Client) GetServer(id int) (server Server, err error) {
if err := c.get("/cloud/server/"+strconv.Itoa(id), &server); err != nil {
return Server{}, err
}
return server, nil
}
// StartServer external method on Client to boot up an instance
func (c *Client) StartServer(id int) error {
if err := c.post("/cloud/server/start/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}
// StopServer external method on Client to shut down an instance
func (c *Client) StopServer(id int) error {
if err := c.post("/cloud/server/shutdown/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}
// RebootServer external method on Client to reboot an instance
func (c *Client) RebootServer(id int) error {
if err := c.post("/cloud/server/reboot/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}
// CreateServer external method on Client to buy and build a new instance.
func (c *Client) CreateServer(name, plan string, locationID, osID int, options *ServerOptions) (server Server, err error) {
values := map[string]string{
"plan": plan,
"fqdn": name,
"location": strconv.Itoa(locationID),
"image": strconv.Itoa(osID),
}
if options != nil {
if options.SSHKeyID != 0 {
values["ssh_key_id"] = strconv.Itoa(options.SSHKeyID)
}
if options.Password != "" {
values["password"] = options.Password
}
if options.CloudConfig != "" {
values["cloud_config"] = base64.StdEncoding.EncodeToString([]byte(options.CloudConfig))
}
}
postData, _ := json.Marshal(values)
if err := c.post("/cloud/buy_build/", postData, &server); err != nil {
return Server{}, err
}
return server, nil
}
// CancelServer external method on Client to cancel/remove from billing an instance.
// this method completely removes an instance, it cannot be rebuilt afterward.
// billing should be prorated to the day or something like that.
// This method requires apikey_allow_cancel to be checked on the account.
func (c *Client) CancelServer(id int) error {
if err := c.post("/cloud/cancel/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}
// ProvisionServer external method on Client to re-build an instance
// This should not be used in Terraform as we will use CreateServer instead
func (c *Client) ProvisionServer(name string, id, locationID, osID int, options *ServerOptions) (JobID, error) {
var jobid JobID
values := map[string]string{"fqdn": name, "location": strconv.Itoa(locationID), "image": strconv.Itoa(osID)}
if options != nil {
if options.SSHKeyID != 0 {
values["ssh_key_id"] = strconv.Itoa(options.SSHKeyID)
}
if options.Password != "" {
values["password"] = options.Password
}
if options.CloudConfig != "" {
values["cloud_config"] = base64.StdEncoding.EncodeToString([]byte(options.CloudConfig))
}
}
postData, _ := json.Marshal(values)
if err := c.post("/cloud/server/"+strconv.Itoa(id), postData, &jobid); err != nil {
return JobID{}, err
}
return jobid, nil
}
// DeleteServer external method on Client to destroy an instance.
// This should not be used in Terraform as we will use CancelServer instead.
// This method requires apikey_allow_delete to be checked on the account
func (c *Client) DeleteServer(id int) error {
if err := c.post("/cloud/server/delete/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}