Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | common "github.com/gophercloud/gophercloud/openstack/common/extensions" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 7 | ) |
| 8 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 9 | // ExtensionPage is a single page of Extension results. |
| 10 | type ExtensionPage struct { |
| 11 | common.ExtensionPage |
| 12 | } |
| 13 | |
| 14 | // IsEmpty returns true if the current page contains at least one Extension. |
| 15 | func (page ExtensionPage) IsEmpty() (bool, error) { |
| 16 | is, err := ExtractExtensions(page) |
| 17 | if err != nil { |
| 18 | return true, err |
| 19 | } |
| 20 | return len(is) == 0, nil |
| 21 | } |
| 22 | |
| 23 | // ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the |
| 24 | // elements into a slice of Extension structs. |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 25 | func ExtractExtensions(page pagination.Page) ([]common.Extension, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 26 | r := page.(ExtensionPage) |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 27 | // Identity v2 adds an intermediate "values" object. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 28 | var s struct { |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 29 | Extensions struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 30 | Values []common.Extension `json:"values"` |
| 31 | } `json:"extensions"` |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 32 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 33 | err := r.ExtractInto(&s) |
| 34 | return s.Extensions.Values, err |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Get retrieves information for a specific extension using its alias. |
Ash Wilson | 07a25bf | 2014-10-13 12:00:32 -0400 | [diff] [blame] | 38 | func Get(c *gophercloud.ServiceClient, alias string) common.GetResult { |
| 39 | return common.Get(c, alias) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // List returns a Pager which allows you to iterate over the full collection of extensions. |
| 43 | // It does not accept query parameters. |
| 44 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 45 | return common.List(c).WithPageCreator(func(r pagination.PageResult) pagination.Page { |
Ash Wilson | 59886a7 | 2014-10-09 13:57:53 -0400 | [diff] [blame] | 46 | return ExtensionPage{ |
| 47 | ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)}, |
| 48 | } |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 49 | }) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 50 | } |