Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 4 | "encoding/base64" |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 5 | "encoding/json" |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 6 | "errors" |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 7 | "fmt" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 8 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 13 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 14 | // List request. |
| 15 | type ListOptsBuilder interface { |
| 16 | ToServerListQuery() (string, error) |
| 17 | } |
Kevin Pike | 9748b7b | 2015-05-05 07:34:07 -0700 | [diff] [blame^] | 18 | |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 19 | // ListOpts allows the filtering and sorting of paginated collections through |
| 20 | // the API. Filtering is achieved by passing in struct field values that map to |
| 21 | // the server attributes you want to see returned. Marker and Limit are used |
| 22 | // for pagination. |
| 23 | type ListOpts struct { |
| 24 | // A time/date stamp for when the server last changed status. |
| 25 | ChangesSince string `q:"changes-since"` |
| 26 | |
| 27 | // Name of the image in URL format. |
| 28 | Image string `q:"image"` |
| 29 | |
| 30 | // Name of the flavor in URL format. |
| 31 | Flavor string `q:"flavor"` |
| 32 | |
| 33 | // Name of the server as a string; can be queried with regular expressions. |
| 34 | // Realize that ?name=bob returns both bob and bobb. If you need to match bob |
| 35 | // only, you can use a regular expression matching the syntax of the |
| 36 | // underlying database server implemented for Compute. |
| 37 | Name string `q:"name"` |
| 38 | |
| 39 | // Value of the status of the server so that you can filter on "ACTIVE" for example. |
| 40 | Status string `q:"status"` |
| 41 | |
| 42 | // Name of the host as a string. |
| 43 | Host string `q:"host"` |
| 44 | |
| 45 | // UUID of the server at which you want to set a marker. |
| 46 | Marker string `q:"marker"` |
| 47 | |
| 48 | // Integer value for the limit of values to return. |
| 49 | Limit int `q:"limit"` |
| 50 | } |
| 51 | |
| 52 | // ToServerListQuery formats a ListOpts into a query string. |
| 53 | func (opts ListOpts) ToServerListQuery() (string, error) { |
| 54 | q, err := gophercloud.BuildQueryString(opts) |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | return q.String(), nil |
| 59 | } |
| 60 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 61 | // List makes a request against the API to list servers accessible to you. |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 62 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 63 | url := listDetailURL(client) |
| 64 | |
| 65 | if opts != nil { |
| 66 | query, err := opts.ToServerListQuery() |
| 67 | if err != nil { |
| 68 | return pagination.Pager{Err: err} |
| 69 | } |
| 70 | url += query |
| 71 | } |
| 72 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 73 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 74 | return ServerPage{pagination.LinkedPageBase{PageResult: r}} |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 77 | return pagination.NewPager(client, url, createPageFn) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 80 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 81 | // The CreateOpts struct in this package does. |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 82 | type CreateOptsBuilder interface { |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 83 | ToServerCreateMap() (map[string]interface{}, error) |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Network is used within CreateOpts to control a new server's network attachments. |
| 87 | type Network struct { |
| 88 | // UUID of a nova-network to attach to the newly provisioned server. |
| 89 | // Required unless Port is provided. |
| 90 | UUID string |
| 91 | |
| 92 | // Port of a neutron network to attach to the newly provisioned server. |
| 93 | // Required unless UUID is provided. |
| 94 | Port string |
| 95 | |
| 96 | // FixedIP [optional] specifies a fixed IPv4 address to be used on this network. |
| 97 | FixedIP string |
| 98 | } |
| 99 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 100 | // Personality is an array of files that are injected into the server at launch. |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 101 | type Personality []*File |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 102 | |
| 103 | // File is used within CreateOpts and RebuildOpts to inject a file into the server at launch. |
Kevin Pike | 9748b7b | 2015-05-05 07:34:07 -0700 | [diff] [blame^] | 104 | // File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested, |
| 105 | // json.Marshal will call File's MarshalJSON method. |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 106 | type File struct { |
| 107 | // Path of the file |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 108 | Path string |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 109 | // Contents of the file. Maximum content size is 255 bytes. |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 110 | Contents []byte |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 113 | // MarshalJSON marshals the escaped file, base64 encoding the contents. |
| 114 | func (f *File) MarshalJSON() ([]byte, error) { |
| 115 | file := struct { |
| 116 | Path string `json:"path"` |
| 117 | Contents string `json:"contents"` |
| 118 | }{ |
| 119 | Path: f.Path, |
| 120 | Contents: base64.StdEncoding.EncodeToString(f.Contents), |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 121 | } |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 122 | return json.Marshal(file) |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 125 | // CreateOpts specifies server creation parameters. |
| 126 | type CreateOpts struct { |
| 127 | // Name [required] is the name to assign to the newly launched server. |
| 128 | Name string |
| 129 | |
| 130 | // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state. |
| 131 | // Optional if using the boot-from-volume extension. |
| 132 | ImageRef string |
| 133 | |
| 134 | // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs. |
| 135 | FlavorRef string |
| 136 | |
| 137 | // SecurityGroups [optional] lists the names of the security groups to which this server should belong. |
| 138 | SecurityGroups []string |
| 139 | |
| 140 | // UserData [optional] contains configuration information or scripts to use upon launch. |
| 141 | // Create will base64-encode it for you. |
| 142 | UserData []byte |
| 143 | |
| 144 | // AvailabilityZone [optional] in which to launch the server. |
| 145 | AvailabilityZone string |
| 146 | |
| 147 | // Networks [optional] dictates how this server will be attached to available networks. |
| 148 | // By default, the server will be attached to all isolated networks for the tenant. |
| 149 | Networks []Network |
| 150 | |
| 151 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 152 | Metadata map[string]string |
| 153 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 154 | // Personality [optional] includes files to inject into the server at launch. |
| 155 | // Create will base64-encode file contents for you. |
| 156 | Personality Personality |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 157 | |
| 158 | // ConfigDrive [optional] enables metadata injection through a configuration drive. |
| 159 | ConfigDrive bool |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 160 | |
| 161 | // AdminPass [optional] sets the root user password. If not set, a randomly-generated |
| 162 | // password will be created and returned in the response. |
| 163 | AdminPass string |
Jon Perritt | 7b9671c | 2015-02-01 22:03:14 -0700 | [diff] [blame] | 164 | |
| 165 | // AccessIPv4 [optional] specifies an IPv4 address for the instance. |
| 166 | AccessIPv4 string |
| 167 | |
| 168 | // AccessIPv6 [optional] specifies an IPv6 address for the instance. |
| 169 | AccessIPv6 string |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 172 | // ToServerCreateMap assembles a request body based on the contents of a CreateOpts. |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 173 | func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) { |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 174 | server := make(map[string]interface{}) |
| 175 | |
| 176 | server["name"] = opts.Name |
| 177 | server["imageRef"] = opts.ImageRef |
| 178 | server["flavorRef"] = opts.FlavorRef |
| 179 | |
| 180 | if opts.UserData != nil { |
| 181 | encoded := base64.StdEncoding.EncodeToString(opts.UserData) |
| 182 | server["user_data"] = &encoded |
| 183 | } |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 184 | if opts.ConfigDrive { |
| 185 | server["config_drive"] = "true" |
| 186 | } |
| 187 | if opts.AvailabilityZone != "" { |
| 188 | server["availability_zone"] = opts.AvailabilityZone |
| 189 | } |
| 190 | if opts.Metadata != nil { |
| 191 | server["metadata"] = opts.Metadata |
| 192 | } |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 193 | if opts.AdminPass != "" { |
| 194 | server["adminPass"] = opts.AdminPass |
| 195 | } |
Jon Perritt | 7b9671c | 2015-02-01 22:03:14 -0700 | [diff] [blame] | 196 | if opts.AccessIPv4 != "" { |
| 197 | server["accessIPv4"] = opts.AccessIPv4 |
| 198 | } |
| 199 | if opts.AccessIPv6 != "" { |
| 200 | server["accessIPv6"] = opts.AccessIPv6 |
| 201 | } |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 202 | |
| 203 | if len(opts.SecurityGroups) > 0 { |
| 204 | securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups)) |
| 205 | for i, groupName := range opts.SecurityGroups { |
| 206 | securityGroups[i] = map[string]interface{}{"name": groupName} |
| 207 | } |
esell | df70994 | 2014-11-13 21:07:11 -0700 | [diff] [blame] | 208 | server["security_groups"] = securityGroups |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 209 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 210 | |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 211 | if len(opts.Networks) > 0 { |
| 212 | networks := make([]map[string]interface{}, len(opts.Networks)) |
| 213 | for i, net := range opts.Networks { |
| 214 | networks[i] = make(map[string]interface{}) |
| 215 | if net.UUID != "" { |
| 216 | networks[i]["uuid"] = net.UUID |
| 217 | } |
| 218 | if net.Port != "" { |
| 219 | networks[i]["port"] = net.Port |
| 220 | } |
| 221 | if net.FixedIP != "" { |
| 222 | networks[i]["fixed_ip"] = net.FixedIP |
| 223 | } |
| 224 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 225 | server["networks"] = networks |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 228 | if len(opts.Personality) > 0 { |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 229 | server["personality"] = opts.Personality |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 232 | return map[string]interface{}{"server": server}, nil |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 235 | // Create requests a server to be provisioned to the user in the current tenant. |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 236 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 237 | var res CreateResult |
| 238 | |
| 239 | reqBody, err := opts.ToServerCreateMap() |
| 240 | if err != nil { |
| 241 | res.Err = err |
| 242 | return res |
| 243 | } |
| 244 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 245 | _, res.Err = client.Post(listURL(client), reqBody, &res.Body, nil) |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 246 | return res |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Delete requests that a server previously provisioned be removed from your account. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 250 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 251 | var res DeleteResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 252 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 253 | return res |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 256 | // Get requests details on a single server, by ID. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 257 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 258 | var result GetResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 259 | _, result.Err = client.Get(getURL(client, id), &result.Body, &gophercloud.RequestOpts{ |
| 260 | OkCodes: []int{200, 203}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 261 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 262 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 265 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jon Perritt | 8204821 | 2014-10-13 22:33:13 -0500 | [diff] [blame] | 266 | type UpdateOptsBuilder interface { |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 267 | ToServerUpdateMap() map[string]interface{} |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 271 | type UpdateOpts struct { |
| 272 | // Name [optional] changes the displayed name of the server. |
| 273 | // The server host name will *not* change. |
| 274 | // Server names are not constrained to be unique, even within the same tenant. |
| 275 | Name string |
| 276 | |
| 277 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
| 278 | AccessIPv4 string |
| 279 | |
| 280 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
| 281 | AccessIPv6 string |
| 282 | } |
| 283 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 284 | // ToServerUpdateMap formats an UpdateOpts structure into a request body. |
| 285 | func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} { |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 286 | server := make(map[string]string) |
| 287 | if opts.Name != "" { |
| 288 | server["name"] = opts.Name |
| 289 | } |
| 290 | if opts.AccessIPv4 != "" { |
| 291 | server["accessIPv4"] = opts.AccessIPv4 |
| 292 | } |
| 293 | if opts.AccessIPv6 != "" { |
| 294 | server["accessIPv6"] = opts.AccessIPv6 |
| 295 | } |
| 296 | return map[string]interface{}{"server": server} |
| 297 | } |
| 298 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 299 | // Update requests that various attributes of the indicated server be changed. |
Jon Perritt | 8204821 | 2014-10-13 22:33:13 -0500 | [diff] [blame] | 300 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 301 | var result UpdateResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 302 | reqBody := opts.ToServerUpdateMap() |
| 303 | _, result.Err = client.Put(updateURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{ |
| 304 | OkCodes: []int{200}, |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 305 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 306 | return result |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 307 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 308 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 309 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 310 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult { |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 311 | var req struct { |
| 312 | ChangePassword struct { |
| 313 | AdminPass string `json:"adminPass"` |
| 314 | } `json:"changePassword"` |
| 315 | } |
| 316 | |
| 317 | req.ChangePassword.AdminPass = newPassword |
| 318 | |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 319 | var res ActionResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 320 | _, res.Err = client.Post(actionURL(client, id), req, nil, nil) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 321 | return res |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 322 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 323 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 324 | // 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] | 325 | // fails to fall within acceptable values. For example, the Reboot() function |
| 326 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 327 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 328 | // other string values instead, perhaps in an effort to break the API of their |
| 329 | // provider. Reboot() returns this error in this situation. |
| 330 | // |
| 331 | // Function identifies which function was called/which function is generating |
| 332 | // the error. |
| 333 | // Argument identifies which formal argument was responsible for producing the |
| 334 | // error. |
| 335 | // 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] | 336 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 337 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 338 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 342 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 343 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 344 | } |
| 345 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 346 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 347 | return e.Error() |
| 348 | } |
| 349 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 350 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 351 | type RebootMethod string |
| 352 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 353 | // These constants determine how a server should be rebooted. |
| 354 | // See the Reboot() function for further details. |
| 355 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 356 | SoftReboot RebootMethod = "SOFT" |
| 357 | HardReboot RebootMethod = "HARD" |
| 358 | OSReboot = SoftReboot |
| 359 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 360 | ) |
| 361 | |
| 362 | // Reboot requests that a given server reboot. |
| 363 | // Two methods exist for rebooting a server: |
| 364 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 365 | // HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM, |
| 366 | // terminating it at the hypervisor level. |
| 367 | // It's done. Caput. Full stop. |
| 368 | // 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] | 369 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 370 | // SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures. |
| 371 | // E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 372 | func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult { |
| 373 | var res ActionResult |
| 374 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 375 | if (how != SoftReboot) && (how != HardReboot) { |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 376 | res.Err = &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 377 | Function: "Reboot", |
| 378 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 379 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 380 | } |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 381 | return res |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 382 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 383 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 384 | reqBody := struct { |
| 385 | C map[string]string `json:"reboot"` |
| 386 | }{ |
| 387 | map[string]string{"type": string(how)}, |
| 388 | } |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 389 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 390 | _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 391 | return res |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 392 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 393 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 394 | // RebuildOptsBuilder is an interface that allows extensions to override the |
| 395 | // default behaviour of rebuild options |
| 396 | type RebuildOptsBuilder interface { |
| 397 | ToServerRebuildMap() (map[string]interface{}, error) |
| 398 | } |
| 399 | |
| 400 | // RebuildOpts represents the configuration options used in a server rebuild |
| 401 | // operation |
| 402 | type RebuildOpts struct { |
| 403 | // Required. The ID of the image you want your server to be provisioned on |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 404 | ImageID string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 405 | |
| 406 | // Name to set the server to |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 407 | Name string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 408 | |
| 409 | // Required. The server's admin password |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 410 | AdminPass string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 411 | |
| 412 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 413 | AccessIPv4 string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 414 | |
| 415 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 416 | AccessIPv6 string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 417 | |
| 418 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 419 | Metadata map[string]string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 420 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 421 | // Personality [optional] includes files to inject into the server at launch. |
| 422 | // Rebuild will base64-encode file contents for you. |
| 423 | Personality Personality |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON |
| 427 | func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) { |
| 428 | var err error |
| 429 | server := make(map[string]interface{}) |
| 430 | |
| 431 | if opts.AdminPass == "" { |
| 432 | err = fmt.Errorf("AdminPass is required") |
| 433 | } |
| 434 | |
| 435 | if opts.ImageID == "" { |
| 436 | err = fmt.Errorf("ImageID is required") |
| 437 | } |
| 438 | |
| 439 | if err != nil { |
| 440 | return server, err |
| 441 | } |
| 442 | |
| 443 | server["name"] = opts.Name |
| 444 | server["adminPass"] = opts.AdminPass |
| 445 | server["imageRef"] = opts.ImageID |
| 446 | |
| 447 | if opts.AccessIPv4 != "" { |
| 448 | server["accessIPv4"] = opts.AccessIPv4 |
| 449 | } |
| 450 | |
| 451 | if opts.AccessIPv6 != "" { |
| 452 | server["accessIPv6"] = opts.AccessIPv6 |
| 453 | } |
| 454 | |
| 455 | if opts.Metadata != nil { |
| 456 | server["metadata"] = opts.Metadata |
| 457 | } |
| 458 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 459 | if len(opts.Personality) > 0 { |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 460 | server["personality"] = opts.Personality |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | return map[string]interface{}{"rebuild": server}, nil |
| 464 | } |
| 465 | |
| 466 | // Rebuild will reprovision the server according to the configuration options |
| 467 | // provided in the RebuildOpts struct. |
| 468 | func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 469 | var result RebuildResult |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 470 | |
| 471 | if id == "" { |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 472 | result.Err = fmt.Errorf("ID is required") |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 473 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 476 | reqBody, err := opts.ToServerRebuildMap() |
| 477 | if err != nil { |
| 478 | result.Err = err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 479 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 482 | _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, nil) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 483 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 486 | // ResizeOptsBuilder is an interface that allows extensions to override the default structure of |
| 487 | // a Resize request. |
| 488 | type ResizeOptsBuilder interface { |
| 489 | ToServerResizeMap() (map[string]interface{}, error) |
| 490 | } |
| 491 | |
| 492 | // ResizeOpts represents the configuration options used to control a Resize operation. |
| 493 | type ResizeOpts struct { |
| 494 | // FlavorRef is the ID of the flavor you wish your server to become. |
| 495 | FlavorRef string |
| 496 | } |
| 497 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 498 | // ToServerResizeMap formats a ResizeOpts as a map that can be used as a JSON request body for the |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 499 | // Resize request. |
| 500 | func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) { |
| 501 | resize := map[string]interface{}{ |
| 502 | "flavorRef": opts.FlavorRef, |
| 503 | } |
| 504 | |
| 505 | return map[string]interface{}{"resize": resize}, nil |
| 506 | } |
| 507 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 508 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 509 | // Note that this implies rebuilding it. |
| 510 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 511 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
| 512 | // While in this state, you can explore the use of the new server's configuration. |
| 513 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 514 | // Otherwise, call RevertResize() to restore the old configuration. |
Ash Wilson | 9e87a92 | 2014-10-23 14:29:22 -0400 | [diff] [blame] | 515 | func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult { |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 516 | var res ActionResult |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 517 | reqBody, err := opts.ToServerResizeMap() |
| 518 | if err != nil { |
| 519 | res.Err = err |
| 520 | return res |
| 521 | } |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 522 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 523 | _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 524 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // ConfirmResize confirms a previous resize operation on a server. |
| 528 | // See Resize() for more details. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 529 | func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult { |
| 530 | var res ActionResult |
| 531 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 532 | reqBody := map[string]interface{}{"confirmResize": nil} |
| 533 | _, res.Err = client.Post(actionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{ |
| 534 | OkCodes: []int{201, 202, 204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 535 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 536 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // RevertResize cancels a previous resize operation on a server. |
| 540 | // See Resize() for more details. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 541 | func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult { |
| 542 | var res ActionResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 543 | reqBody := map[string]interface{}{"revertResize": nil} |
| 544 | _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 545 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 546 | } |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 547 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 548 | // RescueOptsBuilder is an interface that allows extensions to override the |
| 549 | // default structure of a Rescue request. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 550 | type RescueOptsBuilder interface { |
| 551 | ToServerRescueMap() (map[string]interface{}, error) |
| 552 | } |
| 553 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 554 | // RescueOpts represents the configuration options used to control a Rescue |
| 555 | // option. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 556 | type RescueOpts struct { |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 557 | // AdminPass is the desired administrative password for the instance in |
Alex Gaynor | cfec772 | 2014-11-13 13:33:49 -0800 | [diff] [blame] | 558 | // RESCUE mode. If it's left blank, the server will generate a password. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 559 | AdminPass string |
| 560 | } |
| 561 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 562 | // ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 563 | // request body for the Rescue request. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 564 | func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) { |
| 565 | server := make(map[string]interface{}) |
| 566 | if opts.AdminPass != "" { |
| 567 | server["adminPass"] = opts.AdminPass |
| 568 | } |
| 569 | return map[string]interface{}{"rescue": server}, nil |
| 570 | } |
| 571 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 572 | // Rescue instructs the provider to place the server into RESCUE mode. |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 573 | func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult { |
| 574 | var result RescueResult |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 575 | |
| 576 | if id == "" { |
| 577 | result.Err = fmt.Errorf("ID is required") |
| 578 | return result |
| 579 | } |
| 580 | reqBody, err := opts.ToServerRescueMap() |
| 581 | if err != nil { |
| 582 | result.Err = err |
| 583 | return result |
| 584 | } |
| 585 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 586 | _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{ |
| 587 | OkCodes: []int{200}, |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 588 | }) |
| 589 | |
| 590 | return result |
| 591 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 592 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 593 | // ResetMetadataOptsBuilder allows extensions to add additional parameters to the |
| 594 | // Reset request. |
| 595 | type ResetMetadataOptsBuilder interface { |
| 596 | ToMetadataResetMap() (map[string]interface{}, error) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 599 | // MetadataOpts is a map that contains key-value pairs. |
| 600 | type MetadataOpts map[string]string |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 601 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 602 | // ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts. |
| 603 | func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 604 | return map[string]interface{}{"metadata": opts}, nil |
| 605 | } |
| 606 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 607 | // ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts. |
| 608 | func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 609 | return map[string]interface{}{"metadata": opts}, nil |
| 610 | } |
| 611 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 612 | // ResetMetadata will create multiple new key-value pairs for the given server ID. |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 613 | // Note: Using this operation will erase any already-existing metadata and create |
| 614 | // the new metadata provided. To keep any already-existing metadata, use the |
| 615 | // UpdateMetadatas or UpdateMetadata function. |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 616 | func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult { |
| 617 | var res ResetMetadataResult |
| 618 | metadata, err := opts.ToMetadataResetMap() |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 619 | if err != nil { |
| 620 | res.Err = err |
| 621 | return res |
| 622 | } |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 623 | _, res.Err = client.Put(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{ |
| 624 | OkCodes: []int{200}, |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 625 | }) |
| 626 | return res |
| 627 | } |
| 628 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 629 | // Metadata requests all the metadata for the given server ID. |
| 630 | func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 631 | var res GetMetadataResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 632 | _, res.Err = client.Get(metadataURL(client, id), &res.Body, nil) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 633 | return res |
| 634 | } |
| 635 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 636 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to the |
| 637 | // Create request. |
| 638 | type UpdateMetadataOptsBuilder interface { |
| 639 | ToMetadataUpdateMap() (map[string]interface{}, error) |
| 640 | } |
| 641 | |
| 642 | // UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID. |
| 643 | // This operation does not affect already-existing metadata that is not specified |
| 644 | // by opts. |
| 645 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult { |
| 646 | var res UpdateMetadataResult |
| 647 | metadata, err := opts.ToMetadataUpdateMap() |
| 648 | if err != nil { |
| 649 | res.Err = err |
| 650 | return res |
| 651 | } |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 652 | _, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{ |
| 653 | OkCodes: []int{200}, |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 654 | }) |
| 655 | return res |
| 656 | } |
| 657 | |
| 658 | // MetadatumOptsBuilder allows extensions to add additional parameters to the |
| 659 | // Create request. |
| 660 | type MetadatumOptsBuilder interface { |
| 661 | ToMetadatumCreateMap() (map[string]interface{}, string, error) |
| 662 | } |
| 663 | |
| 664 | // MetadatumOpts is a map of length one that contains a key-value pair. |
| 665 | type MetadatumOpts map[string]string |
| 666 | |
| 667 | // ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts. |
| 668 | func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) { |
| 669 | if len(opts) != 1 { |
| 670 | return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.") |
| 671 | } |
| 672 | metadatum := map[string]interface{}{"meta": opts} |
| 673 | var key string |
| 674 | for k := range metadatum["meta"].(MetadatumOpts) { |
| 675 | key = k |
| 676 | } |
| 677 | return metadatum, key, nil |
| 678 | } |
| 679 | |
| 680 | // CreateMetadatum will create or update the key-value pair with the given key for the given server ID. |
| 681 | func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult { |
| 682 | var res CreateMetadatumResult |
| 683 | metadatum, key, err := opts.ToMetadatumCreateMap() |
| 684 | if err != nil { |
| 685 | res.Err = err |
| 686 | return res |
| 687 | } |
| 688 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 689 | _, res.Err = client.Put(metadatumURL(client, id, key), metadatum, &res.Body, &gophercloud.RequestOpts{ |
| 690 | OkCodes: []int{200}, |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 691 | }) |
| 692 | return res |
| 693 | } |
| 694 | |
| 695 | // Metadatum requests the key-value pair with the given key for the given server ID. |
| 696 | func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult { |
| 697 | var res GetMetadatumResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 698 | _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{ |
| 699 | JSONResponse: &res.Body, |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 700 | }) |
| 701 | return res |
| 702 | } |
| 703 | |
| 704 | // DeleteMetadatum will delete the key-value pair with the given key for the given server ID. |
| 705 | func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult { |
| 706 | var res DeleteMetadatumResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 707 | _, res.Err = client.Delete(metadatumURL(client, id, key), &gophercloud.RequestOpts{ |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 708 | JSONResponse: &res.Body, |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 709 | }) |
| 710 | return res |
| 711 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 712 | |
| 713 | // ListAddresses makes a request against the API to list the servers IP addresses. |
| 714 | func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager { |
| 715 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 716 | return AddressPage{pagination.SinglePageBase(r)} |
| 717 | } |
| 718 | return pagination.NewPager(client, listAddressesURL(client, id), createPageFn) |
| 719 | } |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 720 | |
| 721 | // ListAddressesByNetwork makes a request against the API to list the servers IP addresses |
| 722 | // for the given network. |
| 723 | func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager { |
| 724 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 725 | return NetworkAddressPage{pagination.SinglePageBase(r)} |
| 726 | } |
| 727 | return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn) |
| 728 | } |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 729 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 730 | type CreateImageOpts struct { |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 731 | // Name [required] of the image/snapshot |
| 732 | Name string |
| 733 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the created image. |
| 734 | Metadata map[string]string |
| 735 | } |
| 736 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 737 | type CreateImageOptsBuilder interface { |
| 738 | ToServerCreateImageMap() (map[string]interface{}, error) |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 739 | } |
| 740 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 741 | // ToServerCreateImageMap formats a CreateImageOpts structure into a request body. |
| 742 | func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) { |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 743 | var err error |
| 744 | img := make(map[string]interface{}) |
| 745 | if opts.Name == "" { |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 746 | return nil, fmt.Errorf("Cannot create a server image without a name") |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 747 | } |
| 748 | img["name"] = opts.Name |
| 749 | if opts.Metadata != nil { |
| 750 | img["metadata"] = opts.Metadata |
| 751 | } |
| 752 | createImage := make(map[string]interface{}) |
| 753 | createImage["createImage"] = img |
| 754 | return createImage, err |
| 755 | } |
| 756 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 757 | // CreateImage makes a request against the nova API to schedule an image to be created of the server |
| 758 | func CreateImage(client *gophercloud.ServiceClient, serverId string, opts CreateImageOptsBuilder) CreateImageResult { |
| 759 | var res CreateImageResult |
| 760 | reqBody, err := opts.ToServerCreateImageMap() |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 761 | if err != nil { |
| 762 | res.Err = err |
| 763 | return res |
| 764 | } |
| 765 | response, err := client.Post(actionURL(client, serverId), reqBody, nil, &gophercloud.RequestOpts{ |
| 766 | OkCodes: []int{202}, |
| 767 | }) |
| 768 | res.Err = err |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 769 | res.Header = response.Header |
Kevin Pike | 9748b7b | 2015-05-05 07:34:07 -0700 | [diff] [blame^] | 770 | return res |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 771 | } |