blob: cf6cc816da8bf8cb87026a79acc856c02f2de6a1 [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)
Jon Perrittdb0ae142016-03-13 00:33:41 -060017 return len(is) == 0, err
Ash Wilsonfbedc672014-10-03 16:34:22 -040018}
19
20// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
21// elements into a slice of Extension structs.
Ash Wilson07a25bf2014-10-13 12:00:32 -040022func ExtractExtensions(page pagination.Page) ([]common.Extension, error) {
Ash Wilsonfbedc672014-10-03 16:34:22 -040023 // Identity v2 adds an intermediate "values" object.
Jon Perritt3c166472016-02-25 03:07:41 -060024 var s struct {
Ash Wilsonfbedc672014-10-03 16:34:22 -040025 Extensions struct {
Jon Perritt3c166472016-02-25 03:07:41 -060026 Values []common.Extension `json:"values"`
27 } `json:"extensions"`
Ash Wilsonfbedc672014-10-03 16:34:22 -040028 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 err := page.(ExtensionPage).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -060030 return s.Extensions.Values, err
Ash Wilsonad13c422014-10-03 08:35:46 -040031}
32
33// Get retrieves information for a specific extension using its alias.
Ash Wilson07a25bf2014-10-13 12:00:32 -040034func Get(c *gophercloud.ServiceClient, alias string) common.GetResult {
35 return common.Get(c, alias)
Ash Wilsonad13c422014-10-03 08:35:46 -040036}
37
38// List returns a Pager which allows you to iterate over the full collection of extensions.
39// It does not accept query parameters.
40func List(c *gophercloud.ServiceClient) pagination.Pager {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040041 return common.List(c).WithPageCreator(func(r pagination.PageResult) pagination.Page {
Ash Wilson59886a72014-10-09 13:57:53 -040042 return ExtensionPage{
43 ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)},
44 }
Ash Wilsonfc4191f2014-10-10 15:05:27 -040045 })
Ash Wilsonad13c422014-10-03 08:35:46 -040046}