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