Ash Wilson | f4d6363 | 2014-10-09 14:54:06 -0400 | [diff] [blame^] | 1 | package extensions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | common "github.com/rackspace/gophercloud/openstack/common/extensions" |
| 6 | os "github.com/rackspace/gophercloud/openstack/identity/v2/extensions" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // ExtensionPage is the page returned by a pager when traversing over a collection of extensions. |
| 11 | type ExtensionPage struct { |
| 12 | common.ExtensionPage |
| 13 | } |
| 14 | |
| 15 | // IsEmpty checks whether an ExtensionPage struct is empty. |
| 16 | func (r ExtensionPage) IsEmpty() (bool, error) { |
| 17 | is, err := ExtractExtensions(r) |
| 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 os.Extension structs. |
| 26 | func ExtractExtensions(page pagination.Page) ([]os.Extension, error) { |
| 27 | exts, err := common.ExtractExtensions(page.(ExtensionPage).ExtensionPage) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | results := make([]os.Extension, len(exts)) |
| 32 | for i, ext := range exts { |
| 33 | results[i] = os.Extension{Extension: ext} |
| 34 | } |
| 35 | return results, nil |
| 36 | } |
| 37 | |
| 38 | // Get retrieves information for a specific extension using its alias. |
| 39 | func Get(c *gophercloud.ServiceClient, alias string) os.GetResult { |
| 40 | return os.Get(c, alias) |
| 41 | } |
| 42 | |
| 43 | // List returns a Pager which allows you to iterate over the full collection of extensions. |
| 44 | // It does not accept query parameters. |
| 45 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
| 46 | pager := os.List(c) |
| 47 | pager.CreatePage = func(r pagination.LastHTTPResponse) pagination.Page { |
| 48 | return ExtensionPage{ |
| 49 | ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)}, |
| 50 | } |
| 51 | } |
| 52 | return pager |
| 53 | } |