Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 4 | "github.com/racker/perigee" |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 7 | ) |
| 8 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 9 | // ListPage contains a single page of results from a List operation. |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 10 | // Use ExtractImages to convert it into a slice of usable structs. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 11 | type ListPage struct { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 12 | pagination.MarkerPageBase |
| 13 | } |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 14 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 15 | // IsEmpty returns true if a page contains no Image results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 16 | func (page ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 17 | images, err := ExtractImages(page) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 18 | if err != nil { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 19 | return true, err |
| 20 | } |
| 21 | return len(images) == 0, nil |
| 22 | } |
| 23 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 24 | // LastMarker returns the ID of the final Image on the current page of ListPage. |
| 25 | func (page ListPage) LastMarker() (string, error) { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 26 | 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 Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 36 | // GetResult opaquely stores the result of a Get call. |
| 37 | // Use ExtractImage() to translate it into this provider's version of an Image structure. |
| 38 | type GetResult map[string]interface{} |
| 39 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 40 | // List enumerates the available images. |
| 41 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 42 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 43 | p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 44 | p.MarkerPageBase.Owner = p |
| 45 | return p |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 48 | return pagination.NewPager(client, getListURL(client), createPage) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 49 | } |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 50 | |
| 51 | // Get acquires additional detail about a specific image by ID. |
| 52 | // Use ExtractImage() to intepret the result as an openstack Image. |
| 53 | func 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 | } |