blob: 1de318734befee093690e635181ee2717fc4402a [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Ash Wilson7ddf0362014-09-17 10:59:09 -04004 "github.com/racker/perigee"
Ash Wilson9ccf9b62014-09-17 10:07:52 -04005 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08007)
8
Ash Wilsonfd043792014-09-17 10:40:17 -04009// ListPage contains a single page of results from a List operation.
Ash Wilson9ccf9b62014-09-17 10:07:52 -040010// Use ExtractImages to convert it into a slice of usable structs.
Ash Wilsonfd043792014-09-17 10:40:17 -040011type ListPage struct {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040012 pagination.MarkerPageBase
13}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080014
Ash Wilson9ccf9b62014-09-17 10:07:52 -040015// IsEmpty returns true if a page contains no Image results.
Ash Wilsonfd043792014-09-17 10:40:17 -040016func (page ListPage) IsEmpty() (bool, error) {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040017 images, err := ExtractImages(page)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080018 if err != nil {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040019 return true, err
20 }
21 return len(images) == 0, nil
22}
23
Ash Wilsonfd043792014-09-17 10:40:17 -040024// LastMarker returns the ID of the final Image on the current page of ListPage.
25func (page ListPage) LastMarker() (string, error) {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040026 images, err := ExtractImages(page)
27 if err != nil {
28 return "", err
29 }
30 if len(images) == 0 {
31 return "", nil
32 }
33 return images[len(images)-1].ID, nil
34}
35
36// List enumerates the available images.
37func List(client *gophercloud.ServiceClient) pagination.Pager {
38 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfd043792014-09-17 10:40:17 -040039 p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson9ccf9b62014-09-17 10:07:52 -040040 p.MarkerPageBase.Owner = p
41 return p
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080042 }
43
Ash Wilson9ccf9b62014-09-17 10:07:52 -040044 return pagination.NewPager(client, getListURL(client), createPage)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080045}
Ash Wilson7ddf0362014-09-17 10:59:09 -040046
47// Get acquires additional detail about a specific image by ID.
48// Use ExtractImage() to intepret the result as an openstack Image.
Ash Wilsond2f87032014-09-25 11:34:41 -040049func Get(client *gophercloud.ServiceClient, id string) GetResult {
Ash Wilson7ddf0362014-09-17 10:59:09 -040050 var result GetResult
Ash Wilsond2f87032014-09-25 11:34:41 -040051 _, result.Err = perigee.Request("GET", getImageURL(client, id), perigee.Options{
Ash Wilson7ddf0362014-09-17 10:59:09 -040052 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Ash Wilsond2f87032014-09-25 11:34:41 -040053 Results: &result.Resp,
Ash Wilson7ddf0362014-09-17 10:59:09 -040054 OkCodes: []int{200},
55 })
Ash Wilsond2f87032014-09-25 11:34:41 -040056 return result
Ash Wilson7ddf0362014-09-17 10:59:09 -040057}