Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 8 | // GetResult temporarily stores the result of a Get call. |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 9 | // Use its Extract() method to interpret it as an Extension. |
| 10 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 11 | gophercloud.Result |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | // Extract interprets a GetResult as an Extension. |
| 15 | func (r GetResult) Extract() (*Extension, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 16 | var s struct { |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 17 | Extension *Extension `json:"extension"` |
| 18 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 19 | err := r.ExtractInto(&s) |
| 20 | return s.Extension, err |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Extension is a struct that represents an OpenStack extension. |
| 24 | type Extension struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 25 | Updated string `json:"updated"` |
| 26 | Name string `json:"name"` |
| 27 | Links []interface{} `json:"links"` |
| 28 | Namespace string `json:"namespace"` |
| 29 | Alias string `json:"alias"` |
| 30 | Description string `json:"description"` |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // ExtensionPage is the page returned by a pager when traversing over a collection of extensions. |
| 34 | type ExtensionPage struct { |
| 35 | pagination.SinglePageBase |
| 36 | } |
| 37 | |
| 38 | // IsEmpty checks whether an ExtensionPage struct is empty. |
| 39 | func (r ExtensionPage) IsEmpty() (bool, error) { |
| 40 | is, err := ExtractExtensions(r) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 41 | return len(is) == 0, err |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the |
| 45 | // elements into a slice of Extension structs. |
| 46 | // In other words, a generic collection is mapped into a relevant slice. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 47 | func ExtractExtensions(r pagination.Page) ([]Extension, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 48 | var s struct { |
| 49 | Extensions []Extension `json:"extensions"` |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 50 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 51 | err := (r.(ExtensionPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 52 | return s.Extensions, err |
Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame] | 53 | } |