blob: 45040c61b3fe48880d87c8d8a56132cc1d097359 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
Ash Wilson64f44152014-09-05 13:45:03 -04003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilson64f44152014-09-05 13:45:03 -04006)
7
Ash Wilson74e2bb82014-09-30 17:08:48 -04008type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -04009 gophercloud.Result
Ash Wilson74e2bb82014-09-30 17:08:48 -040010}
11
12// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Service.
13// An error is returned if the original call or the extraction failed.
14func (r commonResult) Extract() (*Service, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060015 var s struct {
16 Service *Service `json:"service"`
Ash Wilson74e2bb82014-09-30 17:08:48 -040017 }
Jon Perritt3c166472016-02-25 03:07:41 -060018 err := r.ExtractInto(&s)
19 return s.Service, err
Ash Wilson74e2bb82014-09-30 17:08:48 -040020}
21
22// CreateResult is the deferred result of a Create call.
23type CreateResult struct {
24 commonResult
25}
26
27// GetResult is the deferred result of a Get call.
28type GetResult struct {
29 commonResult
30}
31
32// UpdateResult is the deferred result of an Update call.
33type UpdateResult struct {
34 commonResult
35}
36
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010037// DeleteResult is the deferred result of an Delete call.
38type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050039 gophercloud.ErrResult
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010040}
41
Ash Wilson64f44152014-09-05 13:45:03 -040042// Service is the result of a list or information query.
43type Service struct {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040044 Description *string `json:"description,omitempty"`
45 ID string `json:"id"`
46 Name string `json:"name"`
47 Type string `json:"type"`
48}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040049
Ash Wilson3c8cc772014-09-16 11:40:49 -040050// ServicePage is a single page of Service results.
51type ServicePage struct {
52 pagination.LinkedPageBase
53}
54
55// IsEmpty returns true if the page contains no results.
56func (p ServicePage) IsEmpty() (bool, error) {
57 services, err := ExtractServices(p)
Jon Perritt3c166472016-02-25 03:07:41 -060058 return len(services) == 0, err
Ash Wilson3c8cc772014-09-16 11:40:49 -040059}
60
Ash Wilsonbddac132014-09-12 14:20:16 -040061// ExtractServices extracts a slice of Services from a Collection acquired from List.
Ash Wilson3c8cc772014-09-16 11:40:49 -040062func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060063 r := page.(ServicePage)
64 var s struct {
65 Services []Service `json:"services"`
Ash Wilson64f44152014-09-05 13:45:03 -040066 }
Jon Perritt3c166472016-02-25 03:07:41 -060067 err := r.ExtractInto(&s)
68 return s.Services, err
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040069}