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