blob: f75c7cd5d581842b86337488eb2ce66415155a00 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -07004 "fmt"
Ash Wilson01626a32014-09-17 10:38:07 -04005
Ash Wilson71ff2fe2014-09-25 13:39:27 -04006 "github.com/mitchellh/mapstructure"
Jon Perritt30558642014-04-14 17:07:12 -05007 "github.com/racker/perigee"
Ash Wilson01626a32014-09-17 10:38:07 -04008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080010)
11
Ash Wilsonfd043792014-09-17 10:40:17 -040012// ListPage abstracts the raw results of making a List() request against the API.
Ash Wilson01626a32014-09-17 10:38:07 -040013// As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
14// data provided through the ExtractServers call.
Ash Wilsonfd043792014-09-17 10:40:17 -040015type ListPage struct {
Ash Wilson71ff2fe2014-09-25 13:39:27 -040016 pagination.LinkedPageBase
Ash Wilson01626a32014-09-17 10:38:07 -040017}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080018
Ash Wilson01626a32014-09-17 10:38:07 -040019// IsEmpty returns true if a page contains no Server results.
Ash Wilsonfd043792014-09-17 10:40:17 -040020func (page ListPage) IsEmpty() (bool, error) {
Ash Wilson01626a32014-09-17 10:38:07 -040021 servers, err := ExtractServers(page)
22 if err != nil {
23 return true, err
24 }
25 return len(servers) == 0, nil
26}
27
Ash Wilson71ff2fe2014-09-25 13:39:27 -040028// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
29func (page ListPage) NextPageURL() (string, error) {
30 type link struct {
31 Href string `mapstructure:"href"`
32 Rel string `mapstructure:"rel"`
33 }
34 type resp struct {
35 Links []link `mapstructure:"servers_links"`
36 }
37
38 var r resp
39 err := mapstructure.Decode(page.Body, &r)
Ash Wilson01626a32014-09-17 10:38:07 -040040 if err != nil {
41 return "", err
42 }
Ash Wilson71ff2fe2014-09-25 13:39:27 -040043
44 var url string
45 for _, l := range r.Links {
46 if l.Rel == "next" {
47 url = l.Href
48 }
49 }
50 if url == "" {
Ash Wilson01626a32014-09-17 10:38:07 -040051 return "", nil
52 }
Ash Wilson71ff2fe2014-09-25 13:39:27 -040053
54 return url, nil
Ash Wilson01626a32014-09-17 10:38:07 -040055}
56
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080057// List makes a request against the API to list servers accessible to you.
Ash Wilson01626a32014-09-17 10:38:07 -040058func List(client *gophercloud.ServiceClient) pagination.Pager {
59 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson71ff2fe2014-09-25 13:39:27 -040060 return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080061 }
62
Ash Wilson31f6bde2014-09-25 14:52:12 -040063 return pagination.NewPager(client, detailURL(client), createPage)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080064}
65
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080066// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilsond27e0ff2014-09-25 11:50:31 -040067func Create(client *gophercloud.ServiceClient, opts map[string]interface{}) CreateResult {
68 var result CreateResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040069 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -040070 Results: &result.Resp,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080071 ReqBody: map[string]interface{}{
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080072 "server": opts,
73 },
Ash Wilson01626a32014-09-17 10:38:07 -040074 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080075 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080076 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -040077 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080078}
79
80// Delete requests that a server previously provisioned be removed from your account.
Ash Wilson01626a32014-09-17 10:38:07 -040081func Delete(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -040082 _, err := perigee.Request("DELETE", serverURL(client, id), perigee.Options{
Ash Wilson01626a32014-09-17 10:38:07 -040083 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080084 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080085 })
86 return err
87}
88
Ash Wilson7ddf0362014-09-17 10:59:09 -040089// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -040090func Get(client *gophercloud.ServiceClient, id string) GetResult {
91 var result GetResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040092 _, result.Err = perigee.Request("GET", serverURL(client, id), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -040093 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -040094 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080095 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -040096 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080097}
98
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080099// Update requests that various attributes of the indicated server be changed.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400100func Update(client *gophercloud.ServiceClient, id string, opts map[string]interface{}) UpdateResult {
101 var result UpdateResult
Ash Wilson31f6bde2014-09-25 14:52:12 -0400102 _, result.Err = perigee.Request("PUT", serverURL(client, id), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400103 Results: &result.Resp,
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800104 ReqBody: map[string]interface{}{
105 "server": opts,
106 },
Ash Wilson01626a32014-09-17 10:38:07 -0400107 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800108 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400109 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800110}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700111
Ash Wilson01626a32014-09-17 10:38:07 -0400112// ChangeAdminPassword alters the administrator or root password for a specified server.
113func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) error {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400114 var req struct {
115 ChangePassword struct {
116 AdminPass string `json:"adminPass"`
117 } `json:"changePassword"`
118 }
119
120 req.ChangePassword.AdminPass = newPassword
121
Ash Wilson31f6bde2014-09-25 14:52:12 -0400122 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400123 ReqBody: req,
Ash Wilson01626a32014-09-17 10:38:07 -0400124 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500125 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700126 })
127 return err
128}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700129
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700130// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700131// fails to fall within acceptable values. For example, the Reboot() function
132// expects the "how" parameter to be one of HardReboot or SoftReboot. These
133// constants are (currently) strings, leading someone to wonder if they can pass
134// other string values instead, perhaps in an effort to break the API of their
135// provider. Reboot() returns this error in this situation.
136//
137// Function identifies which function was called/which function is generating
138// the error.
139// Argument identifies which formal argument was responsible for producing the
140// error.
141// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700142type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700143 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500144 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700145}
146
147// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700148func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700149 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
150}
151
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700152func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700153 return e.Error()
154}
155
Ash Wilson01626a32014-09-17 10:38:07 -0400156// RebootMethod describes the mechanisms by which a server reboot can be requested.
157type RebootMethod string
158
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700159// These constants determine how a server should be rebooted.
160// See the Reboot() function for further details.
161const (
Ash Wilson01626a32014-09-17 10:38:07 -0400162 SoftReboot RebootMethod = "SOFT"
163 HardReboot RebootMethod = "HARD"
164 OSReboot = SoftReboot
165 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700166)
167
168// Reboot requests that a given server reboot.
169// Two methods exist for rebooting a server:
170//
Ash Wilson01626a32014-09-17 10:38:07 -0400171// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
172// terminating it at the hypervisor level.
173// It's done. Caput. Full stop.
174// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700175//
Ash Wilson01626a32014-09-17 10:38:07 -0400176// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
177// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine.
178func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) error {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700179 if (how != SoftReboot) && (how != HardReboot) {
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700180 return &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700181 Function: "Reboot",
182 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500183 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700184 }
185 }
Jon Perritt30558642014-04-14 17:07:12 -0500186
Ash Wilson31f6bde2014-09-25 14:52:12 -0400187 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500188 ReqBody: struct {
189 C map[string]string `json:"reboot"`
190 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400191 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700192 },
Ash Wilson01626a32014-09-17 10:38:07 -0400193 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500194 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700195 })
196 return err
197}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700198
Ash Wilson01626a32014-09-17 10:38:07 -0400199// Rebuild requests that the Openstack provider reprovision the server.
200// The rebuild will need to know the server's name and new image reference or ID.
201// In addition, and unlike building a server with Create(), you must provide an administrator password.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700202//
203// Additional options may be specified with the additional map.
204// This function treats a nil map the same as an empty map.
205//
Ash Wilson01626a32014-09-17 10:38:07 -0400206// Rebuild returns a server result as though you had called GetDetail() on the server's ID.
207// The information, however, refers to the new server, not the old.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400208func Rebuild(client *gophercloud.ServiceClient, id, name, password, imageRef string, additional map[string]interface{}) RebuildResult {
209 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700210
211 if id == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400212 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700213 Function: "Rebuild",
214 Argument: "id",
Jon Perritt30558642014-04-14 17:07:12 -0500215 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700216 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400217 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700218 }
219
220 if name == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400221 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700222 Function: "Rebuild",
223 Argument: "name",
Jon Perritt30558642014-04-14 17:07:12 -0500224 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700225 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400226 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700227 }
228
229 if password == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400230 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700231 Function: "Rebuild",
232 Argument: "password",
Jon Perritt30558642014-04-14 17:07:12 -0500233 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700234 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400235 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700236 }
237
238 if imageRef == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400239 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700240 Function: "Rebuild",
241 Argument: "imageRef",
Jon Perritt30558642014-04-14 17:07:12 -0500242 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700243 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400244 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700245 }
246
247 if additional == nil {
248 additional = make(map[string]interface{}, 0)
249 }
250
251 additional["name"] = name
252 additional["imageRef"] = imageRef
253 additional["adminPass"] = password
254
Ash Wilson31f6bde2014-09-25 14:52:12 -0400255 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500256 ReqBody: struct {
257 R map[string]interface{} `json:"rebuild"`
258 }{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700259 additional,
260 },
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400261 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400262 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500263 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700264 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400265 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700266}
267
268// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400269// Note that this implies rebuilding it.
270// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700271// When the resize completes, the server will be in RESIZE_VERIFY state.
272// While in this state, you can explore the use of the new server's configuration.
273// If you like it, call ConfirmResize() to commit the resize permanently.
274// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson01626a32014-09-17 10:38:07 -0400275func Resize(client *gophercloud.ServiceClient, id, flavorRef string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400276 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500277 ReqBody: struct {
278 R map[string]interface{} `json:"resize"`
279 }{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700280 map[string]interface{}{"flavorRef": flavorRef},
281 },
Ash Wilson01626a32014-09-17 10:38:07 -0400282 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500283 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700284 })
285 return err
286}
287
288// ConfirmResize confirms a previous resize operation on a server.
289// See Resize() for more details.
Ash Wilson01626a32014-09-17 10:38:07 -0400290func ConfirmResize(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400291 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500292 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400293 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500294 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700295 })
296 return err
297}
298
299// RevertResize cancels a previous resize operation on a server.
300// See Resize() for more details.
Ash Wilson01626a32014-09-17 10:38:07 -0400301func RevertResize(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400302 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500303 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400304 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500305 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700306 })
307 return err
308}