Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 5 | "github.com/racker/perigee" |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 10 | // ListPage contains a single page of results from a List operation. |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 11 | // Use ExtractImages to convert it into a slice of usable structs. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 12 | type ListPage struct { |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 13 | pagination.LinkedPageBase |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 14 | } |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 15 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 16 | // IsEmpty returns true if a page contains no Image results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 17 | func (page ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 18 | images, err := ExtractImages(page) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 19 | if err != nil { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 20 | return true, err |
| 21 | } |
| 22 | return len(images) == 0, nil |
| 23 | } |
| 24 | |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 25 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 26 | func (page ListPage) NextPageURL() (string, error) { |
| 27 | type link struct { |
| 28 | Href string `mapstructure:"href"` |
| 29 | Rel string `mapstructure:"rel"` |
| 30 | } |
| 31 | type resp struct { |
| 32 | Links []link `mapstructure:"images_links"` |
| 33 | } |
| 34 | |
| 35 | var r resp |
| 36 | err := mapstructure.Decode(page.Body, &r) |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 40 | |
| 41 | var url string |
| 42 | for _, l := range r.Links { |
| 43 | if l.Rel == "next" { |
| 44 | url = l.Href |
| 45 | } |
| 46 | } |
| 47 | if url == "" { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 48 | return "", nil |
| 49 | } |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 50 | |
| 51 | return url, nil |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // List enumerates the available images. |
| 55 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 56 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | 71ff2fe | 2014-09-25 13:39:27 -0400 | [diff] [blame] | 57 | return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 60 | return pagination.NewPager(client, listURL(client), createPage) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 61 | } |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 62 | |
| 63 | // Get acquires additional detail about a specific image by ID. |
| 64 | // Use ExtractImage() to intepret the result as an openstack Image. |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 65 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 66 | var result GetResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame^] | 67 | _, result.Err = perigee.Request("GET", imageURL(client, id), perigee.Options{ |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 68 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 69 | Results: &result.Resp, |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 70 | OkCodes: []int{200}, |
| 71 | }) |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 72 | return result |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 73 | } |