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