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