Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 4 | "crypto/rsa" |
| 5 | "encoding/base64" |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 6 | "encoding/json" |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 7 | "fmt" |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 8 | "net/url" |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 9 | "path" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 10 | "time" |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 11 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 12 | "github.com/gophercloud/gophercloud" |
| 13 | "github.com/gophercloud/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 16 | type serverResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 17 | gophercloud.Result |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // Extract interprets any serverResult as a Server, if possible. |
| 21 | func (r serverResult) Extract() (*Server, error) { |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 22 | var s Server |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 23 | err := r.ExtractInto(&s) |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 24 | return &s, err |
| 25 | } |
| 26 | |
| 27 | func (r serverResult) ExtractInto(v interface{}) error { |
| 28 | return r.Result.ExtractIntoStructPtr(v, "server") |
| 29 | } |
| 30 | |
| 31 | func ExtractServersInto(r pagination.Page, v interface{}) error { |
| 32 | return r.(ServerPage).Result.ExtractIntoSlicePtr(v, "servers") |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // CreateResult temporarily contains the response from a Create call. |
| 36 | type CreateResult struct { |
| 37 | serverResult |
| 38 | } |
| 39 | |
| 40 | // GetResult temporarily contains the response from a Get call. |
| 41 | type GetResult struct { |
| 42 | serverResult |
| 43 | } |
| 44 | |
| 45 | // UpdateResult temporarily contains the response from an Update call. |
| 46 | type UpdateResult struct { |
| 47 | serverResult |
| 48 | } |
| 49 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 50 | // DeleteResult temporarily contains the response from a Delete call. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 51 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 52 | gophercloud.ErrResult |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 55 | // RebuildResult temporarily contains the response from a Rebuild call. |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 56 | type RebuildResult struct { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 57 | serverResult |
| 58 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 59 | |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 60 | // ActionResult represents the result of server action operations, like reboot |
| 61 | type ActionResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 62 | gophercloud.ErrResult |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Alex Gaynor | 587e3e3 | 2014-11-13 10:39:09 -0800 | [diff] [blame] | 65 | // RescueResult represents the result of a server rescue operation |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 66 | type RescueResult struct { |
| 67 | ActionResult |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 68 | } |
| 69 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 70 | // CreateImageResult represents the result of an image creation operation |
| 71 | type CreateImageResult struct { |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 72 | gophercloud.Result |
| 73 | } |
| 74 | |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 75 | // GetPasswordResult represent the result of a get os-server-password operation. |
| 76 | type GetPasswordResult struct { |
| 77 | gophercloud.Result |
| 78 | } |
| 79 | |
| 80 | // ExtractPassword gets the encrypted password. |
| 81 | // If privateKey != nil the password is decrypted with the private key. |
| 82 | // If privateKey == nil the encrypted password is returned and can be decrypted with: |
| 83 | // echo '<pwd>' | base64 -D | openssl rsautl -decrypt -inkey <private_key> |
| 84 | func (r GetPasswordResult) ExtractPassword(privateKey *rsa.PrivateKey) (string, error) { |
jrperritt | 6a4dcc7 | 2016-07-21 18:59:43 -0500 | [diff] [blame] | 85 | var s struct { |
| 86 | Password string `json:"password"` |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 87 | } |
jrperritt | 6a4dcc7 | 2016-07-21 18:59:43 -0500 | [diff] [blame] | 88 | err := r.ExtractInto(&s) |
| 89 | if err == nil && privateKey != nil && s.Password != "" { |
| 90 | return decryptPassword(s.Password, privateKey) |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 91 | } |
jrperritt | 6a4dcc7 | 2016-07-21 18:59:43 -0500 | [diff] [blame] | 92 | return s.Password, err |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func decryptPassword(encryptedPassword string, privateKey *rsa.PrivateKey) (string, error) { |
| 96 | b64EncryptedPassword := make([]byte, base64.StdEncoding.DecodedLen(len(encryptedPassword))) |
| 97 | |
| 98 | n, err := base64.StdEncoding.Decode(b64EncryptedPassword, []byte(encryptedPassword)) |
| 99 | if err != nil { |
| 100 | return "", fmt.Errorf("Failed to base64 decode encrypted password: %s", err) |
| 101 | } |
| 102 | password, err := rsa.DecryptPKCS1v15(nil, privateKey, b64EncryptedPassword[0:n]) |
| 103 | if err != nil { |
| 104 | return "", fmt.Errorf("Failed to decrypt password: %s", err) |
| 105 | } |
| 106 | |
| 107 | return string(password), nil |
| 108 | } |
| 109 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 110 | // ExtractImageID gets the ID of the newly created server image from the header |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 111 | func (r CreateImageResult) ExtractImageID() (string, error) { |
| 112 | if r.Err != nil { |
| 113 | return "", r.Err |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 114 | } |
| 115 | // Get the image id from the header |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 116 | u, err := url.ParseRequestURI(r.Header.Get("Location")) |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 117 | if err != nil { |
Jon Perritt | 1380826 | 2016-03-09 00:50:12 -0600 | [diff] [blame] | 118 | return "", err |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 119 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 120 | imageID := path.Base(u.Path) |
| 121 | if imageID == "." || imageID == "/" { |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 122 | return "", fmt.Errorf("Failed to parse the ID of newly created image: %s", u) |
| 123 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 124 | return imageID, nil |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 127 | // Extract interprets any RescueResult as an AdminPass, if possible. |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 128 | func (r RescueResult) Extract() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 129 | var s struct { |
| 130 | AdminPass string `json:"adminPass"` |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 131 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 132 | err := r.ExtractInto(&s) |
| 133 | return s.AdminPass, err |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 136 | // Server exposes only the standard OpenStack fields corresponding to a given server on the user's account. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 137 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 138 | // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 139 | ID string `json:"id"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 140 | // TenantID identifies the tenant owning this server resource. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 141 | TenantID string `json:"tenant_id"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 142 | // UserID uniquely identifies the user account owning the tenant. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 143 | UserID string `json:"user_id"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 144 | // Name contains the human-readable name for the server. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 145 | Name string `json:"name"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 146 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 147 | Updated time.Time `json:"updated"` |
| 148 | Created time.Time `json:"created"` |
| 149 | HostID string `json:"hostid"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 150 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 151 | Status string `json:"status"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 152 | // Progress ranges from 0..100. |
| 153 | // A request made against the server completes only once Progress reaches 100. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 154 | Progress int `json:"progress"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 155 | // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 156 | AccessIPv4 string `json:"accessIPv4"` |
| 157 | AccessIPv6 string `json:"accessIPv6"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 158 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 159 | Image map[string]interface{} `json:"-"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 160 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 161 | Flavor map[string]interface{} `json:"flavor"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 162 | // Addresses includes a list of all IP addresses assigned to the server, keyed by pool. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 163 | Addresses map[string]interface{} `json:"addresses"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 164 | // Metadata includes a list of all user-specified key-value pairs attached to the server. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 165 | Metadata map[string]string `json:"metadata"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 166 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 167 | Links []interface{} `json:"links"` |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 168 | // KeyName indicates which public key was injected into the server on launch. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 169 | KeyName string `json:"key_name"` |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 170 | // AdminPass will generally be empty (""). However, it will contain the administrative password chosen when provisioning a new server without a set AdminPass setting in the first place. |
| 171 | // Note that this is the ONLY time this field will be valid. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 172 | AdminPass string `json:"adminPass"` |
Joe Topjian | 978bb50 | 2015-02-12 20:55:31 +0000 | [diff] [blame] | 173 | // SecurityGroups includes the security groups that this instance has applied to it |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 174 | SecurityGroups []map[string]interface{} `json:"security_groups"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 175 | } |
| 176 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 177 | func (r *Server) UnmarshalJSON(b []byte) error { |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 178 | type tmp Server |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 179 | var s struct { |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 180 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 181 | Image interface{} `json:"image"` |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 182 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 183 | err := json.Unmarshal(b, &s) |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 188 | *r = Server(s.tmp) |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 189 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 190 | switch t := s.Image.(type) { |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 191 | case map[string]interface{}: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 192 | r.Image = t |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 193 | case string: |
| 194 | switch t { |
| 195 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 196 | r.Image = nil |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 200 | return err |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 203 | // ServerPage abstracts the raw results of making a List() request against the API. |
| 204 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 205 | // data provided through the ExtractServers call. |
| 206 | type ServerPage struct { |
| 207 | pagination.LinkedPageBase |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 210 | // IsEmpty returns true if a page contains no Server results. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 211 | func (r ServerPage) IsEmpty() (bool, error) { |
| 212 | s, err := ExtractServers(r) |
| 213 | return len(s) == 0, err |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 217 | func (r ServerPage) NextPageURL() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 218 | var s struct { |
| 219 | Links []gophercloud.Link `json:"servers_links"` |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 220 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 221 | err := r.ExtractInto(&s) |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 222 | if err != nil { |
| 223 | return "", err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 224 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 225 | return gophercloud.ExtractNextURL(s.Links) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 228 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 229 | func ExtractServers(r pagination.Page) ([]Server, error) { |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 230 | var s []Server |
| 231 | err := ExtractServersInto(r, &s) |
| 232 | return s, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 233 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 234 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 235 | // MetadataResult contains the result of a call for (potentially) multiple key-value pairs. |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 236 | type MetadataResult struct { |
| 237 | gophercloud.Result |
| 238 | } |
| 239 | |
| 240 | // GetMetadataResult temporarily contains the response from a metadata Get call. |
| 241 | type GetMetadataResult struct { |
| 242 | MetadataResult |
| 243 | } |
| 244 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 245 | // ResetMetadataResult temporarily contains the response from a metadata Reset call. |
| 246 | type ResetMetadataResult struct { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 247 | MetadataResult |
| 248 | } |
| 249 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 250 | // UpdateMetadataResult temporarily contains the response from a metadata Update call. |
| 251 | type UpdateMetadataResult struct { |
| 252 | MetadataResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 255 | // MetadatumResult contains the result of a call for individual a single key-value pair. |
| 256 | type MetadatumResult struct { |
| 257 | gophercloud.Result |
| 258 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 259 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 260 | // GetMetadatumResult temporarily contains the response from a metadatum Get call. |
| 261 | type GetMetadatumResult struct { |
| 262 | MetadatumResult |
| 263 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 264 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 265 | // CreateMetadatumResult temporarily contains the response from a metadatum Create call. |
| 266 | type CreateMetadatumResult struct { |
| 267 | MetadatumResult |
| 268 | } |
| 269 | |
| 270 | // DeleteMetadatumResult temporarily contains the response from a metadatum Delete call. |
| 271 | type DeleteMetadatumResult struct { |
| 272 | gophercloud.ErrResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Extract interprets any MetadataResult as a Metadata, if possible. |
| 276 | func (r MetadataResult) Extract() (map[string]string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 277 | var s struct { |
| 278 | Metadata map[string]string `json:"metadata"` |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 279 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 280 | err := r.ExtractInto(&s) |
| 281 | return s.Metadata, err |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 282 | } |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 283 | |
| 284 | // Extract interprets any MetadatumResult as a Metadatum, if possible. |
| 285 | func (r MetadatumResult) Extract() (map[string]string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 286 | var s struct { |
| 287 | Metadatum map[string]string `json:"meta"` |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 288 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 289 | err := r.ExtractInto(&s) |
| 290 | return s.Metadatum, err |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 291 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 292 | |
| 293 | // Address represents an IP address. |
| 294 | type Address struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 295 | Version int `json:"version"` |
| 296 | Address string `json:"addr"` |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 299 | // AddressPage abstracts the raw results of making a ListAddresses() request against the API. |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 300 | // As OpenStack extensions may freely alter the response bodies of structures returned |
| 301 | // to the client, you may only safely access the data provided through the ExtractAddresses call. |
| 302 | type AddressPage struct { |
| 303 | pagination.SinglePageBase |
| 304 | } |
| 305 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 306 | // IsEmpty returns true if an AddressPage contains no networks. |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 307 | func (r AddressPage) IsEmpty() (bool, error) { |
| 308 | addresses, err := ExtractAddresses(r) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 309 | return len(addresses) == 0, err |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 312 | // ExtractAddresses interprets the results of a single page from a ListAddresses() call, |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 313 | // producing a map of addresses. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 314 | func ExtractAddresses(r pagination.Page) (map[string][]Address, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 315 | var s struct { |
| 316 | Addresses map[string][]Address `json:"addresses"` |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 317 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 318 | err := (r.(AddressPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 319 | return s.Addresses, err |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 320 | } |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 321 | |
| 322 | // NetworkAddressPage abstracts the raw results of making a ListAddressesByNetwork() request against the API. |
| 323 | // As OpenStack extensions may freely alter the response bodies of structures returned |
| 324 | // to the client, you may only safely access the data provided through the ExtractAddresses call. |
| 325 | type NetworkAddressPage struct { |
| 326 | pagination.SinglePageBase |
| 327 | } |
| 328 | |
| 329 | // IsEmpty returns true if a NetworkAddressPage contains no addresses. |
| 330 | func (r NetworkAddressPage) IsEmpty() (bool, error) { |
| 331 | addresses, err := ExtractNetworkAddresses(r) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 332 | return len(addresses) == 0, err |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // ExtractNetworkAddresses interprets the results of a single page from a ListAddressesByNetwork() call, |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 336 | // producing a slice of addresses. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 337 | func ExtractNetworkAddresses(r pagination.Page) ([]Address, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 338 | var s map[string][]Address |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 339 | err := (r.(NetworkAddressPage)).ExtractInto(&s) |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 340 | if err != nil { |
| 341 | return nil, err |
| 342 | } |
| 343 | |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 344 | var key string |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 345 | for k := range s { |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 346 | key = k |
| 347 | } |
| 348 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 349 | return s[key], err |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 350 | } |