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. |
Joe Topjian | f464c96 | 2016-09-12 08:02:43 -0600 | [diff] [blame] | 159 | Metadata map[string]string `json:"metadata,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 | // 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) { |
jrperritt | 0d7ed5d | 2016-08-16 11:23:26 -0500 | [diff] [blame] | 185 | sc := opts.ServiceClient |
| 186 | opts.ServiceClient = nil |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 187 | b, err := gophercloud.BuildRequestBody(opts, "") |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 191 | |
| 192 | if opts.UserData != nil { |
| 193 | encoded := base64.StdEncoding.EncodeToString(opts.UserData) |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 194 | b["user_data"] = &encoded |
Jon Perritt | 7b9671c | 2015-02-01 22:03:14 -0700 | [diff] [blame] | 195 | } |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 196 | |
| 197 | if len(opts.SecurityGroups) > 0 { |
| 198 | securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups)) |
| 199 | for i, groupName := range opts.SecurityGroups { |
| 200 | securityGroups[i] = map[string]interface{}{"name": groupName} |
| 201 | } |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 202 | b["security_groups"] = securityGroups |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 203 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 204 | |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 205 | if len(opts.Networks) > 0 { |
| 206 | networks := make([]map[string]interface{}, len(opts.Networks)) |
| 207 | for i, net := range opts.Networks { |
| 208 | networks[i] = make(map[string]interface{}) |
| 209 | if net.UUID != "" { |
| 210 | networks[i]["uuid"] = net.UUID |
| 211 | } |
| 212 | if net.Port != "" { |
| 213 | networks[i]["port"] = net.Port |
| 214 | } |
| 215 | if net.FixedIP != "" { |
| 216 | networks[i]["fixed_ip"] = net.FixedIP |
| 217 | } |
| 218 | } |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 219 | b["networks"] = networks |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Joe Topjian | 50cdddf | 2016-09-16 10:56:09 -0600 | [diff] [blame] | 222 | // If ImageRef isn't provided, check if ImageName was provided to ascertain |
| 223 | // the image ID. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 224 | if opts.ImageRef == "" { |
Joe Topjian | 50cdddf | 2016-09-16 10:56:09 -0600 | [diff] [blame] | 225 | if opts.ImageName != "" { |
| 226 | if sc == nil { |
| 227 | err := ErrNoClientProvidedForIDByName{} |
| 228 | err.Argument = "ServiceClient" |
| 229 | return nil, err |
| 230 | } |
| 231 | imageID, err := images.IDFromName(sc, opts.ImageName) |
| 232 | if err != nil { |
| 233 | return nil, err |
| 234 | } |
| 235 | b["imageRef"] = imageID |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 236 | } |
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 | } |
jrperritt | 0d7ed5d | 2016-08-16 11:23:26 -0500 | [diff] [blame] | 246 | if sc == 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 | } |
jrperritt | 0d7ed5d | 2016-08-16 11:23:26 -0500 | [diff] [blame] | 251 | flavorID, err := flavors.IDFromName(sc, 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. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 262 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 263 | reqBody, err := opts.ToServerCreateMap() |
| 264 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 265 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 266 | return |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 267 | } |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 268 | _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 269 | return |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Delete requests that a server previously provisioned be removed from your account. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 273 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 274 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 275 | return |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 278 | // ForceDelete forces the deletion of a server |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 279 | func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) { |
Jon Perritt | 01618ee | 2016-03-09 03:04:06 -0600 | [diff] [blame] | 280 | _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 281 | return |
Ian Duffy | 370c430 | 2016-01-21 10:44:56 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 284 | // Get requests details on a single server, by ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 285 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 286 | _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 287 | OkCodes: []int{200, 203}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 288 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 289 | return |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 292 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jon Perritt | 8204821 | 2014-10-13 22:33:13 -0500 | [diff] [blame] | 293 | type UpdateOptsBuilder interface { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 294 | ToServerUpdateMap() (map[string]interface{}, error) |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 298 | type UpdateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 299 | // Name changes the displayed name of the server. |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 300 | // The server host name will *not* change. |
| 301 | // 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] | 302 | Name string `json:"name,omitempty"` |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 303 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 304 | // AccessIPv4 provides a new IPv4 address for the instance. |
| 305 | AccessIPv4 string `json:"accessIPv4,omitempty"` |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 306 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 307 | // AccessIPv6 provides a new IPv6 address for the instance. |
| 308 | AccessIPv6 string `json:"accessIPv6,omitempty"` |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 311 | // ToServerUpdateMap formats an UpdateOpts structure into a request body. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 312 | func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) { |
| 313 | return gophercloud.BuildRequestBody(opts, "server") |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 316 | // Update requests that various attributes of the indicated server be changed. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 317 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 318 | b, err := opts.ToServerUpdateMap() |
| 319 | if err != nil { |
| 320 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 321 | return |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 322 | } |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 323 | _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 324 | OkCodes: []int{200}, |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 325 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 326 | return |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 327 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 328 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 329 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 330 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 331 | b := map[string]interface{}{ |
| 332 | "changePassword": map[string]string{ |
| 333 | "adminPass": newPassword, |
| 334 | }, |
| 335 | } |
| 336 | _, r.Err = client.Post(actionURL(client, id), b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 337 | return |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 340 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 341 | type RebootMethod string |
| 342 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 343 | // These constants determine how a server should be rebooted. |
| 344 | // See the Reboot() function for further details. |
| 345 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 346 | SoftReboot RebootMethod = "SOFT" |
| 347 | HardReboot RebootMethod = "HARD" |
| 348 | OSReboot = SoftReboot |
| 349 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 350 | ) |
| 351 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 352 | // RebootOptsBuilder is an interface that options must satisfy in order to be |
| 353 | // used when rebooting a server instance |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 354 | type RebootOptsBuilder interface { |
| 355 | ToServerRebootMap() (map[string]interface{}, error) |
| 356 | } |
| 357 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 358 | // RebootOpts satisfies the RebootOptsBuilder interface |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 359 | type RebootOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 360 | Type RebootMethod `json:"type" required:"true"` |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 361 | } |
| 362 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 363 | // ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder |
| 364 | // interface |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 365 | func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 366 | return gophercloud.BuildRequestBody(opts, "reboot") |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 367 | } |
| 368 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 369 | // Reboot requests that a given server reboot. |
| 370 | // Two methods exist for rebooting a server: |
| 371 | // |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 372 | // 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] | 373 | // terminating it at the hypervisor level. |
| 374 | // It's done. Caput. Full stop. |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 375 | // 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] | 376 | // |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 377 | // SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur. |
| 378 | // E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 379 | func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 380 | b, err := opts.ToServerRebootMap() |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 381 | if err != nil { |
| 382 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 383 | return |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 384 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 385 | _, r.Err = client.Post(actionURL(client, id), b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 386 | return |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 387 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 388 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 389 | // RebuildOptsBuilder is an interface that allows extensions to override the |
| 390 | // default behaviour of rebuild options |
| 391 | type RebuildOptsBuilder interface { |
| 392 | ToServerRebuildMap() (map[string]interface{}, error) |
| 393 | } |
| 394 | |
| 395 | // RebuildOpts represents the configuration options used in a server rebuild |
| 396 | // operation |
| 397 | type RebuildOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 398 | // The server's admin password |
| 399 | AdminPass string `json:"adminPass" required:"true"` |
| 400 | // The ID of the image you want your server to be provisioned on |
| 401 | ImageID string `json:"imageRef"` |
| 402 | ImageName string `json:"-"` |
| 403 | //ImageName string `json:"-"` |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 404 | // Name to set the server to |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 405 | Name string `json:"name,omitempty"` |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 406 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 407 | AccessIPv4 string `json:"accessIPv4,omitempty"` |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 408 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 409 | AccessIPv6 string `json:"accessIPv6,omitempty"` |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 410 | // 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] | 411 | Metadata map[string]string `json:"metadata,omitempty"` |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 412 | // Personality [optional] includes files to inject into the server at launch. |
| 413 | // Rebuild will base64-encode file contents for you. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 414 | Personality Personality `json:"personality,omitempty"` |
| 415 | ServiceClient *gophercloud.ServiceClient `json:"-"` |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON |
| 419 | func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 420 | b, err := gophercloud.BuildRequestBody(opts, "") |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 421 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 422 | return nil, err |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Joe Topjian | 50cdddf | 2016-09-16 10:56:09 -0600 | [diff] [blame] | 425 | // If ImageRef isn't provided, check if ImageName was provided to ascertain |
| 426 | // the image ID. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 427 | if opts.ImageID == "" { |
Joe Topjian | 50cdddf | 2016-09-16 10:56:09 -0600 | [diff] [blame] | 428 | if opts.ImageName != "" { |
| 429 | if opts.ServiceClient == nil { |
| 430 | err := ErrNoClientProvidedForIDByName{} |
| 431 | err.Argument = "ServiceClient" |
| 432 | return nil, err |
| 433 | } |
| 434 | imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName) |
| 435 | if err != nil { |
| 436 | return nil, err |
| 437 | } |
| 438 | b["imageRef"] = imageID |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 439 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 440 | } |
| 441 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 442 | return map[string]interface{}{"rebuild": b}, nil |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Rebuild will reprovision the server according to the configuration options |
| 446 | // provided in the RebuildOpts struct. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 447 | func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 448 | b, err := opts.ToServerRebuildMap() |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 449 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 450 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 451 | return |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 452 | } |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 453 | _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 454 | return |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 457 | // ResizeOptsBuilder is an interface that allows extensions to override the default structure of |
| 458 | // a Resize request. |
| 459 | type ResizeOptsBuilder interface { |
| 460 | ToServerResizeMap() (map[string]interface{}, error) |
| 461 | } |
| 462 | |
| 463 | // ResizeOpts represents the configuration options used to control a Resize operation. |
| 464 | type ResizeOpts struct { |
| 465 | // 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] | 466 | FlavorRef string `json:"flavorRef" required:"true"` |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 467 | } |
| 468 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 469 | // 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] | 470 | // Resize request. |
| 471 | func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 472 | return gophercloud.BuildRequestBody(opts, "resize") |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 475 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 476 | // Note that this implies rebuilding it. |
Gleb | 37b56e8 | 2016-09-06 19:07:58 +0300 | [diff] [blame] | 477 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
| 478 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 479 | // While in this state, you can explore the use of the new server's configuration. |
Gleb | 37b56e8 | 2016-09-06 19:07:58 +0300 | [diff] [blame] | 480 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 481 | // Otherwise, call RevertResize() to restore the old configuration. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 482 | func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 483 | b, err := opts.ToServerResizeMap() |
| 484 | if err != nil { |
| 485 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 486 | return |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 487 | } |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 488 | _, r.Err = client.Post(actionURL(client, id), b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 489 | return |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Gleb | 37b56e8 | 2016-09-06 19:07:58 +0300 | [diff] [blame] | 492 | // ConfirmResize confirms a previous resize operation on a server. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 493 | // See Resize() for more details. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 494 | func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 495 | _, 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] | 496 | OkCodes: []int{201, 202, 204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 497 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 498 | return |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Gleb | 37b56e8 | 2016-09-06 19:07:58 +0300 | [diff] [blame] | 501 | // RevertResize cancels a previous resize operation on a server. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 502 | // See Resize() for more details. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 503 | func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 504 | _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 505 | return |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 506 | } |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 507 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 508 | // RescueOptsBuilder is an interface that allows extensions to override the |
| 509 | // default structure of a Rescue request. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 510 | type RescueOptsBuilder interface { |
| 511 | ToServerRescueMap() (map[string]interface{}, error) |
| 512 | } |
| 513 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 514 | // RescueOpts represents the configuration options used to control a Rescue |
| 515 | // option. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 516 | type RescueOpts struct { |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 517 | // AdminPass is the desired administrative password for the instance in |
Alex Gaynor | cfec772 | 2014-11-13 13:33:49 -0800 | [diff] [blame] | 518 | // 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] | 519 | AdminPass string `json:"adminPass,omitempty"` |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 522 | // 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] | 523 | // request body for the Rescue request. |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 524 | func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 525 | return gophercloud.BuildRequestBody(opts, "rescue") |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Alex Gaynor | 266e933 | 2014-10-28 14:44:04 -0700 | [diff] [blame] | 528 | // Rescue instructs the provider to place the server into RESCUE mode. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 529 | func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 530 | b, err := opts.ToServerRescueMap() |
| 531 | if err != nil { |
| 532 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 533 | return |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 534 | } |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 535 | _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 536 | OkCodes: []int{200}, |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 537 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 538 | return |
Alex Gaynor | 39584a0 | 2014-10-28 13:59:21 -0700 | [diff] [blame] | 539 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 540 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 541 | // ResetMetadataOptsBuilder allows extensions to add additional parameters to the |
| 542 | // Reset request. |
| 543 | type ResetMetadataOptsBuilder interface { |
| 544 | ToMetadataResetMap() (map[string]interface{}, error) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 547 | // MetadataOpts is a map that contains key-value pairs. |
| 548 | type MetadataOpts map[string]string |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 549 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 550 | // ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts. |
| 551 | func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 552 | return map[string]interface{}{"metadata": opts}, nil |
| 553 | } |
| 554 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 555 | // ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts. |
| 556 | func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 557 | return map[string]interface{}{"metadata": opts}, nil |
| 558 | } |
| 559 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 560 | // 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] | 561 | // Note: Using this operation will erase any already-existing metadata and create |
| 562 | // the new metadata provided. To keep any already-existing metadata, use the |
| 563 | // UpdateMetadatas or UpdateMetadata function. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 564 | func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 565 | b, err := opts.ToMetadataResetMap() |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 566 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 567 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 568 | return |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 569 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 570 | _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 571 | OkCodes: []int{200}, |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 572 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 573 | return |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 576 | // Metadata requests all the metadata for the given server ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 577 | func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 578 | _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 579 | return |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 582 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to the |
| 583 | // Create request. |
| 584 | type UpdateMetadataOptsBuilder interface { |
| 585 | ToMetadataUpdateMap() (map[string]interface{}, error) |
| 586 | } |
| 587 | |
| 588 | // UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID. |
| 589 | // This operation does not affect already-existing metadata that is not specified |
| 590 | // by opts. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 591 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 592 | b, err := opts.ToMetadataUpdateMap() |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 593 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 594 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 595 | return |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 596 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 597 | _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 598 | OkCodes: []int{200}, |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 599 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 600 | return |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // MetadatumOptsBuilder allows extensions to add additional parameters to the |
| 604 | // Create request. |
| 605 | type MetadatumOptsBuilder interface { |
| 606 | ToMetadatumCreateMap() (map[string]interface{}, string, error) |
| 607 | } |
| 608 | |
| 609 | // MetadatumOpts is a map of length one that contains a key-value pair. |
| 610 | type MetadatumOpts map[string]string |
| 611 | |
| 612 | // ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts. |
| 613 | func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) { |
| 614 | if len(opts) != 1 { |
Jon Perritt | 1380826 | 2016-03-09 00:50:12 -0600 | [diff] [blame] | 615 | err := gophercloud.ErrInvalidInput{} |
| 616 | err.Argument = "servers.MetadatumOpts" |
| 617 | err.Info = "Must have 1 and only 1 key-value pair" |
| 618 | return nil, "", err |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 619 | } |
| 620 | metadatum := map[string]interface{}{"meta": opts} |
| 621 | var key string |
| 622 | for k := range metadatum["meta"].(MetadatumOpts) { |
| 623 | key = k |
| 624 | } |
| 625 | return metadatum, key, nil |
| 626 | } |
| 627 | |
| 628 | // CreateMetadatum will create or update the key-value pair with the given key for the given server ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 629 | func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 630 | b, key, err := opts.ToMetadatumCreateMap() |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 631 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 632 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 633 | return |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 634 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 635 | _, 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] | 636 | OkCodes: []int{200}, |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 637 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 638 | return |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Metadatum requests the key-value pair with the given key for the given server ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 642 | func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 643 | _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 644 | return |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // DeleteMetadatum will delete the key-value pair with the given key for the given server ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 648 | func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 649 | _, r.Err = client.Delete(metadatumURL(client, id, key), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 650 | return |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 651 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 652 | |
| 653 | // ListAddresses makes a request against the API to list the servers IP addresses. |
| 654 | func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 655 | 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] | 656 | return AddressPage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 657 | }) |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 658 | } |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 659 | |
| 660 | // ListAddressesByNetwork makes a request against the API to list the servers IP addresses |
| 661 | // for the given network. |
| 662 | func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 663 | 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] | 664 | return NetworkAddressPage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 665 | }) |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 666 | } |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 667 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 668 | // CreateImageOptsBuilder is the interface types must satisfy in order to be |
| 669 | // used as CreateImage options |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 670 | type CreateImageOptsBuilder interface { |
| 671 | ToServerCreateImageMap() (map[string]interface{}, error) |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 674 | // CreateImageOpts satisfies the CreateImageOptsBuilder |
| 675 | type CreateImageOpts struct { |
| 676 | // Name of the image/snapshot |
| 677 | Name string `json:"name" required:"true"` |
| 678 | // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image. |
| 679 | Metadata map[string]string `json:"metadata,omitempty"` |
| 680 | } |
| 681 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 682 | // ToServerCreateImageMap formats a CreateImageOpts structure into a request body. |
| 683 | func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 684 | return gophercloud.BuildRequestBody(opts, "createImage") |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 685 | } |
| 686 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 687 | // CreateImage makes a request against the nova API to schedule an image to be created of the server |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 688 | func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 689 | b, err := opts.ToServerCreateImageMap() |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 690 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 691 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 692 | return |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 693 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 694 | resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 695 | OkCodes: []int{202}, |
| 696 | }) |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 697 | r.Err = err |
| 698 | r.Header = resp.Header |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 699 | return |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 700 | } |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 701 | |
| 702 | // IDFromName is a convienience function that returns a server's ID given its name. |
| 703 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 704 | count := 0 |
| 705 | id := "" |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 706 | allPages, err := List(client, nil).AllPages() |
| 707 | if err != nil { |
| 708 | return "", err |
| 709 | } |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 710 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 711 | all, err := ExtractServers(allPages) |
| 712 | if err != nil { |
| 713 | return "", err |
| 714 | } |
| 715 | |
| 716 | for _, f := range all { |
| 717 | if f.Name == name { |
| 718 | count++ |
| 719 | id = f.ID |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | switch count { |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 724 | case 0: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 725 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"} |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 726 | case 1: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 727 | return id, nil |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 728 | default: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 729 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"} |
Jon Perritt | 6b0a883 | 2015-06-04 14:32:30 -0600 | [diff] [blame] | 730 | } |
| 731 | } |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 732 | |
Rickard von Essen | c3d49b7 | 2016-02-16 20:59:18 +0100 | [diff] [blame] | 733 | // GetPassword makes a request against the nova API to get the encrypted administrative password. |
jrperritt | 2f93a63 | 2016-04-13 15:41:20 -0500 | [diff] [blame] | 734 | func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) { |
| 735 | _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil) |
| 736 | return |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 737 | } |