Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -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 | common "github.com/gophercloud/gophercloud/openstack/common/extensions" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 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. |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 26 | func ExtractExtensions(page pagination.Page) ([]common.Extension, error) { |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 27 | // Identity v2 adds an intermediate "values" object. |
| 28 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 29 | var resp struct { |
| 30 | Extensions struct { |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 31 | Values []common.Extension `mapstructure:"values"` |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 32 | } `mapstructure:"extensions"` |
| 33 | } |
| 34 | |
| 35 | err := mapstructure.Decode(page.(ExtensionPage).Body, &resp) |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 36 | return resp.Extensions.Values, err |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Get retrieves information for a specific extension using its alias. |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 40 | func Get(c *gophercloud.ServiceClient, alias string) common.GetResult { |
| 41 | return common.Get(c, alias) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 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 { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 47 | return common.List(c).WithPageCreator(func(r pagination.PageResult) pagination.Page { |
Ash Wilson | 59886a7 | 2014-10-09 13:57:53 -0400 | [diff] [blame] | 48 | return ExtensionPage{ |
| 49 | ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)}, |
| 50 | } |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 51 | }) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 52 | } |