blob: 6c72a0ac2c7e9cca19045c3bbcac369870eb0f08 [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
Jon Perritt30558642014-04-14 17:07:12 -05006 "github.com/racker/perigee"
Ash Wilson01626a32014-09-17 10:38:07 -04007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08009)
10
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080011// List makes a request against the API to list servers accessible to you.
Ash Wilson01626a32014-09-17 10:38:07 -040012func List(client *gophercloud.ServiceClient) pagination.Pager {
13 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson397c78b2014-09-25 15:19:14 -040014 return ServerPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080015 }
16
Ash Wilson31f6bde2014-09-25 14:52:12 -040017 return pagination.NewPager(client, detailURL(client), createPage)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080018}
19
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080020// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilsond27e0ff2014-09-25 11:50:31 -040021func Create(client *gophercloud.ServiceClient, opts map[string]interface{}) CreateResult {
22 var result CreateResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040023 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -040024 Results: &result.Resp,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080025 ReqBody: map[string]interface{}{
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080026 "server": opts,
27 },
Ash Wilson01626a32014-09-17 10:38:07 -040028 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080029 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080030 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -040031 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080032}
33
34// Delete requests that a server previously provisioned be removed from your account.
Ash Wilson01626a32014-09-17 10:38:07 -040035func Delete(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -040036 _, err := perigee.Request("DELETE", serverURL(client, id), perigee.Options{
Ash Wilson01626a32014-09-17 10:38:07 -040037 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080038 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080039 })
40 return err
41}
42
Ash Wilson7ddf0362014-09-17 10:59:09 -040043// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -040044func Get(client *gophercloud.ServiceClient, id string) GetResult {
45 var result GetResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040046 _, result.Err = perigee.Request("GET", serverURL(client, id), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -040047 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -040048 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080049 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -040050 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080051}
52
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080053// Update requests that various attributes of the indicated server be changed.
Ash Wilsond27e0ff2014-09-25 11:50:31 -040054func Update(client *gophercloud.ServiceClient, id string, opts map[string]interface{}) UpdateResult {
55 var result UpdateResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040056 _, result.Err = perigee.Request("PUT", serverURL(client, id), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -040057 Results: &result.Resp,
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080058 ReqBody: map[string]interface{}{
59 "server": opts,
60 },
Ash Wilson01626a32014-09-17 10:38:07 -040061 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080062 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -040063 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -080064}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070065
Ash Wilson01626a32014-09-17 10:38:07 -040066// ChangeAdminPassword alters the administrator or root password for a specified server.
67func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) error {
Ash Wilsondc7daa82014-09-23 16:34:42 -040068 var req struct {
69 ChangePassword struct {
70 AdminPass string `json:"adminPass"`
71 } `json:"changePassword"`
72 }
73
74 req.ChangePassword.AdminPass = newPassword
75
Ash Wilson31f6bde2014-09-25 14:52:12 -040076 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -040077 ReqBody: req,
Ash Wilson01626a32014-09-17 10:38:07 -040078 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -050079 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070080 })
81 return err
82}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -070083
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070084// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -070085// fails to fall within acceptable values. For example, the Reboot() function
86// expects the "how" parameter to be one of HardReboot or SoftReboot. These
87// constants are (currently) strings, leading someone to wonder if they can pass
88// other string values instead, perhaps in an effort to break the API of their
89// provider. Reboot() returns this error in this situation.
90//
91// Function identifies which function was called/which function is generating
92// the error.
93// Argument identifies which formal argument was responsible for producing the
94// error.
95// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070096type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -070097 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -050098 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -070099}
100
101// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700102func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700103 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
104}
105
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700106func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700107 return e.Error()
108}
109
Ash Wilson01626a32014-09-17 10:38:07 -0400110// RebootMethod describes the mechanisms by which a server reboot can be requested.
111type RebootMethod string
112
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700113// These constants determine how a server should be rebooted.
114// See the Reboot() function for further details.
115const (
Ash Wilson01626a32014-09-17 10:38:07 -0400116 SoftReboot RebootMethod = "SOFT"
117 HardReboot RebootMethod = "HARD"
118 OSReboot = SoftReboot
119 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700120)
121
122// Reboot requests that a given server reboot.
123// Two methods exist for rebooting a server:
124//
Ash Wilson01626a32014-09-17 10:38:07 -0400125// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
126// terminating it at the hypervisor level.
127// It's done. Caput. Full stop.
128// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700129//
Ash Wilson01626a32014-09-17 10:38:07 -0400130// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
131// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine.
132func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) error {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700133 if (how != SoftReboot) && (how != HardReboot) {
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700134 return &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700135 Function: "Reboot",
136 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500137 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700138 }
139 }
Jon Perritt30558642014-04-14 17:07:12 -0500140
Ash Wilson31f6bde2014-09-25 14:52:12 -0400141 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500142 ReqBody: struct {
143 C map[string]string `json:"reboot"`
144 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400145 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700146 },
Ash Wilson01626a32014-09-17 10:38:07 -0400147 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500148 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700149 })
150 return err
151}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700152
Ash Wilson01626a32014-09-17 10:38:07 -0400153// Rebuild requests that the Openstack provider reprovision the server.
154// The rebuild will need to know the server's name and new image reference or ID.
155// In addition, and unlike building a server with Create(), you must provide an administrator password.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700156//
157// Additional options may be specified with the additional map.
158// This function treats a nil map the same as an empty map.
159//
Ash Wilson01626a32014-09-17 10:38:07 -0400160// Rebuild returns a server result as though you had called GetDetail() on the server's ID.
161// The information, however, refers to the new server, not the old.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400162func Rebuild(client *gophercloud.ServiceClient, id, name, password, imageRef string, additional map[string]interface{}) RebuildResult {
163 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700164
165 if id == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400166 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700167 Function: "Rebuild",
168 Argument: "id",
Jon Perritt30558642014-04-14 17:07:12 -0500169 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700170 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400171 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700172 }
173
174 if name == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400175 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700176 Function: "Rebuild",
177 Argument: "name",
Jon Perritt30558642014-04-14 17:07:12 -0500178 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700179 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400180 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700181 }
182
183 if password == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400184 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700185 Function: "Rebuild",
186 Argument: "password",
Jon Perritt30558642014-04-14 17:07:12 -0500187 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700188 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400189 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700190 }
191
192 if imageRef == "" {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400193 result.Err = &ErrArgument{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700194 Function: "Rebuild",
195 Argument: "imageRef",
Jon Perritt30558642014-04-14 17:07:12 -0500196 Value: "",
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700197 }
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400198 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700199 }
200
201 if additional == nil {
202 additional = make(map[string]interface{}, 0)
203 }
204
205 additional["name"] = name
206 additional["imageRef"] = imageRef
207 additional["adminPass"] = password
208
Ash Wilson31f6bde2014-09-25 14:52:12 -0400209 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500210 ReqBody: struct {
211 R map[string]interface{} `json:"rebuild"`
212 }{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700213 additional,
214 },
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400215 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400216 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500217 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700218 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400219 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700220}
221
222// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400223// Note that this implies rebuilding it.
224// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700225// When the resize completes, the server will be in RESIZE_VERIFY state.
226// While in this state, you can explore the use of the new server's configuration.
227// If you like it, call ConfirmResize() to commit the resize permanently.
228// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson01626a32014-09-17 10:38:07 -0400229func Resize(client *gophercloud.ServiceClient, id, flavorRef string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400230 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500231 ReqBody: struct {
232 R map[string]interface{} `json:"resize"`
233 }{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700234 map[string]interface{}{"flavorRef": flavorRef},
235 },
Ash Wilson01626a32014-09-17 10:38:07 -0400236 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500237 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700238 })
239 return err
240}
241
242// ConfirmResize confirms a previous resize operation on a server.
243// See Resize() for more details.
Ash Wilson01626a32014-09-17 10:38:07 -0400244func ConfirmResize(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400245 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500246 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400247 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500248 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700249 })
250 return err
251}
252
253// RevertResize cancels a previous resize operation on a server.
254// See Resize() for more details.
Ash Wilson01626a32014-09-17 10:38:07 -0400255func RevertResize(client *gophercloud.ServiceClient, id string) error {
Ash Wilson31f6bde2014-09-25 14:52:12 -0400256 _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500257 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400258 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500259 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700260 })
261 return err
262}