blob: b3f921a7ec1aafeb962fd23c9eb6957b933d13c0 [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"
Ash Wilsonad13c422014-10-03 08:35:46 -04005 "github.com/rackspace/gophercloud"
6 common "github.com/rackspace/gophercloud/openstack/common/extensions"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10// Extension is a single OpenStack extension.
11type Extension struct {
12 common.Extension
13}
14
15// GetResult wraps a GetResult from common.
16type GetResult struct {
17 common.GetResult
18}
19
Ash Wilsonfbedc672014-10-03 16:34:22 -040020// ExtensionPage is a single page of Extension results.
21type ExtensionPage struct {
22 common.ExtensionPage
23}
24
25// IsEmpty returns true if the current page contains at least one Extension.
26func (page ExtensionPage) IsEmpty() (bool, error) {
27 is, err := ExtractExtensions(page)
28 if err != nil {
29 return true, err
30 }
31 return len(is) == 0, nil
32}
33
34// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
35// elements into a slice of Extension structs.
Ash Wilsonad13c422014-10-03 08:35:46 -040036func ExtractExtensions(page pagination.Page) ([]Extension, error) {
Ash Wilsonfbedc672014-10-03 16:34:22 -040037 // Identity v2 adds an intermediate "values" object.
38
39 type extension struct {
40 Updated string `mapstructure:"updated"`
41 Name string `mapstructure:"name"`
42 Namespace string `mapstructure:"namespace"`
43 Alias string `mapstructure:"alias"`
44 Description string `mapstructure:"description"`
45 Links []interface{} `mapstructure:"links"`
46 }
47
48 var resp struct {
49 Extensions struct {
50 Values []extension `mapstructure:"values"`
51 } `mapstructure:"extensions"`
52 }
53
54 err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
Ash Wilsonad13c422014-10-03 08:35:46 -040055 if err != nil {
56 return nil, err
57 }
Ash Wilsonfbedc672014-10-03 16:34:22 -040058
59 exts := make([]Extension, len(resp.Extensions.Values))
60 for i, original := range resp.Extensions.Values {
61 exts[i] = Extension{common.Extension{
62 Updated: original.Updated,
63 Name: original.Name,
64 Namespace: original.Namespace,
65 Alias: original.Alias,
66 Description: original.Description,
67 Links: original.Links,
68 }}
Ash Wilsonad13c422014-10-03 08:35:46 -040069 }
Ash Wilsonfbedc672014-10-03 16:34:22 -040070
71 return exts, err
Ash Wilsonad13c422014-10-03 08:35:46 -040072}
73
74// Get retrieves information for a specific extension using its alias.
75func Get(c *gophercloud.ServiceClient, alias string) GetResult {
76 return GetResult{common.Get(c, alias)}
77}
78
79// List returns a Pager which allows you to iterate over the full collection of extensions.
80// It does not accept query parameters.
81func List(c *gophercloud.ServiceClient) pagination.Pager {
Ash Wilson59886a72014-10-09 13:57:53 -040082 pager := common.List(c)
83 pager.CreatePage = func(r pagination.LastHTTPResponse) pagination.Page {
84 return ExtensionPage{
85 ExtensionPage: common.ExtensionPage{SinglePageBase: pagination.SinglePageBase(r)},
86 }
87 }
88 return pager
Ash Wilsonad13c422014-10-03 08:35:46 -040089}