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