-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_server_actions.go
176 lines (142 loc) · 4.54 KB
/
cloud_server_actions.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
package goarubacloud
const cloudServerPowerOffPath = "SetEnqueueServerPowerOff"
const cloudServerPowerOnPath = "SetEnqueueServerStart"
const cloudServerArchivePath = "ArchiveVirtualServer"
const cloudServerRestorePath = "SetEnqueueServerRestore"
const cloudServerReinitializePath = "SetEnqueueReinitializeServer"
// CloudServerActionsService is an interface for interfacing with the Cloud Server actions
// endpoints of the Arubacloud API
type CloudServerActionsService interface {
PowerOff(int) (*Response, error)
PowerOn(int) (*Response, error)
PowerCycle(int) (*Response, error)
Archive(int) (*Response, error)
Restore(serverId int, CPUQuantity int, RAMQuantity int) (*Response, error)
Reinitialize(*ServerReinitializeRequest) (*Response, error)
}
// CloudServerActionsServiceOp handles communication with the Cloud Server action related
// methods of the Arubacloud API.
type CloudServerActionsServiceOp struct {
client *Client
}
var _ CloudServerActionsService = &CloudServerActionsServiceOp{}
type ServerIdCreate struct {
ServerId int
CPUQuantity int `json:"CPUQuantity,omitempty"`
RAMQuantity int `json:"RAMQuantity,omitempty"`
}
type ServerReinitializeRequest struct {
ServerId int
AdministratorPassword string `json:"AdministratorPassword,omitempty"`
OSTemplateID int `json:"OSTemplateID,omitempty"`
ConfigureIPv6 bool `json:"ConfigureIPv6,omitempty"`
}
type cloudServerActionRequest struct {
ActionPath string
ServerIdCreateRequest *ServerIdCreate
}
// PowerOff a Cloud Server
func (s *CloudServerActionsServiceOp) PowerOff(serverId int) (*Response, error) {
action := &cloudServerActionRequest{ActionPath: cloudServerPowerOffPath,
ServerIdCreateRequest: &ServerIdCreate{ServerId: serverId}}
return s.doAction(action)
}
// PowerOn a Cloud Server
func (s *CloudServerActionsServiceOp) PowerOn(serverId int) (*Response, error) {
action := &cloudServerActionRequest{ActionPath: cloudServerPowerOnPath,
ServerIdCreateRequest: &ServerIdCreate{ServerId: serverId}}
return s.doAction(action)
}
// PowerCycle a Cloud Server
func (s *CloudServerActionsServiceOp) PowerCycle(serverId int) (*Response, error) {
serverDetails, resp, err := s.client.CloudServers.Get(serverId)
if err != nil {
return resp, err
}
if serverDetails.ServerStatus == OFF {
resp, err = s.PowerOn(serverId)
if err != nil {
return nil, err
}
}
resp, err = s.PowerOff(serverId)
if err != nil {
return resp, err
}
err = WaitForServerStatus(s.client, serverId, OFF)
if err != nil {
return nil, err
}
return s.PowerOn(serverId)
}
// Archive Cloud Server
func (s *CloudServerActionsServiceOp) Archive(serverId int) (*Response, error) {
data := struct {
ArchiveVirtualServer interface{} `json:"ArchiveVirtualServer"`
}{ServerIdCreate{ServerId: serverId}}
req, err := s.client.NewRequest(cloudServerArchivePath, data)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Restore Cloud Server
func (s *CloudServerActionsServiceOp) Restore(serverId int, qpu_quantity int, ram_quantity int) (*Response, error) {
data := struct {
SetEnqueueServerRestore interface{} `json:"SetEnqueueServerRestore"`
}{ServerIdCreate{
ServerId: serverId,
CPUQuantity: qpu_quantity,
RAMQuantity: ram_quantity}}
req, err := s.client.NewRequest(cloudServerRestorePath, data)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Restore Cloud Server
func (s *CloudServerActionsServiceOp) Reinitialize(serverReinitializeRequest *ServerReinitializeRequest) (*Response, error) {
serverId := serverReinitializeRequest.ServerId
serverDetails, resp, err := s.client.CloudServers.Get(serverId)
if err != nil {
return resp, err
}
if serverDetails.ServerStatus == ON {
resp, err = s.PowerOff(serverId)
if err != nil {
return nil, err
}
}
err = WaitForServerStatus(s.client, serverId, OFF)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(cloudServerReinitializePath, serverReinitializeRequest)
if err != nil {
return nil, err
}
resp, err = s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
func (s *CloudServerActionsServiceOp) doAction(actionRequest *cloudServerActionRequest) (*Response, error) {
req, err := s.client.NewRequest(actionRequest.ActionPath, actionRequest.ServerIdCreateRequest)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}