Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 6 | ) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 7 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame^] | 8 | // Extension is a struct that represents a Neutron extension. |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 9 | type Extension struct { |
| 10 | Updated string `json:"updated"` |
| 11 | Name string `json:"name"` |
| 12 | Links []interface{} `json:"links"` |
| 13 | Namespace string `json:"namespace"` |
| 14 | Alias string `json:"alias"` |
| 15 | Description string `json:"description"` |
| 16 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 17 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame^] | 18 | // ExtensionPage is the page returned by a pager when traversing over a |
| 19 | // collection of extensions. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 20 | type ExtensionPage struct { |
| 21 | pagination.SinglePageBase |
| 22 | } |
| 23 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame^] | 24 | // IsEmpty checks whether an ExtensionPage struct is empty. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 25 | func (r ExtensionPage) IsEmpty() (bool, error) { |
| 26 | is, err := ExtractExtensions(r) |
| 27 | if err != nil { |
| 28 | return true, err |
| 29 | } |
| 30 | return len(is) == 0, nil |
| 31 | } |
| 32 | |
Jamie Hannaford | c65e192 | 2014-09-22 13:20:58 +0200 | [diff] [blame^] | 33 | // ExtractExtensions accepts a Page struct, specifically an ExtensionPage |
| 34 | // struct, and extracts the elements into a slice of Extension structs. In other |
| 35 | // words, a generic collection is mapped into a relevant slice. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 36 | func ExtractExtensions(page pagination.Page) ([]Extension, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 37 | var resp struct { |
| 38 | Extensions []Extension `mapstructure:"extensions"` |
| 39 | } |
| 40 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 41 | err := mapstructure.Decode(page.(ExtensionPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | return resp.Extensions, nil |
| 47 | } |