blob: 4b2c6a7d424af99f5ed9cd65f5d49d10a7213fdd [file] [log] [blame]
Ash Wilsonad13c422014-10-03 08:35:46 -04001package extensions
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 common "github.com/gophercloud/gophercloud/openstack/common/extensions"
6 "github.com/gophercloud/gophercloud/pagination"
Ash Wilsonad13c422014-10-03 08:35:46 -04007)
8
Ash Wilsonfbedc672014-10-03 16:34:22 -04009// ExtensionPage is a single page of Extension results.
10type ExtensionPage struct {
11 common.ExtensionPage
12}
13
14// IsEmpty returns true if the current page contains at least one Extension.
15func (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 Wilson07a25bf2014-10-13 12:00:32 -040025func ExtractExtensions(page pagination.Page) ([]common.Extension, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060026 r := page.(ExtensionPage)
Ash Wilsonfbedc672014-10-03 16:34:22 -040027 // Identity v2 adds an intermediate "values" object.
Jon Perritt3c166472016-02-25 03:07:41 -060028 var s struct {
Ash Wilsonfbedc672014-10-03 16:34:22 -040029 Extensions struct {
Jon Perritt3c166472016-02-25 03:07:41 -060030 Values []common.Extension `json:"values"`
31 } `json:"extensions"`
Ash Wilsonfbedc672014-10-03 16:34:22 -040032 }
Jon Perritt3c166472016-02-25 03:07:41 -060033 err := r.ExtractInto(&s)
34 return s.Extensions.Values, err
Ash Wilsonad13c422014-10-03 08:35:46 -040035}
36
37// Get retrieves information for a specific extension using its alias.
Ash Wilson07a25bf2014-10-13 12:00:32 -040038func Get(c *gophercloud.ServiceClient, alias string) common.GetResult {
39 return common.Get(c, alias)
Ash Wilsonad13c422014-10-03 08:35:46 -040040}
41
42// List returns a Pager which allows you to iterate over the full collection of extensions.
43// It does not accept query parameters.
44func List(c *gophercloud.ServiceClient) pagination.Pager {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040045 return common.List(c).WithPageCreator(func(r pagination.PageResult) pagination.Page {
Ash Wilson59886a72014-10-09 13:57:53 -040046 return ExtensionPage{
47 ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)},
48 }
Ash Wilsonfc4191f2014-10-10 15:05:27 -040049 })
Ash Wilsonad13c422014-10-03 08:35:46 -040050}