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