Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 5 | "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. |
| 11 | type Extension struct { |
| 12 | common.Extension |
| 13 | } |
| 14 | |
| 15 | // GetResult wraps a GetResult from common. |
| 16 | type GetResult struct { |
| 17 | common.GetResult |
| 18 | } |
| 19 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 20 | // ExtensionPage is a single page of Extension results. |
| 21 | type ExtensionPage struct { |
| 22 | common.ExtensionPage |
| 23 | } |
| 24 | |
| 25 | // IsEmpty returns true if the current page contains at least one Extension. |
| 26 | func (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 Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 36 | func ExtractExtensions(page pagination.Page) ([]Extension, error) { |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 37 | // 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 Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 58 | |
| 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 Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 69 | } |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame] | 70 | |
| 71 | return exts, err |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Get retrieves information for a specific extension using its alias. |
| 75 | func 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. |
| 81 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
Ash Wilson | 59886a7 | 2014-10-09 13:57:53 -0400 | [diff] [blame^] | 82 | 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 Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 89 | } |