Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 3 | import ( |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 5 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // Service is the result of a list or information query. |
| 10 | type Service struct { |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 11 | Description *string `json:"description,omitempty"` |
| 12 | ID string `json:"id"` |
| 13 | Name string `json:"name"` |
| 14 | Type string `json:"type"` |
| 15 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 16 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 17 | // ServicePage is a single page of Service results. |
| 18 | type ServicePage struct { |
| 19 | pagination.LinkedPageBase |
| 20 | } |
| 21 | |
| 22 | // IsEmpty returns true if the page contains no results. |
| 23 | func (p ServicePage) IsEmpty() (bool, error) { |
| 24 | services, err := ExtractServices(p) |
| 25 | if err != nil { |
| 26 | return true, err |
| 27 | } |
| 28 | return len(services) == 0, nil |
| 29 | } |
| 30 | |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 31 | // ExtractServices extracts a slice of Services from a Collection acquired from List. |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 32 | func ExtractServices(page pagination.Page) ([]Service, error) { |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 33 | var response struct { |
| 34 | Services []Service `mapstructure:"services"` |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 37 | err := mapstructure.Decode(page.(ServicePage).Body, &response) |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 38 | return response.Services, err |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 39 | } |