Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 8 | // ListResults contains a single page of results from a List operation. |
| 9 | // Use ExtractImages to convert it into a slice of usable structs. |
| 10 | type ListResults struct { |
| 11 | pagination.MarkerPageBase |
| 12 | } |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 13 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 14 | // IsEmpty returns true if a page contains no Image results. |
| 15 | func (page ListResults) IsEmpty() (bool, error) { |
| 16 | images, err := ExtractImages(page) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 17 | if err != nil { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 18 | return true, err |
| 19 | } |
| 20 | return len(images) == 0, nil |
| 21 | } |
| 22 | |
| 23 | // LastMarker returns the ID of the final Image on the current page of ListResults. |
| 24 | func (page ListResults) LastMarker() (string, error) { |
| 25 | images, err := ExtractImages(page) |
| 26 | if err != nil { |
| 27 | return "", err |
| 28 | } |
| 29 | if len(images) == 0 { |
| 30 | return "", nil |
| 31 | } |
| 32 | return images[len(images)-1].ID, nil |
| 33 | } |
| 34 | |
| 35 | // List enumerates the available images. |
| 36 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 37 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 38 | p := ListResults{pagination.MarkerPageBase{LastHTTPResponse: r}} |
| 39 | p.MarkerPageBase.Owner = p |
| 40 | return p |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 43 | return pagination.NewPager(client, getListURL(client), createPage) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 44 | } |