blob: b4e7bd28564debdf785b619e7616a86ab894b2de [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 "fmt"
5
6 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04007 "github.com/rackspace/gophercloud/pagination"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04008
Ash Wilson64f44152014-09-05 13:45:03 -04009 "github.com/mitchellh/mapstructure"
Ash Wilson64f44152014-09-05 13:45:03 -040010)
11
Ash Wilson74e2bb82014-09-30 17:08:48 -040012type commonResult struct {
13 gophercloud.CommonResult
14}
15
16// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Service.
17// An error is returned if the original call or the extraction failed.
18func (r commonResult) Extract() (*Service, error) {
19 if r.Err != nil {
20 return nil, r.Err
21 }
22
23 var res struct {
24 Service `json:"service"`
25 }
26
27 err := mapstructure.Decode(r.Resp, &res)
28 if err != nil {
29 return nil, fmt.Errorf("Error decoding Service: %v", err)
30 }
31
32 return &res.Service, nil
33}
34
35// CreateResult is the deferred result of a Create call.
36type CreateResult struct {
37 commonResult
38}
39
40// GetResult is the deferred result of a Get call.
41type GetResult struct {
42 commonResult
43}
44
45// UpdateResult is the deferred result of an Update call.
46type UpdateResult struct {
47 commonResult
48}
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}