Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 8 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 9 | gophercloud.Result |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 10 | } |
| 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. |
| 14 | func (r commonResult) Extract() (*Service, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 15 | var s struct { |
| 16 | Service *Service `json:"service"` |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 17 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 18 | err := r.ExtractInto(&s) |
| 19 | return s.Service, err |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | // CreateResult is the deferred result of a Create call. |
| 23 | type CreateResult struct { |
| 24 | commonResult |
| 25 | } |
| 26 | |
| 27 | // GetResult is the deferred result of a Get call. |
| 28 | type GetResult struct { |
| 29 | commonResult |
| 30 | } |
| 31 | |
| 32 | // UpdateResult is the deferred result of an Update call. |
| 33 | type UpdateResult struct { |
| 34 | commonResult |
| 35 | } |
| 36 | |
Jamie Hannaford | 8ab3c14 | 2014-10-27 11:33:39 +0100 | [diff] [blame] | 37 | // DeleteResult is the deferred result of an Delete call. |
| 38 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 39 | gophercloud.ErrResult |
Jamie Hannaford | 8ab3c14 | 2014-10-27 11:33:39 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 42 | // Service is the result of a list or information query. |
| 43 | type Service struct { |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 44 | Description *string `json:"description,omitempty"` |
| 45 | ID string `json:"id"` |
| 46 | Name string `json:"name"` |
| 47 | Type string `json:"type"` |
| 48 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 49 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 50 | // ServicePage is a single page of Service results. |
| 51 | type ServicePage struct { |
| 52 | pagination.LinkedPageBase |
| 53 | } |
| 54 | |
| 55 | // IsEmpty returns true if the page contains no results. |
| 56 | func (p ServicePage) IsEmpty() (bool, error) { |
| 57 | services, err := ExtractServices(p) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 58 | return len(services) == 0, err |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 61 | // ExtractServices extracts a slice of Services from a Collection acquired from List. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame^] | 62 | func ExtractServices(r pagination.Page) ([]Service, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 63 | var s struct { |
| 64 | Services []Service `json:"services"` |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 65 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame^] | 66 | err := (r.(ServicePage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 67 | return s.Services, err |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 68 | } |