blob: e6af80e43e54dcb8c274ac5dda8460847cc332f9 [file] [log] [blame]
Ash Wilsonad13c422014-10-03 08:35:46 -04001package extensions
2
3import (
Ash Wilsonfbedc672014-10-03 16:34:22 -04004 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud"
6 common "github.com/gophercloud/gophercloud/openstack/common/extensions"
7 "github.com/gophercloud/gophercloud/pagination"
Ash Wilsonad13c422014-10-03 08:35:46 -04008)
9
Ash Wilsonfbedc672014-10-03 16:34:22 -040010// ExtensionPage is a single page of Extension results.
11type ExtensionPage struct {
12 common.ExtensionPage
13}
14
15// IsEmpty returns true if the current page contains at least one Extension.
16func (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 Wilson07a25bf2014-10-13 12:00:32 -040026func ExtractExtensions(page pagination.Page) ([]common.Extension, error) {
Ash Wilsonfbedc672014-10-03 16:34:22 -040027 // Identity v2 adds an intermediate "values" object.
28
Ash Wilsonfbedc672014-10-03 16:34:22 -040029 var resp struct {
30 Extensions struct {
Ash Wilson07a25bf2014-10-13 12:00:32 -040031 Values []common.Extension `mapstructure:"values"`
Ash Wilsonfbedc672014-10-03 16:34:22 -040032 } `mapstructure:"extensions"`
33 }
34
35 err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
Ash Wilson07a25bf2014-10-13 12:00:32 -040036 return resp.Extensions.Values, err
Ash Wilsonad13c422014-10-03 08:35:46 -040037}
38
39// Get retrieves information for a specific extension using its alias.
Ash Wilson07a25bf2014-10-13 12:00:32 -040040func Get(c *gophercloud.ServiceClient, alias string) common.GetResult {
41 return common.Get(c, alias)
Ash Wilsonad13c422014-10-03 08:35:46 -040042}
43
44// List returns a Pager which allows you to iterate over the full collection of extensions.
45// It does not accept query parameters.
46func List(c *gophercloud.ServiceClient) pagination.Pager {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040047 return common.List(c).WithPageCreator(func(r pagination.PageResult) pagination.Page {
Ash Wilson59886a72014-10-09 13:57:53 -040048 return ExtensionPage{
49 ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)},
50 }
Ash Wilsonfc4191f2014-10-10 15:05:27 -040051 })
Ash Wilsonad13c422014-10-03 08:35:46 -040052}