| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints | 
|  | 2 |  | 
| Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 3 | import ( | 
| Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" | 
|  | 5 | "github.com/rackspace/gophercloud" | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" | 
| Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 7 | ) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 8 |  | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 9 | type commonResult struct { | 
| Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | gophercloud.Result | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 11 | } | 
|  | 12 |  | 
|  | 13 | // Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Endpoint. | 
|  | 14 | // An error is returned if the original call or the extraction failed. | 
|  | 15 | func (r commonResult) Extract() (*Endpoint, error) { | 
|  | 16 | if r.Err != nil { | 
|  | 17 | return nil, r.Err | 
|  | 18 | } | 
|  | 19 |  | 
|  | 20 | var res struct { | 
|  | 21 | Endpoint `json:"endpoint"` | 
|  | 22 | } | 
|  | 23 |  | 
| Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 24 | err := mapstructure.Decode(r.Body, &res) | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 25 |  | 
| Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame] | 26 | return &res.Endpoint, err | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 27 | } | 
|  | 28 |  | 
|  | 29 | // CreateResult is the deferred result of a Create call. | 
|  | 30 | type CreateResult struct { | 
|  | 31 | commonResult | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | // createErr quickly wraps an error in a CreateResult. | 
|  | 35 | func createErr(err error) CreateResult { | 
| Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 36 | return CreateResult{commonResult{gophercloud.Result{Err: err}}} | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
|  | 39 | // UpdateResult is the deferred result of an Update call. | 
|  | 40 | type UpdateResult struct { | 
|  | 41 | commonResult | 
|  | 42 | } | 
|  | 43 |  | 
| Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 44 | // DeleteResult is the deferred result of an Delete call. | 
|  | 45 | type DeleteResult struct { | 
| Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 46 | gophercloud.ErrResult | 
| Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 49 | // Endpoint describes the entry point for another service's API. | 
|  | 50 | type Endpoint struct { | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 51 | ID           string                   `mapstructure:"id" json:"id"` | 
|  | 52 | Availability gophercloud.Availability `mapstructure:"interface" json:"interface"` | 
|  | 53 | Name         string                   `mapstructure:"name" json:"name"` | 
|  | 54 | Region       string                   `mapstructure:"region" json:"region"` | 
|  | 55 | ServiceID    string                   `mapstructure:"service_id" json:"service_id"` | 
|  | 56 | URL          string                   `mapstructure:"url" json:"url"` | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 59 | // EndpointPage is a single page of Endpoint results. | 
|  | 60 | type EndpointPage struct { | 
|  | 61 | pagination.LinkedPageBase | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | // IsEmpty returns true if no Endpoints were returned. | 
|  | 65 | func (p EndpointPage) IsEmpty() (bool, error) { | 
|  | 66 | es, err := ExtractEndpoints(p) | 
|  | 67 | if err != nil { | 
|  | 68 | return true, err | 
|  | 69 | } | 
|  | 70 | return len(es) == 0, nil | 
|  | 71 | } | 
|  | 72 |  | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 73 | // ExtractEndpoints extracts an Endpoint slice from a Page. | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 74 | func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) { | 
| Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 75 | var response struct { | 
|  | 76 | Endpoints []Endpoint `mapstructure:"endpoints"` | 
| Ash Wilson | 700d13a | 2014-09-05 14:24:16 -0400 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 79 | err := mapstructure.Decode(page.(EndpointPage).Body, &response) | 
| Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame] | 80 |  | 
|  | 81 | return response.Endpoints, err | 
| Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 82 | } |