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