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 | |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 6 | "github.com/racker/perigee" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 11 | // 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] | 12 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 13 | // data provided through the ExtractServers call. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 14 | type ListPage struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 15 | pagination.MarkerPageBase |
| 16 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 17 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 18 | // IsEmpty returns true if a page contains no Server results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 19 | func (page ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 20 | servers, err := ExtractServers(page) |
| 21 | if err != nil { |
| 22 | return true, err |
| 23 | } |
| 24 | return len(servers) == 0, nil |
| 25 | } |
| 26 | |
| 27 | // LastMarker returns the ID of the final server on the current page. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 28 | func (page ListPage) LastMarker() (string, error) { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 29 | servers, err := ExtractServers(page) |
| 30 | if err != nil { |
| 31 | return "", err |
| 32 | } |
| 33 | if len(servers) == 0 { |
| 34 | return "", nil |
| 35 | } |
| 36 | return servers[len(servers)-1].ID, nil |
| 37 | } |
| 38 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 39 | // 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] | 40 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 41 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 42 | p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 43 | p.MarkerPageBase.Owner = p |
| 44 | return p |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 47 | return pagination.NewPager(client, getDetailURL(client), createPage) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 50 | // 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] | 51 | func Create(client *gophercloud.ServiceClient, opts map[string]interface{}) CreateResult { |
| 52 | var result CreateResult |
| 53 | _, result.Err = perigee.Request("POST", getListURL(client), perigee.Options{ |
| 54 | Results: &result.Resp, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 55 | ReqBody: map[string]interface{}{ |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 56 | "server": opts, |
| 57 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 58 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 59 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 60 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 61 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Delete requests that a server previously provisioned be removed from your account. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 65 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 66 | _, err := perigee.Request("DELETE", getServerURL(client, id), perigee.Options{ |
| 67 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 68 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 69 | }) |
| 70 | return err |
| 71 | } |
| 72 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 73 | // Get requests details on a single server, by ID. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 74 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 75 | var result GetResult |
| 76 | _, result.Err = perigee.Request("GET", getServerURL(client, id), perigee.Options{ |
| 77 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 78 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 79 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 80 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 83 | // Update requests that various attributes of the indicated server be changed. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 84 | func Update(client *gophercloud.ServiceClient, id string, opts map[string]interface{}) UpdateResult { |
| 85 | var result UpdateResult |
| 86 | _, result.Err = perigee.Request("PUT", getServerURL(client, id), perigee.Options{ |
| 87 | Results: &result.Resp, |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 88 | ReqBody: map[string]interface{}{ |
| 89 | "server": opts, |
| 90 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 91 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 92 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 93 | return result |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 94 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 95 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 96 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
| 97 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) error { |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 98 | var req struct { |
| 99 | ChangePassword struct { |
| 100 | AdminPass string `json:"adminPass"` |
| 101 | } `json:"changePassword"` |
| 102 | } |
| 103 | |
| 104 | req.ChangePassword.AdminPass = newPassword |
| 105 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 106 | _, err := perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 107 | ReqBody: req, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 108 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 109 | OkCodes: []int{202}, |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 110 | }) |
| 111 | return err |
| 112 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 113 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 114 | // 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] | 115 | // fails to fall within acceptable values. For example, the Reboot() function |
| 116 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 117 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 118 | // other string values instead, perhaps in an effort to break the API of their |
| 119 | // provider. Reboot() returns this error in this situation. |
| 120 | // |
| 121 | // Function identifies which function was called/which function is generating |
| 122 | // the error. |
| 123 | // Argument identifies which formal argument was responsible for producing the |
| 124 | // error. |
| 125 | // 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] | 126 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 127 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 128 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 132 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 133 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 134 | } |
| 135 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 136 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 137 | return e.Error() |
| 138 | } |
| 139 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 140 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 141 | type RebootMethod string |
| 142 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 143 | // These constants determine how a server should be rebooted. |
| 144 | // See the Reboot() function for further details. |
| 145 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 146 | SoftReboot RebootMethod = "SOFT" |
| 147 | HardReboot RebootMethod = "HARD" |
| 148 | OSReboot = SoftReboot |
| 149 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 150 | ) |
| 151 | |
| 152 | // Reboot requests that a given server reboot. |
| 153 | // Two methods exist for rebooting a server: |
| 154 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 155 | // HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM, |
| 156 | // terminating it at the hypervisor level. |
| 157 | // It's done. Caput. Full stop. |
| 158 | // 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] | 159 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 160 | // SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures. |
| 161 | // E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine. |
| 162 | func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) error { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 163 | if (how != SoftReboot) && (how != HardReboot) { |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 164 | return &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 165 | Function: "Reboot", |
| 166 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 167 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 170 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 171 | _, err := perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 172 | ReqBody: struct { |
| 173 | C map[string]string `json:"reboot"` |
| 174 | }{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 175 | map[string]string{"type": string(how)}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 176 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 177 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 178 | OkCodes: []int{202}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 179 | }) |
| 180 | return err |
| 181 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 182 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 183 | // Rebuild requests that the Openstack provider reprovision the server. |
| 184 | // The rebuild will need to know the server's name and new image reference or ID. |
| 185 | // 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] | 186 | // |
| 187 | // Additional options may be specified with the additional map. |
| 188 | // This function treats a nil map the same as an empty map. |
| 189 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 190 | // Rebuild returns a server result as though you had called GetDetail() on the server's ID. |
| 191 | // The information, however, refers to the new server, not the old. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 192 | func Rebuild(client *gophercloud.ServiceClient, id, name, password, imageRef string, additional map[string]interface{}) RebuildResult { |
| 193 | var result RebuildResult |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 194 | |
| 195 | if id == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 196 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 197 | Function: "Rebuild", |
| 198 | Argument: "id", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 199 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 200 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 201 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if name == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 205 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 206 | Function: "Rebuild", |
| 207 | Argument: "name", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 208 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 209 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 210 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if password == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 214 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 215 | Function: "Rebuild", |
| 216 | Argument: "password", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 217 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 218 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 219 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if imageRef == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 223 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 224 | Function: "Rebuild", |
| 225 | Argument: "imageRef", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 226 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 227 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 228 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if additional == nil { |
| 232 | additional = make(map[string]interface{}, 0) |
| 233 | } |
| 234 | |
| 235 | additional["name"] = name |
| 236 | additional["imageRef"] = imageRef |
| 237 | additional["adminPass"] = password |
| 238 | |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 239 | _, result.Err = perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 240 | ReqBody: struct { |
| 241 | R map[string]interface{} `json:"rebuild"` |
| 242 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 243 | additional, |
| 244 | }, |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 245 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 246 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 247 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 248 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 249 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 253 | // Note that this implies rebuilding it. |
| 254 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 255 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
| 256 | // While in this state, you can explore the use of the new server's configuration. |
| 257 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 258 | // Otherwise, call RevertResize() to restore the old configuration. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 259 | func Resize(client *gophercloud.ServiceClient, id, flavorRef string) error { |
| 260 | _, err := perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 261 | ReqBody: struct { |
| 262 | R map[string]interface{} `json:"resize"` |
| 263 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 264 | map[string]interface{}{"flavorRef": flavorRef}, |
| 265 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 266 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 267 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 268 | }) |
| 269 | return err |
| 270 | } |
| 271 | |
| 272 | // ConfirmResize confirms a previous resize operation on a server. |
| 273 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 274 | func ConfirmResize(client *gophercloud.ServiceClient, id string) error { |
| 275 | _, err := perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 276 | ReqBody: map[string]interface{}{"confirmResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 277 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 278 | OkCodes: []int{204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 279 | }) |
| 280 | return err |
| 281 | } |
| 282 | |
| 283 | // RevertResize cancels a previous resize operation on a server. |
| 284 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 285 | func RevertResize(client *gophercloud.ServiceClient, id string) error { |
| 286 | _, err := perigee.Request("POST", getActionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 287 | ReqBody: map[string]interface{}{"revertResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 288 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 289 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 290 | }) |
| 291 | return err |
| 292 | } |