Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 5 | |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 7 | "github.com/racker/perigee" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 12 | // ListPage abstracts the raw results of making a List() request against the API. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 13 | // 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 Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 15 | type ListPage struct { |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 16 | pagination.LinkedPageBase |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 17 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 18 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 19 | // IsEmpty returns true if a page contains no Server results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 20 | func (page ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 21 | servers, err := ExtractServers(page) |
| 22 | if err != nil { |
| 23 | return true, err |
| 24 | } |
| 25 | return len(servers) == 0, nil |
| 26 | } |
| 27 | |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 28 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 29 | func (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 Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 40 | if err != nil { |
| 41 | return "", err |
| 42 | } |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 43 | |
| 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 Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 51 | return "", nil |
| 52 | } |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 53 | |
| 54 | return url, nil |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 57 | // List makes a request against the API to list servers accessible to you. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 58 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 59 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 60 | return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 63 | return pagination.NewPager(client, detailURL(client), createPage) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 66 | // Create requests a server to be provisioned to the user in the current tenant. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 67 | func Create(client *gophercloud.ServiceClient, opts map[string]interface{}) CreateResult { |
| 68 | var result CreateResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 69 | _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{ |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 70 | Results: &result.Resp, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 71 | ReqBody: map[string]interface{}{ |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 72 | "server": opts, |
| 73 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 74 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 75 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 76 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 77 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Delete requests that a server previously provisioned be removed from your account. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 81 | func Delete(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 82 | _, err := perigee.Request("DELETE", serverURL(client, id), perigee.Options{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 83 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 84 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 85 | }) |
| 86 | return err |
| 87 | } |
| 88 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 89 | // Get requests details on a single server, by ID. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 90 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 91 | var result GetResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 92 | _, result.Err = perigee.Request("GET", serverURL(client, id), perigee.Options{ |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 93 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 94 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 95 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 96 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 99 | // Update requests that various attributes of the indicated server be changed. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 100 | func Update(client *gophercloud.ServiceClient, id string, opts map[string]interface{}) UpdateResult { |
| 101 | var result UpdateResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 102 | _, result.Err = perigee.Request("PUT", serverURL(client, id), perigee.Options{ |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 103 | Results: &result.Resp, |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 104 | ReqBody: map[string]interface{}{ |
| 105 | "server": opts, |
| 106 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 107 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 108 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 109 | return result |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 110 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 111 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 112 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
| 113 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) error { |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 114 | var req struct { |
| 115 | ChangePassword struct { |
| 116 | AdminPass string `json:"adminPass"` |
| 117 | } `json:"changePassword"` |
| 118 | } |
| 119 | |
| 120 | req.ChangePassword.AdminPass = newPassword |
| 121 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 122 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 123 | ReqBody: req, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 124 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 125 | OkCodes: []int{202}, |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 126 | }) |
| 127 | return err |
| 128 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 129 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 130 | // ErrArgument errors occur when an argument supplied to a package function |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 131 | // 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 II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 142 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 143 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 144 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 148 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 149 | 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 II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 152 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 153 | return e.Error() |
| 154 | } |
| 155 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 156 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 157 | type RebootMethod string |
| 158 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 159 | // These constants determine how a server should be rebooted. |
| 160 | // See the Reboot() function for further details. |
| 161 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 162 | SoftReboot RebootMethod = "SOFT" |
| 163 | HardReboot RebootMethod = "HARD" |
| 164 | OSReboot = SoftReboot |
| 165 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 166 | ) |
| 167 | |
| 168 | // Reboot requests that a given server reboot. |
| 169 | // Two methods exist for rebooting a server: |
| 170 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 171 | // 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 II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 175 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 176 | // 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. |
| 178 | func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) error { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 179 | if (how != SoftReboot) && (how != HardReboot) { |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 180 | return &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 181 | Function: "Reboot", |
| 182 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 183 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 186 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 187 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 188 | ReqBody: struct { |
| 189 | C map[string]string `json:"reboot"` |
| 190 | }{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 191 | map[string]string{"type": string(how)}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 192 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 193 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 194 | OkCodes: []int{202}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 195 | }) |
| 196 | return err |
| 197 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 198 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 199 | // 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 II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 202 | // |
| 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 Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 206 | // 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 Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 208 | func Rebuild(client *gophercloud.ServiceClient, id, name, password, imageRef string, additional map[string]interface{}) RebuildResult { |
| 209 | var result RebuildResult |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 210 | |
| 211 | if id == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 212 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 213 | Function: "Rebuild", |
| 214 | Argument: "id", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 215 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 216 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 217 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if name == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 221 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 222 | Function: "Rebuild", |
| 223 | Argument: "name", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 224 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 225 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 226 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if password == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 230 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 231 | Function: "Rebuild", |
| 232 | Argument: "password", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 233 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 234 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 235 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if imageRef == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 239 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 240 | Function: "Rebuild", |
| 241 | Argument: "imageRef", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 242 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 243 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 244 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 245 | } |
| 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 Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 255 | _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 256 | ReqBody: struct { |
| 257 | R map[string]interface{} `json:"rebuild"` |
| 258 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 259 | additional, |
| 260 | }, |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 261 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 262 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 263 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 264 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 265 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 269 | // Note that this implies rebuilding it. |
| 270 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 271 | // 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 Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 275 | func Resize(client *gophercloud.ServiceClient, id, flavorRef string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 276 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 277 | ReqBody: struct { |
| 278 | R map[string]interface{} `json:"resize"` |
| 279 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 280 | map[string]interface{}{"flavorRef": flavorRef}, |
| 281 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 282 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 283 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 284 | }) |
| 285 | return err |
| 286 | } |
| 287 | |
| 288 | // ConfirmResize confirms a previous resize operation on a server. |
| 289 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 290 | func ConfirmResize(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 291 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 292 | ReqBody: map[string]interface{}{"confirmResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 293 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 294 | OkCodes: []int{204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 295 | }) |
| 296 | return err |
| 297 | } |
| 298 | |
| 299 | // RevertResize cancels a previous resize operation on a server. |
| 300 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 301 | func RevertResize(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 302 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 303 | ReqBody: map[string]interface{}{"revertResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 304 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 305 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 306 | }) |
| 307 | return err |
| 308 | } |