Jon Perritt | 23e89c3 | 2015-01-19 15:39:45 -0700 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // Provider represents a provider for a particular flavor. |
| 10 | type Provider struct { |
| 11 | // Specifies the name of the provider. The name must not exceed 64 bytes in |
| 12 | // length and is limited to unicode, digits, underscores, and hyphens. |
| 13 | Provider string `mapstructure:"provider"` |
| 14 | // Specifies a list with an href where rel is provider_url. |
| 15 | Links []gophercloud.Link `mapstructure:"links"` |
| 16 | } |
| 17 | |
| 18 | // Flavor represents a mapping configuration to a CDN provider. |
| 19 | type Flavor struct { |
| 20 | // Specifies the name of the flavor. The name must not exceed 64 bytes in |
| 21 | // length and is limited to unicode, digits, underscores, and hyphens. |
| 22 | ID string `mapstructure:"id"` |
| 23 | // Specifies the list of providers mapped to this flavor. |
| 24 | Providers []Provider `mapstructure:"providers"` |
| 25 | // Specifies the self-navigating JSON document paths. |
| 26 | Links []gophercloud.Link `mapstructure:"links"` |
| 27 | } |
| 28 | |
| 29 | // FlavorPage is the page returned by a pager when traversing over a |
| 30 | // collection of CDN flavors. |
| 31 | type FlavorPage struct { |
| 32 | pagination.SinglePageBase |
| 33 | } |
| 34 | |
| 35 | // IsEmpty returns true if a FlavorPage contains no Flavors. |
| 36 | func (r FlavorPage) IsEmpty() (bool, error) { |
| 37 | flavors, err := ExtractFlavors(r) |
| 38 | if err != nil { |
| 39 | return true, err |
| 40 | } |
| 41 | return len(flavors) == 0, nil |
| 42 | } |
| 43 | |
| 44 | // ExtractFlavors extracts and returns Flavors. It is used while iterating over |
| 45 | // a flavors.List call. |
| 46 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
| 47 | var response struct { |
| 48 | Flavors []Flavor `json:"flavors"` |
| 49 | } |
| 50 | |
| 51 | err := mapstructure.Decode(page.(FlavorPage).Body, &response) |
| 52 | return response.Flavors, err |
| 53 | } |
| 54 | |
| 55 | // GetResult represents the result of a get operation. |
| 56 | type GetResult struct { |
| 57 | gophercloud.Result |
| 58 | } |
| 59 | |
| 60 | // Extract is a function that extracts a flavor from a GetResult. |
| 61 | func (r GetResult) Extract() (*Flavor, error) { |
| 62 | if r.Err != nil { |
| 63 | return nil, r.Err |
| 64 | } |
| 65 | |
| 66 | var res Flavor |
| 67 | |
| 68 | err := mapstructure.Decode(r.Body, &res) |
| 69 | |
| 70 | return &res, err |
| 71 | } |