blob: 8736382ed867b2ec352d5d17c5e5514ccb90b600 [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
Ash Wilson7ddf0362014-09-17 10:59:09 -040036// GetResult opaquely stores the result of a Get call.
37// Use ExtractImage() to translate it into this provider's version of an Image structure.
38type GetResult map[string]interface{}
39
Ash Wilson9ccf9b62014-09-17 10:07:52 -040040// List enumerates the available images.
41func List(client *gophercloud.ServiceClient) pagination.Pager {
42 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfd043792014-09-17 10:40:17 -040043 p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson9ccf9b62014-09-17 10:07:52 -040044 p.MarkerPageBase.Owner = p
45 return p
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080046 }
47
Ash Wilson9ccf9b62014-09-17 10:07:52 -040048 return pagination.NewPager(client, getListURL(client), createPage)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080049}
Ash Wilson7ddf0362014-09-17 10:59:09 -040050
51// Get acquires additional detail about a specific image by ID.
52// Use ExtractImage() to intepret the result as an openstack Image.
53func Get(client *gophercloud.ServiceClient, id string) (GetResult, error) {
54 var result GetResult
55 _, err := perigee.Request("GET", getImageURL(client, id), perigee.Options{
56 MoreHeaders: client.Provider.AuthenticatedHeaders(),
57 Results: &result,
58 OkCodes: []int{200},
59 })
60 return result, err
61}