blob: 22c6e9b549df2846cc54867e721fb2693a633704 [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Ash Wilson9ccf9b62014-09-17 10:07:52 -04004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08006)
7
Ash Wilson9ccf9b62014-09-17 10:07:52 -04008// ListResults contains a single page of results from a List operation.
9// Use ExtractImages to convert it into a slice of usable structs.
10type ListResults struct {
11 pagination.MarkerPageBase
12}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080013
Ash Wilson9ccf9b62014-09-17 10:07:52 -040014// IsEmpty returns true if a page contains no Image results.
15func (page ListResults) IsEmpty() (bool, error) {
16 images, err := ExtractImages(page)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080017 if err != nil {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040018 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.
24func (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.
36func 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 II17ae5652014-02-12 20:47:43 -080041 }
42
Ash Wilson9ccf9b62014-09-17 10:07:52 -040043 return pagination.NewPager(client, getListURL(client), createPage)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080044}