blob: e4e068b88dee4061a10103bbde19f2e17fab2b4e [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
Ash Wilson64f44152014-09-05 13:45:03 -04003import (
Ash Wilson74e2bb82014-09-30 17:08:48 -04004 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04005 "github.com/rackspace/gophercloud/pagination"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04006
Ash Wilson64f44152014-09-05 13:45:03 -04007 "github.com/mitchellh/mapstructure"
Ash Wilson64f44152014-09-05 13:45:03 -04008)
9
Ash Wilson74e2bb82014-09-30 17:08:48 -040010type 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)
Ash Wilson74e2bb82014-09-30 17:08:48 -040026
Jamie Hannaforda253adf2014-10-08 17:14:24 +020027 return &res.Service, err
Ash Wilson74e2bb82014-09-30 17:08:48 -040028}
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
Ash Wilson64f44152014-09-05 13:45:03 -040045// Service is the result of a list or information query.
46type Service struct {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040047 Description *string `json:"description,omitempty"`
48 ID string `json:"id"`
49 Name string `json:"name"`
50 Type string `json:"type"`
51}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040052
Ash Wilson3c8cc772014-09-16 11:40:49 -040053// 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
Ash Wilsonbddac132014-09-12 14:20:16 -040067// ExtractServices extracts a slice of Services from a Collection acquired from List.
Ash Wilson3c8cc772014-09-16 11:40:49 -040068func ExtractServices(page pagination.Page) ([]Service, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -040069 var response struct {
70 Services []Service `mapstructure:"services"`
Ash Wilson64f44152014-09-05 13:45:03 -040071 }
72
Ash Wilson3c8cc772014-09-16 11:40:49 -040073 err := mapstructure.Decode(page.(ServicePage).Body, &response)
Ash Wilsonbddac132014-09-12 14:20:16 -040074 return response.Services, err
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040075}