blob: 88510b48a7b776ff672d00602118e50cfbd2bba2 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
Ash Wilson64f44152014-09-05 13:45:03 -04003import (
4 "fmt"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04005
Ash Wilson64f44152014-09-05 13:45:03 -04006 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
8)
9
10// Service is the result of a list or information query.
11type Service struct {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040012 Description *string `json:"description,omitempty"`
13 ID string `json:"id"`
14 Name string `json:"name"`
15 Type string `json:"type"`
16}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040017
Ash Wilson64f44152014-09-05 13:45:03 -040018// ServiceList is a collection of Services.
19type ServiceList struct {
20 gophercloud.PaginationLinks `json:"links"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040021
Ash Wilsonb110fc92014-09-08 13:54:59 -040022 client *gophercloud.ServiceClient
23 Services []Service `json:"services"`
Ash Wilson64f44152014-09-05 13:45:03 -040024}
25
26// Pager indicates that the ServiceList is paginated by next and previous links.
27func (list ServiceList) Pager() gophercloud.Pager {
28 return gophercloud.NewLinkPager(list)
29}
30
Ash Wilsonb110fc92014-09-08 13:54:59 -040031// Concat returns a new collection that's the result of appending a new collection at the end of this one.
32func (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 Wilson64f44152014-09-05 13:45:03 -040039// Service returns the ServiceClient used to acquire this list.
40func (list ServiceList) Service() *gophercloud.ServiceClient {
41 return list.client
42}
43
44// Links accesses pagination information for the current page.
45func (list ServiceList) Links() gophercloud.PaginationLinks {
46 return list.PaginationLinks
47}
48
49// Interpret parses a follow-on JSON response as an additional page.
50func (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.
66func AsServices(results gophercloud.Collection) []Service {
Ash Wilsonb110fc92014-09-08 13:54:59 -040067 return results.(*ServiceList).Services
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040068}