Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame^] | 1 | package extensions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | common "github.com/rackspace/gophercloud/openstack/common/extensions" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // ExtensionPage is a single page of Extension results. |
| 11 | type ExtensionPage struct { |
| 12 | common.ExtensionPage |
| 13 | } |
| 14 | |
| 15 | // IsEmpty returns true if the current page contains at least one Extension. |
| 16 | func (page ExtensionPage) IsEmpty() (bool, error) { |
| 17 | is, err := ExtractExtensions(page) |
| 18 | if err != nil { |
| 19 | return true, err |
| 20 | } |
| 21 | return len(is) == 0, nil |
| 22 | } |
| 23 | |
| 24 | // ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the |
| 25 | // elements into a slice of Extension structs. |
| 26 | func ExtractExtensions(page pagination.Page) ([]common.Extension, error) { |
| 27 | // Identity v2 adds an intermediate "values" object. |
| 28 | |
| 29 | var resp struct { |
| 30 | Extensions struct { |
| 31 | Values []common.Extension `mapstructure:"values"` |
| 32 | } `mapstructure:"extensions"` |
| 33 | } |
| 34 | |
| 35 | err := mapstructure.Decode(page.(ExtensionPage).Body, &resp) |
| 36 | return resp.Extensions.Values, err |
| 37 | } |
| 38 | |
| 39 | // Get retrieves information for a specific extension using its alias. |
| 40 | func Get(c *gophercloud.ServiceClient, alias string) common.GetResult { |
| 41 | return common.Get(c, alias) |
| 42 | } |
| 43 | |
| 44 | // List returns a Pager which allows you to iterate over the full collection of extensions. |
| 45 | // It does not accept query parameters. |
| 46 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
| 47 | return common.List(c).WithPageCreator(func(r pagination.LastHTTPResponse) pagination.Page { |
| 48 | return ExtensionPage{ |
| 49 | ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)}, |
| 50 | } |
| 51 | }) |
| 52 | } |