Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package apiversions |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | type APIVersion struct { |
| 9 | Status string `mapstructure:"status" json:"status"` |
| 10 | ID string `mapstructure:"id" json:"id"` |
| 11 | } |
| 12 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 13 | type APIVersionPage struct { |
| 14 | pagination.SinglePageBase |
| 15 | } |
| 16 | |
| 17 | func (r APIVersionPage) IsEmpty() (bool, error) { |
| 18 | is, err := ExtractAPIVersions(r) |
| 19 | if err != nil { |
| 20 | return true, err |
| 21 | } |
| 22 | return len(is) == 0, nil |
| 23 | } |
| 24 | |
| 25 | func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 26 | var resp struct { |
| 27 | Versions []APIVersion `mapstructure:"versions"` |
| 28 | } |
| 29 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 30 | err := mapstructure.Decode(page.(APIVersionPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | |
| 35 | return resp.Versions, nil |
| 36 | } |
| 37 | |
| 38 | type APIVersionResource struct { |
| 39 | Name string `mapstructure:"name" json:"name"` |
| 40 | Collection string `mapstructure:"collection" json:"collection"` |
| 41 | } |
| 42 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 43 | type APIVersionResourcePage struct { |
| 44 | pagination.SinglePageBase |
| 45 | } |
| 46 | |
| 47 | func (r APIVersionResourcePage) IsEmpty() (bool, error) { |
| 48 | is, err := ExtractVersionResources(r) |
| 49 | if err != nil { |
| 50 | return true, err |
| 51 | } |
| 52 | return len(is) == 0, nil |
| 53 | } |
| 54 | |
| 55 | func ExtractVersionResources(page pagination.Page) ([]APIVersionResource, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 56 | var resp struct { |
| 57 | APIVersionResources []APIVersionResource `mapstructure:"resources"` |
| 58 | } |
| 59 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 60 | err := mapstructure.Decode(page.(APIVersionResourcePage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | |
| 65 | return resp.APIVersionResources, nil |
| 66 | } |