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 ( |
| 4 | "fmt" |
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" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | // Service is the result of a list or information query. |
| 11 | type Service struct { |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 12 | Description *string `json:"description,omitempty"` |
| 13 | ID string `json:"id"` |
| 14 | Name string `json:"name"` |
| 15 | Type string `json:"type"` |
| 16 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 17 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 18 | // ServiceList is a collection of Services. |
| 19 | type ServiceList struct { |
| 20 | gophercloud.PaginationLinks `json:"links"` |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 21 | |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame^] | 22 | client *gophercloud.ServiceClient |
| 23 | Services []Service `json:"services"` |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Pager indicates that the ServiceList is paginated by next and previous links. |
| 27 | func (list ServiceList) Pager() gophercloud.Pager { |
| 28 | return gophercloud.NewLinkPager(list) |
| 29 | } |
| 30 | |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame^] | 31 | // Concat returns a new collection that's the result of appending a new collection at the end of this one. |
| 32 | func (list ServiceList) Concat(other gophercloud.Collection) gophercloud.Collection { |
| 33 | return ServiceList{ |
| 34 | client: list.client, |
| 35 | Services: append(list.Services, AsServices(other)...), |
| 36 | } |
| 37 | } |
| 38 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 39 | // Service returns the ServiceClient used to acquire this list. |
| 40 | func (list ServiceList) Service() *gophercloud.ServiceClient { |
| 41 | return list.client |
| 42 | } |
| 43 | |
| 44 | // Links accesses pagination information for the current page. |
| 45 | func (list ServiceList) Links() gophercloud.PaginationLinks { |
| 46 | return list.PaginationLinks |
| 47 | } |
| 48 | |
| 49 | // Interpret parses a follow-on JSON response as an additional page. |
| 50 | func (list ServiceList) Interpret(json interface{}) (gophercloud.LinkCollection, error) { |
| 51 | mapped, ok := json.(map[string]interface{}) |
| 52 | if !ok { |
| 53 | return nil, fmt.Errorf("Unexpected JSON response: %#v", json) |
| 54 | } |
| 55 | |
| 56 | var result ServiceList |
| 57 | err := mapstructure.Decode(mapped, &result) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | return result, nil |
| 62 | } |
| 63 | |
| 64 | // AsServices extracts a slice of Services from a Collection acquired from List. |
| 65 | // It panics if the Collection does not actually contain Services. |
| 66 | func AsServices(results gophercloud.Collection) []Service { |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame^] | 67 | return results.(*ServiceList).Services |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 68 | } |