blob: 832979c096b0831995a9e71154aec41e8cb4905f [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 Wilsonfd043792014-09-17 10:40:17 -04008// ListPage contains a single page of results from a List operation.
Ash Wilson9ccf9b62014-09-17 10:07:52 -04009// Use ExtractImages to convert it into a slice of usable structs.
Ash Wilsonfd043792014-09-17 10:40:17 -040010type ListPage struct {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040011 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.
Ash Wilsonfd043792014-09-17 10:40:17 -040015func (page ListPage) IsEmpty() (bool, error) {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040016 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
Ash Wilsonfd043792014-09-17 10:40:17 -040023// LastMarker returns the ID of the final Image on the current page of ListPage.
24func (page ListPage) LastMarker() (string, error) {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040025 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 {
Ash Wilsonfd043792014-09-17 10:40:17 -040038 p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson9ccf9b62014-09-17 10:07:52 -040039 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}