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