blob: 1d0d141280670f0799ab3612d7f696515b716b59 [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 {
Ash Wilsonf548aad2014-10-20 08:35:34 -040011 gophercloud.Result
Ash Wilson74e2bb82014-09-30 17:08:48 -040012}
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
Ash Wilsond3dc2542014-10-20 10:10:48 -040025 err := mapstructure.Decode(r.Body, &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
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010045// DeleteResult is the deferred result of an Delete call.
46type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050047 gophercloud.ErrResult
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010048}
49
Ash Wilson64f44152014-09-05 13:45:03 -040050// Service is the result of a list or information query.
51type Service struct {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040052 Description *string `json:"description,omitempty"`
53 ID string `json:"id"`
54 Name string `json:"name"`
55 Type string `json:"type"`
56}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040057
Ash Wilson3c8cc772014-09-16 11:40:49 -040058// ServicePage is a single page of Service results.
59type ServicePage struct {
60 pagination.LinkedPageBase
61}
62
63// IsEmpty returns true if the page contains no results.
64func (p ServicePage) IsEmpty() (bool, error) {
65 services, err := ExtractServices(p)
66 if err != nil {
67 return true, err
68 }
69 return len(services) == 0, nil
70}
71
Ash Wilsonbddac132014-09-12 14:20:16 -040072// ExtractServices extracts a slice of Services from a Collection acquired from List.
Ash Wilson3c8cc772014-09-16 11:40:49 -040073func ExtractServices(page pagination.Page) ([]Service, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -040074 var response struct {
75 Services []Service `mapstructure:"services"`
Ash Wilson64f44152014-09-05 13:45:03 -040076 }
77
Ash Wilson3c8cc772014-09-16 11:40:49 -040078 err := mapstructure.Decode(page.(ServicePage).Body, &response)
Ash Wilsonbddac132014-09-12 14:20:16 -040079 return response.Services, err
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040080}