blob: c6ce1b95bd39d603bd782f4015dc512124222abe [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 Wilson64f44152014-09-05 13:45:03 -040022 client *gophercloud.ServiceClient
23 Page []Service `json:"services"`
24}
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
31// Service returns the ServiceClient used to acquire this list.
32func (list ServiceList) Service() *gophercloud.ServiceClient {
33 return list.client
34}
35
36// Links accesses pagination information for the current page.
37func (list ServiceList) Links() gophercloud.PaginationLinks {
38 return list.PaginationLinks
39}
40
41// Interpret parses a follow-on JSON response as an additional page.
42func (list ServiceList) Interpret(json interface{}) (gophercloud.LinkCollection, error) {
43 mapped, ok := json.(map[string]interface{})
44 if !ok {
45 return nil, fmt.Errorf("Unexpected JSON response: %#v", json)
46 }
47
48 var result ServiceList
49 err := mapstructure.Decode(mapped, &result)
50 if err != nil {
51 return nil, err
52 }
53 return result, nil
54}
55
56// AsServices extracts a slice of Services from a Collection acquired from List.
57// It panics if the Collection does not actually contain Services.
58func AsServices(results gophercloud.Collection) []Service {
59 return results.(*ServiceList).Page
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040060}