blob: e4e068b88dee4061a10103bbde19f2e17fab2b4e [file] [log] [blame]
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +02001package services
2
3import (
4 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/mitchellh/mapstructure"
8)
9
10type commonResult struct {
11 gophercloud.CommonResult
12}
13
14// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Service.
15// An error is returned if the original call or the extraction failed.
16func (r commonResult) Extract() (*Service, error) {
17 if r.Err != nil {
18 return nil, r.Err
19 }
20
21 var res struct {
22 Service `json:"service"`
23 }
24
25 err := mapstructure.Decode(r.Resp, &res)
26
27 return &res.Service, err
28}
29
30// CreateResult is the deferred result of a Create call.
31type CreateResult struct {
32 commonResult
33}
34
35// GetResult is the deferred result of a Get call.
36type GetResult struct {
37 commonResult
38}
39
40// UpdateResult is the deferred result of an Update call.
41type UpdateResult struct {
42 commonResult
43}
44
45// Service is the result of a list or information query.
46type Service struct {
47 Description *string `json:"description,omitempty"`
48 ID string `json:"id"`
49 Name string `json:"name"`
50 Type string `json:"type"`
51}
52
53// ServicePage is a single page of Service results.
54type ServicePage struct {
55 pagination.LinkedPageBase
56}
57
58// IsEmpty returns true if the page contains no results.
59func (p ServicePage) IsEmpty() (bool, error) {
60 services, err := ExtractServices(p)
61 if err != nil {
62 return true, err
63 }
64 return len(services) == 0, nil
65}
66
67// ExtractServices extracts a slice of Services from a Collection acquired from List.
68func ExtractServices(page pagination.Page) ([]Service, error) {
69 var response struct {
70 Services []Service `mapstructure:"services"`
71 }
72
73 err := mapstructure.Decode(page.(ServicePage).Body, &response)
74 return response.Services, err
75}