Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 5 | "github.com/gophercloud/gophercloud" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 9 | // GetResult temporarily stores the result of a Get call. |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 10 | // Use its Extract() method to interpret it as an Extension. |
| 11 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 12 | gophercloud.Result |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | // Extract interprets a GetResult as an Extension. |
| 16 | func (r GetResult) Extract() (*Extension, error) { |
| 17 | if r.Err != nil { |
| 18 | return nil, r.Err |
| 19 | } |
| 20 | |
| 21 | var res struct { |
| 22 | Extension *Extension `json:"extension"` |
| 23 | } |
| 24 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 25 | err := mapstructure.Decode(r.Body, &res) |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 26 | |
Jamie Hannaford | 1926e9c | 2014-10-08 17:14:02 +0200 | [diff] [blame] | 27 | return res.Extension, err |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // Extension is a struct that represents an OpenStack extension. |
| 31 | type Extension struct { |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 32 | Updated string `json:"updated" mapstructure:"updated"` |
| 33 | Name string `json:"name" mapstructure:"name"` |
| 34 | Links []interface{} `json:"links" mapstructure:"links"` |
| 35 | Namespace string `json:"namespace" mapstructure:"namespace"` |
| 36 | Alias string `json:"alias" mapstructure:"alias"` |
| 37 | Description string `json:"description" mapstructure:"description"` |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // ExtensionPage is the page returned by a pager when traversing over a collection of extensions. |
| 41 | type ExtensionPage struct { |
| 42 | pagination.SinglePageBase |
| 43 | } |
| 44 | |
| 45 | // IsEmpty checks whether an ExtensionPage struct is empty. |
| 46 | func (r ExtensionPage) IsEmpty() (bool, error) { |
| 47 | is, err := ExtractExtensions(r) |
| 48 | if err != nil { |
| 49 | return true, err |
| 50 | } |
| 51 | return len(is) == 0, nil |
| 52 | } |
| 53 | |
| 54 | // ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the |
| 55 | // elements into a slice of Extension structs. |
| 56 | // In other words, a generic collection is mapped into a relevant slice. |
| 57 | func ExtractExtensions(page pagination.Page) ([]Extension, error) { |
| 58 | var resp struct { |
| 59 | Extensions []Extension `mapstructure:"extensions"` |
| 60 | } |
| 61 | |
| 62 | err := mapstructure.Decode(page.(ExtensionPage).Body, &resp) |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 63 | |
Jamie Hannaford | 1926e9c | 2014-10-08 17:14:02 +0200 | [diff] [blame] | 64 | return resp.Extensions, err |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 65 | } |