| 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 ( | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 4 | 	"github.com/rackspace/gophercloud" | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 5 | 	"github.com/rackspace/gophercloud/pagination" | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 6 |  | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 7 | 	"github.com/mitchellh/mapstructure" | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 8 | ) | 
 | 9 |  | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 10 | type 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. | 
 | 16 | func (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 Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 26 |  | 
| Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame^] | 27 | 	return &res.Service, err | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 28 | } | 
 | 29 |  | 
 | 30 | // CreateResult is the deferred result of a Create call. | 
 | 31 | type CreateResult struct { | 
 | 32 | 	commonResult | 
 | 33 | } | 
 | 34 |  | 
 | 35 | // GetResult is the deferred result of a Get call. | 
 | 36 | type GetResult struct { | 
 | 37 | 	commonResult | 
 | 38 | } | 
 | 39 |  | 
 | 40 | // UpdateResult is the deferred result of an Update call. | 
 | 41 | type UpdateResult struct { | 
 | 42 | 	commonResult | 
 | 43 | } | 
 | 44 |  | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 45 | // Service is the result of a list or information query. | 
 | 46 | type Service struct { | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 47 | 	Description *string `json:"description,omitempty"` | 
 | 48 | 	ID          string  `json:"id"` | 
 | 49 | 	Name        string  `json:"name"` | 
 | 50 | 	Type        string  `json:"type"` | 
 | 51 | } | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 52 |  | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 53 | // ServicePage is a single page of Service results. | 
 | 54 | type ServicePage struct { | 
 | 55 | 	pagination.LinkedPageBase | 
 | 56 | } | 
 | 57 |  | 
 | 58 | // IsEmpty returns true if the page contains no results. | 
 | 59 | func (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 Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 67 | // ExtractServices extracts a slice of Services from a Collection acquired from List. | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 68 | func ExtractServices(page pagination.Page) ([]Service, error) { | 
| Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 69 | 	var response struct { | 
 | 70 | 		Services []Service `mapstructure:"services"` | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 71 | 	} | 
 | 72 |  | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 73 | 	err := mapstructure.Decode(page.(ServicePage).Body, &response) | 
| Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 74 | 	return response.Services, err | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 75 | } |